Package org.apache.uima.pear.util
Class ProcessUtil.Runner
- java.lang.Object
-
- org.apache.uima.pear.util.ProcessUtil.Runner
-
- All Implemented Interfaces:
java.lang.Runnable
- Enclosing class:
- ProcessUtil
public static class ProcessUtil.Runner extends java.lang.Object implements java.lang.Runnable
TheProcessUtil.Runner
class allows collecting, printing and distributing console output (stdout and stderr) of the specifiedProcess
without blocking the caller. It allows adding standard stream listeners to receive messages printed to the console by selected process.
-
-
Field Summary
Fields Modifier and Type Field Description private static int
__processNumber
private java.lang.Thread
_errThread
private int
_exitCode
private java.util.List<ProcessUtil.StdStreamListener>
_listeners
private java.lang.Thread
_outThread
private java.lang.String
_pName
private boolean
_printToConsole
private java.lang.Process
_process
private java.io.StringWriter
_stdErr
private java.io.StringWriter
_stdOut
private static java.lang.String
DEF_PROC_NAME
-
Constructor Summary
Constructors Constructor Description Runner(java.lang.Process aProcess)
Constructor that takes a givenProcess
object and assigns default process name.Runner(java.lang.Process aProcess, java.lang.String procName)
Constructor that takes a givenProcess
object and a given process name.Runner(java.lang.Process aProcess, java.lang.String procName, boolean printToConsole)
Constructor that takes a givenProcess
object, a given process name, and a givenboolean
flag that enables/disables console printing.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addStreamListener(ProcessUtil.StdStreamListener listener)
Adds a given object, implementing theStdStreamListener
interface to the list of standard stream listeners.java.lang.String
getErrOutput()
int
getExitCode()
java.lang.Process
getProcess()
java.lang.String
getStdOutput()
void
removeListener(ProcessUtil.StdStreamListener listener)
Removes a givenStdStreamListener
object from the list of standard stream listeners.void
run()
Implements theRunnable.run()
method, collecting and printing standard output or standard error messages during the process execution.private void
setExitCode(int exitCode)
Sets a given process exit code.private void
start()
Starts 2 threads for collecting and printing console messages during the process execution.int
waitFor()
Allows the caller to wait for the completion of the process.int
waitFor(long timeout)
Allows the caller to wait for the completion of the process, but no longer than a given timeout value.
-
-
-
Field Detail
-
DEF_PROC_NAME
private static final java.lang.String DEF_PROC_NAME
- See Also:
- Constant Field Values
-
__processNumber
private static int __processNumber
-
_process
private java.lang.Process _process
-
_pName
private java.lang.String _pName
-
_errThread
private java.lang.Thread _errThread
-
_outThread
private java.lang.Thread _outThread
-
_stdOut
private java.io.StringWriter _stdOut
-
_stdErr
private java.io.StringWriter _stdErr
-
_exitCode
private int _exitCode
-
_printToConsole
private boolean _printToConsole
-
_listeners
private java.util.List<ProcessUtil.StdStreamListener> _listeners
-
-
Constructor Detail
-
Runner
public Runner(java.lang.Process aProcess)
Constructor that takes a givenProcess
object and assigns default process name.- Parameters:
aProcess
- The givenProcess
object.
-
Runner
public Runner(java.lang.Process aProcess, java.lang.String procName)
Constructor that takes a givenProcess
object and a given process name.- Parameters:
aProcess
- The givenProcess
object.procName
- The given process name.
-
Runner
public Runner(java.lang.Process aProcess, java.lang.String procName, boolean printToConsole)
Constructor that takes a givenProcess
object, a given process name, and a givenboolean
flag that enables/disables console printing. If the given process name isnull
, the default process name is assigned.- Parameters:
aProcess
- The givenProcess
object.procName
- The given process name.printToConsole
-boolean
flag that enables/disables console printing.
-
-
Method Detail
-
addStreamListener
public void addStreamListener(ProcessUtil.StdStreamListener listener)
Adds a given object, implementing theStdStreamListener
interface to the list of standard stream listeners. Sends to the new listener previously printed standard error and standard output messages.- Parameters:
listener
- The given new standard stream listener.
-
getExitCode
public int getExitCode()
- Returns:
- Process exit code after the process finishes, otherwise
Integer.MAX_VALUE
.
-
getErrOutput
public java.lang.String getErrOutput()
- Returns:
- Standard error messages collected during the process execution.
-
getProcess
public java.lang.Process getProcess()
-
getStdOutput
public java.lang.String getStdOutput()
- Returns:
- Standard output messages collected during the process execution.
-
removeListener
public void removeListener(ProcessUtil.StdStreamListener listener)
Removes a givenStdStreamListener
object from the list of standard stream listeners.- Parameters:
listener
- The givenStdStreamListener
object to be removed from the list.
-
run
public void run()
Implements theRunnable.run()
method, collecting and printing standard output or standard error messages during the process execution.- Specified by:
run
in interfacejava.lang.Runnable
-
setExitCode
private void setExitCode(int exitCode)
Sets a given process exit code.- Parameters:
exitCode
- The given process exit code.
-
start
private void start()
Starts 2 threads for collecting and printing console messages during the process execution.
-
waitFor
public int waitFor()
Allows the caller to wait for the completion of the process.- Returns:
- Process exit code.
-
waitFor
public int waitFor(long timeout)
Allows the caller to wait for the completion of the process, but no longer than a given timeout value.- Parameters:
timeout
- The given timeout value (ms).- Returns:
- Process exit code or
Integer.MAX_VALUE
, if the process has not finished yet.
-
-