springs.util
Class ProcessExecutor

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--springs.util.ProcessExecutor
All Implemented Interfaces:
java.lang.Runnable

public class ProcessExecutor
extends java.lang.Thread

A process executor.

Author:
Sergio Ilarri Artigas

Nested Class Summary
(package private)  class ProcessExecutor.ReaderThread
           
 
Field Summary
private  java.lang.String _commandName
           
private static java.lang.String _ENCODING
           
private  ProcessExecutor.ReaderThread _errorOutputWriter
           
private  java.lang.String _id
           
private  java.io.ObjectOutputStream _oos
           
private  java.lang.String _output
           
private  java.lang.Process _p
           
private  boolean _recordOutput
           
private  boolean _showOutput
           
private  boolean _signalTermination
           
private  ProcessExecutor.ReaderThread _standardOutputWriter
           
private  java.lang.String _stringForEnd
           
private  boolean _stringForEndReceived
           
private  java.lang.String _stringForStart
           
private  boolean _stringForStartReceived
           
private  boolean _terminated
           
private static boolean _USE_ENCODING
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ProcessExecutor(java.lang.String[] commandWithArgs, java.lang.String id, boolean waitForOutput)
          Constructor de la clase.
ProcessExecutor(java.lang.String[] commandWithArgs, java.lang.String id, int numLines)
          Constructor.
ProcessExecutor(java.lang.String commandName, java.lang.String id, boolean waitForOutput)
          Constructor.
ProcessExecutor(java.lang.String commandName, java.lang.String id, int numLines)
          Constructor de la clase.
ProcessExecutor(java.lang.String commandName, java.lang.String id, int numLines, java.lang.Object object, boolean signalTermination)
          Constructor de la clase.
ProcessExecutor(java.lang.String commandName, java.lang.String id, java.lang.String stringForStart, java.lang.String stringForEnd, java.lang.Object object, boolean signalTermination)
          Constructor.
 
Method Summary
private  void createObjectOutputStream()
          Creates an output stream to pass objects to the process.
private  void execute(java.lang.String commandName)
          Starts the execution of a command.
private  void execute(java.lang.String[] commandWithArgs)
          Starts the execution of a command.
 java.lang.String getRecordedOutput()
          Returns the recorded output.
static void main(java.lang.String[] args)
          Main method.
 void passObject(java.lang.Object object)
          Writes an object in the standard input of the process.
private  void prepareReaders()
          Start the readers.
 void sendCommand(java.lang.String command)
          Sends a command to the process.
 void setShowOutput(boolean showOutput)
          Sets if the output of the process should be shown.
 void startRecordingOutput()
          Starts recording the output.
 void stopRecordingOutput()
          Stops recording the output.
 void terminate()
          Terminates the execution.
private  void waitForOutput(int numLines)
          Blocks until the specified number of lines have been received.
private  void waitOutputEnd()
          Waits for an output line that indicates the end of the process.
private  void waitOutputStart()
          Waits for an output line that indicates the start of the process.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, run, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_p

private java.lang.Process _p

_commandName

private java.lang.String _commandName

_id

private java.lang.String _id

_standardOutputWriter

private ProcessExecutor.ReaderThread _standardOutputWriter

_errorOutputWriter

private ProcessExecutor.ReaderThread _errorOutputWriter

_signalTermination

private boolean _signalTermination

_oos

private java.io.ObjectOutputStream _oos

_stringForStart

private java.lang.String _stringForStart

_stringForEnd

private java.lang.String _stringForEnd

_stringForEndReceived

private volatile boolean _stringForEndReceived

_stringForStartReceived

private volatile boolean _stringForStartReceived

_output

private java.lang.String _output

_recordOutput

private boolean _recordOutput

_showOutput

private boolean _showOutput

_terminated

private boolean _terminated

_USE_ENCODING

private static final boolean _USE_ENCODING
See Also:
Constant Field Values

_ENCODING

private static final java.lang.String _ENCODING
See Also:
Constant Field Values
Constructor Detail

ProcessExecutor

public ProcessExecutor(java.lang.String commandName,
                       java.lang.String id,
                       boolean waitForOutput)
Constructor.

Parameters:
commandName - the command name.
id - an identifier for the ProcessExecutor.
waitForOutput - a boolean indicating whether it must block waiting for an output.
See Also:
String

ProcessExecutor

public ProcessExecutor(java.lang.String[] commandWithArgs,
                       java.lang.String id,
                       boolean waitForOutput)
Constructor de la clase.

Parameters:
commandWithArgs - the command name with its arguments.
id - an identifier for the ProcessExecutor.
waitForOutput - a boolean indicating whether it must block waiting for an output.
See Also:
String

ProcessExecutor

public ProcessExecutor(java.lang.String commandName,
                       java.lang.String id,
                       int numLines)
Constructor de la clase.

Parameters:
commandName - the command name.
id - an identifier for the ProcessExecutor.
numLines - number of lines in the output to wait for.
See Also:
String

ProcessExecutor

public ProcessExecutor(java.lang.String commandName,
                       java.lang.String id,
                       int numLines,
                       java.lang.Object object,
                       boolean signalTermination)
                throws java.io.IOException
Constructor de la clase.

Parameters:
commandName - the command name.
id - an identifier for the ProcessExecutor.
numLines - number of lines in the output to wait for.
object - an object to pass as an argument in the standard input of the process.
signalTermination - a boolean indicating if the termination of the process must be signaled by sending a signal through its standard input or by just destroying it.
Throws:
java.io.IOException
See Also:
String, Object

ProcessExecutor

public ProcessExecutor(java.lang.String commandName,
                       java.lang.String id,
                       java.lang.String stringForStart,
                       java.lang.String stringForEnd,
                       java.lang.Object object,
                       boolean signalTermination)
                throws java.io.IOException
Constructor.

Parameters:
commandName - name of the command to execute.
id - an identifier for the process executor.
stringForStart - the string that signals the start of the process.
stringForEnd - the string that signals the end of the process.
object - an object to pass as an argument in the standard input of the process.
signalTermination - a boolean indicating if the termination of the process must be signaled by sending a signal through its standard input or by just destroying it.
Throws:
java.io.IOException
See Also:
String, Object

ProcessExecutor

public ProcessExecutor(java.lang.String[] commandWithArgs,
                       java.lang.String id,
                       int numLines)
Constructor.

Parameters:
commandWithArgs - an array with the command name and its arguments.
id - an identifier for the process executor.
numLines - number of lines in the output to wait for.
See Also:
String
Method Detail

setShowOutput

public void setShowOutput(boolean showOutput)
Sets if the output of the process should be shown.

Parameters:
showOutput - a boolean indicating whether the output should be shown.

startRecordingOutput

public void startRecordingOutput()
Starts recording the output.


stopRecordingOutput

public void stopRecordingOutput()
Stops recording the output. The previous output is de-recorded.


getRecordedOutput

public java.lang.String getRecordedOutput()
Returns the recorded output.

Returns:
the recorded output.
See Also:
String

execute

private void execute(java.lang.String commandName)
Starts the execution of a command.

Parameters:
commandName - the name of the command (with its options).
See Also:
String

execute

private void execute(java.lang.String[] commandWithArgs)
Starts the execution of a command.

Parameters:
commandWithArgs - the name of the command (with its options).
See Also:
String

prepareReaders

private void prepareReaders()
Start the readers.


waitOutputEnd

private void waitOutputEnd()
Waits for an output line that indicates the end of the process.


waitOutputStart

private void waitOutputStart()
Waits for an output line that indicates the start of the process.


waitForOutput

private void waitForOutput(int numLines)
Blocks until the specified number of lines have been received.

Parameters:
numLines - the number of lines to wait for.

terminate

public void terminate()
Terminates the execution.


createObjectOutputStream

private void createObjectOutputStream()
                               throws java.io.IOException
Creates an output stream to pass objects to the process.

Throws:
java.io.IOException - if error.
See Also:
IOException

passObject

public void passObject(java.lang.Object object)
                throws java.io.IOException
Writes an object in the standard input of the process.

Parameters:
object - the object. It must be Serializable.
Throws:
java.io.IOException - if there is an error.
See Also:
Object, Serializable, IOException

sendCommand

public void sendCommand(java.lang.String command)
                 throws java.io.IOException
Sends a command to the process.

Parameters:
command - the command.
Throws:
java.io.IOException - if there is an error.
See Also:
String, IOException

main

public static void main(java.lang.String[] args)
Main method.

Parameters:
args - the command to execute.
See Also:
String