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;
}
}
}
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);
}
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;
}
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();
}
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;
}
Aggregations