com.ibm.are.common
Class CommandRunner

java.lang.Object
  extended by com.ibm.are.common.CommandRunner

public class CommandRunner
extends java.lang.Object

The CommandRunner class provides a level of abstraction to the Runtime.exec method. This class provides additional benefits, including the option to automatically retrieve all output sent to standard out and error, as well as integrating with the same logging capabilities present in many other ARE classes.

Version:
1.0
Author:
IBM

Field Summary
static java.lang.String COPYRIGHT
          Copyright for class bytecode
 
Constructor Summary
  CommandRunner(java.lang.String command)
          Constructs a CommandRunner object that will run the specified command.
  CommandRunner(java.lang.String[] cmdarray)
          Constructs a CommandRunner object that will run the specified command.
protected CommandRunner(java.lang.String[] envp, java.io.File dir)
          Constructs a CommandRunner object that will use the specified environment variables and working directory for the sub-process will be passed along on the Runtime.exec of the specified command.
  CommandRunner(java.lang.String[] cmdarray, java.lang.String[] envp, java.io.File dir)
          Constructs a CommandRunner object that will run the specified command.
  CommandRunner(java.lang.String command, java.lang.String[] envp, java.io.File dir)
          Constructs a CommandRunner object that will run the specified command.
 
Method Summary
 java.lang.Object getCommand()
          Retrieves the command to run
 int run()
          Runs the command.
 int run(java.util.logging.Logger logger)
          Runs the command.
 int run(StringList stdoutOutput, StringList stderrOutput)
          Runs the command.
 int run(StringList stdoutOutput, StringList stderrOutput, java.lang.String charSetName)
          Runs the command.
protected  int runAndLog(StringList stdoutOutput, StringList stderrOutput)
          Runs the command, and uses a previously specified logger to log information.
 void setCommand(java.lang.String command)
          Sets the command to run
 void setCommand(java.lang.String[] cmdarray)
          Sets the command to run
 void setDir(java.io.File dir)
          Sets the working directory for the sub-process that is created when the command is run
 void setEnv(java.lang.String[] envp)
          Sets the environment variables that will be passed along and used by the sub-process that is created when the command is run.
 void setLogger(java.util.logging.Logger logger)
          Associated a logger with the CommandRunner object.
 java.lang.String toString()
          Returns a string representation of this class
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

COPYRIGHT

public static final java.lang.String COPYRIGHT
Copyright for class bytecode

See Also:
Constant Field Values
Constructor Detail

CommandRunner

public CommandRunner(java.lang.String command,
                     java.lang.String[] envp,
                     java.io.File dir)
Constructs a CommandRunner object that will run the specified command. The specified environment variables and working directory for the sub-process will be passed along on the Runtime.exec of the specified command.

Parameters:
command - The command to run
envp - An array of environment variables to pass along to the sub-process
dir - The working directory for the sub-process

CommandRunner

public CommandRunner(java.lang.String[] cmdarray,
                     java.lang.String[] envp,
                     java.io.File dir)
Constructs a CommandRunner object that will run the specified command. The specified environment variables and working directory for the sub-process will be passed along on the Runtime.exec of the specified command.

Parameters:
cmdarray - The command to run
envp - An array of environment variables to pass along to the sub-process
dir - The working directory for the sub-process

CommandRunner

public CommandRunner(java.lang.String command)
Constructs a CommandRunner object that will run the specified command.

Parameters:
command - The command to run

CommandRunner

public CommandRunner(java.lang.String[] cmdarray)
Constructs a CommandRunner object that will run the specified command.

Parameters:
cmdarray - The command to run

CommandRunner

protected CommandRunner(java.lang.String[] envp,
                        java.io.File dir)
Constructs a CommandRunner object that will use the specified environment variables and working directory for the sub-process will be passed along on the Runtime.exec of the specified command.

Parameters:
envp - An array of environment variables to pass along to the sub-process
dir - The working directory for the sub-process
Method Detail

getCommand

public java.lang.Object getCommand()
Retrieves the command to run

Returns:
The command to run

setCommand

public void setCommand(java.lang.String command)
Sets the command to run

Parameters:
command - The command to run

setCommand

public void setCommand(java.lang.String[] cmdarray)
Sets the command to run

Parameters:
cmdarray - The command to run

setLogger

public void setLogger(java.util.logging.Logger logger)
Associated a logger with the CommandRunner object. Any logging that exists for the class will be sent to the specified logger.

Parameters:
logger - The logger to associate with the CommandRunner object

setEnv

public void setEnv(java.lang.String[] envp)
Sets the environment variables that will be passed along and used by the sub-process that is created when the command is run.

Parameters:
envp - An array of environment variables to pass along to the sub-process

setDir

public void setDir(java.io.File dir)
Sets the working directory for the sub-process that is created when the command is run

Parameters:
dir - The working directory for the sub-process

run

public int run(StringList stdoutOutput,
               StringList stderrOutput,
               java.lang.String charSetName)
        throws java.io.IOException,
               java.lang.InterruptedException
Runs the command. All standard out and standard error output is consumed and placed into the provided lists. The output is translated according to the specified character set name. If the character set name is null, the character set specified by the file.encoding system property is used to translate the output.

Parameters:
stdoutOutput - All standard out output resulting from the running of the command will be placed in this list
stderrOutput - All standard error output resulting from the running of the command will be placed in this list
charSetName - The name of the character set to use when translating standard out/err output. If no specific character set should be used, null should be passed for this parameter.
Returns:
The exit value for the sub-process where the command was run
Throws:
java.io.IOException - If an I/O exception occurred while trying to run the command
java.lang.InterruptedException - If the current thread is interrupted while waiting for the sub-process to complete

run

public int run(StringList stdoutOutput,
               StringList stderrOutput)
        throws java.io.IOException,
               java.lang.InterruptedException
Runs the command. All standard out and standard error output is consumed and placed into the provided lists. The output is translated according to the character set specified by the file.encoding system property.

Parameters:
stdoutOutput - All standard out output resulting from the running of the command will be placed in this list
stderrOutput - All standard error output resulting from the running of the command will be placed in this list
Returns:
The exit value for the sub-process where the command was run
Throws:
java.io.IOException - If an I/O exception occurred while trying to run the command
java.lang.InterruptedException - If the current thread is interrupted while waiting for the sub-process to complete

run

public int run()
        throws java.io.IOException,
               java.lang.InterruptedException
Runs the command.

Returns:
The exit value for the sub-process where the command was run
Throws:
java.io.IOException - If an I/O exception occurred while trying to run the command
java.lang.InterruptedException - If the current thread is interrupted while waiting for the sub-process to complete

run

public int run(java.util.logging.Logger logger)
        throws java.io.IOException,
               java.lang.InterruptedException
Runs the command. Any logging by the CommandRunner class that occurs during the setup or running of the command is sent to the specified logger.

Parameters:
logger - The logger to log events to
Returns:
The exit value for the sub-process where the command was run
Throws:
java.io.IOException - If an I/O exception occurred while trying to run the command
java.lang.InterruptedException - If the current thread is interrupted while waiting for the sub-process to complete

runAndLog

protected int runAndLog(StringList stdoutOutput,
                        StringList stderrOutput)
                 throws java.io.IOException,
                        java.lang.InterruptedException
Runs the command, and uses a previously specified logger to log information. All standard out and standard error output is consumed and placed into the provided lists. The output is translated according to the character set specified by the file.encoding system property.

Parameters:
stdoutOutput - All standard out output resulting from the running of the command will be placed in this list
stderrOutput - All standard error output resulting from the running of the command will be placed in this list
Returns:
The exit value for the sub-process where the command was run
Throws:
java.io.IOException - If an I/O exception occurred while trying to run the command
java.lang.InterruptedException - If the current thread is interrupted while waiting for the sub-process to complete

toString

public java.lang.String toString()
Returns a string representation of this class

Overrides:
toString in class java.lang.Object
Returns:
A string representation of this class