KickJava   Java API By Example, From Geeks To Geeks.

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


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.InputStream JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import javax.enterprise.deploy.shared.CommandType JavaDoc;
24 import javax.enterprise.deploy.spi.Target JavaDoc;
25 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
26
27 import org.apache.geronimo.deployment.plugin.ConfigIDExtractor;
28 import org.apache.geronimo.deployment.plugin.TargetImpl;
29 import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
30 import org.apache.geronimo.gbean.AbstractName;
31 import org.apache.geronimo.kernel.InternalKernelException;
32 import org.apache.geronimo.kernel.Kernel;
33 import org.apache.geronimo.kernel.config.ConfigurationManager;
34 import org.apache.geronimo.kernel.config.ConfigurationUtil;
35 import org.apache.geronimo.kernel.config.LifecycleResults;
36 import org.apache.geronimo.kernel.config.NoSuchConfigException;
37 import org.apache.geronimo.kernel.repository.Artifact;
38
39 /**
40  * @version $Rev: 482870 $ $Date: 2006-12-05 21:50:50 -0500 (Tue, 05 Dec 2006) $
41  */

42 public class RedeployCommand extends AbstractDeployCommand {
43     private static final String JavaDoc[] IS_IN_PLACE_CONFIGURATION_SIG = {Artifact.class.getName()};
44     private static final String JavaDoc IS_IN_PLACE_CONFIGURATION_METH = "isInPlaceConfiguration";
45
46     private final TargetModuleID JavaDoc[] modules;
47
48     public RedeployCommand(Kernel kernel, TargetModuleID JavaDoc[] moduleIDList, File JavaDoc moduleArchive, File JavaDoc deploymentPlan) {
49         super(CommandType.REDEPLOY, kernel, moduleArchive, deploymentPlan, null, null, false);
50         this.modules = moduleIDList;
51     }
52
53     public RedeployCommand(Kernel kernel, TargetModuleID JavaDoc[] moduleIDList, InputStream JavaDoc moduleArchive, InputStream JavaDoc deploymentPlan) {
54         super(CommandType.REDEPLOY, kernel, null, null, moduleArchive, deploymentPlan, true);
55         this.modules = moduleIDList;
56     }
57
58     public void run() {
59         if (deployer == null) {
60             return;
61         }
62
63         try {
64             if (spool) {
65                 if (moduleStream != null) {
66                     moduleArchive = createTempFile();
67                     copyTo(moduleArchive, moduleStream);
68                 }
69                 if (deploymentStream != null) {
70                     deploymentPlan = createTempFile();
71                     copyTo(deploymentPlan, deploymentStream);
72                 }
73             }
74             Artifact configID = null;
75             if(deploymentPlan != null) {
76                 String JavaDoc extracted = ConfigIDExtractor.extractModuleIdFromPlan(deploymentPlan);
77                 if(extracted != null) {
78                     configID = Artifact.create(extracted);
79                 }
80             } else {
81                 String JavaDoc extracted = ConfigIDExtractor.extractModuleIdFromArchive(moduleArchive);
82                 if(extracted != null) {
83                     configID = Artifact.create(extracted);
84                 }
85             }
86             if(configID != null && configID.getGroupId() == null) {
87                 configID = new Artifact(Artifact.DEFAULT_GROUP_ID, configID.getArtifactId(),
88                                         configID.getVersion(), configID.getType());
89             }
90
91             ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
92             try {
93                 for (int i = 0; i < modules.length; i++) {
94                     TargetModuleIDImpl module = (TargetModuleIDImpl) modules[i];
95                     Artifact artifact = Artifact.create(module.getModuleID());
96                     if(configID != null && configID.isResolved()) {
97                         if(configID.getGroupId().equals(artifact.getGroupId()) &&
98                                 configID.getArtifactId().equals(artifact.getArtifactId()) &&
99                                 configID.getVersion().equals(artifact.getVersion())) {
100                             redeploySameConfiguration(configurationManager, artifact, module.getTarget());
101                         } else {
102                             redeployUpdatedConfiguration(configurationManager, artifact, module.getTarget());
103                         }
104                     } else {
105                         redeployUpdatedConfiguration(configurationManager, artifact, module.getTarget());
106                     }
107                 }
108             } finally {
109                 ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager);
110             }
111             addWebURLs(kernel);
112             complete("Completed");
113         } catch (Exception JavaDoc e) {
114             doFail(e);
115         } finally {
116             if (spool) {
117                 if (moduleArchive != null) {
118                     moduleArchive.delete();
119                 }
120                 if (deploymentPlan != null) {
121                     deploymentPlan.delete();
122                 }
123             }
124         }
125     }
126
127     private void redeployUpdatedConfiguration(ConfigurationManager manager, Artifact previous, Target JavaDoc target) throws Exception JavaDoc, NoSuchConfigException {
128         // Send the new configuration to the server
129

130             // if the configuration is an in-place one, then redeploys
131
// in in-place mode.
132
TargetImpl impl = (TargetImpl) target;
133         AbstractName storeName = impl.getAbstractName();
134         Boolean JavaDoc inPlaceConfiguration = (Boolean JavaDoc) kernel.invoke(storeName, IS_IN_PLACE_CONFIGURATION_METH, new Object JavaDoc[]{previous}, IS_IN_PLACE_CONFIGURATION_SIG);
135         commandContext.setInPlace(inPlaceConfiguration.booleanValue());
136         doDeploy(target, false);
137         Artifact configID = Artifact.create(getResultTargetModuleIDs()[0].getModuleID());
138         LifecycleResults results = manager.reloadConfiguration(previous, configID.getVersion());
139
140         // Activate it
141
//todo: make this asynchronous
142
boolean newStarted = false;
143         for (Iterator JavaDoc it = results.getStopped().iterator(); it.hasNext();) {
144             Artifact name = (Artifact) it.next();
145             updateStatus("Stopped "+name);
146         }
147         for (Iterator JavaDoc it = results.getUnloaded().iterator(); it.hasNext();) {
148             Artifact name = (Artifact) it.next();
149             updateStatus("Unloaded "+name);
150         }
151         for (Iterator JavaDoc it = results.getLoaded().iterator(); it.hasNext();) {
152             Artifact name = (Artifact) it.next();
153             updateStatus("Loaded "+name);
154         }
155         for (Iterator JavaDoc it = results.getStarted().iterator(); it.hasNext();) {
156             Artifact name = (Artifact) it.next();
157             updateStatus("Started "+name);
158             if(configID.matches(name)) {
159                 newStarted = true;
160             }
161         }
162         for (Iterator JavaDoc it = results.getFailed().keySet().iterator(); it.hasNext();) {
163             Artifact name = (Artifact) it.next();
164             updateStatus("Failed on "+name+": "+results.getFailedCause(name).getMessage());
165             doFail((Exception JavaDoc)results.getFailedCause(name));
166         }
167         if(results.getFailed().size() == 0 && !newStarted) {
168             updateStatus("Note: new module was not started (probably because old module was not running).");
169         }
170     }
171
172     private void redeploySameConfiguration(ConfigurationManager configurationManager, Artifact configID, Target JavaDoc target) throws Exception JavaDoc {
173         if(!configID.isResolved()) {
174             throw new IllegalStateException JavaDoc("Cannot redeploy same module when module ID is not fully resolved ("+configID+")");
175         }
176         try {
177             configurationManager.stopConfiguration(configID);
178             updateStatus("Stopped "+configID);
179         } catch (InternalKernelException e) {
180             Exception JavaDoc cause = (Exception JavaDoc)e.getCause();
181             if(cause instanceof NoSuchConfigException) {
182                 // The modules isn't loaded -- that's OK
183
} else {
184                 throw cause;
185             }
186         } catch(NoSuchConfigException e) {
187             // The module isn't loaded -- that's OK
188
}
189         try {
190             configurationManager.unloadConfiguration(configID);
191             updateStatus("Unloaded "+configID);
192         } catch(InternalKernelException e) {
193             Exception JavaDoc cause = (Exception JavaDoc)e.getCause();
194             if(cause instanceof NoSuchConfigException) {
195                 // The modules isn't loaded -- that's OK
196
} else {
197                 throw cause;
198             }
199         } catch (NoSuchConfigException e) {
200             // The modules isn't loaded -- that's OK
201
}
202
203         // if the configuration is an in-place one, then redeploys
204
// in in-place mode.
205
TargetImpl impl = (TargetImpl) target;
206         AbstractName storeName = impl.getAbstractName();
207         Boolean JavaDoc inPlaceConfiguration = (Boolean JavaDoc) kernel.invoke(storeName, IS_IN_PLACE_CONFIGURATION_METH, new Object JavaDoc[]{configID}, IS_IN_PLACE_CONFIGURATION_SIG);
208         commandContext.setInPlace(inPlaceConfiguration.booleanValue());
209
210         try {
211             configurationManager.uninstallConfiguration(configID);
212             updateStatus("Uninstalled "+configID);
213         } catch(InternalKernelException e) {
214             Exception JavaDoc cause = (Exception JavaDoc)e.getCause();
215             if(cause instanceof NoSuchConfigException) {
216                 throw new IllegalStateException JavaDoc("Module "+configID+" is not installed!");
217             } else {
218                 throw cause;
219             }
220         } catch (NoSuchConfigException e) {
221             throw new IllegalStateException JavaDoc("Module "+configID+" is not installed!");
222         }
223
224         doDeploy(target, false);
225         updateStatus("Deployed "+configID);
226
227         configurationManager.loadConfiguration(configID);
228         configurationManager.startConfiguration(configID);
229         updateStatus("Started " + configID);
230     }
231 }
232
Popular Tags