org.aspectj.util
Class LangUtil.ProcessController

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

public static class LangUtil.ProcessController
extends java.lang.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.


Nested Class Summary
static class LangUtil.ProcessController.Thrown
           
 
Constructor Summary
LangUtil.ProcessController()
           
 
Method Summary
 boolean completed()
           
protected  void doCompleting(LangUtil.ProcessController.Thrown thrown, int result)
          Subclasses implement this to get synchronous notice of completion.
 java.lang.String[] getCommand()
           
 int getResult()
           
 LangUtil.ProcessController.Thrown getThrown()
          Get any Throwable thrown.
 void init(java.io.File java, java.lang.String classpath, java.lang.String mainClass, java.lang.String[] args)
           
 void init(java.lang.String[] command, java.lang.String label)
           
 void init(java.lang.String classpath, java.lang.String mainClass, java.lang.String[] args)
           
 void reinit()
          Permit re-running using the same command if this is not started or if completed.
 void setEnvp(java.lang.String[] envp)
           
 void setErrSnoop(java.io.ByteArrayOutputStream snoop)
           
 void setOutSnoop(java.io.ByteArrayOutputStream snoop)
           
 java.lang.Thread start()
          Start running the process and pipes asynchronously.
 boolean started()
           
 void stop()
          Destroy any process, stop any pipes.
 boolean userStopped()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LangUtil.ProcessController

public LangUtil.ProcessController()
Method Detail

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(java.lang.String classpath,
                       java.lang.String mainClass,
                       java.lang.String[] args)

init

public final void init(java.io.File java,
                       java.lang.String classpath,
                       java.lang.String mainClass,
                       java.lang.String[] args)

init

public final void init(java.lang.String[] command,
                       java.lang.String label)

setEnvp

public final void setEnvp(java.lang.String[] envp)

setErrSnoop

public final void setErrSnoop(java.io.ByteArrayOutputStream snoop)

setOutSnoop

public final void setOutSnoop(java.io.ByteArrayOutputStream snoop)

start

public final java.lang.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 java.lang.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()