Class Workflow
- All Implemented Interfaces:
Serializable
public class Workflow extends Object implements Serializable
Sequence of Step objects linked together. Workflows are always initialized with a message key that denotes the name of the
Workflow, and a Principal that represents its owner.
Workflow lifecycle
A Workflow's state (obtained bygetCurrentState()) will be one of the following:
CREATED: after the Workflow has been instantiated, but before it has been started using thestart()method.RUNNING: after the Workflow has been started using thestart()method, but before it has finished processing all Steps. Note that a Workflow can only be started once; attempting to start it again results in an IllegalStateException. Callers can place the Workflow into the WAITING state by callingwaitstate().WAITING: when the Workflow has temporarily paused, for example because of a pending Decision. Once the responsible actor decides what to do, the caller can change the Workflow back to the RUNNING state by calling therestart()method (this is done automatically by the Decision class, for instance, when theDecision.decide(Outcome)method is invoked)COMPLETED: after the Workflow has finished processing all Steps, without errors.ABORTED: if a Step has elected to abort the Workflow.
Steps and processing algorithm
Workflow Step objects can be of type Decision, Task or other Step subclasses. Decisions require user input, while Tasks
do not. See the Step class for more details.
After instantiating a new Workflow (but before telling it to start()), calling classes should specify the first Step by
executing the setFirstStep(Step) method. Additional Steps can be chained by invoking the first step's
Step.addSuccessor(Outcome, Step) method.
When a Workflow's start method is invoked, the Workflow retrieves the first Step and processes it. This Step, and subsequent
ones, are processed as follows:
- The Step's
Step.start()method executes, which sets the start time. - The Step's
Step.execute()method is called to begin processing, which will return an Outcome to indicate completion, continuation or errors: Outcome.STEP_COMPLETEindicates that the execution method ran without errors, and that the Step should be considered "completed."Outcome.STEP_CONTINUEindicates that the execution method ran without errors, but that the Step is not "complete" and should be put into the WAITING state.Outcome.STEP_ABORTindicates that the execution method encountered errors, and should abort the Step and the Workflow as a whole. When this happens, the Workflow will set the current Step's Outcome toOutcome.STEP_ABORTand invoke the Workflow'sabort()method. The Step's processing errors, if any, can be retrieved byStep.getErrors().- The Outcome of the
executemethod also affects what happens next. Depending on the result (and assuming the Step did not abort), the Workflow will either move on to the next Step or put the Workflow into theWAITINGstate: - If the Outcome denoted "completion" (i.e., its
Step.isCompleted()method returnstrue) then the Step is considered complete; the Workflow looks up the next Step by calling the current Step'sStep.getSuccessor(Outcome)method. Ifsuccessor()returns a non-nullStep, the return value is marked as the current Step and added to the Workflow's Step history. Ifsuccessor()returnsnull, then the Workflow has no more Steps and it enters theCOMPLETEDstate. - If the Outcome did not denote "completion" (i.e., its
Step.isCompleted()method returnsfalse), then the Step still has further work to do. The Workflow enters theWAITINGstate and stops further processing until a caller restarts it.
The currently executing Step can be obtained by getCurrentStep(). The actor for the current Step is returned by
getCurrentActor().
To provide flexibility for specific implementations, the Workflow class provides two additional features that enable Workflow participants (i.e., Workflow subclasses and Step/Task/Decision subclasses) to share context and state information. These two features are named attributes and message arguments:
- Named attributes are simple key-value pairs that Workflow participants can get or set. Keys are Strings; values
can be any Object. Named attributes are set with
setAttribute(String, Serializable)and retrieved withgetAttribute(String). - Message arguments are used in combination with JSPWiki's
InternationalizationManagerto create language-independent user interface messages. The message argument array is retrieved viagetMessageArguments(); the first two array elements will always be these: a String representing work flow owner's name, and a String representing the current actor's name. Workflow participants can add to this array by invokingaddMessageArgument(Serializable).
Example
Workflow Steps can be very powerful when linked together. JSPWiki provides two abstract subclasses classes that you can use to build your own Workflows: Tasks and Decisions. As noted, Tasks are Steps that execute without user intervention, while Decisions require actors (aka Principals) to take action. Decisions and Tasks can be mixed freely to produce some highly elaborate branching structures.
Here is a simple case. For example, suppose you would like to create a Workflow that (a) executes a initialization Task, (b) pauses to obtain an approval Decision from a user in the Admin group, and if approved, (c) executes a "finish" Task. Here's sample code that illustrates how to do it:
// Create workflow; owner is current user
1 Workflow workflow = new Workflow( " workflow.myworkflow ", context.getCurrentUser() );
// Create custom initialization task
2 Step initTask = new InitTask( this );
// Create finish task
3 Step finishTask = new FinishTask( this );
// Create an intermediate decision step
4 Principal actor = new GroupPrincipal( "Admin" );
5 Step decision = new SimpleDecision( this, "decision.AdminDecision", actor );
// Hook the steps together
6 initTask.addSuccessor( Outcome.STEP_COMPLETE, decision );
7 decision.addSuccessor( Outcome.DECISION_APPROVE, finishTask );
// Set workflow's first step
8 workflow.setFirstStep( initTask );
Some comments on the source code:
- Line 1 instantiates the workflow with a sample message key and designated owner Principal, in this case the current wiki user
- Lines 2 and 3 instantiate the custom Task subclasses, which contain the business logic
- Line 4 creates the relevant GroupPrincipal for the
Admingroup, who will be the actor in the Decision step - Line 5 creates the Decision step, passing the Workflow, sample message key, and actor in the constructor
- Line 6 specifies that if the InitTask's Outcome signifies "normal completion" (STEP_COMPLETE), the SimpleDecision step should be invoked next
- Line 7 specifies that if the actor (anyone possessing the
AdminGroupPrincipal) selects DECISION_APPROVE, the FinishTask step should be invoked - Line 8 adds the InitTask (and all of its successor Steps, nicely wired together) to the workflow
- See Also:
- Serialized Form
-
Field Summary
Fields Modifier and Type Field Description static intABORTEDState value: Workflow aborted before completion.static intCOMPLETEDState value: Workflow completed all Steps without errors.static intCREATEDState value: Workflow instantiated, but not started.static intID_NOT_SETID value: the workflow ID has not been set.static intRUNNINGState value: Workflow started, and is running.static intWAITINGState value: Workflow paused, typically because a Step returned an Outcome that doesn't signify "completion." -
Constructor Summary
Constructors Constructor Description Workflow(String messageKey, Principal owner)Constructs a new Workflow object with a supplied message key, owner Principal, and undefined unique identifierID_NOT_SET. -
Method Summary
Modifier and Type Method Description voidabort()Aborts the Workflow by setting the current Step's Outcome toOutcome.STEP_ABORT, and the Workflow's overall state toABORTED.voidaddMessageArgument(Serializable obj)Appends a message argument object to the array returned bygetMessageArguments().protected voidcleanup()Clears the attribute map and sets the current step field tonull.protected voidcomplete()Protected helper method that changes the Workflow's state toCOMPLETEDand sets the current Step tonull.ObjectgetAttribute(String attr)Retrieves a named Object associated with this Workflow.Map<String,Serializable>getAttributes()Retrieves workflow's attributes.PrincipalgetCurrentActor()Returns the actor Principal responsible for the current Step.intgetCurrentState()StepgetCurrentStep()Returns the current Step, ornullif the workflow has not started or already completed.DategetEndTime()The end time for this Workflow, expressed as a system time number.List<Step>getHistory()Returns a Step history for this Workflow as a List, chronologically, from the first Step to the currently executing one.intgetId()Returns the unique identifier for this Workflow.Serializable[]getMessageArguments()Returns an array of message arguments, used byMessageFormatto create localized messages.StringgetMessageKey()Returns an i18n message key for the name of this workflow; for example,workflow.saveWikiPage.PrincipalgetOwner()The owner Principal on whose behalf this Workflow is being executed; that is, the user who created the workflow.StepgetPreviousStep()Convenience method that returns the predecessor of the current Step.DategetStartTime()The start time for this Workflow, expressed as a system time number.booleanisAborted()Returnstrueif the workflow had been previously aborted.booleanisCompleted()Determines whether this Workflow is completed; that is, if it has no additional Steps to perform.booleanisStarted()Determines whether this Workflow has started; that is, itsstart()method has been executed.protected SteppreviousStep(Step step)Protected method that returns the predecessor for a supplied Step.protected voidprocessCurrentStep()Protected method that processes the current Step by callingStep.execute().voidrestart()voidsetAttribute(String attr, Serializable obj)Temporarily associates an object with this Workflow, as a named attribute, for the duration of workflow execution.voidsetFirstStep(Step step)Sets the first Step for this Workflow, which will be executed immediately after thestart()method executes.voidsetId(int id)Sets the unique identifier for this Workflow.voidstart()Starts the Workflow and sets the state toRUNNING.voidwaitstate()Sets the Workflow in theWAITINGstate.
-
Field Details
-
ID_NOT_SET
ID value: the workflow ID has not been set.- See Also:
- Constant Field Values
-
COMPLETED
State value: Workflow completed all Steps without errors.- See Also:
- Constant Field Values
-
ABORTED
State value: Workflow aborted before completion.- See Also:
- Constant Field Values
-
WAITING
State value: Workflow paused, typically because a Step returned an Outcome that doesn't signify "completion."- See Also:
- Constant Field Values
-
RUNNING
State value: Workflow started, and is running.- See Also:
- Constant Field Values
-
CREATED
State value: Workflow instantiated, but not started.- See Also:
- Constant Field Values
-
-
Constructor Details
-
Workflow
Constructs a new Workflow object with a supplied message key, owner Principal, and undefined unique identifierID_NOT_SET. Once instantiated the Workflow is considered to be in theCREATEDstate; a caller must explicitly invoke thestart()method to begin processing.- Parameters:
messageKey- the message key used to construct a localized workflow name, such asworkflow.saveWikiPageowner- the Principal who owns the Workflow. Typically, this is the user who created and submitted it
-
-
Method Details
-
abort
Aborts the Workflow by setting the current Step's Outcome toOutcome.STEP_ABORT, and the Workflow's overall state toABORTED. It also appends the aborted Step into the workflow history, and sets the current step tonull. If the Step is a Decision, it is removed from the DecisionQueue. This method can be called at any point in the lifecycle prior to completion, but it cannot be called twice. It finishes by calling thecleanup()method to flush retained objects. If the Workflow had been previously aborted, this method throws an IllegalStateException. -
addMessageArgument
Appends a message argument object to the array returned bygetMessageArguments(). The object must be an type used by theMessageFormat: String, Date, or Number (BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short). If the object is not of type String, Number or Date, this method throws an IllegalArgumentException.- Parameters:
obj- the object to add
-
getCurrentActor
Returns the actor Principal responsible for the current Step. If there is no current Step, this method returnsnull.- Returns:
- the current actor
-
getCurrentState
- Returns:
- the workflow state
-
getCurrentStep
Returns the current Step, ornullif the workflow has not started or already completed.- Returns:
- the current step
-
getAttribute
Retrieves a named Object associated with this Workflow. If the Workflow has completed or aborted, this method always returnsnull.- Parameters:
attr- the name of the attribute- Returns:
- the value
-
getAttributes
Retrieves workflow's attributes.- Returns:
- workflow's attributes.
-
getEndTime
The end time for this Workflow, expressed as a system time number. This value is equal to the end-time value returned by the final Step'sStep.getEndTime()method, if the workflow has completed. Otherwise, this method returnsStep.TIME_NOT_SET.- Returns:
- the end time
-
getId
Returns the unique identifier for this Workflow. If not set, this method returns ID_NOT_SET (0).- Returns:
- the unique identifier
-
getMessageArguments
Returns an array of message arguments, used by
MessageFormatto create localized messages. The first two array elements will always be these:- String representing the name of the workflow owner (i.e.,
getOwner()) - String representing the name of the current actor (i.e.,
getCurrentActor()). If the current step isnullbecause the workflow hasn't started or has already finished, the value of this argument will be a dash character (-)
Workflow and Step subclasses are free to append items to this collection with
addMessageArgument(Serializable).- Returns:
- the array of message arguments
- String representing the name of the workflow owner (i.e.,
-
getMessageKey
Returns an i18n message key for the name of this workflow; for example,workflow.saveWikiPage.- Returns:
- the name
-
getOwner
The owner Principal on whose behalf this Workflow is being executed; that is, the user who created the workflow.- Returns:
- the name of the Principal who owns this workflow
-
getStartTime
The start time for this Workflow, expressed as a system time number. This value is equal to the start-time value returned by the first Step'sStep.getStartTime()method, if the workflow has started already. Otherwise, this method returnsStep.TIME_NOT_SET.- Returns:
- the start time
-
getHistory
Returns a Step history for this Workflow as a List, chronologically, from the first Step to the currently executing one. The first step is the first item in the array. If the Workflow has not started, this method returns a zero-length array.- Returns:
- an array of Steps representing those that have executed, or are currently executing
-
isAborted
Returnstrueif the workflow had been previously aborted.- Returns:
- the result
-
isCompleted
Determines whether this Workflow is completed; that is, if it has no additional Steps to perform. If the last Step in the workflow is finished, this method will returntrue.- Returns:
trueif the workflow has been started but has no more steps to perform;falseif not.
-
isStarted
Determines whether this Workflow has started; that is, itsstart()method has been executed.- Returns:
trueif the workflow has been started;falseif not.
-
getPreviousStep
Convenience method that returns the predecessor of the current Step. This method simply examines the Workflow history and returns the second-to-last Step.- Returns:
- the predecessor, or
nullif the first Step is currently executing
-
restart
Restarts the Workflow from theWAITINGstate and puts it into theRUNNINGstate again. If the Workflow had not previously been paused, this method throws an IllegalStateException. If any of the Steps in this Workflow throw a WikiException, the Workflow will abort and propagate the exception to callers.- Throws:
WikiException- if the current task'sAbstractStep.execute()method throws an exception
-
setAttribute
Temporarily associates an object with this Workflow, as a named attribute, for the duration of workflow execution. The passed object can be anything required by an executing Step, although it should be serializable. Note that when the workflow completes or aborts, all attributes will be cleared.- Parameters:
attr- the attribute nameobj- the value
-
setFirstStep
Sets the first Step for this Workflow, which will be executed immediately after thestart()method executes. Note than the Step is not marked as the "current" step or added to the Workflow history until thestart()method is called.- Parameters:
step- the first step for the workflow
-
setId
Sets the unique identifier for this Workflow.- Parameters:
id- the unique identifier
-
start
Starts the Workflow and sets the state toRUNNING. If the Workflow has already been started (or previously aborted), this method returns an IllegalStateException. If any of the Steps in this Workflow throw a WikiException, the Workflow will abort and propagate the exception to callers.- Throws:
WikiException- if the current Step'sStep.start()method throws an exception of any kind
-
waitstate
-
cleanup
Clears the attribute map and sets the current step field tonull. -
complete
-
previousStep
Protected method that returns the predecessor for a supplied Step.- Parameters:
step- the Step for which the predecessor is requested- Returns:
- its predecessor, or
nullif the first Step was supplied.
-
processCurrentStep
Protected method that processes the current Step by callingStep.execute(). If theexecutethrows an exception, this method will propagate the exception immediately to callers without aborting.- Throws:
WikiException- if the current Step'sStep.start()method throws an exception of any kind
-