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.InputStream;
020: import java.util.Iterator;
021:
022: import javax.enterprise.deploy.shared.CommandType;
023: import javax.enterprise.deploy.shared.ModuleType;
024: import javax.enterprise.deploy.spi.Target;
025: import javax.enterprise.deploy.spi.TargetModuleID;
026:
027: import org.apache.geronimo.deployment.plugin.ConfigIDExtractor;
028: import org.apache.geronimo.deployment.plugin.TargetImpl;
029: import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
030: import org.apache.geronimo.gbean.AbstractName;
031: import org.apache.geronimo.kernel.InternalKernelException;
032: import org.apache.geronimo.kernel.Kernel;
033: import org.apache.geronimo.kernel.config.ConfigurationManager;
034: import org.apache.geronimo.kernel.config.ConfigurationUtil;
035: import org.apache.geronimo.kernel.config.LifecycleResults;
036: import org.apache.geronimo.kernel.config.NoSuchConfigException;
037: import org.apache.geronimo.kernel.repository.Artifact;
038:
039: /**
040: * @version $Rev: 549455 $ $Date: 2007-06-21 05:12:27 -0700 (Thu, 21 Jun 2007) $
041: */
042: public class RedeployCommand extends AbstractDeployCommand {
043: private static final String[] IS_IN_PLACE_CONFIGURATION_SIG = { Artifact.class
044: .getName() };
045: private static final String IS_IN_PLACE_CONFIGURATION_METH = "isInPlaceConfiguration";
046:
047: private final TargetModuleID[] modules;
048:
049: public RedeployCommand(Kernel kernel,
050: TargetModuleID[] moduleIDList, File moduleArchive,
051: File deploymentPlan) {
052: super (CommandType.REDEPLOY, kernel, moduleArchive,
053: deploymentPlan, null, null, null, false);
054: this .modules = moduleIDList;
055: }
056:
057: public RedeployCommand(Kernel kernel,
058: TargetModuleID[] moduleIDList, InputStream moduleArchive,
059: InputStream deploymentPlan) {
060: super (CommandType.REDEPLOY, kernel, null, null, null,
061: moduleArchive, deploymentPlan, true);
062: this .modules = moduleIDList;
063: }
064:
065: public void run() {
066: if (deployer == null) {
067: return;
068: }
069:
070: try {
071: if (spool) {
072: if (moduleStream != null) {
073: moduleArchive = createTempFile(moduleType == null ? null
074: : moduleType.getModuleExtension());
075: copyTo(moduleArchive, moduleStream);
076: }
077: if (deploymentStream != null) {
078: deploymentPlan = createTempFile(null);
079: copyTo(deploymentPlan, deploymentStream);
080: }
081: }
082: Artifact configID = null;
083: if (deploymentPlan != null) {
084: String extracted = ConfigIDExtractor
085: .extractModuleIdFromPlan(deploymentPlan);
086: if (extracted != null) {
087: configID = Artifact.create(extracted);
088: }
089: } else {
090: String extracted = ConfigIDExtractor
091: .extractModuleIdFromArchive(moduleArchive);
092: if (extracted != null) {
093: configID = Artifact.create(extracted);
094: }
095: }
096: if (configID != null && configID.getGroupId() == null) {
097: configID = new Artifact(Artifact.DEFAULT_GROUP_ID,
098: configID.getArtifactId(),
099: configID.getVersion(), configID.getType());
100: }
101:
102: ConfigurationManager configurationManager = ConfigurationUtil
103: .getConfigurationManager(kernel);
104: try {
105: for (int i = 0; i < modules.length; i++) {
106: TargetModuleIDImpl module = (TargetModuleIDImpl) modules[i];
107: Artifact artifact = Artifact.create(module
108: .getModuleID());
109: if (configID != null && configID.isResolved()) {
110: if (configID.getGroupId().equals(
111: artifact.getGroupId())
112: && configID.getArtifactId().equals(
113: artifact.getArtifactId())
114: && configID.getVersion().equals(
115: artifact.getVersion())) {
116: redeploySameConfiguration(
117: configurationManager, artifact,
118: module.getTarget());
119: } else {
120: redeployUpdatedConfiguration(
121: configurationManager, artifact,
122: module.getTarget());
123: }
124: } else {
125: redeployUpdatedConfiguration(
126: configurationManager, artifact, module
127: .getTarget());
128: }
129: }
130: } finally {
131: ConfigurationUtil.releaseConfigurationManager(kernel,
132: configurationManager);
133: }
134: addWebURLs(kernel);
135: complete("Completed");
136: } catch (Exception e) {
137: doFail(e);
138: } finally {
139: if (spool) {
140: if (moduleArchive != null) {
141: moduleArchive.delete();
142: }
143: if (deploymentPlan != null) {
144: deploymentPlan.delete();
145: }
146: }
147: }
148: }
149:
150: private void redeployUpdatedConfiguration(
151: ConfigurationManager manager, Artifact previous,
152: Target target) throws Exception, NoSuchConfigException {
153: // Send the new configuration to the server
154:
155: // if the configuration is an in-place one, then redeploys
156: // in in-place mode.
157: TargetImpl impl = (TargetImpl) target;
158: AbstractName storeName = impl.getAbstractName();
159: Boolean inPlaceConfiguration = (Boolean) kernel.invoke(
160: storeName, IS_IN_PLACE_CONFIGURATION_METH,
161: new Object[] { previous },
162: IS_IN_PLACE_CONFIGURATION_SIG);
163: commandContext.setInPlace(inPlaceConfiguration.booleanValue());
164: doDeploy(target, false);
165: Artifact configID = Artifact
166: .create(getResultTargetModuleIDs()[0].getModuleID());
167: LifecycleResults results = manager.reloadConfiguration(
168: previous, configID.getVersion());
169:
170: // Activate it
171: //todo: make this asynchronous
172: boolean newStarted = false;
173: for (Iterator it = results.getStopped().iterator(); it
174: .hasNext();) {
175: Artifact name = (Artifact) it.next();
176: updateStatus("Stopped " + name);
177: }
178: for (Iterator it = results.getUnloaded().iterator(); it
179: .hasNext();) {
180: Artifact name = (Artifact) it.next();
181: updateStatus("Unloaded " + name);
182: }
183: for (Iterator it = results.getLoaded().iterator(); it.hasNext();) {
184: Artifact name = (Artifact) it.next();
185: updateStatus("Loaded " + name);
186: }
187: for (Iterator it = results.getStarted().iterator(); it
188: .hasNext();) {
189: Artifact name = (Artifact) it.next();
190: updateStatus("Started " + name);
191: if (configID.matches(name)) {
192: newStarted = true;
193: }
194: }
195: for (Iterator it = results.getFailed().keySet().iterator(); it
196: .hasNext();) {
197: Artifact name = (Artifact) it.next();
198: updateStatus("Failed on " + name + ": "
199: + results.getFailedCause(name).getMessage());
200: doFail((Exception) results.getFailedCause(name));
201: }
202: if (results.getFailed().size() == 0 && !newStarted) {
203: updateStatus("Note: new module was not started (probably because old module was not running).");
204: }
205: }
206:
207: private void redeploySameConfiguration(
208: ConfigurationManager configurationManager,
209: Artifact configID, Target target) throws Exception {
210: if (!configID.isResolved()) {
211: throw new IllegalStateException(
212: "Cannot redeploy same module when module ID is not fully resolved ("
213: + configID + ")");
214: }
215: try {
216: configurationManager.stopConfiguration(configID);
217: updateStatus("Stopped " + configID);
218: } catch (InternalKernelException e) {
219: Exception cause = (Exception) e.getCause();
220: if (cause instanceof NoSuchConfigException) {
221: // The modules isn't loaded -- that's OK
222: } else {
223: throw cause;
224: }
225: } catch (NoSuchConfigException e) {
226: // The module isn't loaded -- that's OK
227: }
228: try {
229: configurationManager.unloadConfiguration(configID);
230: updateStatus("Unloaded " + configID);
231: } catch (InternalKernelException e) {
232: Exception cause = (Exception) e.getCause();
233: if (cause instanceof NoSuchConfigException) {
234: // The modules isn't loaded -- that's OK
235: } else {
236: throw cause;
237: }
238: } catch (NoSuchConfigException e) {
239: // The modules isn't loaded -- that's OK
240: }
241:
242: // if the configuration is an in-place one, then redeploys
243: // in in-place mode.
244: TargetImpl impl = (TargetImpl) target;
245: AbstractName storeName = impl.getAbstractName();
246: Boolean inPlaceConfiguration = (Boolean) kernel.invoke(
247: storeName, IS_IN_PLACE_CONFIGURATION_METH,
248: new Object[] { configID },
249: IS_IN_PLACE_CONFIGURATION_SIG);
250: commandContext.setInPlace(inPlaceConfiguration.booleanValue());
251:
252: try {
253: configurationManager.uninstallConfiguration(configID);
254: updateStatus("Uninstalled " + configID);
255: } catch (InternalKernelException e) {
256: Exception cause = (Exception) e.getCause();
257: if (cause instanceof NoSuchConfigException) {
258: throw new IllegalStateException("Module " + configID
259: + " is not installed!", cause);
260: } else {
261: throw cause;
262: }
263: } catch (NoSuchConfigException e) {
264: throw new IllegalStateException("Module " + configID
265: + " is not installed!", e);
266: }
267:
268: doDeploy(target, false);
269: updateStatus("Deployed " + configID);
270:
271: configurationManager.loadConfiguration(configID);
272: configurationManager.startConfiguration(configID);
273: updateStatus("Started " + configID);
274: }
275: }
|