View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.geronimo.deployment.plugin.jmx;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.net.InetAddress;
23  import java.net.NetworkInterface;
24  import java.net.URL;
25  import java.util.Enumeration;
26  import java.util.Iterator;
27  import java.util.Set;
28  import java.util.Map;
29  import java.util.List;
30  import java.util.ArrayList;
31  import java.util.Arrays;
32  import javax.enterprise.deploy.shared.CommandType;
33  import javax.enterprise.deploy.spi.Target;
34  import javax.enterprise.deploy.spi.TargetModuleID;
35  import javax.enterprise.deploy.spi.status.ProgressEvent;
36  import javax.enterprise.deploy.spi.status.ProgressListener;
37  import javax.management.MBeanServerConnection;
38  import javax.management.remote.JMXConnector;
39  import javax.security.auth.login.FailedLoginException;
40  
41  import org.apache.geronimo.deployment.plugin.GeronimoDeploymentManager;
42  import org.apache.geronimo.deployment.plugin.local.AbstractDeployCommand;
43  import org.apache.geronimo.deployment.plugin.local.DistributeCommand;
44  import org.apache.geronimo.deployment.plugin.local.RedeployCommand;
45  import org.apache.geronimo.deployment.plugin.remote.RemoteDeployUtil;
46  import org.apache.geronimo.gbean.AbstractName;
47  import org.apache.geronimo.gbean.AbstractNameQuery;
48  import org.apache.geronimo.system.jmx.KernelDelegate;
49  import org.apache.geronimo.system.plugin.DownloadResults;
50  import org.apache.geronimo.system.plugin.PluginList;
51  import org.apache.geronimo.system.plugin.DownloadPoller;
52  import org.apache.geronimo.system.plugin.PluginMetadata;
53  import org.apache.geronimo.system.plugin.PluginInstaller;
54  import org.apache.geronimo.system.plugin.PluginRepositoryList;
55  import org.apache.geronimo.kernel.repository.Artifact;
56  import org.apache.commons.logging.Log;
57  import org.apache.commons.logging.LogFactory;
58  
59  /**
60   * Connects to a Kernel in a remote VM (may or many not be on the same machine).
61   *
62   * @version $Rev: 437623 $ $Date: 2006-08-28 02:48:23 -0700 (Mon, 28 Aug 2006) $
63   */
64  public class RemoteDeploymentManager extends JMXDeploymentManager implements GeronimoDeploymentManager {
65      private static final Log log = LogFactory.getLog(RemoteDeploymentManager.class);
66  
67      private JMXConnector jmxConnector;
68      private boolean isSameMachine;
69  
70      public RemoteDeploymentManager(JMXConnector jmxConnector, String hostname) throws IOException {
71          this.jmxConnector = jmxConnector;
72          MBeanServerConnection mbServerConnection = jmxConnector.getMBeanServerConnection();
73          initialize(new KernelDelegate(mbServerConnection));
74          checkSameMachine(hostname);
75      }
76  
77      public boolean isSameMachine() {
78          return isSameMachine;
79      }
80  
81      private void checkSameMachine(String hostname) {
82          isSameMachine = false;
83          if(hostname.equals("localhost") || hostname.equals("127.0.0.1")) {
84              isSameMachine = true;
85              return;
86          }
87          try {
88              InetAddress dest = InetAddress.getByName(hostname);
89              Enumeration en = NetworkInterface.getNetworkInterfaces();
90              while(en.hasMoreElements()) {
91                  NetworkInterface iface = (NetworkInterface) en.nextElement();
92                  Enumeration ine = iface.getInetAddresses();
93                  while (ine.hasMoreElements()) {
94                      InetAddress address = (InetAddress) ine.nextElement();
95                      if(address.equals(dest)) {
96                          isSameMachine = true;
97                      }
98                  }
99              }
100         } catch (Exception e) {
101             log.error("Unable to look up host name '"+hostname+"'; assuming it is a different machine, but this may not get very far.", e);
102         }
103     }
104 
105     public void release() {
106         super.release();
107         try {
108             jmxConnector.close();
109             jmxConnector = null;
110         } catch (IOException e) {
111             throw (IllegalStateException) new IllegalStateException("Unable to close connection").initCause(e);
112         }
113     }
114 
115     protected DistributeCommand createDistributeCommand(Target[] targetList, File moduleArchive, File deploymentPlan) {
116         if(isSameMachine) {
117             return super.createDistributeCommand(targetList, moduleArchive, deploymentPlan);
118         } else {
119             return new org.apache.geronimo.deployment.plugin.remote.DistributeCommand(kernel, targetList, moduleArchive, deploymentPlan);
120         }
121     }
122 
123     protected DistributeCommand createDistributeCommand(Target[] targetList, InputStream moduleArchive, InputStream deploymentPlan) {
124         if(isSameMachine) {
125             return super.createDistributeCommand(targetList, moduleArchive, deploymentPlan);
126         } else {
127             return new org.apache.geronimo.deployment.plugin.remote.DistributeCommand(kernel, targetList, moduleArchive, deploymentPlan);
128         }
129     }
130 
131     protected RedeployCommand createRedeployCommand(TargetModuleID[] moduleIDList, File moduleArchive, File deploymentPlan) {
132         if(isSameMachine) {
133             return super.createRedeployCommand(moduleIDList, moduleArchive, deploymentPlan);
134         } else {
135             return new org.apache.geronimo.deployment.plugin.remote.RedeployCommand(kernel, moduleIDList, moduleArchive, deploymentPlan);
136         }
137     }
138 
139     protected RedeployCommand createRedeployCommand(TargetModuleID[] moduleIDList, InputStream moduleArchive, InputStream deploymentPlan) {
140         if(isSameMachine) {
141             return super.createRedeployCommand(moduleIDList, moduleArchive, deploymentPlan);
142         } else {
143             return new org.apache.geronimo.deployment.plugin.remote.RedeployCommand(kernel, moduleIDList, moduleArchive, deploymentPlan);
144         }
145     }
146 
147     public PluginList listPlugins(URL mavenRepository, String username, String password) throws FailedLoginException, IOException {
148         Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
149         for (Iterator it = set.iterator(); it.hasNext();) {
150             AbstractName name = (AbstractName) it.next();
151             PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class);
152             PluginList results = installer.listPlugins(mavenRepository, username, password);
153             kernel.getProxyManager().destroyProxy(installer);
154             return results;
155         }
156         return null;
157     }
158 
159     public DownloadResults install(PluginList installList, String username, String password) {
160         Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
161         for (Iterator it = set.iterator(); it.hasNext();) {
162             AbstractName name = (AbstractName) it.next();
163             PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class);
164             DownloadResults results = installer.install(installList, username, password);
165             kernel.getProxyManager().destroyProxy(installer);
166             return results;
167         }
168         return null;
169     }
170 
171     public void install(PluginList configsToInstall, String username, String password, DownloadPoller poller) {
172         Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
173         for (Iterator it = set.iterator(); it.hasNext();) {
174             AbstractName name = (AbstractName) it.next();
175             PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class);
176             installer.install(configsToInstall, username, password, poller);
177             kernel.getProxyManager().destroyProxy(installer);
178             return;
179         }
180     }
181 
182     public Object startInstall(PluginList configsToInstall, String username, String password) {
183         Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
184         for (Iterator it = set.iterator(); it.hasNext();) {
185             AbstractName name = (AbstractName) it.next();
186             PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class);
187             Object result = installer.startInstall(configsToInstall, username, password);
188             kernel.getProxyManager().destroyProxy(installer);
189             return result;
190         }
191         return null;
192     }
193 
194     public Object startInstall(File carFile, String username, String password) {
195         File[] args = new File[]{carFile};
196         if(!isSameMachine) {
197             AbstractDeployCommand progress = new AbstractDeployCommand(CommandType.DISTRIBUTE, kernel, null, null, null, null, false) {
198                 public void run() {
199                 }
200             };
201             progress.addProgressListener(new ProgressListener() {
202                 public void handleProgressEvent(ProgressEvent event) {
203                     log.info(event.getDeploymentStatus().getMessage());
204                 }
205             });
206             RemoteDeployUtil.uploadFilesToServer(args, progress);
207         }
208         Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
209         for (Iterator it = set.iterator(); it.hasNext();) {
210             AbstractName name = (AbstractName) it.next();
211             PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class);
212             Object result = installer.startInstall(carFile, username, password);
213             kernel.getProxyManager().destroyProxy(installer);
214             return result;
215         }
216         return null;
217     }
218 
219     public DownloadResults checkOnInstall(Object key) {
220         Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
221         for (Iterator it = set.iterator(); it.hasNext();) {
222             AbstractName name = (AbstractName) it.next();
223             PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class);
224             DownloadResults result = installer.checkOnInstall(key);
225             kernel.getProxyManager().destroyProxy(installer);
226             return result;
227         }
228         return null;
229     }
230 
231     public Map getInstalledPlugins() {
232         Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
233         for (Iterator it = set.iterator(); it.hasNext();) {
234             AbstractName name = (AbstractName) it.next();
235             PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class);
236             Map result = installer.getInstalledPlugins();
237             kernel.getProxyManager().destroyProxy(installer);
238             return result;
239         }
240         return null;
241     }
242 
243     public PluginMetadata getPluginMetadata(Artifact configId) {
244         Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
245         for (Iterator it = set.iterator(); it.hasNext();) {
246             AbstractName name = (AbstractName) it.next();
247             PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class);
248             PluginMetadata result = installer.getPluginMetadata(configId);
249             kernel.getProxyManager().destroyProxy(installer);
250             return result;
251         }
252         return null;
253     }
254 
255     public void updatePluginMetadata(PluginMetadata metadata) {
256         Set set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
257         for (Iterator it = set.iterator(); it.hasNext();) {
258             AbstractName name = (AbstractName) it.next();
259             PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class);
260             installer.updatePluginMetadata(metadata);
261             kernel.getProxyManager().destroyProxy(installer);
262             return;
263         }
264     }
265 
266     public URL[] getRepositories() {
267         List list = new ArrayList();
268         Set set = kernel.listGBeans(new AbstractNameQuery(PluginRepositoryList.class.getName()));
269         for (Iterator it = set.iterator(); it.hasNext();) {
270             AbstractName name = (AbstractName) it.next();
271             PluginRepositoryList repo = (PluginRepositoryList) kernel.getProxyManager().createProxy(name, PluginRepositoryList.class);
272             list.addAll(Arrays.asList(repo.getRepositories()));
273             kernel.getProxyManager().destroyProxy(repo);
274         }
275         return (URL[]) list.toArray(new URL[list.size()]);
276     }
277 }