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.FileOutputStream;
   21   import java.io.IOException;
   22   import java.io.InputStream;
   23   import java.io.OutputStream;
   24   import java.net.URL;
   25   import java.util.Iterator;
   26   import java.util.List;
   27   import java.util.Set;
   28   import javax.enterprise.deploy.shared.CommandType;
   29   import javax.enterprise.deploy.shared.ModuleType;
   30   import javax.enterprise.deploy.spi.Target;
   31   
   32   import org.apache.geronimo.common.DeploymentException;
   33   import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
   34   import org.apache.geronimo.gbean.AbstractName;
   35   import org.apache.geronimo.gbean.AbstractNameQuery;
   36   import org.apache.geronimo.kernel.Kernel;
   37   
   38   /**
   39    * @version $Rev: 654200 $ $Date: 2008-05-07 11:17:09 -0700 (Wed, 07 May 2008) $
   40    */
   41   public abstract class AbstractDeployCommand extends CommandSupport {
   42       protected final Kernel kernel;
   43       private static final String[] DEPLOY_SIG = {boolean.class.getName(), File.class.getName(), File.class.getName(), String.class.getName()};
   44       protected final boolean spool;
   45       protected File moduleArchive;
   46       protected File deploymentPlan;
   47       protected final ModuleType moduleType;
   48       protected InputStream moduleStream;
   49       protected InputStream deploymentStream;
   50       protected AbstractName deployer;
   51   
   52       public AbstractDeployCommand(CommandType command, Kernel kernel, File moduleArchive, File deploymentPlan, ModuleType moduleType, InputStream moduleStream, InputStream deploymentStream, boolean spool) {
   53           super(command);
   54           this.kernel = kernel;
   55           this.moduleArchive = moduleArchive;
   56           this.deploymentPlan = deploymentPlan;
   57           this.moduleType = moduleType;
   58           this.moduleStream = moduleStream;
   59           this.deploymentStream = deploymentStream;
   60           this.spool = spool;
   61           deployer = getDeployerName();
   62       }
   63   
   64       private AbstractName getDeployerName() {
   65           Set deployers = kernel.listGBeans(new AbstractNameQuery("org.apache.geronimo.deployment.Deployer"));
   66           if (deployers.isEmpty()) {
   67               fail("No Deployer GBean present in running Geronimo server. " +
   68                    "This usually indicates a serious problem with the configuration of " +
   69                    "your running Geronimo server.  If " +
   70                    "the deployer is present but not started, the workaround is to run " +
   71                    "a deploy command like 'start geronimo/geronimo-gbean-deployer/1.0/car'.  " +
   72                    "If the deployer service is not present at all (it was undeployed) then " +
   73                    "you need to either re-install Geronimo or get a deployment plan for the " +
   74                    "runtime deployer and distribute it while the server is not running and " +
   75                    "then start the server with a command like the above.  For help on this, " +
   76                    "write to user@geronimo.apache.org and include the contents of your " +
   77                    "var/config/config.xml file.");
   78               return null;
   79           }
   80           Iterator j = deployers.iterator();
   81           AbstractName deployer = (AbstractName) j.next();
   82           if (j.hasNext()) {
   83               fail("More than one deployer found");
   84               return null;
   85           }
   86           return deployer;
   87   
   88       }
   89   
   90       // be careful to clean up the temp file... we tell the vm to delete this on exit
   91       // but VMs can't be trusted to acutally delete the file
   92       // Copied from DeploymentUtil
   93       protected static File createTempFile(String extension) throws IOException {
   94           File tempFile = File.createTempFile("geronimo-deploymentUtil", extension == null? ".tmpdir": extension);
   95           tempFile.deleteOnExit();
   96           return tempFile;
   97       }
   98   
   99       protected void copyTo(File outfile, InputStream is) throws IOException {
  100           byte[] buffer = new byte[4096];
  101           int count;
  102           OutputStream os = new FileOutputStream(outfile);
  103           try {
  104               while ((count = is.read(buffer)) > 0) {
  105                   os.write(buffer, 0, count);
  106               }
  107           } finally {
  108               os.close();
  109           }
  110       }
  111   
  112       public void doDeploy(Target target, boolean finished) throws Exception {
  113           File[] args = {moduleArchive, deploymentPlan};
  114           massageFileNames(args);
  115           Object deployParams[] = new Object[] {Boolean.valueOf(commandContext.isInPlace()), args[0], args[1], target.getName()};
  116           List objectNames = (List) kernel.invoke(deployer, "deploy", deployParams, DEPLOY_SIG);
  117           if (objectNames == null || objectNames.isEmpty()) {
  118               throw new DeploymentException("Server didn't deploy anything");
  119           }
  120           String parentName = (String) objectNames.get(0);
  121           String[] childIDs = new String[objectNames.size()-1];
  122           for (int j=0; j < childIDs.length; j++) {
  123               childIDs[j] = (String)objectNames.get(j+1);
  124           }
  125   
  126           TargetModuleIDImpl moduleID = new TargetModuleIDImpl(target, parentName, childIDs);
  127           if(isWebApp(kernel, parentName)) {
  128               moduleID.setType(ModuleType.WAR);
  129           }
  130           if(moduleID.getChildTargetModuleID() != null) {
  131               for (int i = 0; i < moduleID.getChildTargetModuleID().length; i++) {
  132                   TargetModuleIDImpl id = (TargetModuleIDImpl) moduleID.getChildTargetModuleID()[i];
  133                   if(isWebApp(kernel, id.getModuleID())) {
  134                       id.setType(ModuleType.WAR);
  135                   }
  136               }
  137           }
  138           addModule(moduleID);
  139           if(finished) {
  140               addWebURLs(kernel);
  141               complete("Completed with id " + parentName);
  142           }
  143       }
  144   
  145       protected void massageFileNames(File[] inputs) {
  146       }
  147   
  148       public URL getRemoteDeployUploadURL() throws Exception {
  149          return new URL((String)kernel.getAttribute(deployer, "remoteDeployUploadURL"));
  150       }
  151   }

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