com.bea.adapter.util
Class CommandRunner

java.lang.Object
  extended byjava.lang.Thread
      extended bycom.bea.adapter.util.CommandRunner
All Implemented Interfaces:
Runnable

public class CommandRunner
extends Thread

Helper class for executing a command via a Process obtained by the Runtime object in a thread. The main value-add of this class is to allow for setting a maximum time to wait for the process to return and if the max is exceeded, handle the error reporting gracefully. Here's how to use the CommandRunner to compile a Java file using "javac":

 CommandRunner runner = new CommandRunner("javac HelloWorld.java");
 runner.start();
 if (!CommandRunner.SUCCESS.equals(runner.getExitValue())) {
   throw new IllegalStateException(runner.getErrorMessage());
 }
 


Field Summary
static long DEFAULT_MAX_WAIT
          The default max wait period for waiting for a process to complete.
static String EXCEPTION
          Possible return value from getExitValue; indicates that an exception was encountered while executing the command, such as the executable was not in the user's path.
static String SUCCESS
          Possible return value from getExitValue; indicates that the external process returned 0 as an exit value.
static String TIMEOUT
          Possible return value from getExitValue; indicates the process did NOT return an exit value in the maximum allowable time period, perhaps the external process is hung.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
CommandRunner(String strCommand)
          Constructs an instance to execute a command and wait a maximum of 10 seconds for it to finish.
CommandRunner(String strCommand, long lMaxWait)
          Constructs an instance to execute a command and wait a maximum of lMaxWait milliseconds for it to finish.
 
Method Summary
 String getCommand()
           
 String getErrorMessage()
           
 String getExitValue()
          Returns the exit value from executing the command: CommandRunner.TIMEOUT indicates that the process timed-out, CommandRunner.SUCCESS indicates the process returned 0, CommandRunner.EXCEPTION indicates an exception was encountered while executing the command.
 void run()
          Executes the command established in the constructor.
 
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, 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

EXCEPTION

public static final String EXCEPTION
Possible return value from getExitValue; indicates that an exception was encountered while executing the command, such as the executable was not in the user's path.

See Also:
Constant Field Values

TIMEOUT

public static final String TIMEOUT
Possible return value from getExitValue; indicates the process did NOT return an exit value in the maximum allowable time period, perhaps the external process is hung.

See Also:
Constant Field Values

SUCCESS

public static final String SUCCESS
Possible return value from getExitValue; indicates that the external process returned 0 as an exit value.

See Also:
Constant Field Values

DEFAULT_MAX_WAIT

public static final long DEFAULT_MAX_WAIT
The default max wait period for waiting for a process to complete.

See Also:
Constant Field Values
Constructor Detail

CommandRunner

public CommandRunner(String strCommand)
Constructs an instance to execute a command and wait a maximum of 10 seconds for it to finish.

Parameters:
strCommand - - command to execute

CommandRunner

public CommandRunner(String strCommand,
                     long lMaxWait)
Constructs an instance to execute a command and wait a maximum of lMaxWait milliseconds for it to finish.

Parameters:
strCommand - - command to execute
lMaxWait - - maximum number of milliseconds to wait until the command returns a value.
Method Detail

getExitValue

public String getExitValue()
Returns the exit value from executing the command: CommandRunner.TIMEOUT indicates that the process timed-out, CommandRunner.SUCCESS indicates the process returned 0, CommandRunner.EXCEPTION indicates an exception was encountered while executing the command.

Returns:
a String indicating the outcome of the command.

getCommand

public String getCommand()
Returns:
the command that this instance executes.

getErrorMessage

public String getErrorMessage()
Returns:
a String of information read from the process' error stream.

run

public void run()
Executes the command established in the constructor.