Search in sources :

Example 16 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor 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 17 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project mule by mulesoft.

the class UnixController method getProcessId.

@Override
public int getProcessId() {
    Map<Object, Object> newEnv = this.copyEnvironmentVariables();
    DefaultExecutor executor = new DefaultExecutor();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    executor.setWatchdog(watchdog);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    executor.setStreamHandler(streamHandler);
    if (this.doExecution(executor, new CommandLine(this.muleBin).addArgument("status"), newEnv) == 0) {
        Matcher matcher = STATUS_PATTERN.matcher(outputStream.toString());
        if (matcher.find()) {
            return Integer.parseInt(matcher.group(3));
        } else {
            throw new MuleControllerException("bin/mule status didn't return the expected pattern: " + STATUS);
        }
    } else {
        throw new MuleControllerException("Mule Runtime is not running");
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) Matcher(java.util.regex.Matcher) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 18 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project vertx-openshift-it by cescoffier.

the class OC method executeWithQuotes.

/**
 * This method will look for the OC exec first, then execute a command constructed from <b>args</b> param.
 * @param handleQuoting specifies whether to wrap arguments in quotation marks or not.
 * @param args args from which the command will be constructed
 * @return output of the executed command, wrapped in single quotation marks.
 */
public static String executeWithQuotes(boolean handleQuoting, String... args) {
    File oc_executable = find();
    CommandLine commandLine = new CommandLine(oc_executable);
    commandLine.addArguments(args, handleQuoting);
    DefaultExecutor executor = new DefaultExecutor();
    try {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        PumpStreamHandler handler = new PumpStreamHandler(output);
        executor.setStreamHandler(handler);
        executor.setExitValues(new int[] { 0, 1, 2 });
        executor.execute(commandLine);
        if (!output.toString().isEmpty()) {
            System.out.println("====");
            System.out.println(output);
            System.out.println("====");
        }
        return output.toString();
    } catch (IOException e) {
        throw new RuntimeException("Unable to execute " + commandLine.toString(), e);
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 19 with DefaultExecutor

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

Example 20 with DefaultExecutor

use of org.apache.commons.exec.DefaultExecutor in project storm by apache.

the class ServerUtils method execCommand.

public static int execCommand(String... command) throws ExecuteException, IOException {
    CommandLine cmd = new CommandLine(command[0]);
    for (int i = 1; i < command.length; i++) {
        cmd.addArgument(command[i]);
    }
    DefaultExecutor exec = new DefaultExecutor();
    return exec.execute(cmd);
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) DefaultExecutor(org.apache.commons.exec.DefaultExecutor)

Aggregations

DefaultExecutor (org.apache.commons.exec.DefaultExecutor)82 CommandLine (org.apache.commons.exec.CommandLine)62 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)47 IOException (java.io.IOException)36 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)34 ExecuteException (org.apache.commons.exec.ExecuteException)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)26 File (java.io.File)25 Executor (org.apache.commons.exec.Executor)12 DefaultExecuteResultHandler (org.apache.commons.exec.DefaultExecuteResultHandler)9 ShutdownHookProcessDestroyer (org.apache.commons.exec.ShutdownHookProcessDestroyer)9 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 URL (java.net.URL)4 Properties (java.util.Properties)4 LogOutputStream (org.apache.commons.exec.LogOutputStream)4 InputStream (java.io.InputStream)3 PipedInputStream (java.io.PipedInputStream)3 PipedOutputStream (java.io.PipedOutputStream)3 HashSet (java.util.HashSet)3