net.procsched
Class Command

java.lang.Object
  extended by net.procsched.Command
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
Script

public class Command
extends java.lang.Object
implements java.lang.Runnable

This class is the core of the process scheduling library. This class encapsulates all the information that a process may need for running including its command line arguments, a directory to find the command, the process' i/o streams and more. This class also holds any excpetions thrown during the running of the command. These exceptions can be looked at after or during the running of the program.

Author:
alex

Field Summary
protected  java.lang.String[] args
          The arguments to send the to program.
protected  java.lang.String command
          The program to execute.
protected  java.lang.String dir
          The directory to find the program.
protected  java.util.List<java.lang.Exception> exceptions
          A list which is the command followed by its arguements.
protected  long finish
          When the process finishes.
protected  boolean running
          True if the native process is running.
protected  long start
          When the process starts.
 
Constructor Summary
Command()
          This makes a defualt Command object.
Command(java.lang.String command)
          Makes a Command object that uses the specified command and the default working dir "." and no command line arguments.
Command(java.lang.String command, java.lang.String dir)
          This will make a Command object with the specified command and working dir.
Command(java.lang.String command, java.lang.String dir, java.lang.String[] args)
          This will make a Command that takes the specified arguemnts, uses the specified command name and the specified working directory.
 
Method Summary
 void addListener(CommandTerminationListener cte)
          Adds a listener to this Command object.
 void compressStdErrStdOut(int buf)
          NOT IMPLEMENTED.
 void compressStdOutStdErr()
          NOT IMPLEMENTED.
 boolean errorOccured()
          Returns true if at least one exception has been caught and stored.
 java.util.List<java.lang.Exception> exceptions()
          Returns a List that stores the caught exceptions.
static java.lang.Thread exec(Command c)
          This method will start the command specified.
 java.lang.String[] getArgs()
          Returns the arguments used by this Command
 java.lang.String getCommand()
          Returns the program name used by this Command
 java.lang.String getDir()
          Returns the program directory used by this Command
 boolean getMergeState()
           
 java.io.InputStream getStdErr()
          Similar to Command.getStdOut() but instead returns the stderr.
 java.io.OutputStream getStdIn()
          This method returns the stdin of the invoked process.
 java.io.InputStream getStdOut()
          Returns the *process'* stdout.
 void mergeOutput()
          Merges the stderr and stdout InputStream such that by reading from the stdout you can get both stdout and stderr.
 void redirectStdErr(java.io.OutputStream s, int bufSize)
          Redirects the stderr stream to the specified OutputStream.
 void redirectStdIn(java.io.InputStream s, int bufSize)
          Uses this stream for reading stdin data.
 void redirectStdOut(java.io.OutputStream s, int bufSize)
          Similar to how redirectStdErr() works, only this uses stdout.
 boolean removeListener(CommandTerminationListener cte)
          Removes a listener to this Command object.
 void run()
          This method is called when the Command object should execute the native process.
 void setArgs(java.lang.String[] args)
          Sets the arguements to be passed to the native program.
 void setCommand(java.lang.String command)
          Set the name of the program to invoke.
 void setDir(java.lang.String dir)
          Sets the directory which will be looked in for the program to invoke.
 long time()
          Computes the time that this process has been running for.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

command

protected java.lang.String command
The program to execute.


dir

protected java.lang.String dir
The directory to find the program.


args

protected java.lang.String[] args
The arguments to send the to program.


start

protected long start
When the process starts.


finish

protected long finish
When the process finishes.


running

protected boolean running
True if the native process is running.


exceptions

protected java.util.List<java.lang.Exception> exceptions
A list which is the command followed by its arguements.

Constructor Detail

Command

public Command()
This makes a defualt Command object. This object has no arguments, no command name and uses the "." working directory. It is not a good idea to make this run right after using this constructor. Errors will happen because of the lack of command name.


Command

public Command(java.lang.String command)
Makes a Command object that uses the specified command and the default working dir "." and no command line arguments. A Command made with this constructor can be directly used.

Parameters:
command - The name of the executable to call.

Command

public Command(java.lang.String command,
               java.lang.String dir)
This will make a Command object with the specified command and working dir. It will have no command line arguments. The working dir is where the executable program will be looked for.

Parameters:
command - The native executable to be executed
dir - The dir to find the command

Command

public Command(java.lang.String command,
               java.lang.String dir,
               java.lang.String[] args)
This will make a Command that takes the specified arguemnts, uses the specified command name and the specified working directory. This constructor is the "super" constructor in that all the other constructors call this one. The ProcessBuilder internal to this class uses the arguments specified in this constructor to make a native process on the machine in which the JVM is running. To have more control over the process made by this Command object then the settter methods should be used.

Parameters:
command - The native executable to be executed
dir - The dir to find the command
args - A String[] of the args to be sent to the command
Method Detail

exec

public static java.lang.Thread exec(Command c)
This method will start the command specified. This method should under no circumstances be called on the same command object twice unless the first call has finished. If you do call this method twice then undefined behavior will occur. A good thing to work on would be making this method callable multiple times.

Parameters:
c - The command to execute.
Returns:
The thread that is running the process.

getArgs

public java.lang.String[] getArgs()
Returns the arguments used by this Command

Returns:
The args to send the invoked program.

getCommand

public java.lang.String getCommand()
Returns the program name used by this Command

Returns:
The program name to invoke.

getDir

public java.lang.String getDir()
Returns the program directory used by this Command

Returns:
The directory to find the program to invoke.

getStdOut

public java.io.InputStream getStdOut()
Returns the *process'* stdout. This method returns an InputStream because that stream is what you can read to obtain the data written to the invoked program's stdout.

Returns:
The *invoked process'* stdout.

getStdErr

public java.io.InputStream getStdErr()
Similar to Command.getStdOut() but instead returns the stderr.

Returns:
The *invoked process'* stderr.

getStdIn

public java.io.OutputStream getStdIn()
This method returns the stdin of the invoked process. Thus when you write to this stream you will be sending data to the invoked process' stdin (i.e. when the invoked process calls something like "fgets()" it reads the data written to this OutputStream).

Returns:
The native programs stdin.

setArgs

public void setArgs(java.lang.String[] args)
Sets the arguements to be passed to the native program.

Parameters:
args - Arguments to pass to the nativ program.

setCommand

public void setCommand(java.lang.String command)
Set the name of the program to invoke.

Parameters:
command - The name of the program to invoke.

setDir

public void setDir(java.lang.String dir)
Sets the directory which will be looked in for the program to invoke. Kind of like setting a PATH variable in a unixy OS.

Parameters:
dir - The directory to search for the program to invoke.

getMergeState

public boolean getMergeState()

run

public void run()
This method is called when the Command object should execute the native process.

Specified by:
run in interface java.lang.Runnable

addListener

public void addListener(CommandTerminationListener cte)
Adds a listener to this Command object.

Parameters:
cte - The listener.

removeListener

public boolean removeListener(CommandTerminationListener cte)
Removes a listener to this Command object.

Parameters:
cte - The listener.

time

public long time()
Computes the time that this process has been running for. This method obviously only makes sense if the process has been started. If the process has not finished then it returns the amount of time the process has so far been running for.

Returns:
The time the process ran for ir has been running for.

errorOccured

public boolean errorOccured()
Returns true if at least one exception has been caught and stored.

Returns:
The error state of this command.

exceptions

public java.util.List<java.lang.Exception> exceptions()
Returns a List that stores the caught exceptions.

Returns:
The caught exceptions.

redirectStdErr

public void redirectStdErr(java.io.OutputStream s,
                           int bufSize)
Redirects the stderr stream to the specified OutputStream. This works by reading from the stderr input stream and then writing it to the output stream. The data is only read and written is there is bufSize bytes available.

Parameters:
s - The stream to write stderr to.
bufSize - The number or bytes to read/write at a time.

redirectStdOut

public void redirectStdOut(java.io.OutputStream s,
                           int bufSize)
Similar to how redirectStdErr() works, only this uses stdout.

Parameters:
s - The stream to write stdout to.
bufSize - The number or bytes to read/write at a time.

redirectStdIn

public void redirectStdIn(java.io.InputStream s,
                          int bufSize)
Uses this stream for reading stdin data. The data is read and then written to the process' stdin. Data read/written when there is any amount of data available.

Parameters:
s - The stream to get stdin from.
bufSize - The number of bytes to read/write at a time.

compressStdErrStdOut

public void compressStdErrStdOut(int buf)
                          throws java.io.IOException
NOT IMPLEMENTED.

Throws:
java.io.IOException

compressStdOutStdErr

public void compressStdOutStdErr()
NOT IMPLEMENTED.


mergeOutput

public void mergeOutput()
Merges the stderr and stdout InputStream such that by reading from the stdout you can get both stdout and stderr.