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: */package org.apache.geronimo.deployment.plugin.local;
017:
018: import java.io.File;
019: import java.io.FileOutputStream;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.OutputStream;
023: import java.net.URL;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Set;
027: import javax.enterprise.deploy.shared.CommandType;
028: import javax.enterprise.deploy.shared.ModuleType;
029: import javax.enterprise.deploy.spi.Target;
030:
031: import org.apache.geronimo.common.DeploymentException;
032: import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
033: import org.apache.geronimo.gbean.AbstractName;
034: import org.apache.geronimo.gbean.AbstractNameQuery;
035: import org.apache.geronimo.kernel.Kernel;
036:
037: /**
038: * @version $Rev: 512979 $ $Date: 2007-02-28 13:28:28 -0800 (Wed, 28 Feb 2007) $
039: */
040: public abstract class AbstractDeployCommand extends CommandSupport {
041: protected final Kernel kernel;
042: private static final String[] DEPLOY_SIG = {
043: boolean.class.getName(), File.class.getName(),
044: File.class.getName(), String.class.getName() };
045: protected final boolean spool;
046: protected File moduleArchive;
047: protected File deploymentPlan;
048: protected final ModuleType moduleType;
049: protected InputStream moduleStream;
050: protected InputStream deploymentStream;
051: protected AbstractName deployer;
052:
053: public AbstractDeployCommand(CommandType command, Kernel kernel,
054: File moduleArchive, File deploymentPlan,
055: ModuleType moduleType, InputStream moduleStream,
056: InputStream deploymentStream, boolean spool) {
057: super (command);
058: this .kernel = kernel;
059: this .moduleArchive = moduleArchive;
060: this .deploymentPlan = deploymentPlan;
061: this .moduleType = moduleType;
062: this .moduleStream = moduleStream;
063: this .deploymentStream = deploymentStream;
064: this .spool = spool;
065: deployer = getDeployerName();
066: }
067:
068: private AbstractName getDeployerName() {
069: Set deployers = kernel.listGBeans(new AbstractNameQuery(
070: "org.apache.geronimo.deployment.Deployer"));
071: if (deployers.isEmpty()) {
072: fail("No Deployer GBean present in running Geronimo server. "
073: + "This usually indicates a serious problem with the configuration of "
074: + "your running Geronimo server. If "
075: + "the deployer is present but not started, the workaround is to run "
076: + "a deploy command like 'start geronimo/geronimo-gbean-deployer/1.0/car'. "
077: + "If the deployer service is not present at all (it was undeployed) then "
078: + "you need to either re-install Geronimo or get a deployment plan for the "
079: + "runtime deployer and distribute it while the server is not running and "
080: + "then start the server with a command like the above. For help on this, "
081: + "write to user@geronimo.apache.org and include the contents of your "
082: + "var/config/config.xml file.");
083: return null;
084: }
085: Iterator j = deployers.iterator();
086: AbstractName deployer = (AbstractName) j.next();
087: if (j.hasNext()) {
088: fail("More than one deployer found");
089: return null;
090: }
091: return deployer;
092:
093: }
094:
095: // be careful to clean up the temp file... we tell the vm to delete this on exit
096: // but VMs can't be trusted to acutally delete the file
097: // Copied from DeploymentUtil
098: protected static File createTempFile(String extension)
099: throws IOException {
100: File tempFile = File.createTempFile("geronimo-deploymentUtil",
101: extension == null ? ".tmpdir" : extension);
102: tempFile.deleteOnExit();
103: return tempFile;
104: }
105:
106: protected void copyTo(File outfile, InputStream is)
107: throws IOException {
108: byte[] buffer = new byte[4096];
109: int count;
110: OutputStream os = new FileOutputStream(outfile);
111: try {
112: while ((count = is.read(buffer)) > 0) {
113: os.write(buffer, 0, count);
114: }
115: } finally {
116: os.close();
117: }
118: }
119:
120: protected void doDeploy(Target target, boolean finished)
121: throws Exception {
122: File[] args = { moduleArchive, deploymentPlan };
123: massageFileNames(args);
124: Object deployParams[] = new Object[] {
125: Boolean.valueOf(commandContext.isInPlace()), args[0],
126: args[1], target.getName() };
127: List objectNames = (List) kernel.invoke(deployer, "deploy",
128: deployParams, DEPLOY_SIG);
129: if (objectNames == null || objectNames.isEmpty()) {
130: throw new DeploymentException(
131: "Server didn't deploy anything");
132: }
133: String parentName = (String) objectNames.get(0);
134: String[] childIDs = new String[objectNames.size() - 1];
135: for (int j = 0; j < childIDs.length; j++) {
136: childIDs[j] = (String) objectNames.get(j + 1);
137: }
138:
139: TargetModuleIDImpl moduleID = new TargetModuleIDImpl(target,
140: parentName, childIDs);
141: if (isWebApp(kernel, parentName)) {
142: moduleID.setType(ModuleType.WAR);
143: }
144: if (moduleID.getChildTargetModuleID() != null) {
145: for (int i = 0; i < moduleID.getChildTargetModuleID().length; i++) {
146: TargetModuleIDImpl id = (TargetModuleIDImpl) moduleID
147: .getChildTargetModuleID()[i];
148: if (isWebApp(kernel, id.getModuleID())) {
149: id.setType(ModuleType.WAR);
150: }
151: }
152: }
153: addModule(moduleID);
154: if (finished) {
155: addWebURLs(kernel);
156: complete("Completed with id " + parentName);
157: }
158: }
159:
160: protected void massageFileNames(File[] inputs) {
161: }
162:
163: public URL getRemoteDeployUploadURL() throws Exception {
164: return new URL((String) kernel.getAttribute(deployer,
165: "remoteDeployUploadURL"));
166: }
167: }
|