001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.deployment.plugin.jmx;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.net.InetAddress;
022: import java.net.NetworkInterface;
023: import java.net.URL;
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.Enumeration;
027: import java.util.List;
028: import java.util.Map;
029: import java.util.Set;
030:
031: import javax.enterprise.deploy.shared.CommandType;
032: import javax.enterprise.deploy.shared.ModuleType;
033: import javax.enterprise.deploy.spi.Target;
034: import javax.enterprise.deploy.spi.TargetModuleID;
035: import javax.enterprise.deploy.spi.status.ProgressEvent;
036: import javax.enterprise.deploy.spi.status.ProgressListener;
037: import javax.management.MBeanServerConnection;
038: import javax.management.remote.JMXConnector;
039: import javax.security.auth.login.FailedLoginException;
040:
041: import org.apache.commons.logging.Log;
042: import org.apache.commons.logging.LogFactory;
043: import org.apache.geronimo.deployment.ModuleConfigurer;
044: import org.apache.geronimo.deployment.plugin.GeronimoDeploymentManager;
045: import org.apache.geronimo.deployment.plugin.local.AbstractDeployCommand;
046: import org.apache.geronimo.deployment.plugin.local.DistributeCommand;
047: import org.apache.geronimo.deployment.plugin.local.RedeployCommand;
048: import org.apache.geronimo.deployment.plugin.remote.RemoteDeployUtil;
049: import org.apache.geronimo.gbean.AbstractName;
050: import org.apache.geronimo.gbean.AbstractNameQuery;
051: import org.apache.geronimo.gbean.GBeanInfo;
052: import org.apache.geronimo.gbean.GBeanInfoBuilder;
053: import org.apache.geronimo.kernel.config.NoSuchStoreException;
054: import org.apache.geronimo.kernel.repository.Artifact;
055: import org.apache.geronimo.kernel.repository.Dependency;
056: import org.apache.geronimo.kernel.repository.MissingDependencyException;
057: import org.apache.geronimo.kernel.InvalidGBeanException;
058: import org.apache.geronimo.system.jmx.KernelDelegate;
059: import org.apache.geronimo.system.plugin.DownloadPoller;
060: import org.apache.geronimo.system.plugin.DownloadResults;
061: import org.apache.geronimo.system.plugin.PluginInstaller;
062: import org.apache.geronimo.system.plugin.PluginRepositoryList;
063: import org.apache.geronimo.system.plugin.ServerArchiver;
064: import org.apache.geronimo.system.plugin.model.PluginListType;
065: import org.apache.geronimo.system.plugin.model.PluginType;
066: import org.apache.geronimo.system.plugin.model.AttributesType;
067: import org.codehaus.plexus.archiver.ArchiverException;
068:
069: /**
070: * Connects to a Kernel in a remote VM (may or many not be on the same machine).
071: *
072: * @version $Rev: 630982 $ $Date: 2008-02-25 12:30:54 -0800 (Mon, 25 Feb 2008) $
073: */
074: public class RemoteDeploymentManager extends JMXDeploymentManager
075: implements GeronimoDeploymentManager, ServerArchiver {
076: private static final Log log = LogFactory
077: .getLog(RemoteDeploymentManager.class);
078:
079: private JMXConnector jmxConnector;
080: private boolean isSameMachine;
081:
082: public RemoteDeploymentManager(
083: Collection<ModuleConfigurer> moduleConfigurers) {
084: super (moduleConfigurers);
085: }
086:
087: public void init(JMXConnector jmxConnector, String hostname)
088: throws IOException {
089: this .jmxConnector = jmxConnector;
090: MBeanServerConnection mbServerConnection = jmxConnector
091: .getMBeanServerConnection();
092: initialize(new KernelDelegate(mbServerConnection));
093: checkSameMachine(hostname);
094: }
095:
096: public JMXConnector getJMXConnector() {
097: return this .jmxConnector;
098: }
099:
100: public boolean isSameMachine() {
101: return isSameMachine;
102: }
103:
104: private void checkSameMachine(String hostname) {
105: isSameMachine = false;
106: if (hostname.equals("localhost")
107: || hostname.equals("127.0.0.1")) {
108: isSameMachine = true;
109: return;
110: }
111: try {
112: InetAddress dest = InetAddress.getByName(hostname);
113: Enumeration en = NetworkInterface.getNetworkInterfaces();
114: while (en.hasMoreElements()) {
115: NetworkInterface iface = (NetworkInterface) en
116: .nextElement();
117: Enumeration ine = iface.getInetAddresses();
118: while (ine.hasMoreElements()) {
119: InetAddress address = (InetAddress) ine
120: .nextElement();
121: if (address.equals(dest)) {
122: isSameMachine = true;
123: }
124: }
125: }
126: } catch (Exception e) {
127: log
128: .error(
129: "Unable to look up host name '"
130: + hostname
131: + "'; assuming it is a different machine, but this may not get very far.",
132: e);
133: }
134: }
135:
136: public void release() {
137: super .release();
138: try {
139: jmxConnector.close();
140: jmxConnector = null;
141: } catch (IOException e) {
142: throw (IllegalStateException) new IllegalStateException(
143: "Unable to close connection").initCause(e);
144: }
145: }
146:
147: protected DistributeCommand createDistributeCommand(
148: Target[] targetList, File moduleArchive, File deploymentPlan) {
149: if (isSameMachine) {
150: return super .createDistributeCommand(targetList,
151: moduleArchive, deploymentPlan);
152: } else {
153: return new org.apache.geronimo.deployment.plugin.remote.DistributeCommand(
154: kernel, targetList, moduleArchive, deploymentPlan);
155: }
156: }
157:
158: protected DistributeCommand createDistributeCommand(
159: Target[] targetList, ModuleType moduleType,
160: InputStream moduleArchive, InputStream deploymentPlan) {
161: if (isSameMachine) {
162: return super .createDistributeCommand(targetList,
163: moduleType, moduleArchive, deploymentPlan);
164: } else {
165: return new org.apache.geronimo.deployment.plugin.remote.DistributeCommand(
166: kernel, targetList, moduleType, moduleArchive,
167: deploymentPlan);
168: }
169: }
170:
171: protected RedeployCommand createRedeployCommand(
172: TargetModuleID[] moduleIDList, File moduleArchive,
173: File deploymentPlan) {
174: if (isSameMachine) {
175: return super .createRedeployCommand(moduleIDList,
176: moduleArchive, deploymentPlan);
177: } else {
178: return new org.apache.geronimo.deployment.plugin.remote.RedeployCommand(
179: kernel, moduleIDList, moduleArchive, deploymentPlan);
180: }
181: }
182:
183: protected RedeployCommand createRedeployCommand(
184: TargetModuleID[] moduleIDList, InputStream moduleArchive,
185: InputStream deploymentPlan) {
186: if (isSameMachine) {
187: return super .createRedeployCommand(moduleIDList,
188: moduleArchive, deploymentPlan);
189: } else {
190: return new org.apache.geronimo.deployment.plugin.remote.RedeployCommand(
191: kernel, moduleIDList, moduleArchive, deploymentPlan);
192: }
193: }
194:
195: public PluginListType listPlugins(URL mavenRepository,
196: String username, String password)
197: throws FailedLoginException, IOException {
198: PluginInstaller installer = getPluginInstaller();
199: try {
200: return installer.listPlugins(mavenRepository, username,
201: password);
202: } finally {
203: kernel.getProxyManager().destroyProxy(installer);
204: }
205: }
206:
207: public void validatePlugin(PluginType plugin)
208: throws MissingDependencyException {
209: PluginInstaller installer = getPluginInstaller();
210: try {
211: installer.validatePlugin(plugin);
212: } finally {
213: kernel.getProxyManager().destroyProxy(installer);
214: }
215: }
216:
217: public Dependency[] checkPrerequisites(PluginType plugin) {
218: PluginInstaller installer = getPluginInstaller();
219: try {
220: return installer.checkPrerequisites(plugin);
221: } finally {
222: kernel.getProxyManager().destroyProxy(installer);
223: }
224: }
225:
226: public DownloadResults install(PluginListType configsToInstall,
227: String defaultRepository,
228: boolean restrictToDefaultRepository, String username,
229: String password) {
230: PluginInstaller installer = getPluginInstaller();
231: try {
232: return installer.install(configsToInstall,
233: defaultRepository, restrictToDefaultRepository,
234: username, password);
235: } finally {
236: kernel.getProxyManager().destroyProxy(installer);
237: }
238: }
239:
240: public void install(PluginListType configsToInstall,
241: String defaultRepository,
242: boolean restrictToDefaultRepository, String username,
243: String password, DownloadPoller poller) {
244: PluginInstaller installer = getPluginInstaller();
245: try {
246: installer.install(configsToInstall, defaultRepository,
247: restrictToDefaultRepository, username, password,
248: poller);
249: } finally {
250: kernel.getProxyManager().destroyProxy(installer);
251: }
252: }
253:
254: public Object startInstall(PluginListType configsToInstall,
255: String defaultRepository,
256: boolean restrictToDefaultRepository, String username,
257: String password) {
258: PluginInstaller installer = getPluginInstaller();
259: try {
260: return installer.startInstall(configsToInstall,
261: defaultRepository, restrictToDefaultRepository,
262: username, password);
263: } finally {
264: kernel.getProxyManager().destroyProxy(installer);
265: }
266: }
267:
268: public Object startInstall(File carFile, String defaultRepository,
269: boolean restrictToDefaultRepository, String username,
270: String password) {
271: File[] args = new File[] { carFile };
272: if (!isSameMachine) {
273: AbstractDeployCommand progress = new AbstractDeployCommand(
274: CommandType.DISTRIBUTE, kernel, null, null, null,
275: null, null, false) {
276: public void run() {
277: }
278: };
279: progress.addProgressListener(new ProgressListener() {
280: public void handleProgressEvent(ProgressEvent event) {
281: log.info(event.getDeploymentStatus().getMessage());
282: }
283: });
284: progress.setCommandContext(commandContext);
285: RemoteDeployUtil.uploadFilesToServer(args, progress);
286: }
287: PluginInstaller installer = getPluginInstaller();
288: try {
289: return installer.startInstall(carFile, defaultRepository,
290: restrictToDefaultRepository, username, password);
291: } finally {
292: kernel.getProxyManager().destroyProxy(installer);
293: }
294: }
295:
296: public DownloadResults checkOnInstall(Object key) {
297: PluginInstaller installer = getPluginInstaller();
298: try {
299: return installer.checkOnInstall(key);
300: } finally {
301: kernel.getProxyManager().destroyProxy(installer);
302: }
303: }
304:
305: private PluginInstaller getPluginInstaller() {
306: Set<AbstractName> set = kernel
307: .listGBeans(new AbstractNameQuery(PluginInstaller.class
308: .getName()));
309: for (AbstractName name : set) {
310: return (PluginInstaller) kernel.getProxyManager()
311: .createProxy(name, PluginInstaller.class);
312: }
313: throw new IllegalStateException("No plugin installer found");
314: }
315:
316: private ServerArchiver getServerArchiver() {
317: Set<AbstractName> set = kernel
318: .listGBeans(new AbstractNameQuery(ServerArchiver.class
319: .getName()));
320: for (AbstractName name : set) {
321: return (ServerArchiver) kernel.getProxyManager()
322: .createProxy(name, ServerArchiver.class);
323: }
324: throw new IllegalStateException("No plugin installer found");
325: }
326:
327: //not likely to be useful remotely
328: public PluginListType createPluginListForRepositories(String repo)
329: throws NoSuchStoreException {
330: PluginInstaller installer = getPluginInstaller();
331: try {
332: return installer.createPluginListForRepositories(repo);
333: } finally {
334: kernel.getProxyManager().destroyProxy(installer);
335: }
336: }
337:
338: public Map getInstalledPlugins() {
339: PluginInstaller installer = getPluginInstaller();
340: try {
341: return installer.getInstalledPlugins();
342: } finally {
343: kernel.getProxyManager().destroyProxy(installer);
344: }
345: }
346:
347: public PluginType getPluginMetadata(Artifact configId) {
348: PluginInstaller installer = getPluginInstaller();
349: try {
350: return installer.getPluginMetadata(configId);
351: } finally {
352: kernel.getProxyManager().destroyProxy(installer);
353: }
354: }
355:
356: public void updatePluginMetadata(PluginType metadata) {
357: PluginInstaller installer = getPluginInstaller();
358: try {
359: installer.updatePluginMetadata(metadata);
360: } finally {
361: kernel.getProxyManager().destroyProxy(installer);
362: }
363: }
364:
365: public URL[] getRepositories() {
366: List<URL> list = new ArrayList<URL>();
367: Set<AbstractName> set = kernel
368: .listGBeans(new AbstractNameQuery(
369: PluginRepositoryList.class.getName()));
370: for (AbstractName name : set) {
371: PluginRepositoryList repo = (PluginRepositoryList) kernel
372: .getProxyManager().createProxy(name,
373: PluginRepositoryList.class);
374: list.addAll(repo.getRepositories());
375: kernel.getProxyManager().destroyProxy(repo);
376: }
377: return list.toArray(new URL[list.size()]);
378: }
379:
380: public Artifact installLibrary(File libFile, String groupId)
381: throws IOException {
382: File[] args = new File[] { libFile };
383: if (!isSameMachine) {
384: AbstractDeployCommand progress = new AbstractDeployCommand(
385: CommandType.DISTRIBUTE, kernel, null, null, null,
386: null, null, false) {
387: public void run() {
388: }
389: };
390: progress.addProgressListener(new ProgressListener() {
391: public void handleProgressEvent(ProgressEvent event) {
392: log.info(event.getDeploymentStatus().getMessage());
393: }
394: });
395: progress.setCommandContext(commandContext);
396: RemoteDeployUtil.uploadFilesToServer(args, progress);
397: }
398: Set<AbstractName> set = kernel
399: .listGBeans(new AbstractNameQuery(PluginInstaller.class
400: .getName()));
401: for (AbstractName name : set) {
402: PluginInstaller installer = (PluginInstaller) kernel
403: .getProxyManager().createProxy(name,
404: PluginInstaller.class);
405: Artifact artifact = installer.installLibrary(libFile,
406: groupId);
407: kernel.getProxyManager().destroyProxy(installer);
408: return artifact;
409: }
410: return null;
411: }
412:
413: public DownloadResults installPluginList(
414: String targetRepositoryPath,
415: String relativeTargetServerPath, PluginListType pluginList)
416: throws Exception {
417: PluginInstaller installer = getPluginInstaller();
418: try {
419: return installer.installPluginList(targetRepositoryPath,
420: relativeTargetServerPath, pluginList);
421: } finally {
422: kernel.getProxyManager().destroyProxy(installer);
423: }
424: }
425:
426: public void mergeOverrides(String server, AttributesType overrides)
427: throws InvalidGBeanException, IOException {
428: PluginInstaller installer = getPluginInstaller();
429: try {
430: installer.mergeOverrides(server, overrides);
431: } finally {
432: kernel.getProxyManager().destroyProxy(installer);
433: }
434: }
435:
436: public File archive(String sourcePath, String destPath,
437: Artifact artifact) throws ArchiverException, IOException {
438: ServerArchiver archiver = getServerArchiver();
439: try {
440: return archiver.archive(sourcePath, destPath, artifact);
441: } finally {
442: kernel.getProxyManager().destroyProxy(archiver);
443: }
444: }
445:
446: public static final GBeanInfo GBEAN_INFO;
447: public static final String GBEAN_REF_MODULE_CONFIGURERS = "ModuleConfigurers";
448:
449: static {
450: GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(
451: RemoteDeploymentManager.class,
452: "RemoteDeploymentManager");
453: infoFactory.addInterface(GeronimoDeploymentManager.class);
454: infoFactory.addReference(GBEAN_REF_MODULE_CONFIGURERS,
455: ModuleConfigurer.class);
456:
457: infoFactory
458: .setConstructor(new String[] { GBEAN_REF_MODULE_CONFIGURERS });
459:
460: GBEAN_INFO = infoFactory.getBeanInfo();
461: }
462:
463: public static GBeanInfo getGBeanInfo() {
464: return GBEAN_INFO;
465: }
466:
467: }
|