View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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.Deployer;
34  import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
35  import org.apache.geronimo.gbean.AbstractName;
36  import org.apache.geronimo.gbean.AbstractNameQuery;
37  import org.apache.geronimo.kernel.Kernel;
38  
39  /**
40   * @version $Rev: 399026 $ $Date: 2006-05-02 13:03:28 -0700 (Tue, 02 May 2006) $
41   */
42  public abstract class AbstractDeployCommand extends CommandSupport {
43      protected final Kernel kernel;
44      private static final String[] DEPLOY_SIG = {boolean.class.getName(), File.class.getName(), File.class.getName(), String.class.getName()};
45      protected final boolean spool;
46      protected File moduleArchive;
47      protected File deploymentPlan;
48      protected InputStream moduleStream;
49      protected InputStream deploymentStream;
50      protected AbstractName deployer;
51  
52      public AbstractDeployCommand(CommandType command, Kernel kernel, File moduleArchive, File deploymentPlan, InputStream moduleStream, InputStream deploymentStream, boolean spool) {
53          super(command);
54          this.kernel = kernel;
55          this.moduleArchive = moduleArchive;
56          this.deploymentPlan = deploymentPlan;
57          this.moduleStream = moduleStream;
58          this.deploymentStream = deploymentStream;
59          this.spool = spool;
60          deployer = getDeployerName();
61      }
62  
63      private AbstractName getDeployerName() {
64          Set deployers = kernel.listGBeans(new AbstractNameQuery("org.apache.geronimo.deployment.Deployer"));
65          if (deployers.isEmpty()) {
66              fail("No Deployer GBean present in running Geronimo server. " +
67                   "This usually indicates a serious problem with the configuration of " +
68                   "your running Geronimo server.  If " +
69                   "the deployer is present but not started, the workaround is to run " +
70                   "a deploy command like 'start geronimo/geronimo-gbean-deployer/1.0/car'.  " +
71                   "If the deployer service is not present at all (it was undeployed) then " +
72                   "you need to either re-install Geronimo or get a deployment plan for the " +
73                   "runtime deployer and distribute it while the server is not running and " +
74                   "then start the server with a command like the above.  For help on this, " +
75                   "write to user@geronimo.apache.org and include the contents of your " +
76                   "var/config/config.xml file.");
77              return null;
78          }
79          Iterator j = deployers.iterator();
80          AbstractName deployer = (AbstractName) j.next();
81          if (j.hasNext()) {
82              fail("More than one deployer found");
83              return null;
84          }
85          return deployer;
86  
87      }
88  
89      protected void copyTo(File outfile, InputStream is) throws IOException {
90          byte[] buffer = new byte[4096];
91          int count;
92          OutputStream os = new FileOutputStream(outfile);
93          try {
94              while ((count = is.read(buffer)) > 0) {
95                  os.write(buffer, 0, count);
96              }
97          } finally {
98              os.close();
99          }
100     }
101 
102     protected void doDeploy(Target target, boolean finished) throws Exception {
103         File[] args = {moduleArchive, deploymentPlan};
104         massageFileNames(args);
105         Object deployParams[] = new Object[] {Boolean.valueOf(commandContext.isInPlace()), args[0], args[1], target.getName()};
106         List objectNames = (List) kernel.invoke(deployer, "deploy", deployParams, DEPLOY_SIG);
107         if (objectNames == null || objectNames.isEmpty()) {
108             throw new DeploymentException("Server didn't deploy anything");
109         }
110         String parentName = (String) objectNames.get(0);
111         String[] childIDs = new String[objectNames.size()-1];
112         for (int j=0; j < childIDs.length; j++) {
113             childIDs[j] = (String)objectNames.get(j+1);
114         }
115 
116         TargetModuleIDImpl moduleID = new TargetModuleIDImpl(target, parentName, childIDs);
117         if(isWebApp(kernel, parentName)) {
118             moduleID.setType(ModuleType.WAR);
119         }
120         if(moduleID.getChildTargetModuleID() != null) {
121             for (int i = 0; i < moduleID.getChildTargetModuleID().length; i++) {
122                 TargetModuleIDImpl id = (TargetModuleIDImpl) moduleID.getChildTargetModuleID()[i];
123                 if(isWebApp(kernel, id.getModuleID())) {
124                     id.setType(ModuleType.WAR);
125                 }
126             }
127         }
128         addModule(moduleID);
129         if(finished) {
130             addWebURLs(kernel);
131             complete("Completed with id " + parentName);
132         }
133     }
134 
135     protected void massageFileNames(File[] inputs) {
136     }
137 
138     public URL getRemoteDeployUploadURL() throws Exception {
139        return new URL((String)kernel.getAttribute(deployer, "remoteDeployUploadURL"));
140     }
141 }