Home » geronimo-2.2-source-release » org.apache.geronimo.deployment.plugin.local » [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.local;
   18   
   19   import java.io.File;
   20   import java.io.InputStream;
   21   import java.util.Iterator;
   22   
   23   import javax.enterprise.deploy.shared.CommandType;
   24   import javax.enterprise.deploy.shared.ModuleType;
   25   import javax.enterprise.deploy.spi.Target;
   26   import javax.enterprise.deploy.spi.TargetModuleID;
   27   
   28   import org.apache.geronimo.deployment.plugin.ConfigIDExtractor;
   29   import org.apache.geronimo.deployment.plugin.TargetImpl;
   30   import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
   31   import org.apache.geronimo.gbean.AbstractName;
   32   import org.apache.geronimo.kernel.InternalKernelException;
   33   import org.apache.geronimo.kernel.Kernel;
   34   import org.apache.geronimo.kernel.config.ConfigurationManager;
   35   import org.apache.geronimo.kernel.config.ConfigurationUtil;
   36   import org.apache.geronimo.kernel.config.LifecycleResults;
   37   import org.apache.geronimo.kernel.config.NoSuchConfigException;
   38   import org.apache.geronimo.kernel.repository.Artifact;
   39   
   40   /**
   41    * @version $Rev: 689884 $ $Date: 2008-08-28 09:42:47 -0700 (Thu, 28 Aug 2008) $
   42    */
   43   public class RedeployCommand extends AbstractDeployCommand {
   44       private static final String[] IS_IN_PLACE_CONFIGURATION_SIG =  {Artifact.class.getName()};
   45       private static final String IS_IN_PLACE_CONFIGURATION_METH = "isInPlaceConfiguration";
   46   
   47       private final TargetModuleID[] modules;
   48   
   49       public RedeployCommand(Kernel kernel, TargetModuleID[] moduleIDList, File moduleArchive, File deploymentPlan) {
   50           super(CommandType.REDEPLOY, kernel, moduleArchive, deploymentPlan, null, null, null, false);
   51           this.modules = moduleIDList;
   52       }
   53   
   54       public RedeployCommand(Kernel kernel, TargetModuleID[] moduleIDList, InputStream moduleArchive, InputStream deploymentPlan) {
   55           super(CommandType.REDEPLOY, kernel, null, null, null, moduleArchive, deploymentPlan, true);
   56           this.modules = moduleIDList;
   57       }
   58   
   59       public void run() {
   60           if (deployer == null) {
   61               return;
   62           }
   63   
   64           try {
   65               if (spool) {
   66                   if (moduleStream != null) {
   67                       moduleArchive = createTempFile(moduleType == null? null: moduleType.getModuleExtension());
   68                       copyTo(moduleArchive, moduleStream);
   69                   }
   70                   if (deploymentStream != null) {
   71                       deploymentPlan = createTempFile(null);
   72                       copyTo(deploymentPlan, deploymentStream);
   73                   }
   74               }
   75               Artifact configID = null;
   76               if(deploymentPlan != null) {
   77                   String extracted = ConfigIDExtractor.extractModuleIdFromPlan(deploymentPlan);
   78                   if(extracted != null) {
   79                       configID = Artifact.create(extracted);
   80                   }
   81               } else {
   82                   String extracted = ConfigIDExtractor.extractModuleIdFromArchive(moduleArchive);
   83                   if(extracted != null) {
   84                       configID = Artifact.create(extracted);
   85                   }
   86               }
   87               if(configID != null && configID.getGroupId() == null) {
   88                   configID = new Artifact(Artifact.DEFAULT_GROUP_ID, configID.getArtifactId(),
   89                                           configID.getVersion(), configID.getType());
   90               }
   91   
   92               ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
   93               try {
   94                   for (int i = 0; i < modules.length; i++) {
   95                       TargetModuleIDImpl module = (TargetModuleIDImpl) modules[i];
   96                       Artifact artifact = Artifact.create(module.getModuleID());
   97                       if(configID != null && configID.isResolved()) {
   98                           if(configID.getGroupId().equals(artifact.getGroupId()) &&
   99                                   configID.getArtifactId().equals(artifact.getArtifactId()) &&
  100                                   configID.getVersion().equals(artifact.getVersion())) {
  101                               redeploySameConfiguration(configurationManager, artifact, module.getTarget());
  102                           } else {
  103                               redeployUpdatedConfiguration(configurationManager, artifact, module.getTarget());
  104                           }
  105                       } else {
  106                           redeployUpdatedConfiguration(configurationManager, artifact, module.getTarget());
  107                       }
  108                   }
  109               } finally {
  110                   ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager);
  111               }
  112               addWebURLs(kernel);
  113               complete("Completed");
  114           } catch (Throwable e) {
  115               doFail(e);
  116           } finally {
  117               if (spool) {
  118                   if (moduleArchive != null) {
  119                       moduleArchive.delete();
  120                   }
  121                   if (deploymentPlan != null) {
  122                       deploymentPlan.delete();
  123                   }
  124               }
  125           }
  126       }
  127   
  128       private void redeployUpdatedConfiguration(ConfigurationManager manager, Artifact previous, Target target) throws Exception, NoSuchConfigException {
  129           // Send the new configuration to the server
  130   
  131               // if the configuration is an in-place one, then redeploys
  132               // in in-place mode.
  133           TargetImpl impl = (TargetImpl) target;
  134           AbstractName storeName = impl.getAbstractName();
  135           Boolean inPlaceConfiguration = (Boolean) kernel.invoke(storeName, IS_IN_PLACE_CONFIGURATION_METH, new Object[]{previous}, IS_IN_PLACE_CONFIGURATION_SIG);
  136           commandContext.setInPlace(inPlaceConfiguration.booleanValue());
  137           doDeploy(target, false);
  138           Artifact configID = Artifact.create(getResultTargetModuleIDs()[0].getModuleID());
  139           LifecycleResults results = manager.reloadConfiguration(previous, configID.getVersion());
  140   
  141           // Activate it
  142           //todo: make this asynchronous
  143           boolean newStarted = false;
  144           for (Iterator it = results.getStopped().iterator(); it.hasNext();) {
  145               Artifact name = (Artifact) it.next();
  146               updateStatus("Stopped "+name);
  147           }
  148           for (Iterator it = results.getUnloaded().iterator(); it.hasNext();) {
  149               Artifact name = (Artifact) it.next();
  150               updateStatus("Unloaded "+name);
  151           }
  152           for (Iterator it = results.getLoaded().iterator(); it.hasNext();) {
  153               Artifact name = (Artifact) it.next();
  154               updateStatus("Loaded "+name);
  155           }
  156           for (Iterator it = results.getStarted().iterator(); it.hasNext();) {
  157               Artifact name = (Artifact) it.next();
  158               updateStatus("Started "+name);
  159               if(configID.matches(name)) {
  160                   newStarted = true;
  161               }
  162           }
  163           for (Iterator it = results.getFailed().keySet().iterator(); it.hasNext();) {
  164               Artifact name = (Artifact) it.next();
  165               updateStatus("Failed on "+name+": "+results.getFailedCause(name).getMessage());
  166               doFail((Exception)results.getFailedCause(name));
  167           }
  168           if(results.getFailed().size() == 0 && !newStarted) {
  169               updateStatus("Note: new module was not started (probably because old module was not running).");
  170           }
  171       }
  172   
  173       private void redeploySameConfiguration(ConfigurationManager configurationManager, Artifact configID, Target target) throws Exception {
  174           if(!configID.isResolved()) {
  175               throw new IllegalStateException("Cannot redeploy same module when module ID is not fully resolved ("+configID+")");
  176           }
  177           try {
  178               configurationManager.stopConfiguration(configID);
  179               updateStatus("Stopped "+configID);
  180           } catch (InternalKernelException e) {
  181               Exception cause = (Exception)e.getCause();
  182               if(cause instanceof NoSuchConfigException) {
  183                   // The modules isn't loaded -- that's OK
  184               } else {
  185                   throw cause;
  186               }
  187           } catch(NoSuchConfigException e) {
  188               // The module isn't loaded -- that's OK
  189           }
  190           try {
  191               configurationManager.unloadConfiguration(configID);
  192               updateStatus("Unloaded "+configID);
  193           } catch(InternalKernelException e) {
  194               Exception cause = (Exception)e.getCause();
  195               if(cause instanceof NoSuchConfigException) {
  196                   // The modules isn't loaded -- that's OK
  197               } else {
  198                   throw cause;
  199               }
  200           } catch (NoSuchConfigException e) {
  201               // The modules isn't loaded -- that's OK
  202           }
  203   
  204           // if the configuration is an in-place one, then redeploys
  205           // in in-place mode.
  206           TargetImpl impl = (TargetImpl) target;
  207           AbstractName storeName = impl.getAbstractName();
  208           Boolean inPlaceConfiguration = (Boolean) kernel.invoke(storeName, IS_IN_PLACE_CONFIGURATION_METH, new Object[]{configID}, IS_IN_PLACE_CONFIGURATION_SIG);
  209           commandContext.setInPlace(inPlaceConfiguration.booleanValue());
  210   
  211           try {
  212               configurationManager.uninstallConfiguration(configID);
  213               updateStatus("Uninstalled "+configID);
  214           } catch(InternalKernelException e) {
  215               Exception cause = (Exception)e.getCause();
  216               if(cause instanceof NoSuchConfigException) {
  217                   throw new IllegalStateException("Module "+configID+" is not installed!", cause);
  218               } else {
  219                   throw cause;
  220               }
  221           } catch (NoSuchConfigException e) {
  222               throw new IllegalStateException("Module "+configID+" is not installed!", e);
  223           }
  224   
  225           doDeploy(target, false);
  226           updateStatus("Deployed "+configID);
  227   
  228           configurationManager.loadConfiguration(configID);
  229           configurationManager.startConfiguration(configID);
  230           updateStatus("Started " + configID);
  231       }
  232   }

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