package com.lazerycode.jmeter.mojo;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import com.lazerycode.jmeter.configuration.JMeterArgumentsArray;
import com.lazerycode.jmeter.configuration.JMeterProcessJVMSettings;
import com.lazerycode.jmeter.testrunner.JMeterProcessBuilder;
import com.lazerycode.jmeter.utility.UtilityFunctions;

/**
 * Goal that runs JMeter in server mode.<br/>
 * This goal runs within Lifecycle phase {@link LifecyclePhase#TEST}.
 * @author Philippe Mouawad
 * @since 2.5.0
 */
@Mojo(name = "remote-server", defaultPhase = LifecyclePhase.TEST)
@Execute(goal = "configure")
public class RunJMeterServer extends AbstractJMeterMojo {

	@Parameter(defaultValue = "false")
	private boolean runInBackground;

	/**
	 * Port to listen on
	 */
	@Parameter
	private Integer serverPort;
	
    /**
     * Exported RMI host name
     */
    @Parameter
    private String exportedRmiHostname;
	
	private JMeterArgumentsArray testArgs;

	/**
	 * Load the JMeter server
	 *
	 * @throws MojoExecutionException
	 * @throws MojoFailureException
	 */
	@Override
	public void doExecute() throws MojoExecutionException, MojoFailureException {
		getLog().info(" ");
		getLog().info(LINE_SEPARATOR);
		getLog().info(" STARTING JMETER SERVER ON PORT:"+ serverPort + " WITH EXPORTED HOSTNAME:"+exportedRmiHostname);
		getLog().info(LINE_SEPARATOR);
		testArgs = initializeJMeterArgumentsArray();
		getLog().debug("JMeter is called with the following command line arguments: " + 
		        UtilityFunctions.humanReadableCommandLineOutput(testArgs.buildArgumentsArray()));
		startJMeterServer();
	}

	private JMeterArgumentsArray initializeJMeterArgumentsArray() throws MojoExecutionException {
		JMeterArgumentsArray currentTestArgs = new JMeterArgumentsArray(true, jmeterDirectory.getAbsolutePath());
		currentTestArgs.setProxyConfig(proxyConfig);
        for (File customPropertiesFile : customPropertiesFiles) {
            currentTestArgs.setACustomPropertiesFile(customPropertiesFile);
        }
        currentTestArgs.setLogRootOverride(overrideRootLogLevel);
        currentTestArgs.setLogsDirectory(logsDirectory.getAbsolutePath());
        currentTestArgs.setServerMode(exportedRmiHostname, serverPort);

		return currentTestArgs;
	}

	private void startJMeterServer() throws MojoExecutionException {
	    JMeterProcessJVMSettings jMeterProcessJVMSettings = null;
	    if(this.jMeterProcessJVMSettings == null) {
	        jMeterProcessJVMSettings = new JMeterProcessJVMSettings();
	    } else {
	        jMeterProcessJVMSettings = new JMeterProcessJVMSettings(this.jMeterProcessJVMSettings);
	    }
	    if(exportedRmiHostname!= null && !exportedRmiHostname.isEmpty()) {
	        jMeterProcessJVMSettings.getArguments().add("-Djava.rmi.server.hostname="+exportedRmiHostname);	        
	    }
	    jMeterProcessJVMSettings.getArguments().add("-Djava.awt.headless=true");
	    if(serverPort == null) {
	        throw new MojoExecutionException("serverPort is null, cannot start jmeter server");
	    }
	    jMeterProcessJVMSettings.getArguments().add("-Dserver_port="+serverPort);

		JMeterProcessBuilder jmeterProcessBuilder = new JMeterProcessBuilder(jMeterProcessJVMSettings, 
		        JMeterConfigurationHolder.getInstance().getRuntimeJarName());
		jmeterProcessBuilder.setWorkingDirectory(JMeterConfigurationHolder.getInstance().getWorkingDirectory());
		jmeterProcessBuilder.addArguments(testArgs.buildArgumentsArray());
		try {
			final Process process = jmeterProcessBuilder.startProcess();
			if (!runInBackground) {
				process.waitFor();
			} else {
			    TimeUnit.SECONDS.sleep(1L);
			}
		} catch (InterruptedException ex) {
			getLog().info(" ");
			getLog().info("System Exit Detected!  Stopping GUI...");
			getLog().info(" ");
			Thread.currentThread().interrupt();
		} catch (IOException e) {
			getLog().error("Error starting JMeter with args "+testArgs.buildArgumentsArray()
			    + ", in working directory:"+JMeterConfigurationHolder.getInstance().getWorkingDirectory()
			    , e);
		}
	}
}
@@ -82,46 +82,8 @@
 ion;
-%0Aimport java.util.concurrent.TimeUnit;
 %0A%0Aim
@@ -3884,55 +3884,8 @@
 ();%0A
-%09%09%09%7D else %7B%0A%09%09%09    TimeUnit.SECONDS.sleep(1L);%0A
 %09%09%09%7D

Remove test code