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

项目:sonarlint-cli    文件:CommandExecutor.java   
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);
}
项目:giiwa    文件:Shell.java   
/**
 * run a command with the out, err and in.
 *
 * @param cmd
 *            the command line
 * @param out
 *            the console outputstream
 * @param err
 *            the error outputstream
 * @param in
 *            the inputstream
 * @param workdir
 *            the working dir
 * @return the result
 * @throws IOException
 *             throw IOException if error
 */
public static int run(String cmd, OutputStream out, OutputStream err, InputStream in, String workdir)
        throws IOException {

    try {

        CommandLine cmdLine = CommandLine.parse(cmd);

        ExecuteStreamHandler stream = new PumpStreamHandler(out, err, in);
        int[] exit = new int[512];
        for (int i = 0; i < exit.length; i++) {
            exit[i] = i - 256;
        }
        executor.setExitValues(exit);

        executor.setStreamHandler(stream);
        if (!X.isEmpty(workdir)) {
            executor.setWorkingDirectory(new File(workdir));
        }

        // ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
        // executor.setWatchdog(watchdog);
        // watchdog.destroyProcess();

        return executor.execute(cmdLine);
    } catch (IOException e) {
        log.error("cmd=" + cmd, e);
        throw e;
    }
}
项目:giiwa    文件:Shell.java   
/**
 * using bash to execute the lines.
 *
 * @param lines
 *            the command lines
 * @param out
 *            the output stream
 * @param err
 *            the error stream
 * @param in
 *            the input stream
 * @param workdir
 *            the workdir
 * @return the int
 * @throws IOException
 *             throw IOException if error
 */
public static int bash(String lines, OutputStream out, OutputStream err, InputStream in, String workdir)
        throws IOException {

    File f = new File(UID.id(lines).toLowerCase() + ".bash");

    try {
        if (!X.isEmpty(workdir)) {
            lines = "cd " + workdir + ";" + lines;
        }
        FileUtils.writeStringToFile(f, lines);

        // Execute the file we just creted. No flags are due if it is
        // executed with bash directly
        CommandLine commandLine = CommandLine.parse("bash " + f.getCanonicalPath());

        ExecuteStreamHandler stream = new PumpStreamHandler(out, err, in);

        DefaultExecutor executor = new DefaultExecutor();
        int[] exit = new int[3];
        for (int i = 0; i < exit.length; i++) {
            exit[i] = i - 1;
        }
        executor.setExitValues(exit);

        executor.setStreamHandler(stream);
        if (!X.isEmpty(workdir)) {
            executor.setWorkingDirectory(new File(workdir));
        }

        return executor.execute(commandLine);

    } catch (IOException e) {
        log.error("lines=" + lines, e);
        throw e;
    } finally {
        f.delete();
    }
}
项目: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());
}
项目:varnishtest-exec    文件:VarnishtestExecutor.java   
/**
 * The stream handler is transmitted in the constructor.
 * @throws UnsupportedOperationException
 */
@Override
public void setStreamHandler(ExecuteStreamHandler streamHandler) {
    throw new UnsupportedOperationException();
}
项目:oxCore    文件:ProcessHelper.java   
/**
 * 
 * @param printJobTimeout
 *            the printJobTimeout (ms) before the watchdog terminates the
 *            print process
 * @param printInBackground
 *            printing done in the background or blocking
 * @param streamHandler
 * @return a print result handler (implementing a future)
 * @throws IOException
 *             the test failed
 */
public static PrintResultHandler executeProgram(CommandLine commandLine, long printJobTimeout, boolean printInBackground,
        int successExitValue, ExecuteStreamHandler streamHandler) throws IOException {
    return executeProgram(commandLine, null, printJobTimeout, printInBackground, successExitValue, streamHandler);
}