use of org.apache.commons.exec.PumpStreamHandler in project zeppelin by apache.
the class ShellInterpreter method interpret.
@Override
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
LOGGER.debug("Run shell command '" + cmd + "'");
OutputStream outStream = new ByteArrayOutputStream();
CommandLine cmdLine = CommandLine.parse(shell);
// they need to be delimited by '&&' instead
if (isWindows) {
String[] lines = StringUtils.split(cmd, "\n");
cmd = StringUtils.join(lines, " && ");
}
cmdLine.addArgument(cmd, false);
try {
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler(contextInterpreter.out, contextInterpreter.out));
executor.setWatchdog(new ExecuteWatchdog(Long.valueOf(getProperty(TIMEOUT_PROPERTY))));
executors.put(contextInterpreter.getParagraphId(), executor);
int exitVal = executor.execute(cmdLine);
LOGGER.info("Paragraph " + contextInterpreter.getParagraphId() + " return with exit value: " + exitVal);
return new InterpreterResult(Code.SUCCESS, outStream.toString());
} catch (ExecuteException e) {
int exitValue = e.getExitValue();
LOGGER.error("Can not run " + cmd, e);
Code code = Code.ERROR;
String message = outStream.toString();
if (exitValue == 143) {
code = Code.INCOMPLETE;
message += "Paragraph received a SIGTERM\n";
LOGGER.info("The paragraph " + contextInterpreter.getParagraphId() + " stopped executing: " + message);
}
message += "ExitValue: " + exitValue;
return new InterpreterResult(code, message);
} catch (IOException e) {
LOGGER.error("Can not run " + cmd, e);
return new InterpreterResult(Code.ERROR, e.getMessage());
} finally {
executors.remove(contextInterpreter.getParagraphId());
}
}
use of org.apache.commons.exec.PumpStreamHandler in project robovm by robovm.
the class Executor method initExecutor.
private <T extends org.apache.commons.exec.Executor> T initExecutor(T executor) {
if (streamHandler == null) {
OutputStream pumpOut = null;
OutputStream pumpErr = null;
InputStream pumpIn = null;
if (out != null) {
pumpOut = out;
} else {
pumpOut = new InfoOutputStream(logger);
}
if (err != null) {
pumpErr = err;
} else {
pumpErr = new ErrorOutputStream(logger);
}
if (in != null) {
pumpIn = in;
}
if (pumpOut == System.out) {
pumpOut = new NeverCloseOutputStream(pumpOut);
}
if (pumpErr == System.err) {
pumpErr = new NeverCloseOutputStream(pumpErr);
}
executor.setStreamHandler(new PumpStreamHandler(pumpOut, pumpErr, pumpIn) {
@Override
protected Thread createPump(InputStream is, OutputStream os, boolean closeWhenExhausted) {
return super.createPump(is, os, closeOutputStreams ? true : closeWhenExhausted);
}
});
} else {
executor.setStreamHandler(streamHandler);
}
if (wd != null) {
executor.setWorkingDirectory(wd);
}
executor.setExitValue(0);
return executor;
}
use of org.apache.commons.exec.PumpStreamHandler 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.PumpStreamHandler in project sonarlint-core by SonarSource.
the class CommandExecutor method createStreamHandler.
private ExecuteStreamHandler createStreamHandler() throws IOException {
out = new ByteArrayOutputStream();
err = new ByteArrayOutputStream();
PipedOutputStream outPiped = new PipedOutputStream();
InputStream inPiped = new PipedInputStream(outPiped);
in = outPiped;
TeeOutputStream teeOut = new TeeOutputStream(out, System.out);
TeeOutputStream teeErr = new TeeOutputStream(err, System.err);
return new PumpStreamHandler(teeOut, teeErr, inPiped);
}
use of org.apache.commons.exec.PumpStreamHandler in project tika by apache.
the class PooledTimeSeriesParser method computePoT.
private String computePoT(File input) throws IOException, TikaException {
CommandLine cmdLine = new CommandLine("pooled-time-series");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
cmdLine.addArgument("-f");
cmdLine.addArgument(input.getAbsolutePath());
LOG.trace("Executing: {}", cmdLine);
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValue(0);
ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
exec.setWatchdog(watchdog);
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
exec.setStreamHandler(streamHandler);
int exitValue = exec.execute(cmdLine, EnvironmentUtils.getProcEnvironment());
return outputStream.toString("UTF-8");
}
Aggregations