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.codehaus.mojo.pluginsupport.util.ObjectHolder;
024    
025    import org.apache.geronimo.mavenplugins.geronimo.ServerProxy;
026    import org.apache.geronimo.mavenplugins.geronimo.reporting.ReportingMojoSupport;
027    
028    import java.util.Timer;
029    import java.util.TimerTask;
030    
031    /**
032     * Wait for a Geronimo server to start.
033     *
034     * @goal wait-for-server
035     *
036     * @version $Rev: 524717 $ $Date: 2007-04-01 23:39:19 -0400 (Sun, 01 Apr 2007) $
037     */
038    public class WaitForServerMojo
039        extends ReportingMojoSupport
040    {
041        /**
042         * Time in seconds to wait while verifing that the server has started.
043         *
044         * @parameter expression="${timeout}" default-value="-1"
045         */
046        private int timeout = -1;
047    
048        private Timer timer = new Timer(true);
049    
050        //
051        // TODO: See if start-server can share some of this code
052        //
053    
054        protected void doExecute() throws Exception {
055            log.info("Waiting for Geronimo server...");
056    
057            // Setup a callback to time out verification
058            final ObjectHolder verifyTimedOut = new ObjectHolder();
059    
060            TimerTask timeoutTask = new TimerTask() {
061                public void run() {
062                    verifyTimedOut.set(Boolean.TRUE);
063                }
064            };
065    
066            if (timeout > 0) {
067                log.debug("Starting verify timeout task; triggers in: " + timeout + "s");
068                timer.schedule(timeoutTask, timeout * 1000);
069            }
070    
071            // Verify server started
072            ServerProxy server = new ServerProxy(hostname, port, username, password);
073            boolean started = false;
074            while (!started) {
075                if (verifyTimedOut.isSet()) {
076                    throw new MojoExecutionException("Unable to verify if the server was started in the given time");
077                }
078    
079                started = server.isFullyStarted();
080    
081                if (!started) {
082                    Throwable error = server.getLastError();
083                    if (error != null) {
084                        log.debug("Server query failed; ignoring", error);
085                    }
086    
087                    Thread.sleep(1000);
088                }
089            }
090    
091            // Stop the timer, server should be up now
092            timeoutTask.cancel();
093    
094            log.info("Geronimo server started");
095        }
096    
097        protected String getFullClassName() {
098            return this.getClass().getName();
099        } 
100    }