net.fenyo.gnetwatch.actions
Class ExternalCommand

java.lang.Object
  extended by net.fenyo.gnetwatch.actions.ExternalCommand

public class ExternalCommand
extends java.lang.Object

Instances of this class launch and manage processes outside of the JVM.

Version:
$Id: ExternalCommand.java,v 1.17 2008/05/22 19:46:21 fenyo Exp $
Author:
Alexandre Fenyo

Field Summary
private  java.lang.String[] cmdLine
           
private  java.lang.String directory
           
private  java.io.BufferedReader errReader
           
private static Log log
           
private  boolean merge
           
private  java.lang.Process process
           
private  java.io.BufferedReader reader
           
private  java.lang.StringBuffer sb
           
 
Constructor Summary
ExternalCommand(java.lang.String[] cmdLine)
          Creates an ExternalCommand instance and saves the command line.
ExternalCommand(java.lang.String[] cmdLine, boolean merge)
          Creates an ExternalCommand instance and saves the command line.
ExternalCommand(java.lang.String[] cmdLine, boolean merge, java.lang.String curdir)
           
ExternalCommand(java.lang.String[] cmdLine, java.lang.String directory)
          Creates an ExternalCommand instance and saves the command line.
 
Method Summary
 void end()
          Make sure the underlying file descriptors are closed, to avoid maintening unused resources in an application server JVM for instance.
 void fork()
          Launches a process but do not wait for its completion.
private  void kill()
          Kills the process.
 void logArgs()
          Displays command line arguments.
private  java.lang.String readLine(java.io.Reader r)
          Reads a line from the process output.
 java.lang.String readLineStderr()
          Reads one line of the stderr.
 java.lang.String readLineStdout()
          Reads one line of the stdout.
 java.lang.String readStderr()
          Reads stderr.
 java.lang.String readStdout()
          Reads stdout.
 java.lang.String readStdoutStderr()
          Merges stdout and stderr.
 java.lang.String runStdout()
          Reads the whole standard output.
 java.lang.String runStdoutStderr()
          Reads the whole output.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static Log log

merge

private boolean merge

cmdLine

private final java.lang.String[] cmdLine

directory

private final java.lang.String directory

sb

private final java.lang.StringBuffer sb

process

private java.lang.Process process

reader

private java.io.BufferedReader reader

errReader

private java.io.BufferedReader errReader
Constructor Detail

ExternalCommand

public ExternalCommand(java.lang.String[] cmdLine,
                       java.lang.String directory)
Creates an ExternalCommand instance and saves the command line. any thread.

Parameters:
cmdLine - command line.
directory - path to the current directory for the script to be launched.

ExternalCommand

public ExternalCommand(java.lang.String[] cmdLine)
Creates an ExternalCommand instance and saves the command line. any thread.

Parameters:
cmdLine - command line.

ExternalCommand

public ExternalCommand(java.lang.String[] cmdLine,
                       boolean merge)
Creates an ExternalCommand instance and saves the command line. any thread.

Parameters:
cmdLine - command line.
merge - merge standard output and standard error.

ExternalCommand

public ExternalCommand(java.lang.String[] cmdLine,
                       boolean merge,
                       java.lang.String curdir)
Method Detail

readLine

private java.lang.String readLine(java.io.Reader r)
                           throws java.io.IOException,
                                  java.lang.InterruptedException
Reads a line from the process output.

Parameters:
r - reader.
Returns:
line read.
Throws:
java.io.IOException - IO exception.
java.lang.InterruptedException - exception.

runStdoutStderr

public java.lang.String runStdoutStderr()
                                 throws java.lang.InterruptedException
Reads the whole output.

Returns:
whole output.
Throws:
java.lang.InterruptedException - exception.

runStdout

public java.lang.String runStdout()
                           throws java.lang.InterruptedException
Reads the whole standard output.

Returns:
whole standard output.
Throws:
java.lang.InterruptedException - interrupted.

logArgs

public void logArgs()
Displays command line arguments. any thread.

Parameters:
none. -

fork

public void fork()
          throws java.io.IOException
Launches a process but do not wait for its completion. any thread.

Parameters:
none. -
Throws:
java.io.IOException - i/o exception before reading the process output stream.

readStdoutStderr

public java.lang.String readStdoutStderr()
                                  throws java.io.IOException,
                                         java.lang.InterruptedException
Merges stdout and stderr. Waits for the end of the process output stream. Note that this method can return before the process completion. any thread.

Parameters:
none. -
Returns:
void.
Throws:
java.io.IOException - i/o exception while reading the process output stream.
java.lang.InterruptedException

readStdout

public java.lang.String readStdout()
                            throws java.io.IOException,
                                   java.lang.InterruptedException
Reads stdout. Waits for the end of the process output stream. Note that this method can return before the process completion. any thread.

Parameters:
none. -
Returns:
void.
Throws:
java.io.IOException - i/o exception while reading the process output stream.
java.lang.InterruptedException

readStderr

public java.lang.String readStderr()
                            throws java.io.IOException,
                                   java.lang.InterruptedException
Reads stderr. Waits for the closure of the process output stream. Note that this method can return before the process completion. any thread.

Parameters:
none. -
Returns:
void.
Throws:
java.io.IOException - i/o exception while reading the process output stream.
java.lang.InterruptedException

readLineStdout

public java.lang.String readLineStdout()
                                throws java.io.IOException,
                                       java.lang.InterruptedException
Reads one line of the stdout. any thread.

Parameters:
none. -
Returns:
void.
Throws:
java.io.IOException - i/o exception while reading the process output stream.
java.lang.InterruptedException

readLineStderr

public java.lang.String readLineStderr()
                                throws java.io.IOException,
                                       java.lang.InterruptedException
Reads one line of the stderr. any thread.

Parameters:
none. -
Returns:
void.
Throws:
java.io.IOException - i/o exception while reading the process output stream.
java.lang.InterruptedException

end

public void end()
         throws java.io.IOException
Make sure the underlying file descriptors are closed, to avoid maintening unused resources in an application server JVM for instance. any thread.

Parameters:
none. -
Throws:
java.io.IOException - error while closing streams.

kill

private void kill()
Kills the process. We do not reset process to null since it can be used to get exit code. any thread.

Parameters:
none. -