Search in sources :

Example 1 with PumpStreamHandler

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());
    }
}
Also used : DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Code(org.apache.zeppelin.interpreter.InterpreterResult.Code) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 2 with PumpStreamHandler

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;
}
Also used : PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ErrorOutputStream(org.robovm.compiler.log.ErrorOutputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NeverCloseOutputStream(org.robovm.compiler.util.io.NeverCloseOutputStream) InfoOutputStream(org.robovm.compiler.log.InfoOutputStream) ErrorOutputStream(org.robovm.compiler.log.ErrorOutputStream) NeverCloseOutputStream(org.robovm.compiler.util.io.NeverCloseOutputStream) InfoOutputStream(org.robovm.compiler.log.InfoOutputStream)

Example 3 with PumpStreamHandler

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;
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteStreamHandler(org.apache.commons.exec.ExecuteStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteException(org.apache.commons.exec.ExecuteException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with PumpStreamHandler

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);
}
Also used : TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) PipedInputStream(java.io.PipedInputStream) InputStream(java.io.InputStream) PipedOutputStream(java.io.PipedOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PipedInputStream(java.io.PipedInputStream)

Example 5 with PumpStreamHandler

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");
}
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) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

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