gate.util
Class ProcessManager

java.lang.Object
  extended by gate.util.ProcessManager

public class ProcessManager
extends Object

Class that supports running an external process and either silently consuming its standard output and error streams, or copying them to Java's stdout and stderr. This implementation reads the output and error streams in separate threads, but tries to reuse these threads from one external call to the next, unlike other approaches I've seen (which all spawn a pair of new threads for every external call). As a result, instances of this class are not thread safe. You must use a different instance of ProcessManager in each thread that you use to run external processes.


Nested Class Summary
private  class ProcessManager.StreamGobbler
          Thread body that takes a stream and either consumes it silently or echoes it to another stream.
 
Field Summary
private static boolean DEBUG
          Debug flag.
private  ProcessManager.StreamGobbler stderr
          StreamGobbler thread for standard error.
private  ProcessManager.StreamGobbler stdout
          StreamGobbler thread for standard output.
 
Constructor Summary
ProcessManager()
          Construct a ProcessManager object and start the gobbler threads.
 
Method Summary
 int runProcess(String[] argv, boolean dumpOutput)
          Run the given external process.
 int runProcess(String[] argv, OutputStream out, OutputStream err)
          Run the given external process.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG

private static final boolean DEBUG
Debug flag.

See Also:
Constant Field Values

stdout

private ProcessManager.StreamGobbler stdout
StreamGobbler thread for standard output.


stderr

private ProcessManager.StreamGobbler stderr
StreamGobbler thread for standard error.

Constructor Detail

ProcessManager

public ProcessManager()
Construct a ProcessManager object and start the gobbler threads.

Method Detail

runProcess

public int runProcess(String[] argv,
                      boolean dumpOutput)
               throws IOException
Run the given external process. If an exception results from starting the process, or while reading the output from the process, it will be thrown. Otherwise, the exit value from the process is returned.

Parameters:
argv - the process command line, suitable for passing to Runtime.exec.
dumpOutput - should we copy the process output and error streams to the Java output and error streams or just consume them silently?
Throws:
IOException

runProcess

public int runProcess(String[] argv,
                      OutputStream out,
                      OutputStream err)
               throws IOException
Run the given external process. If an exception results from starting the process, or while reading the output from the process, it will be thrown. Otherwise, the exit value from the process is returned.

Parameters:
argv - the process command line, suitable for passing to Runtime.exec.
dumpOutput - should we copy the process output and error streams to the Java output and error streams or just consume them silently?
Throws:
IOException