Search in sources :

Example 41 with CommandLine

use of org.apache.commons.exec.CommandLine 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)

Example 42 with CommandLine

use of org.apache.commons.exec.CommandLine in project sling 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() {

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

        public void onProcessComplete(int result) {
            log.info("Process execution complete, exit code=" + result);
        }
    };
    final String vmOptions = config.getProperty(PROP_VM_OPTIONS);
    executor = new DefaultExecutor();
    final CommandLine cl = new CommandLine(jvmFullPath);
    if (vmOptions != null && vmOptions.length() > 0) {
        cl.addArguments(vmOptions);
    }
    cl.addArgument("-jar");
    cl.addArgument(jarToExecute.getAbsolutePath());
    // Additional options for the jar that's executed.
    // $JAREXEC_SERVER_PORT$ is replaced our serverPort value
    String jarOptions = config.getProperty(PROP_JAR_OPTIONS);
    if (jarOptions != null && jarOptions.length() > 0) {
        jarOptions = jarOptions.replaceAll("\\$JAREXEC_SERVER_PORT\\$", String.valueOf(serverPort));
        log.info("Executable jar options: {}", jarOptions);
        cl.addArguments(jarOptions);
    }
    final String workFolderOption = config.getProperty(PROP_WORK_FOLDER);
    if (workFolderOption != null && workFolderOption.length() > 0) {
        final File workFolder = new File(workFolderOption);
        if (!workFolder.isDirectory()) {
            throw new IOException("Work dir set by " + PROP_WORK_FOLDER + " option does not exist: " + workFolder.getAbsolutePath());
        }
        log.info("Setting working directory for executable jar: {}", workFolder.getAbsolutePath());
        executor.setWorkingDirectory(workFolder);
    }
    String tmStr = config.getProperty(PROP_EXIT_TIMEOUT_SECONDS);
    final int exitTimeoutSeconds = tmStr == null ? DEFAULT_EXIT_TIMEOUT : Integer.valueOf(tmStr);
    if ("true".equals(config.getProperty(PROP_SYNC_EXEC, ""))) {
        final long start = System.currentTimeMillis();
        log.info("Executing and waiting for result: " + cl);
        final int result = executor.execute(cl);
        final int expected = Integer.valueOf(config.getProperty(PROP_SYNC_EXEC_EXPECTED, "0"));
        log.info("Execution took " + (System.currentTimeMillis() - start) + " msec");
        if (result != expected) {
            throw new ExecutorException("Expected result code " + expected + ", got " + result);
        }
    } else {
        log.info("Executing asynchronously: " + cl);
        executor.setStreamHandler(new PumpStreamHandler());
        final ShutdownHookSingleProcessDestroyer pd = new ShutdownHookSingleProcessDestroyer("java -jar " + jarToExecute.getName(), exitTimeoutSeconds);
        final boolean waitOnShutdown = Boolean.valueOf(config.getProperty(PROP_WAIT_ONSHUTDOWN, "false"));
        log.info("Setting up ProcessDestroyer with waitOnShutdown=" + waitOnShutdown);
        pd.setWaitOnShutdown(waitOnShutdown);
        executor.setProcessDestroyer(pd);
        executor.execute(cl, h);
    }
}
Also used : ExecuteResultHandler(org.apache.commons.exec.ExecuteResultHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) IOException(java.io.IOException) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteException(org.apache.commons.exec.ExecuteException) File(java.io.File)

Example 43 with CommandLine

use of org.apache.commons.exec.CommandLine in project sling 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() {

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

        public void onProcessComplete(int result) {
            log.info("Process execution complete, exit code=" + result);
        }
    };
    final String vmOptions = config.getProperty(PROP_VM_OPTIONS);
    executor = new DefaultExecutor();
    final CommandLine cl = new CommandLine(jvmFullPath);
    if (vmOptions != null && vmOptions.length() > 0) {
        cl.addArguments(vmOptions);
    }
    cl.addArgument("-jar");
    cl.addArgument(jarToExecute.getAbsolutePath());
    // Additional options for the jar that's executed.
    // $JAREXEC_SERVER_PORT$ is replaced our serverPort value
    String jarOptions = config.getProperty(PROP_JAR_OPTIONS);
    if (jarOptions != null && jarOptions.length() > 0) {
        jarOptions = jarOptions.replaceAll("\\$JAREXEC_SERVER_PORT\\$", String.valueOf(serverPort));
        log.info("Executable jar options: {}", jarOptions);
        cl.addArguments(jarOptions);
    }
    final String workFolderOption = config.getProperty(PROP_WORK_FOLDER);
    if (workFolderOption != null && workFolderOption.length() > 0) {
        final File workFolder = new File(workFolderOption);
        if (!workFolder.isDirectory()) {
            throw new IOException("Work dir set by " + PROP_WORK_FOLDER + " option does not exist: " + workFolder.getAbsolutePath());
        }
        log.info("Setting working directory for executable jar: {}", workFolder.getAbsolutePath());
        executor.setWorkingDirectory(workFolder);
    }
    String tmStr = config.getProperty(PROP_EXIT_TIMEOUT_SECONDS);
    final int exitTimeoutSeconds = tmStr == null ? DEFAULT_EXIT_TIMEOUT : Integer.valueOf(tmStr);
    if ("true".equals(config.getProperty(PROP_SYNC_EXEC, ""))) {
        final long start = System.currentTimeMillis();
        log.info("Executing and waiting for result: " + cl);
        final int result = executor.execute(cl);
        final int expected = Integer.valueOf(config.getProperty(PROP_SYNC_EXEC_EXPECTED, "0"));
        log.info("Execution took " + (System.currentTimeMillis() - start) + " msec");
        if (result != expected) {
            throw new ExecutorException("Expected result code " + expected + ", got " + result);
        }
    } else {
        log.info("Executing asynchronously: " + cl);
        executor.setStreamHandler(new PumpStreamHandler());
        final ShutdownHookSingleProcessDestroyer pd = new ShutdownHookSingleProcessDestroyer("java -jar " + jarToExecute.getName(), exitTimeoutSeconds);
        final boolean waitOnShutdown = Boolean.valueOf(config.getProperty(PROP_WAIT_ONSHUTDOWN, "false"));
        log.info("Setting up ProcessDestroyer with waitOnShutdown=" + waitOnShutdown);
        pd.setWaitOnShutdown(waitOnShutdown);
        executor.setProcessDestroyer(pd);
        executor.execute(cl, h);
    }
}
Also used : ExecuteResultHandler(org.apache.commons.exec.ExecuteResultHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) IOException(java.io.IOException) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteException(org.apache.commons.exec.ExecuteException) File(java.io.File)

Example 44 with CommandLine

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

the class EngineControl method start.

public Executor start() throws Exception {
    CommandLine startCmd = toEngineCommand(Command.start);
    context.log.info("Start Axon.ivy Engine in folder: " + context.engineDirectory);
    Executor executor = createEngineExecutor();
    executor.setStreamHandler(createEngineLogStreamForwarder(logLine -> findStartEngineUrl(logLine)));
    executor.setWatchdog(new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT));
    executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    executor.execute(startCmd, asynchExecutionHandler());
    waitForEngineStart(executor);
    return executor;
}
Also used : Executor(org.apache.commons.exec.Executor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LineOrientedOutputStreamRedirector(ch.ivyteam.ivy.maven.util.stream.LineOrientedOutputStreamRedirector) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CommandLine(org.apache.commons.exec.CommandLine) Supplier(java.util.function.Supplier) StringUtils(org.apache.commons.lang3.StringUtils) StartTestEngineMojo(ch.ivyteam.ivy.maven.StartTestEngineMojo) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteException(org.apache.commons.exec.ExecuteException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) OsgiDir(ch.ivyteam.ivy.maven.engine.EngineClassLoaderFactory.OsgiDir) StopWatch(org.apache.commons.lang3.time.StopWatch) ExecuteResultHandler(org.apache.commons.exec.ExecuteResultHandler) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) IOUtils(org.apache.commons.io.IOUtils) CommandLine(org.apache.commons.exec.CommandLine) Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer)

Aggregations

CommandLine (org.apache.commons.exec.CommandLine)44 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)26 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)20 IOException (java.io.IOException)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 ExecuteException (org.apache.commons.exec.ExecuteException)12 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)11 File (java.io.File)8 PipedInputStream (java.io.PipedInputStream)4 PipedOutputStream (java.io.PipedOutputStream)4 DefaultExecuteResultHandler (org.apache.commons.exec.DefaultExecuteResultHandler)4 ExecuteResultHandler (org.apache.commons.exec.ExecuteResultHandler)4 InputStream (java.io.InputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 Map (java.util.Map)3 Executor (org.apache.commons.exec.Executor)3 BufferedWriter (java.io.BufferedWriter)2 DataInputStream (java.io.DataInputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2