Example #1
0
    public void run() {
      if (Thread.currentThread() == this) {
        //                    Environment.setThreadEnvironment(launcherEnvironment);
        Job.setUserInterface(userInterface);
      }
      try {
        PrintWriter pw = null;
        if (redirect != null) pw = new PrintWriter(redirect);

        // read from stream
        InputStreamReader input = new InputStreamReader(in);
        BufferedReader reader = new BufferedReader(input);
        int read = 0;
        while ((read = reader.read(buf)) >= 0) {
          if (pw != null) {
            pw.write(buf, 0, read);
            pw.flush();
          }
        }

        reader.close();
        input.close();

      } catch (java.io.IOException e) {
        ActivityLogger.logException(e);
      }
    }
Example #2
0
  public void run() {
    if (Thread.currentThread() == this) {
      Environment.setThreadEnvironment(launcherEnvironment);
      Job.setUserInterface(userInterface);
    }
    if (outStreamRedir instanceof OutputStreamChecker) {
      ((OutputStreamChecker) outStreamRedir).setExec(this);
    }
    if (errStreamRedir instanceof OutputStreamChecker) {
      ((OutputStreamChecker) errStreamRedir).setExec(this);
    }

    try {
      Runtime rt = Runtime.getRuntime();

      ExecProcessReader outReader = null;
      ExecProcessReader errReader = null;

      // run program
      synchronized (this) {
        try {
          if (command != null) p = rt.exec(command, envVars, dir);
          else p = rt.exec(exec, envVars, dir);
        } catch (IOException e) {
          System.out.println("Error running " + command + ": " + e.getMessage());
          return;
        }

        // eat output (stdout) and stderr from program so it doesn't block
        outReader = new ExecProcessReader(p.getInputStream(), outStreamRedir);
        errReader = new ExecProcessReader(p.getErrorStream(), errStreamRedir);
        outReader.start();
        errReader.start();

        // attach to input of process
        processWriter = new PrintWriter(p.getOutputStream());
      }

      // wait for exit status
      exitVal = p.waitFor();

      // also wait for redir threads to die, if doing redir
      if (outStreamRedir != null) outReader.join();
      if (errStreamRedir != null) errReader.join();

      StringBuffer com = new StringBuffer();
      if (command != null) com.append(command);
      else {
        for (int i = 0; i < exec.length; i++) com.append(exec[i] + " ");
      }

      // System.out.println("Process finished [exit: "+exitVal+"]: "+com.toString());
      synchronized (finishedListeners) {
        FinishedEvent e = new FinishedEvent(this, com.toString(), dir, exitVal);
        ArrayList<FinishedListener> copy = new ArrayList<FinishedListener>();
        // make copy cause listeners may want to remove themselves if process finished
        for (FinishedListener l : finishedListeners) {
          copy.add(l);
        }
        for (FinishedListener l : copy) {
          l.processFinished(e);
        }
      }

      synchronized (this) {
        if (processWriter != null) {
          processWriter.close();
          processWriter = null;
        }
      }

    } catch (Exception e) {
      ActivityLogger.logException(e);
    }
  }