com.massimo.tools.scriptrunner
Class ScriptRunner

java.lang.Object
  extended by java.lang.ThreadGroup
      extended by com.massimo.tools.scriptrunner.ScriptRunner
All Implemented Interfaces:
Runnable, Thread.UncaughtExceptionHandler

public class ScriptRunner
extends ThreadGroup
implements Runnable

Run tasks as specified in an XML file.
This extends ThreadGroup in order to handle uncaught exceptions. To do that, this must run in a new Thread, so Runnable is implemented as well. Extending ThreadGroup also makes waiting for spawned threads easier.
There are no UI concerns here. Main handles the command line interface.


Constructor Summary
ScriptRunner(String fileName, Map<String,String> properties, boolean validate)
          An XML Document is built from the file and the root Task is created by the TaskFactory.
ScriptRunner(Task rootTask)
          Simple constructor.
 
Method Summary
 ExecutionContext getExecutionContext()
          A factory method for the ExecutionContext.
 int getScriptResult()
           
 void run()
          Implement Runnable Creates the ExecutionContext and ensures that the run identifier is set.
 void stopProcessing()
          Attempt to interrupt the root task
 String toString()
           
 void uncaughtException(Thread t, Throwable e)
           Called by JVM when a thread in this group stops because of an uncaught exception.
 void validate()
          Check whether the tasks should be able to run.
 void waitForSpawnedThreads()
          Tasks may be running in spawned Threads.
 
Methods inherited from class java.lang.ThreadGroup
activeCount, activeGroupCount, allowThreadSuspension, checkAccess, destroy, enumerate, enumerate, enumerate, enumerate, getMaxPriority, getName, getParent, interrupt, isDaemon, isDestroyed, list, parentOf, resume, setDaemon, setMaxPriority, stop, suspend
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ScriptRunner

public ScriptRunner(String fileName,
                    Map<String,String> properties,
                    boolean validate)
             throws Exception
An XML Document is built from the file and the root Task is created by the TaskFactory. Any xi:include elements are resolved by XIncluder.

Parameters:
fileName - The XML file name
properties - Command line properties
validate - Whether to use a validating parser or not. A validating parser will check the XML file against the ScriptRunner and XInclude XSD schemas.
Throws:
IOException - if fail to read XML file
ParsingException - if fail to parse XML file
ValidityException - if XML file is not valid
XIncludeException - if XInclude fails
NoIncludeLocationException - if XInclude fails
InclusionLoopException - if XInclude finds an inclusion loop
BadParseAttributeException - if XInclude fails
ClassNotFoundException - if Task class not found
SecurityException - Need to be able to call the current Thread's ClassLoader
NoSuchMethodException - if the constructor signature does not match AbstractTask's constructor
IllegalArgumentException - if neither Task type nor class is set, or the Task class does not implement Task
InstantiationException - if Task class instantiation fails
IllegalAccessException - if an underlying constructor is inaccessible
InvocationTargetException - if an underlying constructor throws an exception
Exception

ScriptRunner

public ScriptRunner(Task rootTask)
Simple constructor. Mostly used by test cases

Parameters:
rootTask -
Method Detail

getExecutionContext

public ExecutionContext getExecutionContext()
                                     throws SRException
A factory method for the ExecutionContext.
Looks for a child connection element with the dataSourceName attribute set to the well-known audit datasource name. If found, the dataSourceClass attribute is used to determine which ExecutionContext to create.
A File context is used if no audit datasource is configured.

Returns:
An ExecutionContext implementation. Defaults to FileExecutionContext.
Throws:
SRException - on failure

getScriptResult

public int getScriptResult()
Returns:
0 for success. Anything else is a failure.

run

public void run()
Implement Runnable Creates the ExecutionContext and ensures that the run identifier is set. Then calls on the root Task to process.

Specified by:
run in interface Runnable

stopProcessing

public void stopProcessing()
Attempt to interrupt the root task


toString

public String toString()
Overrides:
toString in class ThreadGroup
See Also:
ThreadGroup.toString()

uncaughtException

public void uncaughtException(Thread t,
                              Throwable e)
Called by JVM when a thread in this group stops because of an uncaught exception. Override ThreadGroup to log exceptions and stop processing as cleanly as possible.

Specified by:
uncaughtException in interface Thread.UncaughtExceptionHandler
Overrides:
uncaughtException in class ThreadGroup
Parameters:
t - the thread that is about to exit.
e - the uncaught exception.

validate

public void validate()
Check whether the tasks should be able to run. Calls root Task to validate() and reports. Checks for duplicate names that may confuse re-start checking.


waitForSpawnedThreads

public void waitForSpawnedThreads()
Tasks may be running in spawned Threads. These will all be in this ThreadGroup. Wait to join any alive, non-daemon threads.