Class LangUtil.ProcessController

java.lang.Object
org.aspectj.util.LangUtil.ProcessController
Enclosing class:
LangUtil

public static class LangUtil.ProcessController extends Object
Handle an external process asynchrously. start() launches a main thread to wait for the process and pipes streams (in child threads) through to the corresponding streams (e.g., the process System.err to this System.err). This can complete normally, by exception, or on demand by a client. Clients can implement doCompleting(..) to get notice when the process completes.

The following sample code creates a process with a completion callback starts it, and some time later retries the process.

 LangUtil.ProcessController controller = new LangUtil.ProcessController() {
        protected void doCompleting(LangUtil.ProcessController.Thrown thrown, int result) {
                // signal result
        }
 };
 controller.init(new String[] { "java", "-version" }, "java version");
 controller.start();
 // some time later...
 // retry...
 if (!controller.completed()) {
        controller.stop();
        controller.reinit();
        controller.start();
 }
 
warning: Currently this does not close the input or output streams, since doing so prevents their use later.
  • Constructor Details

    • ProcessController

      public ProcessController()
  • Method Details

    • reinit

      public final void reinit()
      Permit re-running using the same command if this is not started or if completed. Can also call this when done with results to release references associated with results (e.g., stack traces).
    • init

      public final void init(String classpath, String mainClass, String[] args)
    • init

      public final void init(File java, String classpath, String mainClass, String[] args)
    • init

      public final void init(String[] command, String label)
    • setEnvp

      public final void setEnvp(String[] envp)
    • setErrSnoop

      public final void setErrSnoop(ByteArrayOutputStream snoop)
    • setOutSnoop

      public final void setOutSnoop(ByteArrayOutputStream snoop)
    • start

      public final Thread start()
      Start running the process and pipes asynchronously.
      Returns:
      Thread started or null if unable to start thread (results available via getThrown(), etc.)
    • stop

      public final void stop()
      Destroy any process, stop any pipes. This waits for the pipes to clear (reading until no more input is available), but does not wait for the input stream for the pipe to close (i.e., not waiting for end-of-file on input stream).
    • getCommand

      public final String[] getCommand()
    • completed

      public final boolean completed()
    • started

      public final boolean started()
    • userStopped

      public final boolean userStopped()
    • getThrown

      public final LangUtil.ProcessController.Thrown getThrown()
      Get any Throwable thrown. Note that the process can complete normally (with a valid return value), at the same time the pipes throw exceptions, and that this may return some exceptions even if the process is not complete.
      Returns:
      null if not complete or Thrown containing exceptions thrown by the process and streams.
    • getResult

      public final int getResult()
    • doCompleting

      protected void doCompleting(LangUtil.ProcessController.Thrown thrown, int result)
      Subclasses implement this to get synchronous notice of completion. All pipes and processes should be complete at this time. To get the exceptions thrown for the pipes, use getThrown(). If there is an exception, the process completed abruptly (including side-effects of the user halting the process). If userStopped() is true, then some client asked that the process be destroyed using stop(). Otherwise, the result code should be the result value returned by the process.
      Parameters:
      thrown - same as getThrown().fromProcess.
      result - same as getResult()
      See Also:
      getThrown(), getResult(), stop()