View Javadoc

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;
23  import java.util.List;
24  import java.util.ArrayList;
25  
26  import javax.enterprise.deploy.spi.DeploymentManager;
27  import javax.enterprise.deploy.spi.Target;
28  import javax.enterprise.deploy.spi.TargetModuleID;
29  import javax.enterprise.deploy.spi.status.ProgressObject;
30  import javax.enterprise.deploy.spi.status.ProgressListener;
31  import javax.enterprise.deploy.spi.status.ProgressEvent;
32  import javax.enterprise.deploy.spi.status.DeploymentStatus;
33  import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
34  import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager;
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.ArtifactItem;
41  
42  import org.apache.maven.plugin.MojoExecutionException;
43  
44  /**
45   * Support for mojos that operate on modules.
46   *
47   * @version $Rev: 450613 $ $Date: 2006-09-27 15:45:46 -0700 (Wed, 27 Sep 2006) $
48   */
49  public abstract class ModuleMojoSupport
50      extends ReportingMojoSupport
51  {
52      private static final String URI_PREFIX = "deployer:geronimo:jmx";
53  
54      /**
55       * List of module artifact configurations.  Artifacts need to point to jar | war | ear | rar archive.
56       *
57       * @parameter
58       */
59      protected ModuleConfig[] modules = null;
60  
61      /**
62       * Cached deployment manager.
63       */
64      private DeploymentManager deploymentManager;
65  
66      /**
67       * Get a deployment manager; if the manager was previosuly initialized then that cached instance is used.
68       *
69       * @return  Deployment manager instance; never null
70       *
71       * @throws IOException
72       * @throws DeploymentManagerCreationException
73       */
74      protected DeploymentManager getDeploymentManager() throws IOException, DeploymentManagerCreationException {
75          if (deploymentManager == null) {
76              // Register the Geronimo factory
77              DeploymentFactoryManager manager = DeploymentFactoryManager.getInstance();
78              manager.registerDeploymentFactory(new DeploymentFactoryImpl());
79  
80              String uri = URI_PREFIX + "://" + hostname + ":" + port;
81  
82              DeploymentFactoryManager factoryManager = DeploymentFactoryManager.getInstance();
83              deploymentManager = factoryManager.getDeploymentManager(uri, username, password);
84          }
85  
86          return deploymentManager;
87      }
88  
89      /**
90       * Waits for the given progress to stop running.
91       *
92       * @param progress  The progress object to wait for.
93       * @return          The status of the deployment; never null
94       *
95       * @throws InterruptedException
96       */
97      protected DeploymentStatus waitFor(final ProgressObject progress) throws InterruptedException {
98          assert progress != null;
99  
100         //
101         // TODO: Add timeout?
102         //
103         
104         ProgressListener listener = new ProgressListener()
105         {
106             public void handleProgressEvent(final ProgressEvent event) {
107                 DeploymentStatus status = event.getDeploymentStatus();
108 
109                 if (!status.isRunning()) {
110                     synchronized (progress) {
111                         progress.notify();
112                     }
113                 }
114             }
115         };
116 
117         progress.addProgressListener(listener);
118 
119         synchronized (progress) {
120             while (progress.getDeploymentStatus().isRunning()) {
121                 progress.wait();
122             }
123         }
124 
125         return progress.getDeploymentStatus();
126     }
127 
128     /**
129      * Returns the Geronimo moduleId for the given artifact.
130      *
131      * @param item  The artifact item to get the moduleId for.
132      * @return      The moduleId of the given artifact item.
133      */
134     protected String getModuleId(final ArtifactItem item) {
135         assert item != null;
136 
137         return item.getGroupId() + "/" + item.getArtifactId() + "/" + item.getVersion() + "/" + item.getType();
138     }
139 
140     /**
141      * Check of the given module is started.
142      *
143      * @param moduleId  The module ID to check
144      * @return          True if the module for this ID is started.
145      *
146      * @throws Exception
147      */
148     protected boolean isModuleStarted(final String moduleId) throws Exception {
149         assert moduleId != null;
150 
151         log.debug("Checking if module is started: " + moduleId);
152         
153         DeploymentManager manager = getDeploymentManager();
154 
155         Target[] targets = manager.getTargets();
156         TargetModuleID targetIds[] = manager.getRunningModules(null, targets);
157 
158         for (int i = 0; i < targetIds.length; i++) {
159             if (moduleId.equals(targetIds[i].getModuleID())) {
160                 return true;
161             }
162         }
163 
164         return false;
165     }
166 
167     protected TargetModuleID[] findModules(final String moduleId, final TargetModuleID targetIds[]) {
168         assert moduleId != null;
169         assert targetIds != null;
170 
171         List found = new ArrayList();
172 
173         log.debug("Scanning for modules that match: " + moduleId);
174         for (int i = 0; i < targetIds.length; i++) {
175             log.debug("Checking: " + targetIds[i].getModuleID());
176 
177             if (moduleId.equals(targetIds[i].getModuleID())) {
178                 found.add(targetIds[i]);
179             }
180         }
181 
182         return (TargetModuleID[]) found.toArray(new TargetModuleID[found.size()]);
183     }
184 
185     //
186     // TODO: Can probably wrap up some of this into findModules with a flag for running or non-running
187     //
188     
189     protected void startModule() throws Exception {
190         assert modules != null;
191 
192         DeploymentManager manager = getDeploymentManager();
193         Target[] targets = manager.getTargets();
194         TargetModuleID[] targetIds = manager.getNonRunningModules(null, targets);
195 
196         for (int i=0; i<modules.length; i++) {
197            String moduleId = getModuleId(modules[i]);
198         
199            if (isModuleStarted(moduleId)) {
200                log.warn("Module is already started: " + moduleId);
201                continue;
202                //throw new MojoExecutionException("Module is already started: " + moduleId);
203            }
204 
205            TargetModuleID[] found = findModules(moduleId, targetIds);
206 
207            if (found.length == 0) {
208                throw new MojoExecutionException("Module is not deployed: " + moduleId);
209            }
210 
211            log.info("Starting module: " + moduleId);
212            ProgressObject progress = manager.start(found);
213 
214            DeploymentStatus status = waitFor(progress);
215            if (status.isFailed()) {
216                throw new MojoExecutionException("Failed to start module: " + moduleId);
217            }
218 
219            log.info("Started module(s):");
220            logModules(found, "    ");
221         }
222     }
223 
224     protected void stopModule() throws Exception {
225         assert modules != null;
226 
227         DeploymentManager manager = getDeploymentManager();
228         Target[] targets = manager.getTargets();
229         TargetModuleID[] targetIds = manager.getRunningModules(null, targets);
230 
231          for (int i=0; i<modules.length; i++) {
232            String moduleId = getModuleId(modules[i]);
233            if (!isModuleStarted(moduleId)) {
234                log.warn("Module is already stopped: " + moduleId);
235                continue;
236                //throw new MojoExecutionException("Module is not started: " + moduleId);
237            }
238 
239            TargetModuleID[] found = findModules(moduleId, targetIds);
240 
241            if (found.length == 0) {
242                throw new MojoExecutionException("Module not deployed: " + moduleId);
243            }
244 
245            log.info("Stopping module: " + moduleId);
246            ProgressObject progress = manager.stop(found);
247 
248            DeploymentStatus status = waitFor(progress);
249            if (status.isFailed()) {
250                throw new MojoExecutionException("Failed to stop module: " + moduleId);
251            }
252 
253            log.info("Stopped module(s):");
254            logModules(found, "    ");
255          }
256     }
257 
258     protected void undeployModule() throws Exception {
259         assert modules != null;
260 
261         stopModule();
262 
263         DeploymentManager manager = getDeploymentManager();
264         Target[] targets = manager.getTargets();
265         TargetModuleID[] targetIds = manager.getNonRunningModules(null, targets);
266 
267         for (int i=0; i<modules.length; i++) {
268           String moduleId = getModuleId(modules[i]);
269 
270           TargetModuleID[] found = findModules(moduleId, targetIds);
271 
272           if (found.length == 0) {
273               throw new Exception("Module is not deployed: " + moduleId);
274           }
275 
276           log.info("Undeploying module: " + moduleId);
277           ProgressObject progress = manager.undeploy(found);
278 
279           DeploymentStatus status = waitFor(progress);
280           if (status.isFailed()) {
281               throw new MojoExecutionException("Failed to undeploy module: " + moduleId);
282           }
283 
284           log.info("Undeployed module(s):");
285           logModules(found, "    ");
286         }
287     }
288 
289     protected void logModules(final TargetModuleID[] targetIds) {
290         logModules(targetIds, "");
291     }
292 
293     protected void logModules(final TargetModuleID[] targetIds, final String pad) {
294         assert targetIds != null;
295         assert pad != null;
296 
297         for (int i=0; i<targetIds.length; i++) {
298             String url = targetIds[i].getWebURL();
299             log.info(pad + "[" + i + "] " + targetIds[i].getModuleID() + (url == null ? "" : ("; URL: " + url)));
300 
301             TargetModuleID[] children = targetIds[i].getChildTargetModuleID();
302             if (children != null) {
303                 logModules(children, pad + "    ");
304             }
305         }
306     }
307 
308 }