Search in sources :

Example 1 with ExecuteStreamHandler

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;
    }
}
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 2 with ExecuteStreamHandler

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

Example 3 with ExecuteStreamHandler

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();
}
Also used : PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteStreamHandler(org.apache.commons.exec.ExecuteStreamHandler)

Example 4 with ExecuteStreamHandler

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

Example 5 with ExecuteStreamHandler

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

Aggregations

ExecuteStreamHandler (org.apache.commons.exec.ExecuteStreamHandler)5 ExecuteException (org.apache.commons.exec.ExecuteException)4 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)4 IOException (java.io.IOException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 CommandLine (org.apache.commons.exec.CommandLine)1 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)1