public final class ExternalProcess extends Object
STDOUT, STDERR and STDIN streams
 in multiple threads managed by a thread pool.
 This class is intended for applications that call an external program many 
 times, e.g. in a loop, and that need high performance throughput, i.e.
 the program's input and output should not be written to disk. The Java
 Runtime.exec(java.lang.String) methods requires the application to read/write
 the external program's input and output streams in multiple threads. 
 Otherwise the calling application may block. However, instantiating multiple 
 threads for each call is extensive. On Linux systems there is also the 
 problem that each Java thread is represented by a single process and the
 number of processes is limited on Linux. Because the Java garbage collector
 does not free the Thread objects properly, an application
 might run out of threads (indicated by a OutOfMemoryError 
 exception) after multiple iterations. Therefore, the 
 ExternalProcess class uses a 
 thread pool.
The simplest way to use this class is by calling the static methods
 execute(String) and 
 execute(String, String, StringWriter, StringWriter). However, these
 methods are not thread safe and no configuration is possible. In the former
 case the program's input, output and error output is redirected to STDIN,
 STDOUT and STDERR of the calling program. In the 
 latter case input is provided as string and output and error output is 
 written to StringWriter objects. The environment, i.e.
 the current working directory and the environment variables, are inherited
 from the calling process. In both cases, a static thread pool of size 
 THREAD_POOL_SIZE is used. The command that should be executed is 
 provided as a string argument.
In scenarios where the environment has to be changed, the program input
 is generated just in time, or the program's output is parsed just in time,
 the use of an explicit instance of the ExternalProcess class
 is recommended. This instance could be initialized with a custom thread pool.
 Otherwise a SimpleThreadPool of size 3 is used.
 The input and output is managed by multithreaded 
 input handler and 
 output handler objects.
 There are four predefined handlers that read the program's input from a
 Reader object or a InputStream object and
 write the program's output to a Writer object or a
 OutputStream object. These classes are called:
 ReaderInputHandler, 
 SimpleInputHandler,
 WriterOutputHandler and
 SimpleOutputHandler. If no handlers are
 specified the input and output is redirected to the standards streams of 
 the calling process.
Before one of the methods execute() or 
 execute(Properties) is called, the commands property should be set. One may include placeholders of the form
 %PARAM% within the commands. If a 
 Properties object is passed to the 
 execute(Properties) method, the placeholders are replaced by the 
 particular property value. Therefore, the Properties object
 must contain a key named PARAM (case doesn't matter). The 
 environment for calling the external program can be configured using the
 properties workingDirectory and
 environmentProperties.
Finally, the sleepTime property can be
 increased, in case the output handlers are not able to catch the whole
 program's output within the given time. The default value is 
 SLEEP_TIME [in milliseconds].
Process| Modifier and Type | Field and Description | 
|---|---|
static int | 
SLEEP_TIME
Number of milliseconds the execute method should pauses after the 
 external process has finished the execution. 
 | 
static int | 
THREAD_POOL_SIZE
Size of the thread pool for the static execute methods. 
 | 
| Constructor and Description | 
|---|
ExternalProcess()
Initializes the external process. 
 | 
ExternalProcess(ThreadPool threadPool)
Initializes the external process. 
 | 
| Modifier and Type | Method and Description | 
|---|---|
int | 
execute()
Executes the external process and waits for its termination. 
 | 
int | 
execute(Properties variables)
Executes the external process and waits for its termination. 
 | 
static int | 
execute(String commands)
Executes an external program. 
 | 
static int | 
execute(String commands,
              String inputString,
              StringWriter outputString,
              StringWriter errorString)
Executes an external program. 
 | 
protected void | 
finalize() | 
String | 
getCommands()
Gets the command line including the path or name of the external program
 and its command line arguments. 
 | 
String[] | 
getEnvironmentProperties()
Gets environment variables for the external process. 
 | 
OutputHandler | 
getErrorHandler()
Gets the output error handler which is responsible for the standard error
 output of the external process. 
 | 
InputHandler | 
getInputHandler()
Gets the input handler which is responsible for the standard input
 of the external process. 
 | 
OutputHandler | 
getOutputHandler()
Gets the output handler which is responsible for the standard output
 of the external process. 
 | 
int | 
getSleepTime()
Gets the number of milliseconds the execute(Properties)
 method should pauses after the external process is terminated. 
 | 
File | 
getWorkingDirectory()
Gets the working directory for the external process. 
 | 
static String | 
joinCommands(Object[] commandList)
Joins a command list to a single command string. 
 | 
static void | 
main(String[] args)
Runs an external program from the command line. 
 | 
static String | 
resolveCommands(String commands,
                              Properties variables)
Resolves the given command line by replacing all placeholder of the 
 format  
%NAME% with the values from the given properties
 for the corresponding keys of the format NAME. | 
void | 
setCommands(String commands)
Sets the command line including the path or name of the external program
 and its command line arguments. 
 | 
void | 
setEnvironmentProperties(String[] environmentProperties)
Sets environment variables for the external process. 
 | 
void | 
setErrorHandler(OutputHandler errorHandler)
Sets the output error handler which is responsible for the standard error
 output of the external process. 
 | 
void | 
setInputHandler(InputHandler inputHandler)
Sets the input handler which is responsible for the standard input
 of the external process. 
 | 
void | 
setOutputHandler(OutputHandler outputHandler)
Sets the output handler which is responsible for the standard output
 of the external process. 
 | 
void | 
setSleepTime(int sleepTime)
Sets the number of milliseconds the execute(Properties)
 method should pauses after the external process is terminated. 
 | 
void | 
setWorkingDirectory(File workingDirectory)
Sets the working directory for the external process. 
 | 
ThreadPool | 
threadPool()
Gets the thread pool which is used for the input and output handlers. 
 | 
public static final int THREAD_POOL_SIZE
public static final int SLEEP_TIME
public ExternalProcess()
public ExternalProcess(ThreadPool threadPool)
threadPool - a thread pool with at least three threads or 
 null if the default thread pool should be usedpublic static void main(String[] args)
args - the path or the name of the external program and its command
 line argumentspublic static String resolveCommands(String commands, Properties variables) throws NullPointerException
%NAME% with the values from the given properties
 for the corresponding keys of the format NAME.commands - the given command linevariables - the placeholders or null if no resolvement
 should be performedNullPointerException - if commands is 
 null.public static int execute(String commands) throws IOException, InterruptedException, NullPointerException, SecurityException, IllegalArgumentException
STDIN, the program output is written to 
 STDOUT and the program error output is written to
 STDERR.
 Note: This method is not thread-safe.
commands - the command line including the path
 or the name of the external program and its command line argumentsSecurityException - if a security manager exists and its 
 checkExec method doesn't allow creation of a subprocess.IOException - if an I/O error occurs.NullPointerException - if commands is 
 null.IllegalArgumentException - if commandList is empty.InterruptedException - if the current thread is 
 interrupted by another thread 
 while it is waiting, then the wait is ended and an 
 InterruptedException is thrown.public static int execute(String commands, String inputString, StringWriter outputString, StringWriter errorString) throws IOException, InterruptedException, NullPointerException, SecurityException, IllegalArgumentException
Note: This method is not thread-safe.
commands - the command line including the path
 or the name of the external program and its command line argumentsinputString - the input for the external programm or
 null if the input should be read from STDINoutputString - the output of the external programm or
 null if the output should be written to STDOUTerrorString - the error output of the external program or
 null if the error output should be written to 
 STDERRSecurityException - if a security manager exists and its 
 checkExec method doesn't allow creation of a subprocess.IOException - if an I/O error occurs.NullPointerException - if commandList is 
 null.IllegalArgumentException - if commandList is empty.InterruptedException - if the current thread is 
 interrupted by another thread 
 while it is waiting, then the wait is ended and an 
 InterruptedException is thrown.public static String joinCommands(Object[] commandList) throws NullPointerException
commandList - the list of the command and its argumentsNullPointerException - if commandList is 
 null.public int execute() throws IOException, InterruptedException, SecurityException, IllegalArgumentException
IllegalArgumentException - if the command is emptySecurityException - if a security manager exists and its 
 checkExec method doesn't allow creation of a subprocess.IOException - if an I/O error occurs.InterruptedException - if the current thread is 
 interrupted by another thread 
 while it is waiting, then the wait is ended and an 
 InterruptedException is thrown.public int execute(Properties variables) throws IOException, InterruptedException, SecurityException, IllegalArgumentException
variables - a list of key-value-pairs that should be used to replace
 placeholders in the command line. May be null.SecurityException - if a security manager exists and its 
 checkExec method doesn't allow creation of a subprocess.IllegalArgumentException - if the command is emptyIOException - if an I/O error occurs.InterruptedException - if the current thread is 
 interrupted by another thread 
 while it is waiting, then the wait is ended and an 
 InterruptedException is thrown.public String getCommands()
public void setCommands(String commands) throws NullPointerException
commands - the command lineNullPointerException - if commands is 
 null.public String[] getEnvironmentProperties()
name=value or null if the environment variables
 should be inherited from the parent processpublic void setEnvironmentProperties(String[] environmentProperties)
environmentProperties - a list of strings in the format
 name=value or null if the environment variables
 should be inherited from the parent processpublic OutputHandler getErrorHandler()
public void setErrorHandler(OutputHandler errorHandler)
errorHandler - the error output handler or null if the
 error output should be redirected to STDERRpublic InputHandler getInputHandler()
public void setInputHandler(InputHandler inputHandler)
inputHandler - the input handler or null if the
 input should be read from STDINpublic OutputHandler getOutputHandler()
public void setOutputHandler(OutputHandler outputHandler)
outputHandler - the output handler or null if the
 output should be redirected to STDOUTpublic ThreadPool threadPool()
public File getWorkingDirectory()
null if it should be 
 inherited from the parent processpublic void setWorkingDirectory(File workingDirectory)
workingDirectory - the working directory or null if it 
 should be inherited from the parent processpublic int getSleepTime()
public void setSleepTime(int sleepTime) throws IllegalArgumentException
sleepTime - time in millisecondsIllegalArgumentException - if sleepTime is negative.Copyright © 2014 BioJava. All rights reserved.