use of org.apache.commons.exec.ExecuteStreamHandler in project robovm by robovm.
the class Executor method execCapture.
public String execCapture() throws IOException {
ExecuteStreamHandler oldStreamHandler = streamHandler;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CommandLine commandLine = generateCommandLine();
try {
streamHandler(new PumpStreamHandler(baos));
logCommandLine(commandLine);
DefaultExecutor executor = initExecutor(new DefaultExecutor());
executor.execute(commandLine, generateEnv());
return new String(baos.toByteArray()).trim();
} catch (ExecuteException e) {
String output = new String(baos.toByteArray()).trim();
if (output.length() > 0 && e.getMessage().startsWith("Process exited with an error")) {
StackTraceElement[] origStackTrace = e.getStackTrace();
e = new ExecuteException("Command '" + commandLine + "' failed with output: " + output + " ", e.getExitValue());
e.setStackTrace(origStackTrace);
}
throw e;
} finally {
streamHandler = oldStreamHandler;
}
}
use of org.apache.commons.exec.ExecuteStreamHandler in project oxCore by GluuFederation.
the class ProcessHelper method executeProgram.
public static boolean executeProgram(CommandLine commandLine, String workingDirectory, boolean executeInBackground, int successExitValue, OutputStream outputStream) {
long printJobTimeout = PRINT_JOB_TIMEOUT;
ExecuteStreamHandler streamHandler = null;
if (outputStream != null) {
streamHandler = new PumpStreamHandler(outputStream);
}
PrintResultHandler printResult = null;
try {
LOG.debug(String.format("Preparing to start process %s", commandLine.toString()));
printResult = executeProgram(commandLine, workingDirectory, printJobTimeout, executeInBackground, successExitValue, streamHandler);
LOG.debug(String.format("Successfully start process %s", commandLine.toString()));
} catch (Exception ex) {
LOG.trace(String.format("Problem during starting process %s", commandLine.toString()), ex);
ex.printStackTrace();
return false;
}
// come back to check the print result
LOG.debug(String.format("Waiting for the proces %s finish", commandLine.toString()));
try {
if (printResult == null) {
return false;
}
printResult.waitFor();
} catch (InterruptedException ex) {
LOG.error(String.format("Problem during process execution %s", commandLine.toString()), ex);
}
LOG.debug(String.format("Process %s has finished", commandLine.toString()));
return true;
}
use of org.apache.commons.exec.ExecuteStreamHandler 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.ExecuteStreamHandler in project fmv by f-agu.
the class FMVExecutor method executeAsynchronous.
/**
* @param executeDelegate
* @param command
* @param executorService
* @param before
* @param after
* @param ioExceptionConsumer
* @return
*/
public FMVFuture<Integer> executeAsynchronous(ExecuteDelegate executeDelegate, CommandLine command, ExecutorService executorService, Runnable before, IntConsumer after, IOExceptionConsumer ioExceptionConsumer) {
ExecuteStreamHandler streamHandler = getStreamHandler();
WritablePumpStreamHandler wpsh = streamHandler instanceof WritablePumpStreamHandler ? (WritablePumpStreamHandler) streamHandler : null;
return new FMVFuture<>(executorService.submit(() -> {
if (before != null) {
before.run();
}
try {
int exitValue = executeDelegate.execute(this, command);
if (after != null) {
after.accept(exitValue);
}
return exitValue;
} catch (IOException e) {
if (ioExceptionConsumer == null) {
throw e;
}
ioExceptionConsumer.accept(e);
if (e instanceof ExecuteException) {
return ((ExecuteException) e).getExitValue();
}
// TODO
return -1;
}
}), wpsh.getProcessInputStream());
}
use of org.apache.commons.exec.ExecuteStreamHandler in project frontend-maven-plugin by eirslett.
the class ProcessExecutor method execute.
private int execute(final Logger logger, final OutputStream stdout, final OutputStream stderr) throws ProcessExecutionException {
logger.debug("Executing command line {}", commandLine);
try {
ExecuteStreamHandler streamHandler = new PumpStreamHandler(stdout, stderr);
executor.setStreamHandler(streamHandler);
int exitValue = executor.execute(commandLine, environment);
logger.debug("Exit value {}", exitValue);
return exitValue;
} catch (ExecuteException e) {
if (executor.getWatchdog() != null && executor.getWatchdog().killedProcess()) {
throw new ProcessExecutionException("Process killed after timeout");
}
throw new ProcessExecutionException(e);
} catch (IOException e) {
throw new ProcessExecutionException(e);
}
}
Aggregations