Search in sources :

Example 26 with Executor

use of org.apache.commons.exec.Executor in project zeppelin by apache.

the class MongoDbInterpreter method stopProcess.

private void stopProcess(String paragraphId) {
    if (runningProcesses.containsKey(paragraphId)) {
        final Executor executor = runningProcesses.get(paragraphId);
        final ExecuteWatchdog watchdog = executor.getWatchdog();
        watchdog.destroyProcess();
        runningProcesses.remove(paragraphId);
    }
}
Also used : Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog)

Example 27 with Executor

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

Executor (org.apache.commons.exec.Executor)27 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)16 File (java.io.File)12 CommandLine (org.apache.commons.exec.CommandLine)10 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)10 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)10 Test (org.junit.Test)10 IOException (java.io.IOException)8 BaseEngineProjectMojoTest (ch.ivyteam.ivy.maven.BaseEngineProjectMojoTest)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 DefaultExecuteResultHandler (org.apache.commons.exec.DefaultExecuteResultHandler)6 ExecuteException (org.apache.commons.exec.ExecuteException)6 ShutdownHookProcessDestroyer (org.apache.commons.exec.ShutdownHookProcessDestroyer)6 StartTestEngineMojo (ch.ivyteam.ivy.maven.test.StartTestEngineMojo)2 OutputStream (java.io.OutputStream)2 PrintStream (java.io.PrintStream)2 ExecuteResultHandler (org.apache.commons.exec.ExecuteResultHandler)2 StopWatch (org.apache.commons.lang3.time.StopWatch)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.linesOf (org.assertj.core.api.Assertions.linesOf)2