Search in sources :

Example 46 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project evosuite by EvoSuite.

the class ProcessLauncher method launchNewProcess.

public int launchNewProcess(File baseDir, String cmdString, int timeout) throws IOException, ProcessTimeoutException {
    DefaultExecutor executor = new DefaultExecutor();
    ExecuteWatchdog timeoutWatchdog = new ExecuteWatchdog(timeout);
    executor.setWatchdog(timeoutWatchdog);
    PumpStreamHandler streamHandler = new PumpStreamHandler(this.outAndErr, this.outAndErr, this.input);
    executor.setStreamHandler(streamHandler);
    if (baseDir != null) {
        executor.setWorkingDirectory(baseDir);
    }
    int exitValue;
    try {
        logger.debug("About to execute command " + cmdString);
        exitValue = executor.execute(CommandLine.parse(cmdString));
        if (executor.isFailure(exitValue) && timeoutWatchdog.killedProcess()) {
            // it was killed on purpose by the watchdog
            logger.debug("A timeout occured while executing a process");
            logger.debug("The command is " + cmdString);
            throw new ProcessTimeoutException("A timeout occurred while executing command " + cmdString);
        }
        return exitValue;
    } catch (ExecuteException e) {
        if (timeoutWatchdog.killedProcess()) {
            logger.debug("A timeout occured while executing a process");
            logger.debug("The command is " + cmdString);
            throw new ProcessTimeoutException("A timeout occurred while executing command " + cmdString);
        } else {
            throw e;
        }
    }
}
Also used : PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 47 with PumpStreamHandler

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

the class AbstractOSController method executeSyncCommand.

private int executeSyncCommand(String command, String[] args, Map<Object, Object> newEnv, int timeout) throws MuleControllerException {
    CommandLine commandLine = new CommandLine(muleBin);
    commandLine.addArgument(command);
    commandLine.addArguments(args);
    DefaultExecutor executor = new DefaultExecutor();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    executor.setWatchdog(watchdog);
    executor.setStreamHandler(new PumpStreamHandler());
    return doExecution(executor, commandLine, newEnv);
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog)

Example 48 with PumpStreamHandler

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

the class MuleUtils method executeCommand.

public static int executeCommand(String command, String... envVars) throws IOException {
    CommandLine cmdLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    Map<String, String> env = addEnvProperties(envVars);
    ExecuteWatchdog watchDog = new ExecuteWatchdog(TIMEOUT);
    executor.setWatchdog(watchDog);
    executor.setStreamHandler(new PumpStreamHandler());
    int result = executor.execute(cmdLine, env);
    if (executor.isFailure(result)) {
        if (watchDog.killedProcess()) {
            throw new RuntimeException("Reached timeout while running: " + cmdLine);
        }
        throw new RuntimeException("Process failed with return code [" + result + "]: " + cmdLine);
    }
    return result;
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog)

Example 49 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project fmv by f-agu.

the class ExecHelper method createFMVExecutor.

/**
 * @param workingFolder
 * @param defaultReaDLine
 * @return
 * @throws IOException
 */
protected FMVExecutor createFMVExecutor(File workingFolder, ReadLine defaultReaDLine) throws IOException {
    checkStreamHandler();
    // executeStreamHandler
    if (executeStreamHandler != null) {
        return FMVExecutor.with(workingFolder).executeStreamHandler(executeStreamHandler).build();
    }
    // input/out/err
    if (input != null || out != null || err != null) {
        // input & whatever
        ReadLineOutputStream outRL = new ReadLineOutputStream(out, getOutReadLine(defaultReaDLine));
        ReadLineOutputStream errRL = new ReadLineOutputStream(err, getErrReadLine(defaultReaDLine));
        ExecuteStreamHandler customExecuteStreamHandler = new PumpStreamHandler(outRL, errRL, input);
        return FMVExecutor.with(workingFolder).executeStreamHandler(customExecuteStreamHandler).build();
    }
    // ReadLine
    return FMVExecutor.with(workingFolder).out(getOutReadLine(defaultReaDLine)).err(getErrReadLine(defaultReaDLine)).charset(charset).lookReader(lookReader).build();
}
Also used : PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteStreamHandler(org.apache.commons.exec.ExecuteStreamHandler)

Example 50 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project jstorm by alibaba.

the class JStormUtils method launchProcess.

/**
 * If it is backend, please set resultHandler, such as DefaultExecuteResultHandler
 * If it is frontend, ByteArrayOutputStream.toString will return the calling result
 * <p>
 * This function will ignore whether the command is successfully executed or not
 *
 * @param command       command to be executed
 * @param environment   env vars
 * @param workDir       working directory
 * @param resultHandler exec result handler
 * @return output stream
 * @throws IOException
 */
@Deprecated
public static ByteArrayOutputStream launchProcess(String command, final Map environment, final String workDir, ExecuteResultHandler resultHandler) throws IOException {
    String[] cmdlist = command.split(" ");
    CommandLine cmd = new CommandLine(cmdlist[0]);
    for (String cmdItem : cmdlist) {
        if (!StringUtils.isBlank(cmdItem)) {
            cmd.addArgument(cmdItem);
        }
    }
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    if (!StringUtils.isBlank(workDir)) {
        executor.setWorkingDirectory(new File(workDir));
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(out, out);
    executor.setStreamHandler(streamHandler);
    try {
        if (resultHandler == null) {
            executor.execute(cmd, environment);
        } else {
            executor.execute(cmd, environment, resultHandler);
        }
    } catch (ExecuteException ignored) {
    }
    return out;
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteException(org.apache.commons.exec.ExecuteException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)58 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)49 CommandLine (org.apache.commons.exec.CommandLine)44 IOException (java.io.IOException)32 ByteArrayOutputStream (java.io.ByteArrayOutputStream)29 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)23 ExecuteException (org.apache.commons.exec.ExecuteException)22 File (java.io.File)17 DefaultExecuteResultHandler (org.apache.commons.exec.DefaultExecuteResultHandler)9 Executor (org.apache.commons.exec.Executor)9 InputStream (java.io.InputStream)5 OutputStream (java.io.OutputStream)5 ExecuteStreamHandler (org.apache.commons.exec.ExecuteStreamHandler)5 PipedInputStream (java.io.PipedInputStream)4 PipedOutputStream (java.io.PipedOutputStream)4 HashMap (java.util.HashMap)4 LogOutputStream (org.apache.commons.exec.LogOutputStream)4 FileOutputStream (java.io.FileOutputStream)3 URL (java.net.URL)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3