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.InputStream;
   21   import java.util.ArrayList;
   22   import java.util.Collection;
   23   import java.util.List;
   24   import java.util.Locale;
   25   
   26   import javax.enterprise.deploy.model.DeployableObject;
   27   import javax.enterprise.deploy.shared.DConfigBeanVersionType;
   28   import javax.enterprise.deploy.shared.ModuleType;
   29   import javax.enterprise.deploy.spi.DeploymentConfiguration;
   30   import javax.enterprise.deploy.spi.DeploymentManager;
   31   import javax.enterprise.deploy.spi.Target;
   32   import javax.enterprise.deploy.spi.TargetModuleID;
   33   import javax.enterprise.deploy.spi.exceptions.DConfigBeanVersionUnsupportedException;
   34   import javax.enterprise.deploy.spi.exceptions.InvalidModuleException;
   35   import javax.enterprise.deploy.spi.exceptions.TargetException;
   36   import javax.enterprise.deploy.spi.status.ProgressObject;
   37   
   38   import org.apache.geronimo.deployment.ModuleConfigurer;
   39   import org.apache.geronimo.deployment.plugin.TargetImpl;
   40   import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
   41   import org.apache.geronimo.deployment.plugin.local.CommandSupport;
   42   import org.apache.geronimo.deployment.plugin.local.DistributeCommand;
   43   import org.apache.geronimo.deployment.plugin.local.RedeployCommand;
   44   import org.apache.geronimo.deployment.plugin.local.StartCommand;
   45   import org.apache.geronimo.deployment.plugin.local.StopCommand;
   46   import org.apache.geronimo.deployment.plugin.local.UndeployCommand;
   47   import org.apache.geronimo.gbean.AbstractName;
   48   import org.apache.geronimo.kernel.Kernel;
   49   import org.apache.geronimo.kernel.config.ConfigurationInfo;
   50   import org.apache.geronimo.kernel.config.ConfigurationManager;
   51   import org.apache.geronimo.kernel.config.ConfigurationModuleType;
   52   import org.apache.geronimo.kernel.config.ConfigurationUtil;
   53   import org.apache.geronimo.kernel.management.State;
   54   
   55   
   56   /**
   57    * @version $Rev: 597690 $ $Date: 2007-11-23 07:43:02 -0800 (Fri, 23 Nov 2007) $
   58    */
   59   public abstract class JMXDeploymentManager implements DeploymentManager {
   60   
   61       protected Kernel kernel;
   62       private ConfigurationManager configurationManager;
   63       protected CommandContext commandContext;
   64       private final Collection<ModuleConfigurer> moduleConfigurers;
   65   
   66       public JMXDeploymentManager(Collection<ModuleConfigurer> moduleConfigurers) {
   67           if (null == moduleConfigurers) {
   68               throw new IllegalArgumentException("moduleConfigurers is required");
   69           }
   70           this.moduleConfigurers = moduleConfigurers;
   71       }
   72       
   73       protected void initialize(Kernel kernel) {
   74           this.kernel = kernel;
   75           configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
   76           commandContext = new CommandContext(true, true, null, null, false);
   77       }
   78   
   79       public void setAuthentication(String username, String password) {
   80           commandContext.setUsername(username);
   81           commandContext.setPassword(password);
   82       }
   83   
   84       public void release() {
   85           if(kernel != null && configurationManager != null) {
   86               try {
   87                   ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager);
   88               } finally {
   89                   configurationManager = null;
   90                   kernel = null;
   91               }
   92           }
   93       }
   94   
   95       public Target[] getTargets() {
   96           if (kernel == null) {
   97               throw new IllegalStateException("Disconnected");
   98           }
   99           List stores = configurationManager.listStores();
  100           if (stores.isEmpty()) {
  101               return null;
  102           }
  103   
  104           Target[] targets = new Target[stores.size()];
  105           for (int i = 0; i < stores.size(); i++) {
  106               AbstractName storeName = (AbstractName) stores.get(i);
  107               targets[i] = new TargetImpl(storeName, null);
  108           }
  109           return targets;
  110       }
  111   
  112       public TargetModuleID[] getAvailableModules(final ModuleType moduleType, Target[] targetList) throws TargetException {
  113           ConfigFilter filter = new ConfigFilter() {
  114               public boolean accept(ConfigurationInfo info) {
  115                   return moduleType == null || info.getType() == ConfigurationModuleType.getFromValue(moduleType.getValue());
  116               }
  117           };
  118           return getModules(targetList, filter);
  119       }
  120   
  121       public TargetModuleID[] getNonRunningModules(final ModuleType moduleType, Target[] targetList) throws TargetException {
  122           ConfigFilter filter = new ConfigFilter() {
  123               public boolean accept(ConfigurationInfo info) {
  124                   return info.getState() != State.RUNNING && (moduleType == null || info.getType() == ConfigurationModuleType.getFromValue(moduleType.getValue()));
  125               }
  126           };
  127           return getModules(targetList, filter);
  128       }
  129   
  130       public TargetModuleID[] getRunningModules(final ModuleType moduleType, Target[] targetList) throws TargetException {
  131           ConfigFilter filter = new ConfigFilter() {
  132               public boolean accept(ConfigurationInfo info) {
  133                   return info.getState() == State.RUNNING && (moduleType == null || info.getType() == ConfigurationModuleType.getFromValue(moduleType.getValue()));
  134               }
  135           };
  136           return getModules(targetList, filter);
  137       }
  138   
  139       private static interface ConfigFilter {
  140           boolean accept(ConfigurationInfo info);
  141       }
  142   
  143       private TargetModuleID[] getModules(Target[] targetList, ConfigFilter filter) throws TargetException {
  144           if (kernel == null) {
  145               throw new IllegalStateException("Disconnected");
  146           }
  147   
  148           if (targetList == null) {
  149               return null;
  150           }
  151   
  152           try {
  153               ArrayList<TargetModuleIDImpl> result = new ArrayList<TargetModuleIDImpl>();
  154               for (Target aTargetList : targetList) {
  155                   TargetImpl target = (TargetImpl) aTargetList;
  156                   AbstractName storeName = target.getAbstractName();
  157                   List infos = configurationManager.listConfigurations(storeName);
  158                   for (Object info1 : infos) {
  159                       ConfigurationInfo info = (ConfigurationInfo) info1;
  160                       if (filter.accept(info)) {
  161                           String name = info.getConfigID().toString();
  162                           List list = CommandSupport.loadChildren(kernel, name);
  163                           TargetModuleIDImpl moduleID = new TargetModuleIDImpl(target, name, (String[]) list.toArray(new String[list.size()]));
  164                           moduleID.setType(CommandSupport.convertModuleType(info.getType()));
  165                           if (moduleID.getChildTargetModuleID() != null) {
  166                               for (int k = 0; k < moduleID.getChildTargetModuleID().length; k++) {
  167                                   TargetModuleIDImpl child = (TargetModuleIDImpl) moduleID.getChildTargetModuleID()[k];
  168                                   if (CommandSupport.isWebApp(kernel, child.getModuleID())) {
  169                                       child.setType(ModuleType.WAR);
  170                                   }
  171                               }
  172                           }
  173                           result.add(moduleID);
  174                       }
  175                   }
  176               }
  177               CommandSupport.addWebContextPaths(kernel, result);
  178               return result.size() == 0 ? null : result.toArray(new TargetModuleID[result.size()]);
  179           } catch (Exception e) {
  180               throw (TargetException) new TargetException(e.getMessage()).initCause(e);
  181           }
  182       }
  183   
  184       public ProgressObject distribute(Target[] targetList, File moduleArchive, File deploymentPlan) {
  185           if (kernel == null) {
  186               throw new IllegalStateException("Disconnected");
  187           }
  188           DistributeCommand command = createDistributeCommand(targetList, moduleArchive, deploymentPlan);
  189           command.setCommandContext(commandContext);
  190           new Thread(command).start();
  191           return command;
  192       }
  193   
  194       /**
  195        * @deprecated
  196        */
  197       public ProgressObject distribute(Target[] targetList, InputStream moduleArchive, InputStream deploymentPlan) {
  198           return distribute(targetList, null, moduleArchive, deploymentPlan);
  199       }
  200   
  201       public ProgressObject distribute(Target[] targetList, ModuleType moduleType, InputStream moduleArchive, InputStream deploymentPlan) throws IllegalStateException {
  202           if (kernel == null) {
  203               throw new IllegalStateException("Disconnected");
  204           }
  205           DistributeCommand command = createDistributeCommand(targetList, moduleType, moduleArchive, deploymentPlan);
  206           command.setCommandContext(commandContext);
  207           new Thread(command).start();
  208           return command;
  209       }
  210   
  211       public ProgressObject start(TargetModuleID[] moduleIDList) {
  212           if (kernel == null) {
  213               throw new IllegalStateException("Disconnected");
  214           }
  215           StartCommand command = new StartCommand(kernel, moduleIDList);
  216           command.setCommandContext(commandContext);
  217           new Thread(command).start();
  218           return command;
  219       }
  220   
  221       public ProgressObject stop(TargetModuleID[] moduleIDList) {
  222           if (kernel == null) {
  223               throw new IllegalStateException("Disconnected");
  224           }
  225           StopCommand command = new StopCommand(kernel, moduleIDList);
  226           command.setCommandContext(commandContext);
  227           new Thread(command).start();
  228           return command;
  229       }
  230   
  231       public ProgressObject undeploy(TargetModuleID[] moduleIDList) {
  232           if (kernel == null) {
  233               throw new IllegalStateException("Disconnected");
  234           }
  235           UndeployCommand command = new UndeployCommand(kernel, moduleIDList);
  236           command.setCommandContext(commandContext);
  237           new Thread(command).start();
  238           return command;
  239       }
  240   
  241       public boolean isRedeploySupported() {
  242           return true;
  243       }
  244   
  245       public ProgressObject redeploy(TargetModuleID[] moduleIDList, File moduleArchive, File deploymentPlan) {
  246           if (kernel == null) {
  247               throw new IllegalStateException("Disconnected");
  248           }
  249           RedeployCommand command = createRedeployCommand(moduleIDList, moduleArchive, deploymentPlan);
  250           command.setCommandContext(commandContext);
  251           new Thread(command).start();
  252           return command;
  253       }
  254   
  255       public ProgressObject redeploy(TargetModuleID[] moduleIDList, InputStream moduleArchive, InputStream deploymentPlan) {
  256           if (kernel == null) {
  257               throw new IllegalStateException("Disconnected");
  258           }
  259           RedeployCommand command = createRedeployCommand(moduleIDList, moduleArchive, deploymentPlan);
  260           command.setCommandContext(commandContext);
  261           new Thread(command).start();
  262           return command;
  263       }
  264   
  265       public Locale[] getSupportedLocales() {
  266           return new Locale[]{getDefaultLocale()};
  267       }
  268   
  269       public Locale getCurrentLocale() {
  270           return getDefaultLocale();
  271       }
  272   
  273       public Locale getDefaultLocale() {
  274           return Locale.getDefault();
  275       }
  276   
  277       public boolean isLocaleSupported(Locale locale) {
  278           return getDefaultLocale().equals(locale);
  279       }
  280   
  281       public void setLocale(Locale locale) {
  282           throw new UnsupportedOperationException("Cannot set Locale");
  283       }
  284   
  285       public DConfigBeanVersionType getDConfigBeanVersion() {
  286           return DConfigBeanVersionType.V1_4;
  287       }
  288   
  289       public boolean isDConfigBeanVersionSupported(DConfigBeanVersionType version) {
  290           return DConfigBeanVersionType.V1_4.equals(version);
  291       }
  292   
  293       public void setDConfigBeanVersion(DConfigBeanVersionType version) throws DConfigBeanVersionUnsupportedException {
  294           if (!isDConfigBeanVersionSupported(version)) {
  295               throw new DConfigBeanVersionUnsupportedException("Version not supported " + version);
  296           }
  297       }
  298   
  299       public DeploymentConfiguration createConfiguration(DeployableObject dObj) throws InvalidModuleException {
  300           if (dObj == null) {
  301               throw new NullPointerException("No deployable object supplied to configure");
  302           }
  303           ModuleConfigurer configurer = null;
  304           for (ModuleConfigurer moduleConfigurer : moduleConfigurers) {
  305               if (moduleConfigurer.getModuleType() == dObj.getType()) {
  306                   configurer = moduleConfigurer;
  307                  break;
  308               }
  309           }
  310           if (configurer == null) {
  311               throw new InvalidModuleException("No configurer for module type: " + dObj.getType() + " registered");
  312           }
  313           return configurer.createConfiguration(dObj);
  314       }
  315   
  316       protected DistributeCommand createDistributeCommand(Target[] targetList, File moduleArchive, File deploymentPlan) {
  317           return new DistributeCommand(kernel, targetList, moduleArchive, deploymentPlan);
  318       }
  319   
  320       protected DistributeCommand createDistributeCommand(Target[] targetList, ModuleType moduleType, InputStream moduleArchive, InputStream deploymentPlan) {
  321           return new DistributeCommand(kernel, targetList, moduleType, moduleArchive, deploymentPlan);
  322       }
  323   
  324       protected RedeployCommand createRedeployCommand(TargetModuleID[] moduleIDList, File moduleArchive, File deploymentPlan) {
  325           return new RedeployCommand(kernel, moduleIDList, moduleArchive, deploymentPlan);
  326       }
  327   
  328       protected RedeployCommand createRedeployCommand(TargetModuleID[] moduleIDList, InputStream moduleArchive, InputStream deploymentPlan) {
  329           return new RedeployCommand(kernel, moduleIDList, moduleArchive, deploymentPlan);
  330       }
  331   
  332       public void setLogConfiguration(boolean shouldLog, boolean verboseStatus) {
  333           commandContext.setLogErrors(shouldLog);
  334           commandContext.setVerbose(verboseStatus);
  335       }
  336   
  337       public void setInPlace(boolean inPlace) {
  338           commandContext.setInPlace(inPlace);
  339       }
  340   }

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