Search in sources :

Example 1 with Executor

use of org.apache.commons.exec.Executor in project oxCore by GluuFederation.

the class ProcessHelper method executeProgram.

/**
 * @param printJobTimeout
 *            the printJobTimeout (ms) before the watchdog terminates the print
 *            process
 * @param printInBackground
 *            printing done in the background or blocking
 * @param streamHandler
 * @return a print result handler (implementing a future)
 * @throws IOException
 *             the test failed
 */
public static PrintResultHandler executeProgram(CommandLine commandLine, String workingDirectory, long printJobTimeout, boolean printInBackground, int successExitValue, ExecuteStreamHandler streamHandler) throws IOException {
    ExecuteWatchdog watchdog = null;
    PrintResultHandler resultHandler;
    // Create the executor and consider the successExitValue as success
    Executor executor = new DefaultExecutor();
    executor.setExitValue(successExitValue);
    if (StringHelper.isNotEmpty(workingDirectory)) {
        executor.setWorkingDirectory(new File(workingDirectory));
    }
    // Redirect streams if needed
    if (streamHandler != null) {
        executor.setStreamHandler(streamHandler);
    }
    // Create a watchdog if requested
    if (printJobTimeout > 0) {
        watchdog = new ExecuteWatchdog(printJobTimeout);
        executor.setWatchdog(watchdog);
    }
    // Pass a "ExecuteResultHandler" when doing background printing
    if (printInBackground) {
        LOG.debug(String.format("Executing non-blocking process %s", commandLine.toString()));
        resultHandler = new PrintResultHandler(watchdog);
        executor.execute(commandLine, resultHandler);
    } else {
        LOG.debug(String.format("Executing blocking process %s", commandLine.toString()));
        successExitValue = executor.execute(commandLine);
        resultHandler = new PrintResultHandler(successExitValue);
    }
    return resultHandler;
}
Also used : Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) File(java.io.File)

Example 2 with Executor

use of org.apache.commons.exec.Executor in project jaqy by Teradata.

the class Os method shell.

public void shell(File dir, String cmd) throws Exception {
    CommandLine commandLine;
    if (s_windows) {
        commandLine = new CommandLine("cmd");
        commandLine.addArgument("/C");
        commandLine.addArgument(cmd, false);
    } else {
        commandLine = new CommandLine("/bin/bash");
        commandLine.addArgument("-c");
        commandLine.addArgument(cmd, false);
    }
    Executor executor = new DefaultExecutor();
    executor.setWorkingDirectory(dir);
    executor.setStreamHandler(new PumpStreamHandler(System.out, System.out));
    executor.execute(commandLine);
}
Also used : 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)

Example 3 with Executor

use of org.apache.commons.exec.Executor in project scala-maven-plugin by davidB.

the class JavaMainCallerByFork method run.

@Override
public boolean run(boolean displayCmd, boolean throwFailure) throws Exception {
    List<String> cmd = buildCommand();
    displayCmd(displayCmd, cmd);
    Executor exec = new DefaultExecutor();
    // err and out are redirected to out
    if (!_redirectToLog) {
        exec.setStreamHandler(new PumpStreamHandler(System.out, System.err, System.in));
    } else {
        exec.setStreamHandler(new PumpStreamHandler(new LogOutputStream() {

            private LevelState _previous = new LevelState();

            @Override
            protected void processLine(String line, int level) {
                try {
                    _previous = LogProcessorUtils.levelStateOf(line, _previous);
                    switch(_previous.level) {
                        case ERROR:
                            requester.getLog().error(line);
                            break;
                        case WARNING:
                            requester.getLog().warn(line);
                            break;
                        default:
                            requester.getLog().info(line);
                            break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }));
    }
    CommandLine cl = new CommandLine(cmd.get(0));
    for (int i = 1; i < cmd.size(); i++) {
        cl.addArgument(cmd.get(i), false);
    }
    try {
        int exitValue = exec.execute(cl);
        if (exitValue != 0) {
            if (throwFailure) {
                throw new MojoFailureException("command line returned non-zero value:" + exitValue);
            }
            return false;
        }
        if (!displayCmd)
            tryDeleteArgFile(cmd);
        return true;
    } catch (ExecuteException exc) {
        if (throwFailure) {
            throw exc;
        }
        return false;
    }
}
Also used : 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) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ExecuteException(org.apache.commons.exec.ExecuteException) LevelState(scala_maven_executions.LogProcessorUtils.LevelState) LogOutputStream(org.apache.commons.exec.LogOutputStream) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 4 with Executor

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

the class MvelHelperTest method executeExternalJavaProcess.

protected String executeExternalJavaProcess(Class<?> mainClass) throws IOException {
    String classpath = MvelTestUtils.getClassPath();
    // See javadoc on MvelOverloadFailureReproduction for description of why we need to execute the test in a new JVM
    CommandLine cmdLine = new CommandLine("java");
    cmdLine.addArgument("-cp");
    cmdLine.addArgument(classpath, true);
    cmdLine.addArgument(mainClass.getName());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Executor executor = new DefaultExecutor();
    executor.setStreamHandler(new PumpStreamHandler(baos));
    try {
        executor.execute(cmdLine, new HashMap<String, String>());
    } catch (IOException e) {
        throw new IOException(new String(baos.toByteArray()));
    }
    return new String(baos.toByteArray());
}
Also used : 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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 5 with Executor

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

the class ElasticsearchRunner method runElasticsearch.

/**
 * Run Elasticsearch
 * @param host the host Elasticsearch should bind to
 * @param port the port Elasticsearch should listen on
 * @param elasticsearchInstallPath the path where Elasticsearch is installed
 * @return an observable that emits exactly one item when Elasticsearch has started
 */
public Observable<Void> runElasticsearch(String host, int port, String elasticsearchInstallPath) {
    JsonObject config = vertx.getOrCreateContext().config();
    String storage = config.getString(ConfigConstants.STORAGE_FILE_PATH);
    String root = storage + "/index";
    return vertx.<Void>rxExecuteBlocking(f -> {
        log.info("Starting Elasticsearch ...");
        // get Elasticsearch executable
        String executable = FilenameUtils.separatorsToSystem(elasticsearchInstallPath);
        executable = FilenameUtils.concat(executable, "bin");
        if (SystemUtils.IS_OS_WINDOWS) {
            executable = FilenameUtils.concat(executable, "elasticsearch.bat");
        } else {
            executable = FilenameUtils.concat(executable, "elasticsearch");
        }
        // start Elasticsearch
        CommandLine cmdl = new CommandLine(executable);
        cmdl.addArgument("-Ecluster.name=georocket-cluster");
        cmdl.addArgument("-Enode.name=georocket-node");
        cmdl.addArgument("-Enetwork.host=" + host);
        cmdl.addArgument("-Ehttp.port=" + port);
        cmdl.addArgument("-Epath.data=" + root + "/data");
        executor = new DefaultExecutor();
        executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
        executor.setWatchdog(new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT));
        // set ES_JAVA_OPTS environment variable if necessary
        Map<String, String> env = new HashMap<>(System.getenv());
        if (!env.containsKey("ES_JAVA_OPTS")) {
            String javaOpts = config.getString(ConfigConstants.INDEX_ELASTICSEARCH_JAVA_OPTS);
            if (javaOpts != null) {
                env.put("ES_JAVA_OPTS", javaOpts);
            }
        }
        try {
            executor.execute(cmdl, env, new DefaultExecuteResultHandler() {

                @Override
                public void onProcessComplete(final int exitValue) {
                    log.info("Elasticsearch quit with exit code: " + exitValue);
                }

                @Override
                public void onProcessFailed(final ExecuteException e) {
                    if (!stopped) {
                        log.error("Elasticsearch execution failed", e);
                    }
                }
            });
            f.complete();
        } catch (IOException e) {
            f.fail(e);
        }
    }).toObservable();
}
Also used : Executor(org.apache.commons.exec.Executor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) NoStackTraceThrowable(io.vertx.core.impl.NoStackTraceThrowable) IOException(java.io.IOException) HashMap(java.util.HashMap) CommandLine(org.apache.commons.exec.CommandLine) LoggerFactory(io.vertx.core.logging.LoggerFactory) Observable(rx.Observable) Map(java.util.Map) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer) JsonObject(io.vertx.core.json.JsonObject) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteException(org.apache.commons.exec.ExecuteException) RxUtils(io.georocket.util.RxUtils) Logger(io.vertx.core.logging.Logger) SystemUtils(org.apache.commons.lang.SystemUtils) FilenameUtils(org.apache.commons.io.FilenameUtils) DefaultExecuteResultHandler(org.apache.commons.exec.DefaultExecuteResultHandler) ConfigConstants(io.georocket.constants.ConfigConstants) Vertx(io.vertx.rxjava.core.Vertx) CommandLine(org.apache.commons.exec.CommandLine) DefaultExecuteResultHandler(org.apache.commons.exec.DefaultExecuteResultHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) JsonObject(io.vertx.core.json.JsonObject) ExecuteException(org.apache.commons.exec.ExecuteException) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) 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