001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.geronimo.deployment.plugin.local;
018
019 import java.io.File;
020 import java.io.FileOutputStream;
021 import java.io.IOException;
022 import java.io.InputStream;
023 import java.io.OutputStream;
024 import java.net.URL;
025 import java.util.Iterator;
026 import java.util.List;
027 import java.util.Set;
028 import javax.enterprise.deploy.shared.CommandType;
029 import javax.enterprise.deploy.shared.ModuleType;
030 import javax.enterprise.deploy.spi.Target;
031
032 import org.apache.geronimo.common.DeploymentException;
033 import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
034 import org.apache.geronimo.gbean.AbstractName;
035 import org.apache.geronimo.gbean.AbstractNameQuery;
036 import org.apache.geronimo.kernel.Kernel;
037
038 /**
039 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
040 */
041 public abstract class AbstractDeployCommand extends CommandSupport {
042 protected final Kernel kernel;
043 private static final String[] DEPLOY_SIG = {boolean.class.getName(), File.class.getName(), File.class.getName(), String.class.getName()};
044 protected final boolean spool;
045 protected File moduleArchive;
046 protected File deploymentPlan;
047 protected final ModuleType moduleType;
048 protected InputStream moduleStream;
049 protected InputStream deploymentStream;
050 protected AbstractName deployer;
051
052 public AbstractDeployCommand(CommandType command, Kernel kernel, File moduleArchive, File deploymentPlan, ModuleType moduleType, InputStream moduleStream, InputStream deploymentStream, boolean spool) {
053 super(command);
054 this.kernel = kernel;
055 this.moduleArchive = moduleArchive;
056 this.deploymentPlan = deploymentPlan;
057 this.moduleType = moduleType;
058 this.moduleStream = moduleStream;
059 this.deploymentStream = deploymentStream;
060 this.spool = spool;
061 deployer = getDeployerName();
062 }
063
064 private AbstractName getDeployerName() {
065 Set deployers = kernel.listGBeans(new AbstractNameQuery("org.apache.geronimo.deployment.Deployer"));
066 if (deployers.isEmpty()) {
067 fail("No Deployer GBean present in running Geronimo server. " +
068 "This usually indicates a serious problem with the configuration of " +
069 "your running Geronimo server. If " +
070 "the deployer is present but not started, the workaround is to run " +
071 "a deploy command like 'start geronimo/geronimo-gbean-deployer/1.0/car'. " +
072 "If the deployer service is not present at all (it was undeployed) then " +
073 "you need to either re-install Geronimo or get a deployment plan for the " +
074 "runtime deployer and distribute it while the server is not running and " +
075 "then start the server with a command like the above. For help on this, " +
076 "write to user@geronimo.apache.org and include the contents of your " +
077 "var/config/config.xml file.");
078 return null;
079 }
080 Iterator j = deployers.iterator();
081 AbstractName deployer = (AbstractName) j.next();
082 if (j.hasNext()) {
083 fail("More than one deployer found");
084 return null;
085 }
086 return deployer;
087
088 }
089
090 // be careful to clean up the temp file... we tell the vm to delete this on exit
091 // but VMs can't be trusted to acutally delete the file
092 // Copied from DeploymentUtil
093 protected static File createTempFile(String extension) throws IOException {
094 File tempFile = File.createTempFile("geronimo-deploymentUtil", extension == null? ".tmpdir": extension);
095 tempFile.deleteOnExit();
096 return tempFile;
097 }
098
099 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 protected 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 }