Class ProcessExecutor<E extends Exception>
- Type Parameters:
E- The type of the exceptions the handle* methods may throw.
This class is not thread-safe, it assumes that never two of its methods are executed simultaneously.
When an instance of this class is created, the corresponding process is started immediately.
Then some text may be written to stdin of the process with the println(String) method.
Afterwards join() has to be called, which reads the output from the process and calls
the handle* methods. This method blocks, i.e. when it returns the process has terminated. Now the
get* methods may be used to get the output of the process.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionProcessExecutor(LogManager logger, Class<E> exceptionClass, @Nullable File executionDirectory, String... cmd) ProcessExecutor(LogManager logger, Class<E> exceptionClass, String... cmd) ProcessExecutor(LogManager logger, Class<E> exceptionClass, Map<String, String> environmentOverride, @Nullable File executionDirectory, String... cmd) Create an instance and immediately execute the supplied command with the supplied environment variables.ProcessExecutor(LogManager logger, Class<E> exceptionClass, Map<String, String> environmentOverride, String... cmd) -
Method Summary
Modifier and TypeMethodDescriptionReturns the complete output to stderr of the process.Returns the complete output of the process.protected voidhandleErrorOutput(String line) Handle one line of stderr output from the process.protected voidhandleExitCode(int code) Handle the exit code of the process.protected voidhandleOutput(String line) Handle one line of output from the process.booleanChecks whether the process has finished already.intjoin()Wait for the process to terminate and read all of it's output.intjoin(long timelimit) Wait for the process to terminate.voidWrite a String to the process.voidWrite a String to the process.voidsendEOF()Sends the EOF (end of file) signal to stdin of the process.
-
Field Details
-
logger
-
-
Constructor Details
-
ProcessExecutor
public ProcessExecutor(LogManager logger, Class<E> exceptionClass, String... cmd) throws IOException - Throws:
IOException
-
ProcessExecutor
public ProcessExecutor(LogManager logger, Class<E> exceptionClass, @Nullable File executionDirectory, String... cmd) throws IOException - Throws:
IOException
-
ProcessExecutor
public ProcessExecutor(LogManager logger, Class<E> exceptionClass, Map<String, String> environmentOverride, String... cmd) throws IOException- Throws:
IOException
-
ProcessExecutor
public ProcessExecutor(LogManager logger, Class<E> exceptionClass, Map<String, String> environmentOverride, @Nullable File executionDirectory, String... cmd) throws IOExceptionCreate an instance and immediately execute the supplied command with the supplied environment variables.The map with the environment parameters will override the values from the default environment. Values in the map may be null, which means that this variable is removed from the environment. Null is not allowed as a key in the map.
Whenever a line is read on stdout or stderr of the process, the
handleOutput(String)or thehandleErrorOutput(String)are called respectively.It is strongly advised to call
join()sometimes, as otherwise there may be resources not being cleaned up properly. Also exceptions thrown by the handling methods would get swallowed.- Parameters:
logger- A LogManager for debug output.exceptionClass- The type of exception that the handler methods may throw.environmentOverride- Map with environment variables to set.executionDirectory- The directory for the command to be executed in.cmd- The command with arguments to execute.- Throws:
IOException- If the process cannot be executed.- See Also:
-
-
Method Details
-
println
Write a String to the process. May only be called beforejoin()was called, as afterwards the process is not running anymore.- Throws:
IOException
-
print
Write a String to the process. May only be called beforejoin()was called, as afterwards the process is not running anymore.- Throws:
IOException
-
sendEOF
Sends the EOF (end of file) signal to stdin of the process.- Throws:
IOException
-
join
Wait for the process to terminate.- Parameters:
timelimit- Maximum time to wait for process (in milliseconds)- Returns:
- The exit code of the process.
- Throws:
IOException- passed from the handle* methods.E- passed from the handle* methods.TimeoutException- If timeout is hit.InterruptedException- If the current thread is interrupted.
-
join
Wait for the process to terminate and read all of it's output. Whenever a line is read on stdout or stderr of the process, thehandleOutput(String)or thehandleErrorOutput(String)are called respectively.- Returns:
- The exit code of the process.
- Throws:
IOException- passed from the handle* methods.E- passed from the handle* methods.InterruptedException- If the current thread is interrupted.
-
handleOutput
Handle one line of output from the process. This method may be overwritten by clients. The default implementation logs the line on level ALL and adds it to a list which may later be retrieved withgetOutput(). It never throws an exception (but client implementations may do so).This method will be called in a new thread.
- Throws:
E- Overwriting methods may throw this exception which will be propagated.
-
handleErrorOutput
Handle one line of stderr output from the process. This method may be overwritten by clients. The default implementation logs the line on level WARNING and adds it to a list which may later be retrieved withgetErrorOutput(). It never throws an exception (but client implementations may do so).This method will be called in a new thread.
- Throws:
E- Overwriting methods may throw this exception which will be propagated.
-
handleExitCode
Handle the exit code of the process. This method may be overwritten by clients. The default implementation logs the code on level WARNING, if it is non-zero.This method will be called in a new thread.
- Throws:
E- Overwriting methods may throw this exception which will be propagated.
-
isFinished
public boolean isFinished()Checks whether the process has finished already. This is true exactly ifjoin()has been called. -
getOutput
Returns the complete output of the process. May only be called afterjoin()has been called. -
getErrorOutput
Returns the complete output to stderr of the process. May only be called afterjoin()has been called.
-