Ejemplo n.º 1
0
 /**
  * Copies a {@link ProcStarter}.
  */
 public ProcStarter copy() {
     ProcStarter rhs = new ProcStarter().cmds(commands).pwd(pwd).masks(masks).stdin(stdin).stdout(stdout).stderr(stderr).envs(envs);
     rhs.reverseStdin  = this.reverseStdin;
     rhs.reverseStderr = this.reverseStderr;
     rhs.reverseStdout = this.reverseStdout;
     return rhs;
 }
 private String buildCommandLine(ProcStarter ps) {
   StringBuilder b = new StringBuilder();
   for (String cmd : ps.cmds()) {
     if (b.length() > 0) b.append(' ');
     if (cmd.indexOf(' ') >= 0) b.append('"').append(cmd).append('"');
     else b.append(cmd);
   }
   return b.toString();
 }
  public Proc launch(ProcStarter ps) throws IOException {
    maskedPrintCommandLine(ps.cmds(), ps.masks(), ps.pwd());

    // TODO: environment variable handling

    String name = ps.cmds().toString();

    final Process proc;
    try {
      proc = launcher.launch(buildCommandLine(ps), ps.pwd().getRemote());
    } catch (JIException e) {
      throw new IOException(e);
    } catch (InterruptedException e) {
      throw new IOException(e);
    }
    final Thread t1 =
        new StreamCopyThread("stdout copier: " + name, proc.getInputStream(), ps.stdout(), false);
    t1.start();
    final Thread t2 =
        new StreamCopyThread("stdin copier: " + name, ps.stdin(), proc.getOutputStream(), true);
    t2.start();

    return new Proc() {
      public boolean isAlive() throws IOException, InterruptedException {
        try {
          proc.exitValue();
          return false;
        } catch (IllegalThreadStateException e) {
          return true;
        }
      }

      public void kill() throws IOException, InterruptedException {
        t1.interrupt();
        t2.interrupt();
        proc.destroy();
      }

      public int join() throws IOException, InterruptedException {
        try {
          t1.join();
          t2.join();
          return proc.waitFor();
        } finally {
          proc.destroy();
        }
      }

      @Override
      public InputStream getStdout() {
        throw new UnsupportedOperationException();
      }

      @Override
      public InputStream getStderr() {
        throw new UnsupportedOperationException();
      }

      @Override
      public OutputStream getStdin() {
        throw new UnsupportedOperationException();
      }
    };
  }