|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.schmant.support.AbstractArgumentChecker
org.schmant.task.stub.AbstractTask<T>
public abstract class AbstractTask<T extends TaskSpecification<T>>
This is an abstract base class that task implementations are encouraged to inherit (either directly or through any of this class' subclasses). It implements all run control logic for tasks.
This class implements the run()
method defined by the TaskRunnable
interface. Subclasses must implement an runInternal(Report)
method. They may also choose to override other methods implemented by this
class. See the documentation below for details.
This object also implements TaskSpecification
, making all inheriting
task objects their own specification objects too.
Constructor Summary | |
---|---|
protected |
AbstractTask()
Create the task. |
Method Summary | |
---|---|
boolean |
cancel(boolean mayInterruptIfRunning)
Cancel a task that has not been run or is running. |
void |
copyProperties(T spec)
Copy the properties to the target object. |
Task |
createTask()
Create a task from this specification object. |
protected String |
getDefaultLogFooter()
Get the default log footer to log after the task has been successfully run if no other log footer is set. |
protected String |
getDefaultLogHeader()
Get the default log header to log (to level INFO) before the task is run if the task user has not set a custom header. |
TaskDependency |
getDependencyForTasksScheduledByThisTask()
This implementation returns this . |
Observable |
getDependencyObservable()
Get an Observable that will notify all observers when this task
has been run. |
Throwable |
getFailureCause()
If the task failed, this method can be used to retrieve the failure cause. |
protected TwoState<String> |
getLogFooter()
Get the log footer property. |
protected TwoState<String> |
getLogHeader()
Get the log header property. |
Lock |
getSatisfiedStateLock()
Get the Lock that protects the dependency's satisfied
property. |
protected T |
getThis()
Get this cast to its actual type. |
boolean |
isCancelled()
Is the task canceled? |
boolean |
isDone()
Has this task been run? |
boolean |
isInterrupted()
Was the task interrupted? |
boolean |
isRunning()
Is the task currently running? |
boolean |
isSatisfied()
Is this dependency satisfied? |
boolean |
isSuccessful()
Was the task successful? (I.e: did it not throw an exception?) |
Task |
run()
This method does the following: Validates the task (see validate(Report) )
Runs it (see runInternal(Report) )
In case of errors from the runInternal(Report) method, this
method logs them and rethrows them as SchmantException :s. |
protected abstract void |
runInternal(Report r)
Subclasses implement this. |
T |
setLogFooter(String s)
Set the task's log footer (the message that is logged to level info after the task has completed successfully). |
T |
setLogHeader(String s)
Set the task's log header (the message that is logged to level info before the task is run). |
T |
setReportLevel(Level l)
Set the Report level to use when running tasks
created by this factory. |
T |
setTraceLogging(boolean b)
Enable or disable trace logging for tasks created by this factory. |
String |
toString()
Default implementation for tasks. |
protected Collection<String> |
validate(Report r)
This method may be overridden by subclasses to validate their configuration (including their TaskFactory :s) before the task is
run. |
Methods inherited from class org.schmant.support.AbstractArgumentChecker |
---|
check, check, check, check |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
protected AbstractTask()
This is declared protected. Tasks objects should be created from their factories.
Method Detail |
---|
protected final T getThis()
this
cast to its actual type. This is useful for returning
the right type of this
when implementing setter methods that
return the called object.
this
cast to its actual type.public final Observable getDependencyObservable()
Observable
that will notify all observers when this task
has been run.
getDependencyObservable
in interface TaskDependency
Observable
to observe.public final T setLogHeader(String s)
LoggingConfigurable
info
before the task is run). If no log header is set, the default log
header from getDefaultLogHeader()
is used.
setLogHeader
in interface LoggingConfigurable<T extends TaskSpecification<T>>
s
- The log header. Set to null
to not log a header.
this
protected final TwoState<String> getLogHeader()
public final T setLogFooter(String s)
LoggingConfigurable
info
after the task has completed successfully). Set to null
to
disable the footer.
setLogFooter
in interface LoggingConfigurable<T extends TaskSpecification<T>>
s
- The footer to log.
this
protected final TwoState<String> getLogFooter()
public final T setTraceLogging(boolean b)
LoggingConfigurable
Task configuration is trace logged if trace logging is enabled for the task itself or for its task factory.
setTraceLogging
in interface LoggingConfigurable<T extends TaskSpecification<T>>
b
- Set to true
to enable trace logging.
this
.public final T setReportLevel(Level l)
LoggingConfigurable
Report
level to use when running tasks
created by this factory.
setReportLevel
in interface LoggingConfigurable<T extends TaskSpecification<T>>
l
- The report level. If null
, the report level will not be
modified.
this
.public final boolean isDone()
Task
isDone
in interface Task
true
if this task has been run.public final boolean isSuccessful()
Task
isSuccessful
in interface Task
true
if the task has been successfully run.public final boolean isSatisfied()
TaskDependency
Implementation note: This method should be protected by the same
lock that can be retrieved by calling TaskDependency.getSatisfiedStateLock()
.
isSatisfied
in interface TaskDependency
true
if the dependency is satisfied.public final boolean cancel(boolean mayInterruptIfRunning)
Task
cancel
in interface Task
mayInterruptIfRunning
- Should the task be interrupted if it is
running?public final boolean isCancelled()
Task
isCancelled
in interface Task
true
if the task was canceled.public final boolean isInterrupted()
Task
isInterrupted
in interface Task
true
if the task was interrupted.public final boolean isRunning()
Task
isRunning
in interface Task
true
if the task has been successfully run.public final Throwable getFailureCause()
Task
getFailureCause
in interface Task
public final Lock getSatisfiedStateLock()
TaskDependency
Lock
that protects the dependency's satisfied
property. This lock must be locked before the dependency's satisfied
state can be update.
An external class can lock this to ensure that the satisfied state is not updated while it holds the lock.
getSatisfiedStateLock
in interface TaskDependency
TaskDependency.isSatisfied()
public TaskDependency getDependencyForTasksScheduledByThisTask()
this
. Subclasses that use
TaskExecutor
:s must override this method to
return a dependency object for all of its scheduled tasks.
getDependencyForTasksScheduledByThisTask
in interface Task
this
protected Collection<String> validate(Report r)
TaskFactory
:s) before the task is
run.
Validation errors are collected as strings. If this method is overridden,
always call super.validate
.
r
- A report to log warning and debug messages to. Validation errors
should not be logged, though. They are logged later.
protected String getDefaultLogHeader()
This is called after validating the task.
Return null
to disable logging.
protected String getDefaultLogFooter()
null
(= no logging). Subclasses may override this to return something
else.
null
protected abstract void runInternal(Report r) throws Exception
validate(Report)
:d before
this method is called, see run()
.
r
- The current thread's Report
.
Exception
- On errors.public final Task run()
validate(Report)
)runInternal(Report)
)runInternal(Report)
method, this
method logs them and rethrows them as SchmantException
:s.
run
in interface TaskRunnable
public final Task createTask()
TaskSpecification
This method is called by the task factory. After calling this method, the task factory does not modify the specification any more.
createTask
in interface TaskSpecification<T extends TaskSpecification<T>>
this
.public void copyProperties(T spec)
Subclasses must override this to copy their properties. The
overriding method must call super.copyProperties
.
copyProperties
in interface TaskSpecification<T extends TaskSpecification<T>>
spec
- The specification to copy properties to.public String toString()
This implementation just returns the class name.
toString
in class Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |