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.File;
23  import java.util.List;
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  
27  import javax.enterprise.deploy.spi.DeploymentManager;
28  import javax.enterprise.deploy.spi.Target;
29  import javax.enterprise.deploy.spi.TargetModuleID;
30  import javax.enterprise.deploy.spi.status.ProgressObject;
31  import javax.enterprise.deploy.spi.status.DeploymentStatus;
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: 450613 $ $Date: 2006-09-27 15:45:46 -0700 (Wed, 27 Sep 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 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 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 {
77          List completed = new ArrayList();
78  
79          if (moduleArchive != null) {
80              log.info("Using non-artifact based module archive: " + moduleArchive);
81  
82              TargetModuleID[] ids = distribute(moduleArchive, modulePlan);
83              completed.add(ids);
84          }
85          else if (modules == null || modules.length == 0) {
86              throw new MojoExecutionException("At least one module configuration (or moduleArchive) must be specified");
87          }
88  
89          if (modules != null && modules.length != 0) {
90              log.info("Using artifact based module archive(s)...");
91  
92              for (int i=0; i<modules.length; i++) {
93                  TargetModuleID[] ids = distribute(getModuleArchive(modules[i]), modules[i].getPlan());
94                  completed.add(ids);
95              }
96          }
97  
98          if (startModules) {
99              log.info("Starting modules...");
100 
101             Iterator iter = completed.iterator();
102             while (iter.hasNext()) {
103                 TargetModuleID[] moduleIds = (TargetModuleID[])iter.next();
104                 for (int i=0; i < moduleIds.length; i++) {
105                     String url = moduleIds[i].getWebURL();
106                     log.info("Starting module: " + moduleIds[i].getModuleID() + (url == null ? "" : ("; URL: " + url)));
107                 }
108 
109                 ProgressObject progress = getDeploymentManager().start(moduleIds);
110                 DeploymentStatus status = waitFor(progress);
111 
112                 if (status.isFailed()) {
113                     throw new MojoExecutionException("Failed to start modules: " + status.getMessage());
114                 }
115 
116                 log.info("Started module(s):");
117                 logModules(moduleIds, "    ");
118             }
119         }
120     }
121 
122     private File getModuleArchive(final ModuleConfig module) throws MojoExecutionException {
123         Artifact artifact = getArtifact(module);
124 
125         File file = artifact.getFile();
126         if (file == null) {
127             throw new MojoExecutionException("Module artifact does not have an attached file: " + module);
128         }
129 
130         String type = artifact.getType();
131         log.debug("Artifact file is: " + file + " (" + type + ")");
132 
133         if ((!"war".equals(type)) &&
134             (!"ear".equals(type)) &&
135             (!"rar".equals(type)) &&
136             (!"jar".equals(type)))
137         {
138             throw new MojoExecutionException("Module does not look like a J2EE archive: " + module);
139         }
140 
141         return file;
142     }
143 
144     private TargetModuleID[] distribute(final File file, final File plan) throws Exception {
145         assert file != null;
146         
147         log.info("Distributing module artifact: " + file);
148 
149         DeploymentManager manager = getDeploymentManager();
150         Target[] targets = manager.getTargets();
151         ProgressObject progress = manager.distribute(targets, file, plan);
152         DeploymentStatus status = waitFor(progress);
153 
154         if (status.isFailed()) {
155             throw new MojoExecutionException("Distribution failed: " + status.getMessage());
156         }
157 
158         return progress.getResultTargetModuleIDs();
159     }
160 
161     protected String getGoalName() {
162         //
163         // FIXME: There has to be way this can be computed instead of hardcoded absolutely.
164         //
165         return "deploy-module";
166     }
167 }