KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > mavenplugins > geronimo > server > StartServerMojo


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.server;
21
22 import java.io.File JavaDoc;
23
24 import java.util.Timer JavaDoc;
25 import java.util.TimerTask JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.ArrayList JavaDoc;
33
34 import org.apache.maven.plugin.MojoExecutionException;
35
36 import org.apache.tools.ant.taskdefs.Java;
37
38 import org.apache.geronimo.genesis.util.ObjectHolder;
39 import org.apache.geronimo.mavenplugins.geronimo.ServerProxy;
40
41 import org.codehaus.plexus.util.FileUtils;
42
43 /**
44  * Start the Geronimo server.
45  *
46  * @goal start-server
47  *
48  * @version $Rev: 482883 $ $Date: 2006-12-05 23:02:14 -0500 (Tue, 05 Dec 2006) $
49  */

50 public class StartServerMojo
51     extends InstallerMojoSupport
52 {
53     /**
54      * Flag to control if we background the server or block Maven execution.
55      *
56      * @parameter expression="${background}" default-value="false"
57      */

58     private boolean background = false;
59
60     /**
61      * Set the maximum memory for the forked JVM.
62      *
63      * @parameter expression="${maximumMemory}"
64      */

65     private String JavaDoc maximumMemory = null;
66
67     /**
68      * Enable quiet mode.
69      *
70      * @parameter expression="${quiet}" default-value="false"
71      */

72     private boolean quiet = false;
73
74     /**
75      * Enable verbose mode.
76      *
77      * @parameter expression="${verbose}" default-value="false"
78      */

79     private boolean verbose = false;
80
81     /**
82      * Enable veryverbose mode.
83      *
84      * @parameter expression="${veryverbose}" default-value="false"
85      */

86     private boolean veryverbose = false;
87
88     /**
89      * Time in seconds to wait before terminating the forked JVM.
90      *
91      * @parameter expression="${timeout}" default-value="-1"
92      */

93     private int timeout = -1;
94
95     /**
96      * Time in seconds to wait while verifing that the server has started.
97      *
98      * @parameter expression="${verifyTimeout}" default-value="-1"
99      */

100     private int verifyTimeout = -1;
101
102     /**
103      * An array of option sets which can be enabled by setting optionSetId.
104      *
105      * @parameter
106      */

107     private OptionSet[] optionSets = null;
108
109     /**
110      * A comma seperated list of optionSets to enabled.
111      *
112      * @parameter expression="${options}"
113      */

114     private String JavaDoc options = null;
115
116     /**
117      * A list of module names to be started using --override.
118      *
119      * @parameter
120      */

121     private String JavaDoc[] startModules = null;
122
123     private Timer JavaDoc timer = new Timer JavaDoc(true);
124
125     protected void doExecute() throws Exception JavaDoc {
126         installAssembly();
127
128         log.info("Starting Geronimo server...");
129         
130         // Setup the JVM to start the server with
131
final Java java = (Java)createTask("java");
132         java.setJar(new File JavaDoc(geronimoHome, "bin/server.jar"));
133         java.setDir(geronimoHome);
134         java.setFailonerror(true);
135         java.setFork(true);
136         
137         if (timeout > 0) {
138             java.setTimeout(new Long JavaDoc(timeout * 1000));
139         }
140
141         if (maximumMemory != null) {
142             java.setMaxmemory(maximumMemory);
143         }
144
145         // Apply option sets
146
if (options != null && (optionSets == null || optionSets.length == 0)) {
147             throw new MojoExecutionException("At least one optionSet must be defined to select one using options");
148         }
149         else if (options == null) {
150             options = "default";
151         }
152         
153         if (optionSets != null && optionSets.length != 0) {
154             OptionSet[] sets = selectOptionSets();
155
156             for (int i=0; i < sets.length; i++) {
157                 if (log.isDebugEnabled()) {
158                     log.debug("Selected option set: " + sets[i]);
159                 }
160                 else {
161                     log.info("Selected option set: " + sets[i].getId());
162                 }
163
164                 String JavaDoc[] options = sets[i].getOptions();
165                 if (options != null) {
166                     for (int j=0; j < options.length; j++) {
167                         java.createJvmarg().setValue(options[j]);
168                     }
169                 }
170
171                 Properties JavaDoc props = sets[i].getProperties();
172                 if (props != null) {
173                     Iterator JavaDoc iter = props.keySet().iterator();
174                     while (iter.hasNext()) {
175                         String JavaDoc name = (String JavaDoc)iter.next();
176                         String JavaDoc value = props.getProperty(name);
177
178                         setSystemProperty(java, name, value);
179                     }
180                 }
181             }
182         }
183
184         // Set the properties which we pass to the JVM from the startup script
185
setSystemProperty(java, "org.apache.geronimo.base.dir", geronimoHome);
186         setSystemProperty(java, "java.io.tmpdir", new File JavaDoc(geronimoHome, "var/temp"));
187         setSystemProperty(java, "java.endorsed.dirs", appendSystemPath("java.endorsed.dirs", new File JavaDoc(geronimoHome, "lib/endorsed")));
188         setSystemProperty(java, "java.ext.dirs", appendSystemPath("java.ext.dirs", new File JavaDoc(geronimoHome, "lib/ext")));
189         
190         if (quiet) {
191             java.createArg().setValue("--quiet");
192         }
193         else {
194             java.createArg().setValue("--long");
195         }
196
197         if (verbose) {
198             java.createArg().setValue("--verbose");
199         }
200
201         if (veryverbose) {
202             java.createArg().setValue("--veryverbose");
203         }
204
205         if (startModules != null) {
206             if (startModules.length == 0) {
207                 throw new MojoExecutionException("At least one module name must be configured with startModule");
208             }
209
210             log.info("Overriding the set of modules to be started");
211
212             java.createArg().setValue("--override");
213
214             for (int i=0; i < startModules.length; i++) {
215                 java.createArg().setValue(startModules[i]);
216             }
217         }
218
219         //
220
// TODO: Check if this really does capture STDERR or not!
221
//
222

223         if (logOutput) {
224             File JavaDoc file = getLogFile();
225             FileUtils.forceMkdir(file.getParentFile());
226
227             log.info("Redirecting output to: " + file);
228             
229             java.setOutput(file);
230         }
231
232         // Holds any exception that was thrown during startup
233
final ObjectHolder errorHolder = new ObjectHolder();
234
235         // Start the server int a seperate thread
236
Thread JavaDoc t = new Thread JavaDoc("Geronimo Server Runner") {
237             public void run() {
238                 try {
239                     java.execute();
240                 }
241                 catch (Exception JavaDoc e) {
242                     errorHolder.set(e);
243
244                     //
245
// NOTE: Don't log here, as when the JVM exists an exception will get thrown by Ant
246
// but that should be fine.
247
//
248
}
249             }
250         };
251         t.start();
252
253         log.debug("Waiting for Geronimo server...");
254
255         // Setup a callback to time out verification
256
final ObjectHolder verifyTimedOut = new ObjectHolder();
257
258         TimerTask JavaDoc timeoutTask = new TimerTask JavaDoc() {
259             public void run() {
260                 verifyTimedOut.set(Boolean.TRUE);
261             }
262         };
263
264         if (verifyTimeout > 0) {
265             log.debug("Starting verify timeout task; triggers in: " + verifyTimeout + "s");
266             timer.schedule(timeoutTask, verifyTimeout * 1000);
267         }
268
269         // Verify server started
270
ServerProxy server = new ServerProxy(hostname, port, username, password);
271         boolean started = false;
272         while (!started) {
273             if (verifyTimedOut.isSet()) {
274                 throw new MojoExecutionException("Unable to verify if the server was started in the given time");
275             }
276
277             if (errorHolder.isSet()) {
278                 throw new MojoExecutionException("Failed to start Geronimo server", (Throwable JavaDoc)errorHolder.get());
279             }
280
281             started = server.isFullyStarted();
282
283             if (!started) {
284                 Throwable JavaDoc error = server.getLastError();
285                 if (error != null) {
286                     log.debug("Server query failed; ignoring", error);
287                 }
288
289                 Thread.sleep(1000);
290             }
291         }
292
293         // Stop the timer, server should be up now
294
timeoutTask.cancel();
295         
296         log.info("Geronimo server started");
297
298         if (!background) {
299             log.info("Waiting for Geronimo server to shutdown...");
300
301             t.join();
302         }
303     }
304
305     private String JavaDoc appendSystemPath(final String JavaDoc name, final File JavaDoc file) {
306         assert name != null;
307         assert file != null;
308
309         return System.getProperty(name) + File.pathSeparator + file.getPath();
310     }
311
312     private OptionSet[] selectOptionSets() throws MojoExecutionException {
313         // Make a map of the option sets and validate ids
314
Map JavaDoc map = new HashMap JavaDoc();
315         for (int i=0; i<optionSets.length; i++) {
316             if (log.isDebugEnabled()) {
317                 log.debug("Checking option set: " + optionSets[i]);
318             }
319             
320             String JavaDoc id = optionSets[i].getId();
321
322             if (id == null && optionSets.length > 1) {
323                 throw new MojoExecutionException("Must specify id for optionSet when more than one optionSet is configured");
324             }
325             else if (id == null && optionSets.length == 1) {
326                 id = "default";
327                 optionSets[i].setId(id);
328             }
329
330             assert id != null;
331             id = id.trim();
332
333             if (map.containsKey(id)) {
334                 throw new MojoExecutionException("Must specify unique id for optionSet: " + id);
335             }
336             map.put(id, optionSets[i]);
337         }
338
339         StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(options, ",");
340
341         List JavaDoc selected = new ArrayList JavaDoc();
342         while (stok.hasMoreTokens()) {
343             String JavaDoc id = stok.nextToken();
344             OptionSet set = (OptionSet)map.get(id);
345             
346             if (set == null) {
347                 if ("default".equals(id)) {
348                     log.debug("Default optionSet selected, but no optionSet defined with that id; ignoring");
349                 }
350                 else {
351                     log.warn("Missing optionSet for id: " + id);
352                 }
353             }
354             else {
355                 selected.add(set);
356             }
357         }
358
359         return (OptionSet[]) selected.toArray(new OptionSet[selected.size()]);
360     }
361
362     protected String JavaDoc getFullClassName() {
363         return this.getClass().getName();
364     }
365 }
366
Popular Tags