wt.util
Class ProcessLauncher

java.lang.Object
  extended bywt.util.ProcessLauncher

public class ProcessLauncher
extends Object

Performs the process using another thread to start sending output, before processing has completed.

Supported API: true

Extendable: false

See Also:
Process

Nested Class Summary
private  class ProcessLauncher.PrintStreamSpewer
          Thread to read from an input stream (line-by-line) and print it to a print stream until the input stream closes.
 class ProcessLauncher.TimeoutException
           
private  class ProcessLauncher.TimeoutMonitor
           
 
Field Summary
private  String[] arguments_
           
private  String command_
           
private  Properties environment_
           
private  ProcessLauncher.PrintStreamSpewer errSpewer_
           
private  PrintStream errStream_
           
private  PrintWriter errWriter_
           
private  String executable_
           
private  ProcessLauncher.PrintStreamSpewer outSpewer_
           
private  PrintStream outStream_
           
private  PrintWriter outWriter_
           
private  Process process_
           
private  ProcessLauncher.TimeoutMonitor timeoutMonitor_
           
private  boolean verbose
           
 
Constructor Summary
ProcessLauncher(String command)
          Create an instance to run the command;

Supported API: true
ProcessLauncher(String executable, String[] args)
           
ProcessLauncher(String executable, String[] args, Properties environment_variables)
           
 
Method Summary
private static String buildCommand(String executable, String[] arguments)
           
private  void cleanUpProcess()
           
private  void cleanUpTimeoutMonitor()
           
private  String[] formatEnvironment()
           
 String getCommand()
          Tell the client what the command was when constructor was called.
static void main(String[] args)
          Use java to launch a process to execute a command.
 int runProcess()
          Launch the subprocess and wait for it to complete.
 int runProcess(long inactivity_timeout, long overall_timeout)
          Launch the sub-process but do not wait beyond overall_timeout for it to complete.
 void setErrorStream(PrintStream error_stream)
          The errors from the process and local class methods will be written to this PrintStream, for reading to begin before processing completes.
 void setErrorWriter(PrintWriter error_writer)
          The errors from the process and local class methods will be written to this PrintWriter, for reading to begin after the process completes.
 void setOutStream(PrintStream out_stream)
          The output from the process's output stream will be written to this PrintStream, for reading to begin before processing completes.
 void setOutWriter(PrintWriter out_writer)
          The output from the process's output stream will be written to this PrintWriter, for reading to begin after the process completes.
 void setVerbosity(boolean verbosity)
           
private  void startSpewersForProcess()
          Start moving single-line Strings from the process_'s InputStream and ErrorStream into the caller's output.
private  int waitForProcessToExitOrTimeOut()
           
private  void waitForSpewersToStop()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

executable_

private String executable_

arguments_

private String[] arguments_

environment_

private Properties environment_

command_

private String command_

process_

private Process process_

timeoutMonitor_

private ProcessLauncher.TimeoutMonitor timeoutMonitor_

outSpewer_

private ProcessLauncher.PrintStreamSpewer outSpewer_

errSpewer_

private ProcessLauncher.PrintStreamSpewer errSpewer_

outStream_

private PrintStream outStream_

errStream_

private PrintStream errStream_

outWriter_

private PrintWriter outWriter_

errWriter_

private PrintWriter errWriter_

verbose

private boolean verbose
Constructor Detail

ProcessLauncher

public ProcessLauncher(String command)
Create an instance to run the command;

Supported API: true

Parameters:
command - Process this command.

ProcessLauncher

public ProcessLauncher(String executable,
                       String[] args)

ProcessLauncher

public ProcessLauncher(String executable,
                       String[] args,
                       Properties environment_variables)
Method Detail

buildCommand

private static String buildCommand(String executable,
                                   String[] arguments)

getCommand

public final String getCommand()
Tell the client what the command was when constructor was called.

Supported API: true

Returns:
The command entered by this class' constructor.

setErrorWriter

public void setErrorWriter(PrintWriter error_writer)
The errors from the process and local class methods will be written to this PrintWriter, for reading to begin after the process completes. Will only take effect if invoked prior to runProcess().

Supported API: true

Parameters:
error_writer - After processing completes, review errors from here.

setOutWriter

public void setOutWriter(PrintWriter out_writer)
The output from the process's output stream will be written to this PrintWriter, for reading to begin after the process completes. Will only take effect if invoked prior to runProcess().

Supported API: true

Parameters:
out_writer - After processing completes, review the process' progression from here.

setErrorStream

public void setErrorStream(PrintStream error_stream)
The errors from the process and local class methods will be written to this PrintStream, for reading to begin before processing completes. Will only take effect if invoked prior to runProcess().

Supported API: true

Parameters:
error_stream - While still processing, look at errors from here.

setOutStream

public void setOutStream(PrintStream out_stream)
The output from the process's output stream will be written to this PrintStream, for reading to begin before processing completes. Will only take effect if invoked prior to runProcess().

Supported API: true

Parameters:
out_stream - While still processing, look at the process' progression from here.

runProcess

public final int runProcess()
                     throws IOException,
                            ProcessLauncher.TimeoutException
Launch the subprocess and wait for it to complete.

Supported API: true

Returns:
the return code of the subprocess
Throws:
IOException - Forwarded from runProcess(long,long).
ProcessLauncher.TimeoutException

runProcess

public final int runProcess(long inactivity_timeout,
                            long overall_timeout)
                     throws IOException,
                            ProcessLauncher.TimeoutException
Launch the sub-process but do not wait beyond overall_timeout for it to complete. For backward compatibility, the definition has "throws IOException", but is never thrown. Instead, errors are seen with the return, and in the writers and streams.

Parameters:
inactivity_timeout - Max milliseconds between process additions to the output (= 0 means no max).
overall_timeout - Max milliseconds for all process output (= 0 means no max).
Returns:
The return code of the sub-process.
Throws:
IOException - If unable to process the command.
ProcessLauncher.TimeoutException

formatEnvironment

private String[] formatEnvironment()

startSpewersForProcess

private void startSpewersForProcess()
Start moving single-line Strings from the process_'s InputStream and ErrorStream into the caller's output.


waitForSpewersToStop

private void waitForSpewersToStop()

waitForProcessToExitOrTimeOut

private int waitForProcessToExitOrTimeOut()
                                   throws IOException,
                                          ProcessLauncher.TimeoutException
Throws:
IOException
ProcessLauncher.TimeoutException

cleanUpTimeoutMonitor

private void cleanUpTimeoutMonitor()

cleanUpProcess

private void cleanUpProcess()

main

public static void main(String[] args)
Use java to launch a process to execute a command.

Parameters:
args - The command.

setVerbosity

public void setVerbosity(boolean verbosity)