KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > plugin > jmx > RemoteDeploymentManager


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.net.InetAddress JavaDoc;
23 import java.net.NetworkInterface JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import javax.enterprise.deploy.shared.CommandType JavaDoc;
33 import javax.enterprise.deploy.spi.Target JavaDoc;
34 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
35 import javax.enterprise.deploy.spi.status.ProgressEvent JavaDoc;
36 import javax.enterprise.deploy.spi.status.ProgressListener JavaDoc;
37 import javax.management.MBeanServerConnection JavaDoc;
38 import javax.management.remote.JMXConnector JavaDoc;
39 import javax.security.auth.login.FailedLoginException JavaDoc;
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: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
63  */

64 public class RemoteDeploymentManager extends JMXDeploymentManager implements GeronimoDeploymentManager {
65     private static final Log log = LogFactory.getLog(RemoteDeploymentManager.class);
66
67     private JMXConnector JavaDoc jmxConnector;
68     private boolean isSameMachine;
69
70     public RemoteDeploymentManager(JMXConnector JavaDoc jmxConnector, String JavaDoc hostname) throws IOException JavaDoc {
71         this.jmxConnector = jmxConnector;
72         MBeanServerConnection JavaDoc 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 JavaDoc 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 JavaDoc dest = InetAddress.getByName(hostname);
89             Enumeration JavaDoc en = NetworkInterface.getNetworkInterfaces();
90             while(en.hasMoreElements()) {
91                 NetworkInterface JavaDoc iface = (NetworkInterface JavaDoc) en.nextElement();
92                 Enumeration JavaDoc ine = iface.getInetAddresses();
93                 while (ine.hasMoreElements()) {
94                     InetAddress JavaDoc address = (InetAddress JavaDoc) ine.nextElement();
95                     if(address.equals(dest)) {
96                         isSameMachine = true;
97                     }
98                 }
99             }
100         } catch (Exception JavaDoc 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 JavaDoc e) {
111             throw (IllegalStateException JavaDoc) new IllegalStateException JavaDoc("Unable to close connection").initCause(e);
112         }
113     }
114
115     protected DistributeCommand createDistributeCommand(Target JavaDoc[] targetList, File JavaDoc moduleArchive, File JavaDoc 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 JavaDoc[] targetList, InputStream JavaDoc moduleArchive, InputStream JavaDoc 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 JavaDoc[] moduleIDList, File JavaDoc moduleArchive, File JavaDoc 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 JavaDoc[] moduleIDList, InputStream JavaDoc moduleArchive, InputStream JavaDoc 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 JavaDoc mavenRepository, String JavaDoc username, String JavaDoc password) throws FailedLoginException JavaDoc, IOException JavaDoc {
148         Set JavaDoc set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
149         for (Iterator JavaDoc 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 JavaDoc username, String JavaDoc password) {
160         Set JavaDoc set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
161         for (Iterator JavaDoc 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 JavaDoc username, String JavaDoc password, DownloadPoller poller) {
172         Set JavaDoc set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
173         for (Iterator JavaDoc 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 JavaDoc startInstall(PluginList configsToInstall, String JavaDoc username, String JavaDoc password) {
183         Set JavaDoc set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
184         for (Iterator JavaDoc it = set.iterator(); it.hasNext();) {
185             AbstractName name = (AbstractName) it.next();
186             PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class);
187             Object JavaDoc result = installer.startInstall(configsToInstall, username, password);
188             kernel.getProxyManager().destroyProxy(installer);
189             return result;
190         }
191         return null;
192     }
193
194     public Object JavaDoc startInstall(File JavaDoc carFile, String JavaDoc username, String JavaDoc password) {
195         File JavaDoc[] args = new File JavaDoc[]{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 JavaDoc() {
202                 public void handleProgressEvent(ProgressEvent JavaDoc event) {
203                     log.info(event.getDeploymentStatus().getMessage());
204                 }
205             });
206             RemoteDeployUtil.uploadFilesToServer(args, progress);
207         }
208         Set JavaDoc set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
209         for (Iterator JavaDoc it = set.iterator(); it.hasNext();) {
210             AbstractName name = (AbstractName) it.next();
211             PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class);
212             Object JavaDoc 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 JavaDoc key) {
220         Set JavaDoc set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
221         for (Iterator JavaDoc 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 JavaDoc getInstalledPlugins() {
232         Set JavaDoc set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
233         for (Iterator JavaDoc it = set.iterator(); it.hasNext();) {
234             AbstractName name = (AbstractName) it.next();
235             PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class);
236             Map JavaDoc 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 JavaDoc set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
245         for (Iterator JavaDoc 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 JavaDoc set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
257         for (Iterator JavaDoc 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 JavaDoc[] getRepositories() {
267         List JavaDoc list = new ArrayList JavaDoc();
268         Set JavaDoc set = kernel.listGBeans(new AbstractNameQuery(PluginRepositoryList.class.getName()));
269         for (Iterator JavaDoc 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 JavaDoc[]) list.toArray(new URL JavaDoc[list.size()]);
276     }
277 }
278
Popular Tags