org.sadun.util
Class CommandRunner

java.lang.Object
  extended byjava.lang.Thread
      extended byorg.sadun.util.CommandRunner
All Implemented Interfaces:
java.lang.Runnable

public class CommandRunner
extends java.lang.Thread

A class to run a command in a separate thread, monitor its progress via a monitor window and being notified of its completion, exit status, and elasped time.

Version:
1.0
Author:
cris

Nested Class Summary
static class CommandRunner.Adapter
          An helper class which implements all the methods in the CommandRunner.Listenerinterface in an empty way.
static interface CommandRunner.Listener
          Classes implementing this interface may receive CommandRunnerevents.
static class CommandRunner.StdOutAdapter
           
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
CommandRunner(java.lang.Class cls, java.lang.String[] args)
          Create a command runner with no parent window, running a class' main() method with the given arguments.
CommandRunner(java.lang.Class cls, java.lang.String[] args, boolean daemon)
          Create a command runner with no parent window, running a class' main() method with the given arguments.
CommandRunner(java.awt.Component parent, java.lang.Class cls, java.lang.String[] args)
          Create a command runner with the given parent window, running a class' main() method with the given arguments.
CommandRunner(java.awt.Component parent, java.lang.Class cls, java.lang.String[] args, boolean daemon)
          Create a command runner with given parent window, running a class' main() method with the given arguments.
CommandRunner(java.awt.Component parent, java.lang.String command, java.lang.String[] args)
          Create a command runner with given parent window, running a system command with the given arguments.
CommandRunner(java.awt.Component parent, java.lang.String command, java.lang.String[] args, boolean daemon)
          Create a command runner with given parent window, running a system command with the given arguments.
CommandRunner(java.lang.String command, java.lang.String[] args)
          Create a command runner with no parent window, running a system command with the given arguments.
CommandRunner(java.lang.String command, java.lang.String[] args, boolean daemon)
          Create a command runner with no parent window, running a system command with the given arguments.
 
Method Summary
 void addListener(CommandRunner.Listener listener)
          Add a listener to the listener set.
 java.lang.String getLastOutput()
          Returns the output of the last execution, or null if either no execution has occurred yet, or isCaptureOutput()is false.
 boolean isCaptureOutput()
          Return true if the runner is set to capture and store the output of the program run.
 boolean isCloseWindowOnTermination()
          Returns the closeWindowOnTermination.
 boolean isOpenWindowOnStartup()
          Returns the openWindowOnStartup.
 boolean isOutputElapsedTime()
          Returns the outputElapsedTime.
 boolean isOutputOnSystemOut()
          Returns the outputOnSystemOut.
static void main(java.lang.String[] args)
           
 void removeListener(CommandRunner.Listener listener)
          Remove a listener to the listener set.
 void run()
          Run the command.
 void setCaptureOutput(boolean captureOutput)
          Set the capture-output property.
 void setCloseWindowOnTermination(boolean closeWindowOnTermination)
          Sets the closeWindowOnTermination.
 void setOpenWindowOnStartup(boolean openWindowOnStartup)
          Sets the openWindowOnStartup.
 void setOutputElapsedTime(boolean outputElapsedTime)
          Sets the outputElapsedTime.
 void setOutputOnSystemOut(boolean outputOnSystemOut)
          Set the output-on-system-out property.
 
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
 

Constructor Detail

CommandRunner

public CommandRunner(java.awt.Component parent,
                     java.lang.String command,
                     java.lang.String[] args,
                     boolean daemon)
Create a command runner with given parent window, running a system command with the given arguments. If used as a thread, the command runner has the given daemon status.

Parameters:
parent - the parent window for the CommandRunner monitor
command - the executable to run
args - the arguments to pass to the executable
daemon - the daemon status of this Thread

CommandRunner

public CommandRunner(java.awt.Component parent,
                     java.lang.Class cls,
                     java.lang.String[] args,
                     boolean daemon)
Create a command runner with given parent window, running a class' main() method with the given arguments. If used as a thread, the command runner has the given daemon status.

Parameters:
parent - the parent window for the CommandRunner monitor
cls - the class whose main method is to be run
args - the arguments to pass to the executable
daemon - the daemon status of this Thread

CommandRunner

public CommandRunner(java.lang.Class cls,
                     java.lang.String[] args,
                     boolean daemon)
Create a command runner with no parent window, running a class' main() method with the given arguments. If used as a thread, the command runner has the given daemon status.

Parameters:
cls - the class whose main method is to be run
args - the arguments to pass to the executable
daemon - the daemon status of this Thread

CommandRunner

public CommandRunner(java.awt.Component parent,
                     java.lang.Class cls,
                     java.lang.String[] args)
Create a command runner with the given parent window, running a class' main() method with the given arguments. If used as a thread, the command runner has daemon status (it terminates if it is the only one thread running).

Parameters:
parent - the parent window for the CommandRunner monitor
cls - the class whose main method is to be run
args - the arguments to pass to the executable

CommandRunner

public CommandRunner(java.lang.Class cls,
                     java.lang.String[] args)
Create a command runner with no parent window, running a class' main() method with the given arguments. If used as a thread, the command runner has daemon status (it terminates if it is the only one thread running).

Parameters:
cls - the class whose main method is to be run
args - the arguments to pass to the executable

CommandRunner

public CommandRunner(java.lang.String command,
                     java.lang.String[] args,
                     boolean daemon)
Create a command runner with no parent window, running a system command with the given arguments. If used as a thread, the command runner has the given daemon status.

Parameters:
command - the executable to run
args - the arguments to pass to the executable
daemon - the daemon status of this Thread

CommandRunner

public CommandRunner(java.awt.Component parent,
                     java.lang.String command,
                     java.lang.String[] args)
Create a command runner with given parent window, running a system command with the given arguments. If used as a thread, the command runner has daemon status (it terminates if it is the only one thread running).

Parameters:
parent - the parent window for the CommandRunner monitor
command - the executable to run
args - the arguments to pass to the executable

CommandRunner

public CommandRunner(java.lang.String command,
                     java.lang.String[] args)
Create a command runner with no parent window, running a system command with the given arguments. If used as a thread, the command runner has daemon status (it terminates if it is the only one thread running).

Parameters:
command - the executable to run
args - the arguments to pass to the executable
Method Detail

addListener

public void addListener(CommandRunner.Listener listener)
Add a listener to the listener set. On run(), each listener is notifyed of events concerning the run via its CommandRunner.Listenerinterface, in arbitrary order.

Parameters:
listener - the CommandRunner.Listener object to add.

removeListener

public void removeListener(CommandRunner.Listener listener)
Remove a listener to the listener set. If the given listener is not in the listeners set, this method does nothing.

Parameters:
listener - the CommandRunner.Listener object to remove.

run

public void run()
Run the command. Depending on the various flag, command output goes to a window or standard output, or both. Each listener registered via addListener is notifyed of events concerning the run via its CommandRunner.Listenerinterface, in arbitrary order.


isCloseWindowOnTermination

public boolean isCloseWindowOnTermination()
Returns the closeWindowOnTermination.

Returns:
boolean

isOpenWindowOnStartup

public boolean isOpenWindowOnStartup()
Returns the openWindowOnStartup.

Returns:
boolean

isOutputOnSystemOut

public boolean isOutputOnSystemOut()
Returns the outputOnSystemOut.

Returns:
boolean

setCloseWindowOnTermination

public void setCloseWindowOnTermination(boolean closeWindowOnTermination)
Sets the closeWindowOnTermination.

Parameters:
closeWindowOnTermination - The closeWindowOnTermination to set

setOpenWindowOnStartup

public void setOpenWindowOnStartup(boolean openWindowOnStartup)
Sets the openWindowOnStartup.

Parameters:
openWindowOnStartup - The openWindowOnStartup to set

setOutputOnSystemOut

public void setOutputOnSystemOut(boolean outputOnSystemOut)
Set the output-on-system-out property. If true, the runner will (also) send the execution output to standard output.

Parameters:
outputOnSystemOut - The outputOnSystemOut to set

isCaptureOutput

public boolean isCaptureOutput()
Return true if the runner is set to capture and store the output of the program run.

Returns:
boolean
See Also:
getLastOutput()

getLastOutput

public java.lang.String getLastOutput()
Returns the output of the last execution, or null if either no execution has occurred yet, or isCaptureOutput()is false.

Returns:
String

setCaptureOutput

public void setCaptureOutput(boolean captureOutput)
Set the capture-output property. If true the output of the last run of the command is stored in memory, and can be retrieved with getLastOutput().

Parameters:
captureOutput - The value to set.

isOutputElapsedTime

public boolean isOutputElapsedTime()
Returns the outputElapsedTime.

Returns:
boolean

setOutputElapsedTime

public void setOutputElapsedTime(boolean outputElapsedTime)
Sets the outputElapsedTime.

Parameters:
outputElapsedTime - The outputElapsedTime to set

main

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