Package org.aspectj.util
Class LangUtil.ProcessController
java.lang.Object
org.aspectj.util.LangUtil.ProcessController
- Enclosing class:
LangUtil
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
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal boolean
protected void
doCompleting
(LangUtil.ProcessController.Thrown thrown, int result) Subclasses implement this to get synchronous notice of completion.final String[]
final int
Get any Throwable thrown.final void
final void
final void
final void
reinit()
Permit re-running using the same command if this is not started or if completed.final void
final void
setErrSnoop
(ByteArrayOutputStream snoop) final void
setOutSnoop
(ByteArrayOutputStream snoop) final Thread
start()
Start running the process and pipes asynchronously.final boolean
started()
final void
stop()
Destroy any process, stop any pipes.final boolean
-
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
-
init
-
init
-
setEnvp
-
setErrSnoop
-
setOutSnoop
-
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
-
completed
public final boolean completed() -
started
public final boolean started() -
userStopped
public final boolean userStopped() -
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
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, usegetThrown()
. If there is an exception, the process completed abruptly (including side-effects of the user halting the process). IfuserStopped()
is true, then some client asked that the process be destroyed usingstop()
. Otherwise, the result code should be the result value returned by the process.- Parameters:
thrown
- same asgetThrown().fromProcess
.result
- same asgetResult()
- See Also:
-