KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > mavenplugins > geronimo > module > ModuleMojoSupport


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */

19
20 package org.apache.geronimo.mavenplugins.geronimo.module;
21
22 import java.io.IOException JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25
26 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
27 import javax.enterprise.deploy.spi.Target JavaDoc;
28 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
29 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
30 import javax.enterprise.deploy.spi.status.ProgressListener JavaDoc;
31 import javax.enterprise.deploy.spi.status.ProgressEvent JavaDoc;
32 import javax.enterprise.deploy.spi.status.DeploymentStatus JavaDoc;
33 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException JavaDoc;
34 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager JavaDoc;
35
36 import org.apache.geronimo.deployment.plugin.factories.DeploymentFactoryImpl;
37 import org.apache.geronimo.mavenplugins.geronimo.ModuleConfig;
38 import org.apache.geronimo.mavenplugins.geronimo.reporting.ReportingMojoSupport;
39
40 import org.apache.geronimo.genesis.util.ArtifactItem;
41
42 import org.apache.maven.plugin.MojoExecutionException;
43
44 /**
45  * Support for mojos that operate on modules.
46  *
47  * @version $Rev: 478288 $ $Date: 2006-11-22 14:07:14 -0500 (Wed, 22 Nov 2006) $
48  */

49 public abstract class ModuleMojoSupport
50     extends ReportingMojoSupport
51 {
52     private static final String JavaDoc URI_PREFIX = "deployer:geronimo:jmx";
53
54     /**
55      * List of module artifact configurations. Artifacts need to point to jar | war | ear | rar archive.
56      * Module artifact's configurations should match the moduleId specified in the plan, if plan exists.
57      *
58      * @parameter
59      */

60     protected ModuleConfig[] modules;
61
62     /**
63      * Cached deployment manager.
64      */

65     private DeploymentManager JavaDoc deploymentManager;
66
67     /**
68      * Get a deployment manager; if the manager was previosuly initialized then that cached instance is used.
69      *
70      * @return Deployment manager instance; never null
71      *
72      * @throws IOException
73      * @throws DeploymentManagerCreationException
74      */

75     protected DeploymentManager JavaDoc getDeploymentManager() throws IOException JavaDoc, DeploymentManagerCreationException JavaDoc {
76         if (deploymentManager == null) {
77             // Register the Geronimo factory
78
DeploymentFactoryManager JavaDoc manager = DeploymentFactoryManager.getInstance();
79             manager.registerDeploymentFactory(new DeploymentFactoryImpl());
80
81             String JavaDoc uri = URI_PREFIX + "://" + hostname + ":" + port;
82
83             DeploymentFactoryManager JavaDoc factoryManager = DeploymentFactoryManager.getInstance();
84             deploymentManager = factoryManager.getDeploymentManager(uri, username, password);
85         }
86
87         return deploymentManager;
88     }
89
90     /**
91      * Waits for the given progress to stop running.
92      *
93      * @param progress The progress object to wait for.
94      * @return The status of the deployment; never null
95      *
96      * @throws InterruptedException
97      */

98     protected DeploymentStatus JavaDoc waitFor(final ProgressObject JavaDoc progress) throws InterruptedException JavaDoc {
99         assert progress != null;
100
101         //
102
// TODO: Add timeout?
103
//
104

105         ProgressListener JavaDoc listener = new ProgressListener JavaDoc()
106         {
107             public void handleProgressEvent(final ProgressEvent JavaDoc event) {
108                 DeploymentStatus JavaDoc status = event.getDeploymentStatus();
109
110                 if (!status.isRunning()) {
111                     synchronized (progress) {
112                         progress.notify();
113                     }
114                 }
115             }
116         };
117
118         progress.addProgressListener(listener);
119
120         synchronized (progress) {
121             while (progress.getDeploymentStatus().isRunning()) {
122                 progress.wait();
123             }
124         }
125
126         return progress.getDeploymentStatus();
127     }
128
129     /**
130      * Returns the Geronimo moduleId for the given artifact.
131      *
132      * @param item The artifact item to get the moduleId for.
133      * @return The moduleId of the given artifact item.
134      */

135     protected String JavaDoc getModuleId(final ArtifactItem item) {
136         assert item != null;
137
138         return item.getGroupId() + "/" + item.getArtifactId() + "/" + item.getVersion() + "/" + item.getType();
139     }
140
141     /**
142      * Given a list of modules, return a list of non-running ones.
143      *
144      * @param moduleIds The list of modules to check
145      * @return The list of modules that are not running
146      *
147      * @throws Exception
148      */

149     protected TargetModuleID JavaDoc[] getNonRunningModules(final TargetModuleID JavaDoc[] moduleIds) throws Exception JavaDoc {
150         assert moduleIds != null;
151
152         List JavaDoc modulesList = new ArrayList JavaDoc();
153
154         DeploymentManager JavaDoc manager = getDeploymentManager();
155
156         Target JavaDoc[] targets = manager.getTargets();
157         TargetModuleID JavaDoc runningModuleIds[] = manager.getRunningModules(null, targets);
158
159         for (int j = 0; j < moduleIds.length; j++) {
160             String JavaDoc moduleId = moduleIds[j].getModuleID();
161             log.debug("Checking if module is running: " + moduleId);
162
163             boolean found = false;
164             for (int i = 0; i < runningModuleIds.length; i++) {
165             String JavaDoc runningModuleId = runningModuleIds[i].getModuleID();
166                 if (moduleId.equals(runningModuleId)) {
167                     log.debug("Module is running: " + moduleId);
168                     found = true;
169                     break;
170                 }
171             }
172
173             if (!found) {
174                 log.debug("Module is not running: " + moduleId);
175                 modulesList.add(moduleIds[j]);
176             }
177         }
178         return (TargetModuleID JavaDoc[]) modulesList.toArray(new TargetModuleID JavaDoc[modulesList.size()]);
179     }
180
181     /**
182      * Check if the given module is started.
183      *
184      * @param moduleId The module ID to check
185      * @return True if the module for this ID is started.
186      *
187      * @throws Exception
188      */

189     protected boolean isModuleStarted(final String JavaDoc moduleId) throws Exception JavaDoc {
190         assert moduleId != null;
191
192         log.debug("Checking if module is started: " + moduleId);
193         
194         DeploymentManager JavaDoc manager = getDeploymentManager();
195
196         Target JavaDoc[] targets = manager.getTargets();
197         TargetModuleID JavaDoc targetIds[] = manager.getRunningModules(null, targets);
198
199         for (int i = 0; i < targetIds.length; i++) {
200             if (moduleId.equals(targetIds[i].getModuleID())) {
201                 return true;
202             }
203         }
204
205         return false;
206     }
207
208     protected TargetModuleID JavaDoc[] findModules(final String JavaDoc moduleId, final TargetModuleID JavaDoc targetIds[]) {
209         assert moduleId != null;
210         assert targetIds != null;
211
212         List JavaDoc found = new ArrayList JavaDoc();
213
214         log.debug("Scanning for modules that match: " + moduleId);
215         for (int i = 0; i < targetIds.length; i++) {
216             log.debug("Checking: " + targetIds[i].getModuleID());
217
218             if (moduleId.equals(targetIds[i].getModuleID())) {
219                 found.add(targetIds[i]);
220             }
221         }
222
223         return (TargetModuleID JavaDoc[]) found.toArray(new TargetModuleID JavaDoc[found.size()]);
224     }
225
226     //
227
// TODO: Can probably wrap up some of this into findModules with a flag for running or non-running
228
//
229

230     protected void startModule() throws Exception JavaDoc {
231         assert modules != null;
232
233         for (int i=0; i<modules.length; i++) {
234            String JavaDoc moduleId = getModuleId(modules[i]);
235         
236            if (isModuleStarted(moduleId)) {
237                log.warn("Module is already started: " + moduleId);
238                continue;
239                //throw new MojoExecutionException("Module is already started: " + moduleId);
240
}
241
242            DeploymentManager JavaDoc manager = getDeploymentManager();
243            Target JavaDoc[] targets = manager.getTargets();
244            TargetModuleID JavaDoc[] targetIds = manager.getNonRunningModules(null, targets);
245
246            TargetModuleID JavaDoc[] found = findModules(moduleId, targetIds);
247
248            if (found.length == 0) {
249                throw new MojoExecutionException("Module is not deployed: " + moduleId);
250            }
251
252            log.info("Starting module: " + moduleId);
253            ProgressObject JavaDoc progress = manager.start(found);
254
255            DeploymentStatus JavaDoc status = waitFor(progress);
256            if (status.isFailed()) {
257                throw new MojoExecutionException("Failed to start module: " + moduleId);
258            }
259
260            log.info("Started module(s):");
261            logModules(found, " ");
262         }
263     }
264
265     protected void stopModule() throws Exception JavaDoc {
266         assert modules != null;
267
268         DeploymentManager JavaDoc manager = getDeploymentManager();
269         Target JavaDoc[] targets = manager.getTargets();
270         TargetModuleID JavaDoc[] targetIds = manager.getRunningModules(null, targets);
271
272          for (int i=0; i<modules.length; i++) {
273            String JavaDoc moduleId = getModuleId(modules[i]);
274            if (!isModuleStarted(moduleId)) {
275                log.info("Module is already stopped: " + moduleId);
276                continue;
277                //throw new MojoExecutionException("Module is not started: " + moduleId);
278
}
279
280            TargetModuleID JavaDoc[] found = findModules(moduleId, targetIds);
281
282            if (found.length == 0) {
283                throw new MojoExecutionException("Module not deployed: " + moduleId);
284            }
285
286            log.info("Stopping module: " + moduleId);
287            ProgressObject JavaDoc progress = manager.stop(found);
288
289            DeploymentStatus JavaDoc status = waitFor(progress);
290            if (status.isFailed()) {
291                throw new MojoExecutionException("Failed to stop module: " + moduleId);
292            }
293
294            log.info("Stopped module(s):");
295            logModules(found, " ");
296          }
297     }
298
299     protected void undeployModule() throws Exception JavaDoc {
300         assert modules != null;
301
302         stopModule();
303
304         DeploymentManager JavaDoc manager = getDeploymentManager();
305         Target JavaDoc[] targets = manager.getTargets();
306         TargetModuleID JavaDoc[] targetIds = manager.getNonRunningModules(null, targets);
307
308         for (int i=0; i<modules.length; i++) {
309           String JavaDoc moduleId = getModuleId(modules[i]);
310
311           TargetModuleID JavaDoc[] found = findModules(moduleId, targetIds);
312
313           if (found.length == 0) {
314               log.info("Module is not deployed: " + moduleId);
315               continue;
316               //throw new Exception("Module is not deployed: " + moduleId);
317
}
318
319           log.info("Undeploying module: " + moduleId);
320           ProgressObject JavaDoc progress = manager.undeploy(found);
321
322           DeploymentStatus JavaDoc status = waitFor(progress);
323           if (status.isFailed()) {
324               throw new MojoExecutionException("Failed to undeploy module: " + moduleId);
325           }
326
327           log.info("Undeployed module(s):");
328           logModules(found, " ");
329         }
330     }
331
332     protected void logModules(final TargetModuleID JavaDoc[] targetIds) {
333         logModules(targetIds, "");
334     }
335
336     protected void logModules(final TargetModuleID JavaDoc[] targetIds, final String JavaDoc pad) {
337         assert targetIds != null;
338         assert pad != null;
339
340         for (int i=0; i<targetIds.length; i++) {
341             String JavaDoc url = targetIds[i].getWebURL();
342             log.info(pad + "[" + i + "] " + targetIds[i].getModuleID() + (url == null ? "" : ("; URL: " + url)));
343
344             TargetModuleID JavaDoc[] children = targetIds[i].getChildTargetModuleID();
345             if (children != null) {
346                 logModules(children, pad + " ");
347             }
348         }
349     }
350
351 }
352
Popular Tags