KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > plugin > local > AbstractDeployCommand


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 JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.enterprise.deploy.shared.CommandType JavaDoc;
29 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
30 import javax.enterprise.deploy.spi.Target JavaDoc;
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: 482870 $ $Date: 2006-12-05 21:50:50 -0500 (Tue, 05 Dec 2006) $
40  */

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