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.InputStream;
21 import java.util.Iterator;
22 import javax.enterprise.deploy.shared.CommandType;
23 import javax.enterprise.deploy.spi.Target;
24 import javax.enterprise.deploy.spi.TargetModuleID;
25 import org.apache.geronimo.deployment.plugin.ConfigIDExtractor;
26 import org.apache.geronimo.deployment.plugin.TargetImpl;
27 import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
28 import org.apache.geronimo.deployment.util.DeploymentUtil;
29 import org.apache.geronimo.gbean.AbstractName;
30 import org.apache.geronimo.kernel.InternalKernelException;
31 import org.apache.geronimo.kernel.Kernel;
32 import org.apache.geronimo.kernel.config.ConfigurationManager;
33 import org.apache.geronimo.kernel.config.ConfigurationUtil;
34 import org.apache.geronimo.kernel.config.LifecycleResults;
35 import org.apache.geronimo.kernel.config.NoSuchConfigException;
36 import org.apache.geronimo.kernel.repository.Artifact;
37
38 /**
39 * @version $Rev: 428535 $ $Date: 2006-08-03 14:36:04 -0700 (Thu, 03 Aug 2006) $
40 */
41 public class RedeployCommand extends AbstractDeployCommand {
42 private static final String[] IS_IN_PLACE_CONFIGURATION_SIG = {Artifact.class.getName()};
43 private static final String IS_IN_PLACE_CONFIGURATION_METH = "isInPlaceConfiguration";
44
45 private final TargetModuleID[] modules;
46
47 public RedeployCommand(Kernel kernel, TargetModuleID[] moduleIDList, File moduleArchive, File deploymentPlan) {
48 super(CommandType.REDEPLOY, kernel, moduleArchive, deploymentPlan, null, null, false);
49 this.modules = moduleIDList;
50 }
51
52 public RedeployCommand(Kernel kernel, TargetModuleID[] moduleIDList, InputStream moduleArchive, InputStream deploymentPlan) {
53 super(CommandType.REDEPLOY, kernel, null, null, moduleArchive, deploymentPlan, true);
54 this.modules = moduleIDList;
55 }
56
57 public void run() {
58 if (deployer == null) {
59 return;
60 }
61
62 try {
63 if (spool) {
64 if (moduleStream != null) {
65 moduleArchive = DeploymentUtil.createTempFile();
66 copyTo(moduleArchive, moduleStream);
67 }
68 if (deploymentStream != null) {
69 deploymentPlan = DeploymentUtil.createTempFile();
70 copyTo(deploymentPlan, deploymentStream);
71 }
72 }
73 Artifact configID = null;
74 if(deploymentPlan != null) {
75 String extracted = ConfigIDExtractor.extractModuleIdFromPlan(deploymentPlan);
76 if(extracted != null) {
77 configID = Artifact.create(extracted);
78 }
79 } else {
80 String extracted = ConfigIDExtractor.extractModuleIdFromArchive(moduleArchive);
81 if(extracted != null) {
82 configID = Artifact.create(extracted);
83 }
84 }
85 if(configID != null && configID.getGroupId() == null) {
86 configID = new Artifact(Artifact.DEFAULT_GROUP_ID, configID.getArtifactId(),
87 configID.getVersion(), configID.getType());
88 }
89
90 ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
91 try {
92 for (int i = 0; i < modules.length; i++) {
93 TargetModuleIDImpl module = (TargetModuleIDImpl) modules[i];
94 Artifact artifact = Artifact.create(module.getModuleID());
95 if(configID != null && configID.isResolved()) {
96 if(configID.getGroupId().equals(artifact.getGroupId()) &&
97 configID.getArtifactId().equals(artifact.getArtifactId()) &&
98 configID.getVersion().equals(artifact.getVersion())) {
99 redeploySameConfiguration(configurationManager, artifact, module.getTarget());
100 } else {
101 redeployUpdatedConfiguration(configurationManager, artifact, module.getTarget());
102 }
103 } else {
104 redeployUpdatedConfiguration(configurationManager, artifact, module.getTarget());
105 }
106 }
107 } finally {
108 ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager);
109 }
110 addWebURLs(kernel);
111 complete("Completed");
112 } catch (Exception e) {
113 doFail(e);
114 } finally {
115 if (spool) {
116 if (moduleArchive != null) {
117 moduleArchive.delete();
118 }
119 if (deploymentPlan != null) {
120 deploymentPlan.delete();
121 }
122 }
123 }
124 }
125
126 private void redeployUpdatedConfiguration(ConfigurationManager manager, Artifact previous, Target target) throws Exception, NoSuchConfigException {
127
128
129
130
131 TargetImpl impl = (TargetImpl) target;
132 AbstractName storeName = impl.getAbstractName();
133 Boolean inPlaceConfiguration = (Boolean) kernel.invoke(storeName, IS_IN_PLACE_CONFIGURATION_METH, new Object[]{previous}, IS_IN_PLACE_CONFIGURATION_SIG);
134 commandContext.setInPlace(inPlaceConfiguration.booleanValue());
135 doDeploy(target, false);
136 Artifact configID = Artifact.create(getResultTargetModuleIDs()[0].getModuleID());
137 LifecycleResults results = manager.reloadConfiguration(previous, configID.getVersion());
138
139
140
141 boolean newStarted = false;
142 for (Iterator it = results.getStopped().iterator(); it.hasNext();) {
143 Artifact name = (Artifact) it.next();
144 updateStatus("Stopped "+name);
145 }
146 for (Iterator it = results.getUnloaded().iterator(); it.hasNext();) {
147 Artifact name = (Artifact) it.next();
148 updateStatus("Unloaded "+name);
149 }
150 for (Iterator it = results.getLoaded().iterator(); it.hasNext();) {
151 Artifact name = (Artifact) it.next();
152 updateStatus("Loaded "+name);
153 }
154 for (Iterator it = results.getStarted().iterator(); it.hasNext();) {
155 Artifact name = (Artifact) it.next();
156 updateStatus("Started "+name);
157 if(configID.matches(name)) {
158 newStarted = true;
159 }
160 }
161 for (Iterator it = results.getFailed().keySet().iterator(); it.hasNext();) {
162 Artifact name = (Artifact) it.next();
163 updateStatus("Failed on "+name+": "+results.getFailedCause(name).getMessage());
164 doFail((Exception)results.getFailedCause(name));
165 }
166 if(results.getFailed().size() == 0 && !newStarted) {
167 updateStatus("Note: new module was not started (probably because old module was not running).");
168 }
169 }
170
171 private void redeploySameConfiguration(ConfigurationManager configurationManager, Artifact configID, Target target) throws Exception {
172 if(!configID.isResolved()) {
173 throw new IllegalStateException("Cannot redeploy same module when module ID is not fully resolved ("+configID+")");
174 }
175 try {
176 configurationManager.stopConfiguration(configID);
177 updateStatus("Stopped "+configID);
178 } catch (InternalKernelException e) {
179 Exception cause = (Exception)e.getCause();
180 if(cause instanceof NoSuchConfigException) {
181
182 } else {
183 throw cause;
184 }
185 } catch(NoSuchConfigException e) {
186
187 }
188 try {
189 configurationManager.unloadConfiguration(configID);
190 updateStatus("Unloaded "+configID);
191 } catch(InternalKernelException e) {
192 Exception cause = (Exception)e.getCause();
193 if(cause instanceof NoSuchConfigException) {
194
195 } else {
196 throw cause;
197 }
198 } catch (NoSuchConfigException e) {
199
200 }
201
202
203
204 TargetImpl impl = (TargetImpl) target;
205 AbstractName storeName = impl.getAbstractName();
206 Boolean inPlaceConfiguration = (Boolean) kernel.invoke(storeName, IS_IN_PLACE_CONFIGURATION_METH, new Object[]{configID}, IS_IN_PLACE_CONFIGURATION_SIG);
207 commandContext.setInPlace(inPlaceConfiguration.booleanValue());
208
209 try {
210 configurationManager.uninstallConfiguration(configID);
211 updateStatus("Uninstalled "+configID);
212 } catch(InternalKernelException e) {
213 Exception cause = (Exception)e.getCause();
214 if(cause instanceof NoSuchConfigException) {
215 throw new IllegalStateException("Module "+configID+" is not installed!");
216 } else {
217 throw cause;
218 }
219 } catch (NoSuchConfigException e) {
220 throw new IllegalStateException("Module "+configID+" is not installed!");
221 }
222
223 doDeploy(target, false);
224 updateStatus("Deployed "+configID);
225
226 configurationManager.loadConfiguration(configID);
227 configurationManager.startConfiguration(configID);
228 updateStatus("Started " + configID);
229 }
230 }