/* * ======================================================================== * * Copyright 2005 Vincent Massol. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ======================================================================== */ package org.codehaus.cargo.maven2; import org.apache.maven.plugin.MojoExecutionException; import org.codehaus.cargo.container.Container; import org.codehaus.cargo.container.LocalContainer; /** * Start a container using Cargo. * * @version $Id: StartContainerMojo.java 529 2005-10-04 20:44:42Z mark $ * @goal start */ public class CargoContainerStartMojo extends AbstractCargoContainerMojo implements Runnable { /** * When we keep the container started, this is the time we wait before checking if * the container is stopped. */ private static final int SLEEP = 100; /** * Decides whether to wait after the container is started or to return the execution * flow to the user. * * @parameter default-value = "false" * @required */ private boolean wait; private Container container; /** * @see org.apache.maven.plugin.Mojo#execute() */ public void execute() throws MojoExecutionException { this.container = getContainerElement().createContainer(); if (!(this.container instanceof LocalContainer)) { throw new MojoExecutionException("Only local containers can be started"); } ((LocalContainer) this.container).start(); if (this.wait) { startWaitThread(); } } /** * Wait indefinitely till the server is stopped by some external command. */ public void run() { while (!this.container.getState().isStopped()) { try { Thread.sleep(SLEEP); } catch (InterruptedException exception) { getLog().warn(exception); } } } /** * Start the thread that will wait indefinitely till the server is stopped by some external * command. */ private void startWaitThread() { new Thread(this).start(); } }