Java 类org.apache.commons.exec.ExecuteResultHandler 实例源码

项目:project-build-plugin    文件:EngineControl.java   
private ExecuteResultHandler asynchExecutionHandler()
{
  return new ExecuteResultHandler()
    {
      @Override
      public void onProcessFailed(ExecuteException ex)
      {
        throw new RuntimeException("Engine operation failed.", ex);
      }

      @Override
      public void onProcessComplete(int exitValue)
      {
        context.log.info("Engine process stopped.");
      }
    };
}
项目:device    文件:ShellCommand.java   
@SuppressWarnings("rawtypes")
public static void execAsync(String display, CommandLine commandline)
        throws ShellCommandException {
    log.debug("executing async command: " + commandline);
    DefaultExecutor exec = new DefaultExecutor();

    ExecuteResultHandler handler = new DefaultExecuteResultHandler();
    PumpStreamHandler streamHandler = new PumpStreamHandler(
            new PritingLogOutputStream());
    exec.setStreamHandler(streamHandler);
    try {
        if (display == null || display.isEmpty()) {
            exec.execute(commandline, handler);
        } else {
            Map env = EnvironmentUtils.getProcEnvironment();
            EnvironmentUtils.addVariableToEnvironment(env, "DISPLAY=:"
                    + display);

            exec.execute(commandline, env, handler);
        }
    } catch (Exception e) {
        throw new ShellCommandException(
                "An error occured while executing shell command: "
                        + commandline, e);
    }
}
项目:contrail-java-api    文件:ApiTestCommon.java   
public static void launchContrailServer(int port) throws Exception {
    try {
        DefaultExecutor exec = new DefaultExecutor();
        int exitValues[] = {1};
        exec.setExitValues(exitValues);

        String workingDir = System.getProperty("user.dir");
        String path = workingDir + "/../../config/api-server/tests/";
        File f = new File(path);
        exec.setWorkingDirectory(f);
        exec.setStreamHandler(new PumpStreamHandler(new ByteArrayOutputStream()));
        CommandLine cmd = buildServerLaunchCmd(port);
        ExecuteResultHandler handler = null;
        exec.execute(cmd, handler);
        /* sleep 5 seconds for server to get started */
        Thread.sleep(5000);
    } catch (Exception e) {
        s_logger.debug(e);
        String cause = e.getMessage();
        if (cause.equals("python: not found"))
            System.out.println("No python interpreter found.");
        throw e; 
    }
}
项目:ABRAID-MP    文件:CommonsExecProcessRunnerTest.java   
@Test
public void runCallsExecuteWithCorrectCommand() throws Exception {
    // Arrange
    Executor mockExecutor = mock(Executor.class);
    File executable = testFolder.newFile("file1");
    String[] executionArguments = new String[]{"arg1", "arg2", "${arg3}"};
    File script = testFolder.newFile("file2");
    HashMap<String, File> fileArguments = new HashMap<>();
    fileArguments.put("arg3", script);
    ProcessRunner target = new CommonsExecProcessRunner(mockExecutor, testFolder.getRoot(), executable,
            executionArguments, fileArguments, 10);
    String expectation = "[" + executable + ", arg1, arg2, " + script + "]";

    // Act
    target.run(mock(ProcessHandler.class));

    // Assert
    ArgumentCaptor<CommandLine> commandLineCaptor = captorForClass(CommandLine.class);
    verify(mockExecutor).execute(commandLineCaptor.capture(), any(ExecuteResultHandler.class));
    assertThat(commandLineCaptor.getValue().toString()).isEqualTo(expectation);
}
项目:ABRAID-MP    文件:CommonsExecProcessRunnerTest.java   
@Test
public void runWrapsInnerExceptionInErrorCase() throws Exception {
    // Arrange
    Executor mockExecutor = mock(Executor.class);
    ExecuteException expectedCause = new ExecuteException("foo", -1);
    doThrow(expectedCause).when(mockExecutor).execute(any(CommandLine.class), any(ExecuteResultHandler.class));

    ProcessRunner target = new CommonsExecProcessRunner(mockExecutor, testFolder.getRoot(), new File("exe"),
            new String[0], new HashMap<String, File>(), 10);

    // Act
    catchException(target).run(mock(ProcessHandler.class));
    Exception result = caughtException();

    // Assert
    assertThat(result).isInstanceOf(ProcessException.class);
    assertThat(result.getCause()).isEqualTo(expectedCause);
}
项目:testgrid    文件:CommonUtils.java   
public static ExecuteWatchdog execASync(String line, File workDir, Map<String, String> environment, OutputStream output, ExecuteResultHandler erh,
        long timeout) throws IOException {
    if (environment != null) {
        Map<String, String> sysenv = System.getenv();
        for (String key : sysenv.keySet()) {
            boolean contains = false;
            for (String k : environment.keySet()) {
                if (k.equalsIgnoreCase(key)) {
                    contains = true;
                    break;
                }
            }
            if (!contains)
                environment.put(key, sysenv.get(key));
        }
    }
    DefaultExecutor executor = new DefaultExecutor();
    if (workDir != null)
        executor.setWorkingDirectory(workDir);
    PumpStreamHandler sh = new PumpStreamHandler(output, output, null);
    executor.setStreamHandler(sh);
    ExecuteWatchdog watchdog = new ProcessTreeWatchDog(timeout);
    executor.setWatchdog(watchdog);
    executor.execute(CommandLine.parse(line), environment, erh);
    return watchdog;
}
项目:testgrid    文件:CommonUtils.java   
public static ExecuteWatchdog execASync(String line, File workDir, Map<String, String> environment, OutputStream output, ExecuteResultHandler erh,
        long timeout, Logger log) throws IOException {
    if (environment != null) {
        Map<String, String> sysenv = System.getenv();
        for (String key : sysenv.keySet()) {
            boolean contains = false;
            for (String k : environment.keySet()) {
                if (k.equalsIgnoreCase(key)) {
                    contains = true;
                    break;
                }
            }
            if (!contains)
                environment.put(key, sysenv.get(key));
        }
    }
    DefaultExecutor executor = new DefaultExecutor();
    if (workDir != null)
        executor.setWorkingDirectory(workDir);
    PumpStreamHandler sh = new PumpStreamHandler(output, output, null);
    executor.setStreamHandler(sh);
    ExecuteWatchdog watchdog = new ProcessTreeWatchDog(timeout, log);
    executor.setWatchdog(watchdog);
    executor.execute(CommandLine.parse(line), environment, erh);
    return watchdog;
}
项目:dohko    文件:CommandExample.java   
public static void main(String[] args) throws ExecuteException, IOException, InterruptedException
    {
        CommandLine command = new CommandLine("/bin/bash");
        command.addArgument("-c", false);
        command.addArgument("iperf3 -t 30 -c iperf.scottlinux.com >> output.txt", false);

        //Process process = Runtime.getRuntime().exec(new String[]{"bash", "-c", "iperf3 -t 60 -c localhost"});
       // System.out.println(new Mirror().on(process).get().field("pid"));

        //process.waitFor();

//        System.out.println(process.exitValue());
//        ManagementFactory.getRuntimeMXBean().getName();  
//        System.out.println(IOUtils.readLines(process.getInputStream()));

        //String command = "iperf3 -t 30 -c iperf.scottlinux.com";

        ExecuteWatchdog watchdog = new ExecuteWatchdog(10);

        final DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(new PumpStreamHandler());
        executor.setExitValue(1);

        executor.execute(command, new ExecuteResultHandler()
        {
            @Override
            public void onProcessFailed(ExecuteException e)
            {
                e.printStackTrace();
            }

            @Override
            public void onProcessComplete(int exitValue)
            {
                System.out.println(exitValue);
            }
        });
    }
项目:dohko    文件:CmsearchCommand.java   
@Override
public void execute(CommandLine command) throws ShellException, IOException
{
    // ProcessBuilder pb = new ProcessBuilder("bash", "c", instruction);
    // Process start = pb.start();
    //
    // try
    // {
    // int exitValue = start.waitFor();
    // }
    // catch (InterruptedException exception)
    // {
    // throw new ShellException();
    // }

    final DefaultExecutor executor = new DefaultExecutor();
    executor.execute(command, new ExecuteResultHandler()
    {
        @Override
        public void onProcessFailed(ExecuteException e)
        {
        }

        @Override
        public void onProcessComplete(int exitValue)
        {
        }
    });
}
项目:dohko    文件:Worker.java   
public void execute(final Application application, ExecuteResultHandler executeResultHandler, ExecuteStreamHandler streamHandler) throws ExecuteException, IOException
{
    final String commandLine = application.getCommandLine();

    DefaultExecutor executor = new DefaultExecutor();

    CommandLine command = new CommandLine("/bin/sh");
    command.addArgument("-c", false);
    command.addArgument(commandLine, false);
    executor.setStreamHandler(streamHandler);
    executor.execute(command, System.getenv(), executeResultHandler);

    LOG.debug("Launched the execution of task: [{}], uuid: [{}]", commandLine, application.getId());
}
项目:jstorm    文件:JStormUtils.java   
/**
 * If it is backend, please set resultHandler, such as DefaultExecuteResultHandler
 * If it is frontend, ByteArrayOutputStream.toString will return the calling result
 * <p>
 * This function will ignore whether the command is successfully executed or not
 *
 * @param command       command to be executed
 * @param environment   env vars
 * @param workDir       working directory
 * @param resultHandler exec result handler
 * @return output stream
 * @throws IOException
 */
@Deprecated
public static ByteArrayOutputStream launchProcess(
        String command, final Map environment, final String workDir,
        ExecuteResultHandler resultHandler) throws IOException {

    String[] cmdlist = command.split(" ");

    CommandLine cmd = new CommandLine(cmdlist[0]);
    for (String cmdItem : cmdlist) {
        if (!StringUtils.isBlank(cmdItem)) {
            cmd.addArgument(cmdItem);
        }
    }

    DefaultExecutor executor = new DefaultExecutor();

    executor.setExitValue(0);
    if (!StringUtils.isBlank(workDir)) {
        executor.setWorkingDirectory(new File(workDir));
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    PumpStreamHandler streamHandler = new PumpStreamHandler(out, out);
    executor.setStreamHandler(streamHandler);

    try {
        if (resultHandler == null) {
            executor.execute(cmd, environment);
        } else {
            executor.execute(cmd, environment, resultHandler);
        }
    } catch (ExecuteException ignored) {
    }

    return out;

}
项目:jstorm-0.9.6.3-    文件:JStormUtils.java   
/**
 * If it is backend, please set resultHandler, such as DefaultExecuteResultHandler
 * If it is frontend, ByteArrayOutputStream.toString get the result
 * 
 * This function don't care whether the command is successfully or not
 * 
 * @param command
 * @param environment
 * @param workDir
 * @param resultHandler
 * @return
 * @throws IOException
 */
public static ByteArrayOutputStream launchProcess(String command, final Map environment,
        final String workDir, ExecuteResultHandler resultHandler)
        throws IOException {

    String[] cmdlist = command.split(" ");

    CommandLine cmd = new CommandLine(cmdlist[0]);
    for (String cmdItem : cmdlist) {
        if (StringUtils.isBlank(cmdItem) == false) {
            cmd.addArgument(cmdItem);
        }
    }

    DefaultExecutor executor = new DefaultExecutor();

    executor.setExitValue(0);
    if (StringUtils.isBlank(workDir) == false) {
        executor.setWorkingDirectory(new File(workDir));
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    PumpStreamHandler streamHandler = new PumpStreamHandler(out, out);
    if (streamHandler != null) {
        executor.setStreamHandler(streamHandler);
    }

    try {
        if (resultHandler == null) {
            executor.execute(cmd, environment);
        } else {
            executor.execute(cmd, environment, resultHandler);
        }
    }catch(ExecuteException e) {

        // @@@@ 
        // failed to run command
    }

    return out;

}
项目:learn_jstorm    文件:JStormUtils.java   
/**
 * If it is backend, please set resultHandler, such as DefaultExecuteResultHandler
 * If it is frontend, ByteArrayOutputStream.toString get the result
 * 
 * This function don't care whether the command is successfully or not
 * 
 * @param command
 * @param environment
 * @param workDir
 * @param resultHandler
 * @return
 * @throws IOException
 */
public static ByteArrayOutputStream launchProcess(String command, final Map environment,
        final String workDir, ExecuteResultHandler resultHandler)
        throws IOException {

    String[] cmdlist = command.split(" ");

    CommandLine cmd = new CommandLine(cmdlist[0]);
    for (String cmdItem : cmdlist) {
        if (StringUtils.isBlank(cmdItem) == false) {
            cmd.addArgument(cmdItem);
        }
    }

    DefaultExecutor executor = new DefaultExecutor();

    executor.setExitValue(0);
    if (StringUtils.isBlank(workDir) == false) {
        executor.setWorkingDirectory(new File(workDir));
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    PumpStreamHandler streamHandler = new PumpStreamHandler(out, out);
    if (streamHandler != null) {
        executor.setStreamHandler(streamHandler);
    }

    try {
        if (resultHandler == null) {
            executor.execute(cmd, environment);
        } else {
            executor.execute(cmd, environment, resultHandler);
        }
    }catch(ExecuteException e) {

        // @@@@ 
        // failed to run command
    }

    return out;

}
项目:jstrom    文件:JStormUtils.java   
/**
 * If it is backend, please set resultHandler, such as DefaultExecuteResultHandler If it is frontend, ByteArrayOutputStream.toString get the result
 * <p/>
 * This function don't care whether the command is successfully or not
 * 
 * @param command
 * @param environment
 * @param workDir
 * @param resultHandler
 * @return
 * @throws IOException
 */
public static ByteArrayOutputStream launchProcess(String command, final Map environment, final String workDir, ExecuteResultHandler resultHandler)
        throws IOException {

    String[] cmdlist = command.split(" ");

    CommandLine cmd = new CommandLine(cmdlist[0]);
    for (String cmdItem : cmdlist) {
        if (StringUtils.isBlank(cmdItem) == false) {
            cmd.addArgument(cmdItem);
        }
    }

    DefaultExecutor executor = new DefaultExecutor();

    executor.setExitValue(0);
    if (StringUtils.isBlank(workDir) == false) {
        executor.setWorkingDirectory(new File(workDir));
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    PumpStreamHandler streamHandler = new PumpStreamHandler(out, out);
    if (streamHandler != null) {
        executor.setStreamHandler(streamHandler);
    }

    try {
        if (resultHandler == null) {
            executor.execute(cmd, environment);
        } else {
            executor.execute(cmd, environment, resultHandler);
        }
    } catch (ExecuteException e) {

        // @@@@
        // failed to run command
    }

    return out;

}
项目:Tstream    文件:JStormUtils.java   
/**
 * If it is backend, please set resultHandler, such as DefaultExecuteResultHandler
 * If it is frontend, ByteArrayOutputStream.toString get the result
 * 
 * This function don't care whether the command is successfully or not
 * 
 * @param command
 * @param environment
 * @param workDir
 * @param resultHandler
 * @return
 * @throws IOException
 */
public static ByteArrayOutputStream launchProcess(String command, final Map environment,
        final String workDir, ExecuteResultHandler resultHandler)
        throws IOException {

    String[] cmdlist = command.split(" ");

    CommandLine cmd = new CommandLine(cmdlist[0]);
    for (String cmdItem : cmdlist) {
        if (StringUtils.isBlank(cmdItem) == false) {
            cmd.addArgument(cmdItem);
        }
    }

    DefaultExecutor executor = new DefaultExecutor();

    executor.setExitValue(0);
    if (StringUtils.isBlank(workDir) == false) {
        executor.setWorkingDirectory(new File(workDir));
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    PumpStreamHandler streamHandler = new PumpStreamHandler(out, out);
    if (streamHandler != null) {
        executor.setStreamHandler(streamHandler);
    }

    try {
        if (resultHandler == null) {
            executor.execute(cmd, environment);
        } else {
            executor.execute(cmd, environment, resultHandler);
        }
    }catch(ExecuteException e) {

        // @@@@ 
        // failed to run command
    }

    return out;

}
项目:testgrid    文件:CommonUtils.java   
public static ExecuteWatchdog execASync(String line, Map<String, String> environment, OutputStream output, ExecuteResultHandler erh, long timeout)
        throws IOException {
    return execASync(line, null, environment, output, erh, timeout);
}