Search in sources :

Example 56 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project tycho by eclipse.

the class JdkLibraryInfoProvider method generateLibraryInfo.

private LibraryInfo generateLibraryInfo(String javaHome) {
    String executable = javaHome + File.separator + "bin" + File.separator + "java";
    if (File.separatorChar == '\\') {
        executable = executable + ".exe";
    }
    if (!new File(executable).isFile()) {
        getLog().warn(executable + " not found. Fallback to scan " + javaHome + "/lib/*.jar and " + javaHome + "/lib/ext/*.jar for bootclasspath");
        return new LibraryInfo("unknown", scanLibFolders(javaHome), new String[0], new String[0]);
    }
    CommandLine cli = new CommandLine(executable);
    cli.addArguments(new String[] { "-classpath", getLibDetectorJar().getAbsolutePath(), "org.eclipse.tycho.libdetector.LibraryDetector" }, false);
    DefaultExecutor executor = new DefaultExecutor();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(30 * 1000L);
    executor.setWatchdog(watchdog);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PumpStreamHandler handler = new PumpStreamHandler(outputStream);
    executor.setStreamHandler(handler);
    int exitValue = -1;
    try {
        exitValue = executor.execute(cli);
    } catch (ExecuteException e) {
        if (watchdog.killedProcess()) {
            throw new RuntimeException("Timeout 30 s exceeded. Commandline " + cli.toString() + " was killed. Output: " + outputStream.toString());
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    if (exitValue == 0) {
        return parseLibraryInfo(outputStream.toString(), javaHome);
    } else {
        throw new RuntimeException(cli.toString() + " process exit code was " + exitValue + ". Output: " + outputStream.toString());
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) LibraryInfo(org.eclipse.tycho.compiler.jdt.copied.LibraryInfo) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ExecuteException(org.apache.commons.exec.ExecuteException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 57 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler 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)

Example 58 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project Robot by fo0.

the class Commander method execute.

public String execute(boolean wait, boolean shell, String homedir, boolean quoting, List<String> cmds) {
    if (cmds == null || cmds.isEmpty()) {
        Logger.info("stopped cmd command is empty");
        return null;
    }
    CommandLine cmdLine = null;
    if (shell) {
        switch(OSCheck.getOperatingSystemType()) {
            case Windows:
                cmdLine = new CommandLine("cmd");
                if (shell) {
                    cmdLine.addArgument("/c");
                }
                break;
            case Linux:
                cmdLine = new CommandLine("/bin/bash");
                if (shell) {
                    cmdLine.addArgument("-c");
                }
                break;
        }
        cmdLine.addArguments(cmds.stream().toArray(String[]::new), quoting);
    } else {
        cmdLine = new CommandLine(cmds.get(0));
        cmdLine.addArguments(cmds.stream().skip(1).toArray(String[]::new), quoting);
    }
    Logger.debug("HomeDir: '" + Paths.get(homedir).toAbsolutePath() + "' => " + cmdLine.getExecutable() + ", " + StringUtils.join(cmdLine.getArguments(), ","));
    try {
        Executor executor = createDefaultExecutor();
        executor.setStreamHandler(new PumpStreamHandler(new OutputStream() {

            @Override
            public void write(int b) throws IOException {
                try {
                    String str = String.valueOf((char) b);
                    if (listener != null)
                        listener.event(str);
                    buffer.append(str);
                } catch (Exception e) {
                    Logger.debug(e.getMessage());
                }
            }
        }, new OutputStream() {

            @Override
            public void write(int b) throws IOException {
                error = true;
                try {
                    String str = String.valueOf((char) b);
                    if (listener != null)
                        listener.event(str);
                    buffer.append(str);
                } catch (Exception e) {
                    Logger.debug(e.getMessage());
                }
            }
        }));
        // configure timeout
        // executor.setWatchdog(new ExecuteWatchdog(-1));
        executor.setWorkingDirectory(new File(homedir));
        if (wait) {
            executor.execute(cmdLine);
        } else {
            DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
            executor.execute(cmdLine, resultHandler);
        }
    } catch (Exception e) {
        error = true;
        Logger.error("failed commander in Cmd: " + cmdLine + " | " + e);
        Logger.debug(e.getMessage());
    }
    return buffer.toString();
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecuteResultHandler(org.apache.commons.exec.DefaultExecuteResultHandler) OutputStream(java.io.OutputStream) File(java.io.File) IOException(java.io.IOException)

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