2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.maven.plugin;
11 import java.net.MalformedURLException;
12 import java.util.ArrayList;
13 import java.util.List;
15 import org.apache.maven.plugin.MojoExecutionException;
16 import org.apache.maven.plugin.MojoFailureException;
17 import org.apache.maven.plugins.annotations.LifecyclePhase;
18 import org.apache.maven.plugins.annotations.Mojo;
19 import org.apache.maven.plugins.annotations.Parameter;
23 * Starts the controller
25 @Mojo( name = "run", defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST )
26 public class StartControllerMojo extends AbstractControllerMojo {
27 public static final String REDIRECT_LOG = "controller.out";
30 * The timeout value for starting the controller. Defaults to 60 secs
32 @Parameter(defaultValue = "60")
33 public int timeoutSecs = 60;
36 * The startArgs for starting the controller
38 @Parameter(required = false)
39 protected List<String> startArgs = new ArrayList<String>();
42 * The time to wait after successfully connecting to the controller and
43 * before returning from execution.
45 @Parameter(required = false)
46 protected int warmupTimeSecs = 10;
50 public void start() throws MojoExecutionException, MojoFailureException {
52 // if we can still connect to a controller, bail out
54 getLog().error("A controller is already running. Shutdown and retry.");
55 throw new MojoFailureException("Controller is already running.");
57 startArgs.add("-D" + CTRL_PROP);
58 Process process = invokeScript(startArgs, REDIRECT_LOG);
59 getLog().info("Controller starting... (waiting for open ports)");
61 waitForListening(process);
62 getLog().info("Controller port open. Waiting for warmup: "
64 Thread.sleep(warmupTimeSecs*1000);
65 } catch (Exception e) {
66 throw new MojoExecutionException(e.getMessage());
68 getLog().info("Controller started successfully.");
71 protected boolean waitForListening(Process process)
72 throws MalformedURLException, InterruptedException, MojoExecutionException
74 long timeElapsedMillis = 0L;
75 long sleepTimeMillis = 2000L; // 2 secs
76 long timeoutMillis = timeoutSecs * 1000;
78 while (timeElapsedMillis < timeoutMillis) {
79 long timeRemaining = timeoutMillis - timeElapsedMillis;
81 long toSleep = (sleepTimeMillis > timeRemaining)
82 ? timeRemaining : sleepTimeMillis;
83 Thread.sleep(toSleep);
84 timeElapsedMillis += toSleep;
88 if (!isControllerRunning()) {
89 throw new MojoExecutionException("Process seems to have exited prematurely.");