Class Flow
- All Implemented Interfaces:
Annotated
,FlowDefinition
A simple Flow definition could do nothing more than execute an action and display a view all in one request. A more elaborate Flow definition may be long-lived and execute across a series of requests, invoking many possible paths, actions, and subflows.
Especially in Intranet applications there are often "controlled navigations" where the user is not free to do what he or she wants but must follow the guidelines provided by the system to complete a process that is transactional in nature (the quintessential example would be a 'checkout' flow of a shopping cart application). This is a typical use case appropriate to model as a flow.
Structurally a Flow is composed of a set of states. A State
is a point in a flow where a behavior is
executed; for example, showing a view, executing an action, spawning a subflow, or terminating the flow. Different
types of states execute different behaviors in a polymorphic fashion.
Each TransitionableState
type has one or more transitions that when executed move a flow to another state.
These transitions define the supported paths through the flow.
A state transition is triggered by the occurrence of an event. An event is something that happens the flow should respond to, for example a user input event like ("submit") or an action execution result event like ("success"). When an event occurs in a state of a Flow that event drives a state transition that decides what to do next.
Each Flow has exactly one start state. A start state is simply a marker noting the state executions of this Flow definition should start in. The first state added to the flow will become the start state by default.
Flow definitions may have one or more flow exception handlers. A FlowExecutionExceptionHandler
can execute
custom behavior in response to a specific exception (or set of exceptions) that occur in a state of one of this
flow's executions.
Instances of this class are typically built by FlowBuilder
implementations but may also be directly instantiated.
This class and the rest of the Spring Web Flow (SWF) engine have been designed with minimal dependencies on other libraries. Spring Web Flow is usable in a standalone fashion. The engine system is fully usable outside an HTTP servlet environment, for example in tests, or standalone applications. One of the major architectural benefits of Spring Web Flow is the ability to design reusable, high-level controller modules that may be executed in any environment.
Note: flows are singleton definition objects so they should be thread-safe. You can think a flow definition as
analogous to a Java class, defining all the behavior of an application module. The core behaviors
start
, resume(RequestControlContext)
,
on event
,
end
, and
handleException(FlowExecutionException, RequestControlContext)
. Each method accepts a request context
that allows for this flow to access execution state in a thread safe manner. A flow execution is
what models a running instance of this flow definition, somewhat analogous to a java object that is an instance of a
class.
- Author:
- Keith Donald, Erwin Vervaet, Colin Sampaleanu, Jeremy Grelle
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionprotected final org.apache.commons.logging.Log
Logger, can be used in subclasses.Fields inherited from class org.springframework.webflow.core.AnnotatedObject
CAPTION_PROPERTY, DESCRIPTION_PROPERTY
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
Add given state definition to this flow definition.void
addVariable
(FlowVariable variable) Adds a flow variable.void
addVariables
(FlowVariable... variables) Adds flow variables.boolean
containsState
(String stateId) Is a state with the provided id present in this flow?static Flow
create
(String id, AttributeMap<?> attributes) Create a new flow with the given id and attributes.void
destroy()
Destroy this flow definition, releasing any resources.void
end
(RequestControlContext context, String outcome, MutableAttributeMap<?> output) Inform this flow definition that an execution session of itself has ended.boolean
org.springframework.context.ApplicationContext
Returns a reference to application context hosting application objects and services used by this flow definition.Returns the class loader used by this flow definition to load classes.Returns the list of actions executed by this flow when an execution of the flow ends.Returns the set of exception handlers, allowing manipulation of how exceptions are handled when thrown during flow execution.getGlobalTransition
(String eventId) Returns the transition that matches the event with the provided id.Returns the set of transitions eligible for execution by this flow if no state-level transition is matched.getId()
Returns the unique id of this flow.Returns the configured flow input mapper, or null if none.Returns the configured flow output mapper, or null if none.String[]
Returns the outcomes that are possible for this flow to reach.Returns the list of actions executed by this flow when an execution of the flow starts.Return this flow's starting point.Returns the state definition with the specified id.int
Returns the number of states defined in this flow.String[]
Convenience accessor that returns an ordered array of the Stringids
for the state definitions associated with this flow definition.getStateInstance
(String stateId) Lookup the identified state instance of this flow.getTransitionableState
(String stateId) Return theTransitionableState
with givenstateId
.getVariable
(String name) Returns the flow variable with the given name.Returns the flow variables.boolean
handleEvent
(RequestControlContext context) Handle the last event that occurred against an active session of this flow.boolean
handleException
(FlowExecutionException exception, RequestControlContext context) Handle an exception that occurred during an execution of this flow.int
hashCode()
boolean
Returns true if this flow definition is currently in development (running in development mode).void
restoreVariables
(RequestContext context) void
resume
(RequestControlContext context) Resume a paused session for this flow in its current view state.void
setApplicationContext
(org.springframework.context.ApplicationContext applicationContext) Sets a reference to the application context hosting application objects needed by this flow.void
setInputMapper
(Mapper inputMapper) Sets the mapper to map flow input attributes.void
setOutputMapper
(Mapper outputMapper) Sets the mapper to map flow output attributes.void
setStartState
(String stateId) Set the start state for this flow to the state with the providedstateId
; a state must exist by the providedstateId
.void
setStartState
(State state) Set the start state for this flow to the state provided; any state may be the start state.void
start
(RequestControlContext context, MutableAttributeMap<?> input) Start a new session for this flow in its start state.toString()
Methods inherited from class org.springframework.webflow.core.AnnotatedObject
getAttributes, getCaption, getDescription, setCaption, setDescription
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.springframework.webflow.core.Annotated
getAttributes, getCaption, getDescription
-
Field Details
-
logger
protected final org.apache.commons.logging.Log loggerLogger, can be used in subclasses.
-
-
Constructor Details
-
Flow
Construct a new flow definition with the given id. The id should be unique among all flows.- Parameters:
id
- the flow identifier
-
-
Method Details
-
create
Create a new flow with the given id and attributes.- Parameters:
id
- the flow idattributes
- the attributes- Returns:
- the flow
-
getId
Description copied from interface:FlowDefinition
Returns the unique id of this flow.- Specified by:
getId
in interfaceFlowDefinition
- Returns:
- the flow id
-
getStartState
Description copied from interface:FlowDefinition
Return this flow's starting point.- Specified by:
getStartState
in interfaceFlowDefinition
- Returns:
- the start state
-
getState
Description copied from interface:FlowDefinition
Returns the state definition with the specified id.- Specified by:
getState
in interfaceFlowDefinition
- Parameters:
stateId
- the state id- Returns:
- the state definition
-
getPossibleOutcomes
Description copied from interface:FlowDefinition
Returns the outcomes that are possible for this flow to reach.- Specified by:
getPossibleOutcomes
in interfaceFlowDefinition
- Returns:
- the possible outcomes
-
getClassLoader
Description copied from interface:FlowDefinition
Returns the class loader used by this flow definition to load classes.- Specified by:
getClassLoader
in interfaceFlowDefinition
- Returns:
- the class loader
-
getApplicationContext
public org.springframework.context.ApplicationContext getApplicationContext()Description copied from interface:FlowDefinition
Returns a reference to application context hosting application objects and services used by this flow definition.- Specified by:
getApplicationContext
in interfaceFlowDefinition
- Returns:
- the application context
-
inDevelopment
public boolean inDevelopment()Description copied from interface:FlowDefinition
Returns true if this flow definition is currently in development (running in development mode).- Specified by:
inDevelopment
in interfaceFlowDefinition
- Returns:
- the development flag
-
add
Add given state definition to this flow definition. Marked protected, as this method is to be called by the (privileged) state definition classes themselves during state construction as part of a FlowBuilder invocation.- Parameters:
state
- the state to add- Throws:
IllegalArgumentException
- when the state cannot be added to the flow; for instance if another state shares the same id as the one provided or if given state already belongs to another flow
-
getStateCount
public int getStateCount()Returns the number of states defined in this flow.- Returns:
- the state count
-
containsState
Is a state with the provided id present in this flow?- Parameters:
stateId
- the state id- Returns:
- true if yes, false otherwise
-
setStartState
Set the start state for this flow to the state with the providedstateId
; a state must exist by the providedstateId
.- Parameters:
stateId
- the id of the new start state- Throws:
IllegalArgumentException
- when no state exists with the id you provided
-
setStartState
Set the start state for this flow to the state provided; any state may be the start state.- Parameters:
state
- the new start state- Throws:
IllegalArgumentException
- given state has not been added to this flow
-
getTransitionableState
public TransitionableState getTransitionableState(String stateId) throws IllegalArgumentException, ClassCastException Return theTransitionableState
with givenstateId
.- Parameters:
stateId
- id of the state to look up- Returns:
- the transitionable state
- Throws:
IllegalArgumentException
- if the identified state cannot be foundClassCastException
- when the identified state is not transitionable
-
getStateInstance
Lookup the identified state instance of this flow.- Parameters:
stateId
- the state id- Returns:
- the state
- Throws:
IllegalArgumentException
- if the identified state cannot be found
-
getStateIds
Convenience accessor that returns an ordered array of the Stringids
for the state definitions associated with this flow definition.- Returns:
- the state ids
-
addVariable
Adds a flow variable.- Parameters:
variable
- the variable
-
addVariables
Adds flow variables.- Parameters:
variables
- the variables
-
getVariable
Returns the flow variable with the given name.- Parameters:
name
- the name of the variable
-
getVariables
Returns the flow variables. -
getInputMapper
Returns the configured flow input mapper, or null if none.- Returns:
- the input mapper
-
setInputMapper
Sets the mapper to map flow input attributes.- Parameters:
inputMapper
- the input mapper
-
getStartActionList
Returns the list of actions executed by this flow when an execution of the flow starts. The returned list is mutable.- Returns:
- the start action list
-
getEndActionList
Returns the list of actions executed by this flow when an execution of the flow ends. The returned list is mutable.- Returns:
- the end action list
-
getOutputMapper
Returns the configured flow output mapper, or null if none.- Returns:
- the output mapper
-
setOutputMapper
Sets the mapper to map flow output attributes.- Parameters:
outputMapper
- the output mapper
-
getExceptionHandlerSet
Returns the set of exception handlers, allowing manipulation of how exceptions are handled when thrown during flow execution. Exception handlers are invoked when an exception occurs at execution time and can execute custom exception handling logic as well as select an error view to display. Exception handlers attached at the flow level have an opportunity to handle exceptions that aren't handled at the state level.- Returns:
- the exception handler set
-
getGlobalTransitionSet
Returns the set of transitions eligible for execution by this flow if no state-level transition is matched. The returned set is mutable.- Returns:
- the global transition set
-
getGlobalTransition
Returns the transition that matches the event with the provided id.- Parameters:
eventId
- the event id- Returns:
- the transition that matches, or null if no match is found.
-
setApplicationContext
public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) Sets a reference to the application context hosting application objects needed by this flow.- Parameters:
applicationContext
- the application context
-
equals
-
hashCode
public int hashCode() -
start
public void start(RequestControlContext context, MutableAttributeMap<?> input) throws FlowExecutionException Start a new session for this flow in its start state. This boils down to the following:- Create (setup) all registered flow variables (
addVariable(FlowVariable)
) in flow scope. - Map provided input data into the flow. Typically data will be mapped into flow scope using the registered
input mapper (
setInputMapper(Mapper)
). - Execute all registered start actions (
getStartActionList()
). - Enter the configured start state (
setStartState(State)
)
- Parameters:
context
- the flow execution control contextinput
- eligible input into the session- Throws:
FlowExecutionException
- when an exception occurs starting the flow
- Create (setup) all registered flow variables (
-
resume
Resume a paused session for this flow in its current view state.- Parameters:
context
- the flow execution control context- Throws:
FlowExecutionException
- when an exception occurs during the resume operation
-
handleEvent
Handle the last event that occurred against an active session of this flow.- Parameters:
context
- the flow execution control context
-
end
public void end(RequestControlContext context, String outcome, MutableAttributeMap<?> output) throws FlowExecutionException Inform this flow definition that an execution session of itself has ended. As a result, the flow will do the following:- Execute all registered end actions (
getEndActionList()
). - Map data available in the flow execution control context into provided output map using a registered output
mapper (
setOutputMapper(Mapper)
).
- Parameters:
context
- the flow execution control contextoutcome
- the logical flow outcome that will be returned by the session, generally the id of the terminating end stateoutput
- initial output produced by the session that is eligible for modification by this method- Throws:
FlowExecutionException
- when an exception occurs ending this flow
- Execute all registered end actions (
-
destroy
public void destroy()Description copied from interface:FlowDefinition
Destroy this flow definition, releasing any resources. After the flow is destroyed it cannot be started again.- Specified by:
destroy
in interfaceFlowDefinition
-
handleException
public boolean handleException(FlowExecutionException exception, RequestControlContext context) throws FlowExecutionException Handle an exception that occurred during an execution of this flow.- Parameters:
exception
- the exception that occurredcontext
- the flow execution control context- Throws:
FlowExecutionException
-
restoreVariables
-
toString
-