com.massimo.tools.scriptrunner
Class AbstractTask

java.lang.Object
  extended by com.massimo.tools.scriptrunner.AbstractTask
All Implemented Interfaces:
Task
Direct Known Subclasses:
AbstractCompoundTask, AbstractGenerator, DataSourcesTask, PauseTask, PrintTask, ProcessTask, PropertiesTask, SleepTask, SQLTask, TemplateTask, ThreadsTask, XSLTask

public abstract class AbstractTask
extends Object
implements Task

Abstract implementation of the Task interface, handling much of the housekeeping.

This class introduces and handles the XML attributes:

Task.process(ExecutionContext) is replaced by the sequence:
  1. preProcess(ExecutionContext)
  2. doProcess(ExecutionContext)
  3. postProcess(ExecutionContext, int)
Task.validate(ValidationContext) is replaced by doValidate(ValidationContext).

Note that any Task created by the TaskFactory requires a constructor that matches the signature of the AbstractTask constructor, AbstractTask(Task, String, Element, Map).


Field Summary
protected  Element definition
          The task defining XML
protected  String name
          The Task's unique name
protected  Task parent
          The parent of this.
protected  Map<String,String> properties
          Populated with both task element and child connection attributes
protected  String type
          This Task's type
 
Fields inherited from interface com.massimo.tools.scriptrunner.Task
DATA_ERROR, ERROR, FINISHED, INTERRUPTED, PROMPT, RUNNING, START, SUCCESS
 
Constructor Summary
protected AbstractTask(Task parent, String type, Element definition, Map<String,String> parentProperties)
          Basic constructor, as used by TaskFactory.
 
Method Summary
protected abstract  int doProcess(ExecutionContext context)
          Implementing classes only need to do their own work and log progress when appropriate.
protected abstract  int doValidate(ValidationContext context)
          Check whether this task should be able to run.
protected  boolean getBooleanProperty(String propertyName, boolean defaultValue)
          Convenience method
protected  String getCurrentWorkingDirectory()
          Used for bind or parameter substitution.
 Element getDefinition()
          
protected  int getIntProperty(String propertyName, int defaultValue)
          Convenience method
protected abstract  org.apache.log4j.Logger getLogger()
           
 String getName()
          
 Task getParent()
          
 Map<String,String> getProperties()
          
protected  int getStartProcessingRow()
          The field returned, startProcessingRow, is set by process(ExecutionContext) is log-progress is true.
protected  String getText()
          The XOM Node.getValue() method returns the XPath 1.0 string-value of Node.
 String getType()
          
 void interrupt()
          Tells the Task to stop processing.
protected  boolean isIgnoreableError(int errorCode)
           
protected  boolean isIgnoreableError(String errorCode)
           
 boolean isInterrupted()
           
protected  boolean isLogProgress()
           
protected  void postProcess(ExecutionContext context, int result)
          Run after doProcess() and result has been checked.
protected  void preProcess(ExecutionContext context)
          Run immendiately before doProcess() Default implementation: do nothing
 int process(ExecutionContext context)
          The central work method.
static String resultCodeToString(int resultCode)
           
protected  String substituteParameters(String text)
          Uses MessageFormat to substitute parameter values into text based on position.
 String toString()
           
 int validate(ValidationContext context)
          Check whether this task should be able to run.
protected  void validateConnections(ValidationContext context)
          Validate any child connection elements
protected  int validateParameters(ValidationContext context, String text)
          Check MessageFormat parameter values exist for all formats in text.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

definition

protected Element definition
The task defining XML


name

protected String name
The Task's unique name


parent

protected Task parent
The parent of this. Null for the root Task.


properties

protected Map<String,String> properties
Populated with both task element and child connection attributes


type

protected String type
This Task's type

Constructor Detail

AbstractTask

protected AbstractTask(Task parent,
                       String type,
                       Element definition,
                       Map<String,String> parentProperties)
Basic constructor, as used by TaskFactory.
Reads and processes the XML attributes and elements: name, properties, log-progress, and ignore-error-codes.

Parameters:
parent - A parent Task, or null for the root Task.
type - The type of task, e.g. SQL, Print, Serial
definition - Task to execute, including new connection properties.
parentProperties -
Method Detail

doProcess

protected abstract int doProcess(ExecutionContext context)
                          throws Exception
Implementing classes only need to do their own work and log progress when appropriate.

Parameters:
context -
Returns:
O for success. Anything else is a failure.
Throws:
Exception
See Also:
process(ExecutionContext)

doValidate

protected abstract int doValidate(ValidationContext context)
                           throws Exception
Check whether this task should be able to run. Implementations should check for the prescence of files, attributes,...

Parameters:
context -
Returns:
O for success. Anything else is a failure.
Throws:
Exception

getBooleanProperty

protected boolean getBooleanProperty(String propertyName,
                                     boolean defaultValue)
Convenience method

Parameters:
propertyName -
defaultValue -
Returns:
boolean

getCurrentWorkingDirectory

protected final String getCurrentWorkingDirectory()
Used for bind or parameter substitution.

Returns:
The current working directory, derived from the base URI of the defining XML.

getDefinition

public Element getDefinition()

Specified by:
getDefinition in interface Task
Returns:
The XML Element defining the Task

getIntProperty

protected int getIntProperty(String propertyName,
                             int defaultValue)
Convenience method

Parameters:
propertyName -
defaultValue -
Returns:
int

getLogger

protected abstract org.apache.log4j.Logger getLogger()
Returns:
Class logger

getName

public String getName()

Specified by:
getName in interface Task
Returns:
Task name

getParent

public Task getParent()

Specified by:
getParent in interface Task
Returns:
The parent Task or null for the root task

getProperties

public Map<String,String> getProperties()

Specified by:
getProperties in interface Task
Returns:
Map of properties known by this Task

getStartProcessingRow

protected final int getStartProcessingRow()
The field returned, startProcessingRow, is set by process(ExecutionContext) is log-progress is true.

Returns:
The row to start, or re-start, processing at.

getText

protected final String getText()
The XOM Node.getValue() method returns the XPath 1.0 string-value of Node. This includes all text inside child elements, which is not quite what is needed. The text specific to this element is in one or more Text nodes.

Returns:
Concatenated XML Text nodes for the task

getType

public String getType()

Specified by:
getType in interface Task
Returns:
Task type

interrupt

public void interrupt()
Tells the Task to stop processing. This does not garrantee that the task will return immediately.

Specified by:
interrupt in interface Task

isIgnoreableError

protected final boolean isIgnoreableError(int errorCode)
Parameters:
errorCode -
Returns:
Can we ignore errorCode?

isIgnoreableError

protected final boolean isIgnoreableError(String errorCode)
Parameters:
errorCode -
Returns:
Can we ignore errorCode?

isInterrupted

public boolean isInterrupted()
Returns:
Has this task been interrupted?

isLogProgress

protected final boolean isLogProgress()
Returns:
Should this maintain the audit trail?

postProcess

protected void postProcess(ExecutionContext context,
                           int result)
Run after doProcess() and result has been checked. Default implementation: do nothing

Parameters:
context -
result -

preProcess

protected void preProcess(ExecutionContext context)
Run immendiately before doProcess() Default implementation: do nothing

Parameters:
context -

process

public int process(ExecutionContext context)
            throws Exception
The central work method.
Acts as a wrapper for sub-classes' doProcess(ExecutionContext) in order to do housekeeping.
Checks ExecutionContext to see whether this task should execute.
Handles logging of start and final result, including when interrupted.
Handles locally-set class paths.
Checks for ignore-able error results and resets to success when appropriate.
Executes the pre- and post-process hooks.

Specified by:
process in interface Task
Parameters:
context - A concrete ExecutionContext
Returns:
O for success. Anything else is a failure.
Throws:
Exception

resultCodeToString

public static final String resultCodeToString(int resultCode)
Parameters:
resultCode -
Returns:
String representation of the result code

substituteParameters

protected final String substituteParameters(String text)
Uses MessageFormat to substitute parameter values into text based on position.

Parameters:
text -
Returns:
text with substitutions
Throws:
IllegalArgumentException - if a parameter value is not found.

toString

public String toString()
Overrides:
toString in class Object

validate

public int validate(ValidationContext context)
Check whether this task should be able to run.
Implementations should check for the prescence of files, parameters, bind variables,... Each object is responsible for adding itself to the context and reporting errors through the context.
Acts as a wrapper for doValidate() in order to do housekeeping.
Handles adding task to the heirachy and the failed error message.
Handles locally-set class paths.

Specified by:
validate in interface Task
Parameters:
context - A concrete ValidationContext
Returns:
O for success. Anything else is a failure.

validateConnections

protected void validateConnections(ValidationContext context)
Validate any child connection elements

Parameters:
context -

validateParameters

protected final int validateParameters(ValidationContext context,
                                       String text)
Check MessageFormat parameter values exist for all formats in text.

Parameters:
context -
text -
Returns:
O for success. Anything else is a failure.