KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
28 import javax.enterprise.deploy.spi.Target JavaDoc;
29 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
30 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
31 import javax.enterprise.deploy.spi.status.DeploymentStatus JavaDoc;
32
33 import org.apache.maven.artifact.Artifact;
34 import org.apache.maven.plugin.MojoExecutionException;
35
36 import org.apache.geronimo.mavenplugins.geronimo.ModuleConfig;
37
38 /**
39  * Deploy modules (and optionally starting them) to a Geronimo server.
40  *
41  * @goal deploy-module
42  *
43  * @version $Rev: 481315 $ $Date: 2006-12-01 12:41:06 -0500 (Fri, 01 Dec 2006) $
44  */

45 public class DeployModuleMojo
46     extends ModuleMojoSupport
47 {
48     /**
49      * A file which points to a specific module's jar | war | ear | rar archive.
50      * If this parameter is set, then it will be used instead of from the
51      * modules configuration.
52      *
53      * @parameter expression="${moduleArchive}"
54      * @optional
55      */

56     protected File JavaDoc moduleArchive = null;
57
58     /**
59      * The fully qualified path of the external plan file (geronimo-web.xml).
60      * The application module may already have included in the package a deployment plan or the
61      * application is so simple that may not require any deployment plan.
62      *
63      * @parameter expression="${modulePlan}"
64      * @optional
65      */

66     private File JavaDoc modulePlan = null;
67
68     /**
69      * Flag to indicate if modules should be started after they have been distributed to the server.
70      *
71      * @parameter default-value="true"
72      * @optional
73      */

74     private boolean startModules = false;
75
76     protected void doExecute() throws Exception JavaDoc {
77         List JavaDoc completed = new ArrayList JavaDoc();
78
79         if (moduleArchive != null || modulePlan != null) {
80             log.info("Using non-artifact based module archive: " + moduleArchive);
81             log.info("Using non-artifact based plan: " + modulePlan);
82
83             TargetModuleID JavaDoc[] ids = distribute(moduleArchive, modulePlan);
84             completed.add(ids);
85         }
86         else if (modules == null || modules.length == 0) {
87             throw new MojoExecutionException("At least one module configuration (or moduleArchive) must be specified");
88         }
89
90         if (modules != null && modules.length != 0) {
91             log.info("Using artifact based module archive(s)...");
92
93             for (int i=0; i<modules.length; i++) {
94                 TargetModuleID JavaDoc[] ids = distribute(getModuleArchive(modules[i]), modules[i].getPlan());
95                 completed.add(ids);
96             }
97         }
98
99         if (startModules) {
100             log.info("Starting modules...");
101
102             Iterator JavaDoc iter = completed.iterator();
103             while (iter.hasNext()) {
104                 TargetModuleID JavaDoc[] moduleIds = (TargetModuleID JavaDoc[])iter.next();
105                 for (int i=0; i < moduleIds.length; i++) {
106                     String JavaDoc url = moduleIds[i].getWebURL();
107                     log.info("Starting module: " + moduleIds[i].getModuleID() + (url == null ? "" : ("; URL: " + url)));
108                 }
109
110                 ProgressObject JavaDoc progress = getDeploymentManager().start(moduleIds);
111                 DeploymentStatus JavaDoc status = waitFor(progress);
112
113                 if (status.isFailed()) {
114                     throw new MojoExecutionException("Failed to start modules: " + status.getMessage());
115                 }
116
117                 log.info("Started module(s):");
118                 logModules(moduleIds, " ");
119             }
120         }
121     }
122
123     private File JavaDoc getModuleArchive(final ModuleConfig module) throws MojoExecutionException {
124         Artifact artifact = getArtifact(module);
125
126         File JavaDoc file = artifact.getFile();
127         if (file == null) {
128             throw new MojoExecutionException("Module artifact does not have an attached file: " + module);
129         }
130
131         String JavaDoc type = artifact.getType();
132         log.debug("Artifact file is: " + file + " (" + type + ")");
133
134         if ((!"war".equals(type)) &&
135             (!"ear".equals(type)) &&
136             (!"rar".equals(type)) &&
137             (!"jar".equals(type)))
138         {
139             throw new MojoExecutionException("Module does not look like a J2EE archive: " + module);
140         }
141
142         return file;
143     }
144
145     private TargetModuleID JavaDoc[] distribute(final File JavaDoc file, final File JavaDoc plan) throws Exception JavaDoc {
146         log.info("Distributing module artifact: " + file + " with plan " + plan);
147
148         DeploymentManager JavaDoc manager = getDeploymentManager();
149         Target JavaDoc[] targets = manager.getTargets();
150         ProgressObject JavaDoc progress = manager.distribute(targets, file, plan);
151         DeploymentStatus JavaDoc status = waitFor(progress);
152
153         if (status.isFailed()) {
154             //
155
// FIXME: There must be a better way to handle this.
156
//
157
if (status.getMessage().indexOf("already exists") < 0 ) {
158                 throw new MojoExecutionException("Distribution failed: " + status.getMessage());
159             }
160             log.info("Module already exists");
161         }
162         
163
164         return (progress != null) ? progress.getResultTargetModuleIDs() : null;
165     }
166
167     protected String JavaDoc getFullClassName() {
168         return this.getClass().getName();
169     }
170 }
171
Popular Tags