Home » geronimo-2.2-source-release » org.apache.geronimo.deployment.plugin.jmx » [javadoc | source]

    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;
   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.ArrayList;
   26   import java.util.Collection;
   27   import java.util.Enumeration;
   28   import java.util.List;
   29   import java.util.Map;
   30   import java.util.Set;
   31   
   32   import javax.enterprise.deploy.shared.CommandType;
   33   import javax.enterprise.deploy.shared.ModuleType;
   34   import javax.enterprise.deploy.spi.Target;
   35   import javax.enterprise.deploy.spi.TargetModuleID;
   36   import javax.enterprise.deploy.spi.status.ProgressEvent;
   37   import javax.enterprise.deploy.spi.status.ProgressListener;
   38   import javax.management.MBeanServerConnection;
   39   import javax.management.remote.JMXConnector;
   40   import javax.security.auth.login.FailedLoginException;
   41   
   42   import org.slf4j.Logger;
   43   import org.slf4j.LoggerFactory;
   44   import org.apache.geronimo.deployment.ModuleConfigurer;
   45   import org.apache.geronimo.deployment.plugin.GeronimoDeploymentManager;
   46   import org.apache.geronimo.deployment.plugin.local.AbstractDeployCommand;
   47   import org.apache.geronimo.deployment.plugin.local.DistributeCommand;
   48   import org.apache.geronimo.deployment.plugin.local.RedeployCommand;
   49   import org.apache.geronimo.deployment.plugin.remote.RemoteDeployUtil;
   50   import org.apache.geronimo.gbean.AbstractName;
   51   import org.apache.geronimo.gbean.AbstractNameQuery;
   52   import org.apache.geronimo.gbean.GBeanInfo;
   53   import org.apache.geronimo.gbean.GBeanInfoBuilder;
   54   import org.apache.geronimo.kernel.config.NoSuchStoreException;
   55   import org.apache.geronimo.kernel.repository.Artifact;
   56   import org.apache.geronimo.kernel.repository.Dependency;
   57   import org.apache.geronimo.kernel.repository.MissingDependencyException;
   58   import org.apache.geronimo.kernel.InvalidGBeanException;
   59   import org.apache.geronimo.kernel.GBeanNotFoundException;
   60   import org.apache.geronimo.system.jmx.KernelDelegate;
   61   import org.apache.geronimo.system.plugin.DownloadPoller;
   62   import org.apache.geronimo.system.plugin.DownloadResults;
   63   import org.apache.geronimo.system.plugin.PluginInstaller;
   64   import org.apache.geronimo.system.plugin.PluginRepositoryList;
   65   import org.apache.geronimo.system.plugin.ServerArchiver;
   66   import org.apache.geronimo.system.plugin.model.PluginListType;
   67   import org.apache.geronimo.system.plugin.model.PluginType;
   68   import org.apache.geronimo.system.plugin.model.AttributesType;
   69   import org.codehaus.plexus.archiver.ArchiverException;
   70   
   71   /**
   72    * Connects to a Kernel in a remote VM (may or many not be on the same machine).
   73    *
   74    * @version $Rev: 699202 $ $Date: 2008-09-26 00:26:53 -0700 (Fri, 26 Sep 2008) $
   75    */
   76   public class RemoteDeploymentManager extends JMXDeploymentManager implements GeronimoDeploymentManager, ServerArchiver {
   77       private static final Logger log = LoggerFactory.getLogger(RemoteDeploymentManager.class);
   78   
   79       private JMXConnector jmxConnector;
   80       private boolean isSameMachine;
   81   
   82       public RemoteDeploymentManager(Collection<ModuleConfigurer> moduleConfigurers) {
   83           super(moduleConfigurers);
   84       }
   85   
   86       public void init(JMXConnector jmxConnector, String hostname) throws IOException {
   87           this.jmxConnector = jmxConnector;
   88           MBeanServerConnection mbServerConnection = jmxConnector.getMBeanServerConnection();
   89           initialize(new KernelDelegate(mbServerConnection));
   90           checkSameMachine(hostname);
   91       }
   92   
   93       public JMXConnector getJMXConnector() {
   94           return this.jmxConnector;
   95       }
   96       
   97       public boolean isSameMachine() {
   98           return isSameMachine;
   99       }
  100   
  101       private void checkSameMachine(String hostname) {
  102           isSameMachine = false;
  103           if (hostname.equals("localhost") || hostname.equals("127.0.0.1")) {
  104               isSameMachine = true;
  105               return;
  106           }
  107           try {
  108               InetAddress dest = InetAddress.getByName(hostname);
  109               Enumeration en = NetworkInterface.getNetworkInterfaces();
  110               while (en.hasMoreElements()) {
  111                   NetworkInterface iface = (NetworkInterface) en.nextElement();
  112                   Enumeration ine = iface.getInetAddresses();
  113                   while (ine.hasMoreElements()) {
  114                       InetAddress address = (InetAddress) ine.nextElement();
  115                       if (address.equals(dest)) {
  116                           isSameMachine = true;
  117                       }
  118                   }
  119               }
  120           } catch (Exception e) {
  121               log.error(
  122                       "Unable to look up host name '" + hostname + "'; assuming it is a different machine, but this may not get very far.",
  123                       e);
  124           }
  125       }
  126   
  127       public void release() {
  128           super.release();
  129           try {
  130               jmxConnector.close();
  131               jmxConnector = null;
  132           } catch (IOException e) {
  133               throw (IllegalStateException) new IllegalStateException("Unable to close connection").initCause(e);
  134           }
  135       }
  136   
  137       protected DistributeCommand createDistributeCommand(Target[] targetList, File moduleArchive, File deploymentPlan) {
  138           if (isSameMachine) {
  139               return super.createDistributeCommand(targetList, moduleArchive, deploymentPlan);
  140           } else {
  141               return new org.apache.geronimo.deployment.plugin.remote.DistributeCommand(kernel, targetList, moduleArchive,
  142                       deploymentPlan);
  143           }
  144       }
  145   
  146       protected DistributeCommand createDistributeCommand(Target[] targetList, ModuleType moduleType, InputStream moduleArchive, InputStream deploymentPlan) {
  147           if (isSameMachine) {
  148               return super.createDistributeCommand(targetList, moduleType, moduleArchive, deploymentPlan);
  149           } else {
  150               return new org.apache.geronimo.deployment.plugin.remote.DistributeCommand(kernel, targetList, moduleType,
  151                       moduleArchive, deploymentPlan);
  152           }
  153       }
  154   
  155       protected RedeployCommand createRedeployCommand(TargetModuleID[] moduleIDList, File moduleArchive, File deploymentPlan) {
  156           if (isSameMachine) {
  157               return super.createRedeployCommand(moduleIDList, moduleArchive, deploymentPlan);
  158           } else {
  159               return new org.apache.geronimo.deployment.plugin.remote.RedeployCommand(kernel, moduleIDList, moduleArchive,
  160                       deploymentPlan);
  161           }
  162       }
  163   
  164       protected RedeployCommand createRedeployCommand(TargetModuleID[] moduleIDList, InputStream moduleArchive, InputStream deploymentPlan) {
  165           if (isSameMachine) {
  166               return super.createRedeployCommand(moduleIDList, moduleArchive, deploymentPlan);
  167           } else {
  168               return new org.apache.geronimo.deployment.plugin.remote.RedeployCommand(kernel, moduleIDList, moduleArchive,
  169                       deploymentPlan);
  170           }
  171       }
  172   
  173        public <T> T getImplementation(Class<T> clazz) {
  174            try {
  175                return kernel.getGBean(clazz);
  176            } catch (GBeanNotFoundException e) {
  177                throw new IllegalStateException("No implementation for " + clazz.getName(), e);
  178            }
  179        }
  180   
  181   
  182   
  183       public PluginListType listPlugins(URL mavenRepository) throws FailedLoginException, IOException {
  184           PluginInstaller installer = getPluginInstaller();
  185           try {
  186               return installer.listPlugins(mavenRepository);
  187           } finally {
  188               kernel.getProxyManager().destroyProxy(installer);
  189           }
  190       }
  191       
  192       public boolean validatePlugin(PluginType plugin) throws MissingDependencyException {
  193           PluginInstaller installer = getPluginInstaller();
  194           try {
  195               return installer.validatePlugin(plugin);
  196           } finally {
  197               kernel.getProxyManager().destroyProxy(installer);
  198           }
  199       }
  200   
  201       public Dependency[] checkPrerequisites(PluginType plugin) {
  202           PluginInstaller installer = getPluginInstaller();
  203           try {
  204               return installer.checkPrerequisites(plugin);
  205           } finally {
  206               kernel.getProxyManager().destroyProxy(installer);
  207           }
  208       }
  209   
  210   
  211       public DownloadResults install(PluginListType configsToInstall, String defaultRepository, boolean restrictToDefaultRepository, String username, String password) {
  212           PluginInstaller installer = getPluginInstaller();
  213           try {
  214               return installer.install(configsToInstall, defaultRepository, restrictToDefaultRepository, username, password);
  215           } finally {
  216               kernel.getProxyManager().destroyProxy(installer);
  217           }
  218       }
  219   
  220       public void install(PluginListType configsToInstall, String defaultRepository, boolean restrictToDefaultRepository, String username, String password, DownloadPoller poller) {
  221           PluginInstaller installer = getPluginInstaller();
  222           try {
  223               installer.install(configsToInstall, defaultRepository, restrictToDefaultRepository, username, password, poller);
  224           } finally {
  225               kernel.getProxyManager().destroyProxy(installer);
  226           }
  227       }
  228   
  229       public Object startInstall(PluginListType configsToInstall, String defaultRepository, boolean restrictToDefaultRepository, String username, String password) {
  230           PluginInstaller installer = getPluginInstaller();
  231           try {
  232               return installer.startInstall(configsToInstall, defaultRepository, restrictToDefaultRepository, username, password);
  233           } finally {
  234               kernel.getProxyManager().destroyProxy(installer);
  235           }
  236       }
  237   
  238       public Object startInstall(File carFile, String defaultRepository, boolean restrictToDefaultRepository, String username, String password) {
  239           File[] args = new File[]{carFile};
  240           if (!isSameMachine) {
  241               AbstractDeployCommand progress = new AbstractDeployCommand(CommandType.DISTRIBUTE, kernel, null, null, null,
  242                       null, null, false) {
  243                   public void run() {
  244                   }
  245               };
  246               progress.addProgressListener(new ProgressListener() {
  247                   public void handleProgressEvent(ProgressEvent event) {
  248                       log.info(event.getDeploymentStatus().getMessage());
  249                   }
  250               });
  251               progress.setCommandContext(commandContext);
  252               RemoteDeployUtil.uploadFilesToServer(args, progress);
  253           }
  254           PluginInstaller installer = getPluginInstaller();
  255           try {
  256               // make sure to pass args[0] as RemoteDeployUtil.uploadFilesToServer will update
  257               // the args argument with the filenames returned from the server
  258               return installer.startInstall(args[0], defaultRepository, restrictToDefaultRepository, username, password);
  259           } finally {
  260               kernel.getProxyManager().destroyProxy(installer);
  261           }
  262       }
  263   
  264       public DownloadResults checkOnInstall(Object key) {
  265           PluginInstaller installer = getPluginInstaller();
  266           try {
  267               return installer.checkOnInstall(key);
  268           } finally {
  269               kernel.getProxyManager().destroyProxy(installer);
  270           }
  271       }
  272   
  273       public DownloadResults checkOnInstall(Object key, boolean remove) {
  274           PluginInstaller installer = getPluginInstaller();
  275           try {
  276               return installer.checkOnInstall(key, remove);
  277           } finally {
  278               kernel.getProxyManager().destroyProxy(installer);
  279           }
  280       }
  281   
  282       private PluginInstaller getPluginInstaller() {
  283           return getImplementation(PluginInstaller.class);
  284       }
  285       private ServerArchiver getServerArchiver() {
  286           return getImplementation(ServerArchiver.class);
  287       }
  288   
  289       //not likely to be useful remotely
  290       public PluginListType createPluginListForRepositories(String repo) throws NoSuchStoreException {
  291           PluginInstaller installer = getPluginInstaller();
  292           try {
  293               return installer.createPluginListForRepositories(repo);
  294           } finally {
  295               kernel.getProxyManager().destroyProxy(installer);
  296           }
  297       }
  298   
  299       public Map getInstalledPlugins() {
  300           PluginInstaller installer = getPluginInstaller();
  301           try {
  302               return installer.getInstalledPlugins();
  303           } finally {
  304               kernel.getProxyManager().destroyProxy(installer);
  305           }
  306       }
  307   
  308       public PluginType getPluginMetadata(Artifact configId) {
  309           PluginInstaller installer = getPluginInstaller();
  310           try {
  311               return installer.getPluginMetadata(configId);
  312           } finally {
  313               kernel.getProxyManager().destroyProxy(installer);
  314           }
  315       }
  316   
  317       public void updatePluginMetadata(PluginType metadata) {
  318           PluginInstaller installer = getPluginInstaller();
  319           try {
  320               installer.updatePluginMetadata(metadata);
  321           } finally {
  322               kernel.getProxyManager().destroyProxy(installer);
  323           }
  324       }
  325   
  326       public URL[] getRepositories() {
  327           List<URL> list = new ArrayList<URL>();
  328           Set<AbstractName> set = kernel.listGBeans(new AbstractNameQuery(PluginRepositoryList.class.getName()));
  329           for (AbstractName name : set) {
  330               PluginRepositoryList repo = (PluginRepositoryList) kernel.getProxyManager().createProxy(name,
  331                       PluginRepositoryList.class);
  332               list.addAll(repo.getRepositories());
  333               kernel.getProxyManager().destroyProxy(repo);
  334           }
  335           return list.toArray(new URL[list.size()]);
  336       }
  337   
  338       public Artifact installLibrary(File libFile, String groupId) throws IOException {
  339           File[] args = new File[]{libFile};
  340           if(!isSameMachine) {
  341               AbstractDeployCommand progress = new AbstractDeployCommand(CommandType.DISTRIBUTE, kernel, null, null, null, null, null, false) {
  342                   public void run() {
  343                   }
  344               };
  345               progress.addProgressListener(new ProgressListener() {
  346                   public void handleProgressEvent(ProgressEvent event) {
  347                       log.info(event.getDeploymentStatus().getMessage());
  348                   }
  349               });
  350               progress.setCommandContext(commandContext);
  351               RemoteDeployUtil.uploadFilesToServer(args, progress);
  352           }
  353           Set<AbstractName> set = kernel.listGBeans(new AbstractNameQuery(PluginInstaller.class.getName()));
  354           for (AbstractName name : set) {
  355               PluginInstaller installer = (PluginInstaller) kernel.getProxyManager().createProxy(name, PluginInstaller.class);
  356               // make sure to pass args[0] as RemoteDeployUtil.uploadFilesToServer will update
  357               // the args argument with the filenames returned from the server
  358               Artifact artifact = installer.installLibrary(args[0], groupId);
  359               kernel.getProxyManager().destroyProxy(installer);
  360               return artifact;
  361           }
  362           return null;
  363       }
  364   
  365       public DownloadResults installPluginList(String targetRepositoryPath, String relativeTargetServerPath, PluginListType pluginList) throws Exception {
  366           PluginInstaller installer = getPluginInstaller();
  367           try {
  368               return installer.installPluginList(targetRepositoryPath, relativeTargetServerPath, pluginList);
  369           } finally {
  370               kernel.getProxyManager().destroyProxy(installer);
  371           }
  372       }
  373   
  374       public void mergeOverrides(String server, AttributesType overrides) throws InvalidGBeanException, IOException {
  375           PluginInstaller installer = getPluginInstaller();
  376           try {
  377               installer.mergeOverrides(server, overrides);
  378           } finally {
  379               kernel.getProxyManager().destroyProxy(installer);
  380           }
  381       }
  382   
  383       public File archive(String sourcePath, String destPath, Artifact artifact) throws ArchiverException, IOException {
  384           ServerArchiver archiver = getServerArchiver();
  385           try {
  386               return archiver.archive(sourcePath, destPath, artifact);
  387           } finally {
  388               kernel.getProxyManager().destroyProxy(archiver);
  389           }
  390       }
  391   
  392       public static final GBeanInfo GBEAN_INFO;
  393       public static final String GBEAN_REF_MODULE_CONFIGURERS = "ModuleConfigurers";
  394   
  395       static {
  396           GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(RemoteDeploymentManager.class,
  397                   "RemoteDeploymentManager");
  398           infoFactory.addInterface(GeronimoDeploymentManager.class);
  399           infoFactory.addReference(GBEAN_REF_MODULE_CONFIGURERS, ModuleConfigurer.class);
  400   
  401           infoFactory.setConstructor(new String[]{GBEAN_REF_MODULE_CONFIGURERS});
  402   
  403           GBEAN_INFO = infoFactory.getBeanInfo();
  404       }
  405   
  406       public static GBeanInfo getGBeanInfo() {
  407           return GBEAN_INFO;
  408       }
  409   
  410   }

Home » geronimo-2.2-source-release » org.apache.geronimo.deployment.plugin.jmx » [javadoc | source]