Search in sources :

Example 16 with Executor

use of org.apache.commons.exec.Executor in project project-build-plugin by axonivy.

the class TestStartEngine method testKillEngineOnVmExit.

@Test
public void testKillEngineOnVmExit() throws Exception {
    StartTestEngineMojo mojo = rule.getMojo();
    Executor startedProcess = null;
    try {
        startedProcess = mojo.startEngine();
        assertThat(startedProcess.getProcessDestroyer()).isInstanceOf(ShutdownHookProcessDestroyer.class);
        ShutdownHookProcessDestroyer jvmShutdownHoock = (ShutdownHookProcessDestroyer) startedProcess.getProcessDestroyer();
        assertThat(jvmShutdownHoock.size()).as("One started engine process must be killed on VM end.").isEqualTo(1);
    } finally {
        kill(startedProcess);
    }
}
Also used : Executor(org.apache.commons.exec.Executor) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer) Test(org.junit.Test)

Example 17 with Executor

use of org.apache.commons.exec.Executor in project project-build-plugin by axonivy.

the class TestStartEngine method canStartEngine.

@Test
public void canStartEngine() throws Exception {
    StartTestEngineMojo mojo = rule.getMojo();
    assertThat(getProperty(EngineControl.Property.TEST_ENGINE_URL)).isNull();
    assertThat(getProperty(EngineControl.Property.TEST_ENGINE_LOG)).isNull();
    Executor startedProcess = null;
    try {
        startedProcess = mojo.startEngine();
        assertThat(getProperty(EngineControl.Property.TEST_ENGINE_URL)).startsWith("http://");
        assertThat(new File(getProperty(EngineControl.Property.TEST_ENGINE_LOG))).exists();
    } finally {
        kill(startedProcess);
    }
}
Also used : Executor(org.apache.commons.exec.Executor) File(java.io.File) Test(org.junit.Test)

Example 18 with Executor

use of org.apache.commons.exec.Executor in project alliance by codice.

the class MpegTsUdpClient method executeFFmpeg.

private static DefaultExecuteResultHandler executeFFmpeg(final CommandLine command, final int timeoutSeconds, final PumpStreamHandler streamHandler) throws IOException {
    final ExecuteWatchdog watchdog = new ExecuteWatchdog(timeoutSeconds * 1000);
    final Executor executor = new DefaultExecutor();
    executor.setWatchdog(watchdog);
    if (streamHandler != null) {
        executor.setStreamHandler(streamHandler);
    }
    final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    executor.execute(command, resultHandler);
    return resultHandler;
}
Also used : Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) DefaultExecuteResultHandler(org.apache.commons.exec.DefaultExecuteResultHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog)

Example 19 with Executor

use of org.apache.commons.exec.Executor in project smarthome by eclipse.

the class ExecUtil method executeCommandLineAndWaitResponse.

/**
 * <p>
 * Executes <code>commandLine</code>. Sometimes (especially observed on MacOS) the commandLine isn't executed
 * properly. In that cases another exec-method is to be used. To accomplish this please use the special delimiter '
 * <code>@@</code>'. If <code>commandLine</code> contains this delimiter it is split into a String[] array and the
 * special exec-method is used.
 *
 * <p>
 * A possible {@link IOException} gets logged but no further processing is done.
 *
 * @param commandLine the command line to execute
 * @param timeout timeout for execution in milliseconds
 * @return response data from executed command line
 */
public static String executeCommandLineAndWaitResponse(String commandLine, int timeout) {
    String retval = null;
    CommandLine cmdLine = null;
    if (commandLine.contains(CMD_LINE_DELIMITER)) {
        String[] cmdArray = commandLine.split(CMD_LINE_DELIMITER);
        cmdLine = new CommandLine(cmdArray[0]);
        for (int i = 1; i < cmdArray.length; i++) {
            cmdLine.addArgument(cmdArray[i], false);
        }
    } else {
        cmdLine = CommandLine.parse(commandLine);
    }
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    Executor executor = new DefaultExecutor();
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(stdout);
    executor.setExitValues(null);
    executor.setStreamHandler(streamHandler);
    executor.setWatchdog(watchdog);
    Logger logger = LoggerFactory.getLogger(ExecUtil.class);
    try {
        executor.execute(cmdLine, resultHandler);
        logger.debug("executed commandLine '{}'", commandLine);
    } catch (ExecuteException e) {
        logger.warn("couldn't execute commandLine '{}'", commandLine, e);
    } catch (IOException e) {
        logger.warn("couldn't execute commandLine '{}'", commandLine, e);
    }
    // can safely request the exit code
    try {
        resultHandler.waitFor();
        int exitCode = resultHandler.getExitValue();
        retval = StringUtils.chomp(stdout.toString());
        if (resultHandler.getException() != null) {
            logger.warn("{}", resultHandler.getException().getMessage());
        } else {
            logger.debug("exit code '{}', result '{}'", exitCode, retval);
        }
    } catch (InterruptedException e) {
        logger.warn("Timeout occurred when executing commandLine '{}'", commandLine, e);
    }
    return retval;
}
Also used : DefaultExecuteResultHandler(org.apache.commons.exec.DefaultExecuteResultHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Logger(org.slf4j.Logger) CommandLine(org.apache.commons.exec.CommandLine) Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 20 with Executor

use of org.apache.commons.exec.Executor in project stanbol by apache.

the class JarExecutor method start.

/**
 * Start the jar if not done yet, and setup runtime hook to stop it.
 */
public void start() throws Exception {
    final ExecuteResultHandler h = new ExecuteResultHandler() {

        @Override
        public void onProcessFailed(ExecuteException ex) {
            log.error("Process execution failed:" + ex, ex);
        }

        @Override
        public void onProcessComplete(int result) {
            log.info("Process execution complete, exit code=" + result);
        }
    };
    final String vmOptions = System.getProperty("jar.executor.vm.options");
    final Executor e = new DefaultExecutor();
    if (this.workingDirectory != null) {
        e.setWorkingDirectory(this.workingDirectory);
    }
    final CommandLine cl = new CommandLine(javaExecutable);
    if (vmOptions != null && vmOptions.length() > 0) {
        // not the case for common usage patterns
        for (String option : StringUtils.split(vmOptions, " ")) {
            cl.addArgument(option);
        }
    }
    cl.addArgument("-jar");
    cl.addArgument(jarToExecute.getAbsolutePath());
    cl.addArgument("-p");
    cl.addArgument(String.valueOf(serverPort));
    log.info("Executing " + cl);
    e.setStreamHandler(new PumpStreamHandler());
    e.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    e.execute(cl, h);
}
Also used : ExecuteResultHandler(org.apache.commons.exec.ExecuteResultHandler) CommandLine(org.apache.commons.exec.CommandLine) Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteException(org.apache.commons.exec.ExecuteException) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer)

Aggregations

Executor (org.apache.commons.exec.Executor)27 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)16 File (java.io.File)12 CommandLine (org.apache.commons.exec.CommandLine)10 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)10 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)10 Test (org.junit.Test)10 IOException (java.io.IOException)8 BaseEngineProjectMojoTest (ch.ivyteam.ivy.maven.BaseEngineProjectMojoTest)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 DefaultExecuteResultHandler (org.apache.commons.exec.DefaultExecuteResultHandler)6 ExecuteException (org.apache.commons.exec.ExecuteException)6 ShutdownHookProcessDestroyer (org.apache.commons.exec.ShutdownHookProcessDestroyer)6 StartTestEngineMojo (ch.ivyteam.ivy.maven.test.StartTestEngineMojo)2 OutputStream (java.io.OutputStream)2 PrintStream (java.io.PrintStream)2 ExecuteResultHandler (org.apache.commons.exec.ExecuteResultHandler)2 StopWatch (org.apache.commons.lang3.time.StopWatch)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.linesOf (org.assertj.core.api.Assertions.linesOf)2