use of org.apache.commons.exec.DefaultExecutor in project sakuli by ConSol.
the class CommandLineUtil method runCommand.
public static CommandLineResult runCommand(String command, boolean throwException) throws SakuliCheckedException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream error = new ByteArrayOutputStream();
CommandLineResult result = new CommandLineResult();
try {
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler(outputStream, error));
int exitCode = executor.execute(CommandLine.parse(command));
result.setExitCode(exitCode);
result.setOutput(error.toString() + outputStream.toString());
} catch (Exception e) {
if (throwException) {
throw new SakuliCheckedException(e, String.format("Error during execution of command '%s': %s", command, error.toString()));
}
result.setExitCode(resolveExitCode(e.getMessage()));
result.setOutput(e.getMessage());
}
return result;
}
use of org.apache.commons.exec.DefaultExecutor in project feign by OpenFeign.
the class WikipediaExampleIT method runMain.
@Test
public void runMain() throws Exception {
final String jar = Arrays.stream(new File("target").listFiles()).filter(file -> file.getName().startsWith("feign-example-wikipedia") && file.getName().endsWith(".jar")).findFirst().map(File::getAbsolutePath).get();
final String line = "java -jar " + jar;
final CommandLine cmdLine = CommandLine.parse(line);
final int exitValue = new DefaultExecutor().execute(cmdLine);
assertThat(exitValue, CoreMatchers.equalTo(0));
}
use of org.apache.commons.exec.DefaultExecutor in project feign by OpenFeign.
the class GitHubExampleIT method runMain.
@Test
public void runMain() throws Exception {
final String jar = Arrays.stream(new File("target").listFiles()).filter(file -> file.getName().startsWith("feign-example-github") && file.getName().endsWith(".jar")).findFirst().map(File::getAbsolutePath).get();
final String line = "java -jar " + jar;
final CommandLine cmdLine = CommandLine.parse(line);
final int exitValue = new DefaultExecutor().execute(cmdLine);
assertThat(exitValue, CoreMatchers.equalTo(0));
}
use of org.apache.commons.exec.DefaultExecutor in project ddf by codice.
the class VideoThumbnailPlugin method executeFFmpeg.
private DefaultExecuteResultHandler executeFFmpeg(final CommandLine command, final int timeoutSeconds, final PumpStreamHandler streamHandler) throws IOException {
final ExecuteWatchdog watchdog = new ExecuteWatchdog(timeoutSeconds * 1000L);
final Executor executor = new DefaultExecutor();
final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
if (streamHandler != null) {
executor.setStreamHandler(streamHandler);
}
executor.setWatchdog(watchdog);
executeWithPrivilege(command, executor, resultHandler);
return resultHandler;
}
use of org.apache.commons.exec.DefaultExecutor in project hive by apache.
the class ExecServiceImpl method auxRun.
private ExecBean auxRun(String program, List<String> args, Map<String, String> env) throws NotAuthorizedException, ExecuteException, IOException {
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValues(null);
// Setup stdout and stderr
int nbytes = appConf.getInt(AppConfig.EXEC_MAX_BYTES_NAME, -1);
ByteArrayOutputStream outStream = new MaxByteArrayOutputStream(nbytes);
ByteArrayOutputStream errStream = new MaxByteArrayOutputStream(nbytes);
executor.setStreamHandler(new PumpStreamHandler(outStream, errStream));
// Only run for N milliseconds
int timeout = appConf.getInt(AppConfig.EXEC_TIMEOUT_NAME, 0);
ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
executor.setWatchdog(watchdog);
CommandLine cmd = makeCommandLine(program, args);
LOG.info("Running: " + cmd);
ExecBean res = new ExecBean();
res.exitcode = executor.execute(cmd, execEnv(env));
String enc = appConf.get(AppConfig.EXEC_ENCODING_NAME);
res.stdout = outStream.toString(enc);
res.stderr = errStream.toString(enc);
try {
watchdog.checkException();
} catch (Exception ex) {
LOG.error("Command: " + cmd + " failed. res=" + res, ex);
}
if (watchdog.killedProcess()) {
String msg = " was terminated due to timeout(" + timeout + "ms). See " + AppConfig.EXEC_TIMEOUT_NAME + " property";
LOG.warn("Command: " + cmd + msg + " res=" + res);
res.stderr += " Command " + msg;
}
if (res.exitcode != 0) {
LOG.info("Command: " + cmd + " failed. res=" + res);
}
return res;
}
Aggregations