001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.geronimo.mavenplugins.geronimo.server;
020    
021    import org.apache.maven.plugin.MojoExecutionException;
022    
023    import org.apache.geronimo.genesis.ObjectHolder;
024    import org.apache.geronimo.mavenplugins.geronimo.ServerProxy;
025    import org.apache.geronimo.mavenplugins.geronimo.reporting.ReportingMojoSupport;
026    
027    import java.util.Timer;
028    import java.util.TimerTask;
029    
030    /**
031     * Wait for a Geronimo server to start.
032     *
033     * @goal wait-for-server
034     *
035     * @version $Rev: 453457 $ $Date: 2006-10-05 18:51:18 -0700 (Thu, 05 Oct 2006) $
036     */
037    public class WaitForServerMojo
038        extends ReportingMojoSupport
039    {
040        /**
041         * Time in seconds to wait while verifing that the server has started.
042         *
043         * @parameter expression="${timeout}" default-value="-1"
044         */
045        private int timeout = -1;
046    
047        private Timer timer = new Timer(true);
048    
049        //
050        // TODO: See if start-server can share some of this code
051        //
052    
053        protected void doExecute() throws Exception {
054            log.info("Waiting for Geronimo server...");
055    
056            // Setup a callback to time out verification
057            final ObjectHolder verifyTimedOut = new ObjectHolder();
058    
059            TimerTask timeoutTask = new TimerTask() {
060                public void run() {
061                    verifyTimedOut.set(Boolean.TRUE);
062                }
063            };
064    
065            if (timeout > 0) {
066                log.debug("Starting verify timeout task; triggers in: " + timeout + "s");
067                timer.schedule(timeoutTask, timeout * 1000);
068            }
069    
070            // Verify server started
071            ServerProxy server = new ServerProxy(hostname, port, username, password);
072            boolean started = false;
073            while (!started) {
074                if (verifyTimedOut.isSet()) {
075                    throw new MojoExecutionException("Unable to verify if the server was started in the given time");
076                }
077    
078                started = server.isFullyStarted();
079    
080                if (!started) {
081                    Throwable error = server.getLastError();
082                    if (error != null) {
083                        log.debug("Server query failed; ignoring", error);
084                    }
085    
086                    Thread.sleep(1000);
087                }
088            }
089    
090            // Stop the timer, server should be up now
091            timeoutTask.cancel();
092    
093            log.info("Geronimo server started");
094        }
095    
096        protected String getGoalName() {
097            //
098            //FIXME: There has to be way this can be computed instead of hardcoded absolutely.
099            //
100            return "wait-for-server";
101        }
102    }