KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.geronimo.mavenplugins.geronimo.server;
20
21 import org.apache.maven.plugin.MojoExecutionException;
22
23 import org.apache.geronimo.genesis.util.ObjectHolder;
24 import org.apache.geronimo.mavenplugins.geronimo.ServerProxy;
25 import org.apache.geronimo.mavenplugins.geronimo.reporting.ReportingMojoSupport;
26
27 import java.util.Timer JavaDoc;
28 import java.util.TimerTask JavaDoc;
29
30 /**
31  * Wait for a Geronimo server to start.
32  *
33  * @goal wait-for-server
34  *
35  * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $
36  */

37 public class WaitForServerMojo
38     extends ReportingMojoSupport
39 {
40     /**
41      * Time in seconds to wait while verifing that the server has started.
42      *
43      * @parameter expression="${timeout}" default-value="-1"
44      */

45     private int timeout = -1;
46
47     private Timer JavaDoc timer = new Timer JavaDoc(true);
48
49     //
50
// TODO: See if start-server can share some of this code
51
//
52

53     protected void doExecute() throws Exception JavaDoc {
54         log.info("Waiting for Geronimo server...");
55
56         // Setup a callback to time out verification
57
final ObjectHolder verifyTimedOut = new ObjectHolder();
58
59         TimerTask JavaDoc timeoutTask = new TimerTask JavaDoc() {
60             public void run() {
61                 verifyTimedOut.set(Boolean.TRUE);
62             }
63         };
64
65         if (timeout > 0) {
66             log.debug("Starting verify timeout task; triggers in: " + timeout + "s");
67             timer.schedule(timeoutTask, timeout * 1000);
68         }
69
70         // Verify server started
71
ServerProxy server = new ServerProxy(hostname, port, username, password);
72         boolean started = false;
73         while (!started) {
74             if (verifyTimedOut.isSet()) {
75                 throw new MojoExecutionException("Unable to verify if the server was started in the given time");
76             }
77
78             started = server.isFullyStarted();
79
80             if (!started) {
81                 Throwable JavaDoc error = server.getLastError();
82                 if (error != null) {
83                     log.debug("Server query failed; ignoring", error);
84                 }
85
86                 Thread.sleep(1000);
87             }
88         }
89
90         // Stop the timer, server should be up now
91
timeoutTask.cancel();
92
93         log.info("Geronimo server started");
94     }
95
96     protected String JavaDoc getFullClassName() {
97         return this.getClass().getName();
98     }
99 }
100
Popular Tags