Interface Chain<C extends Context>
- Type Parameters:
C
- Type of the context associated with this chain
- All Superinterfaces:
Command<C>
- All Known Implementing Classes:
ChainBase
Chain
represents a configured list of
Command
s that will be executed in order to perform processing
on a specified Context
. Each included Command
will be
executed in turn, until either one of them returns true
,
one of the executed Command
s throws an exception,
or the end of the chain has been reached. The Chain
itself will
return the return value of the last Command
that was executed
(if no exception was thrown), or rethrow the thrown exception.
Note that Chain
extends Command
, so that the two can
be used interchangeably when a Command
is expected. This makes it
easy to assemble workflows in a hierarchical manner by combining subchains
into an overall processing chain.
To protect applications from evolution of this interface, specialized
implementations of Chain
should generally be created by extending
the provided base class ChainBase
)
rather than directly implementing this interface.
Chain
implementations should be designed in a thread-safe
manner, suitable for execution on multiple threads simultaneously. In
general, this implies that the state information identifying which
Command
is currently being executed should be maintained in a
local variable inside the execute()
method, rather than
in an instance variable. The Command
s in a Chain
may be
configured (via calls to addCommand()
) at any time before
the execute()
method of the Chain
is first called.
After that, the configuration of the Chain
is frozen.
- Version:
- $Revision$ $Date$
- Author:
- Craig R. McClanahan
-
Field Summary
Fields inherited from interface org.apache.commons.chain.Command
CONTINUE_PROCESSING, PROCESSING_COMPLETE
-
Method Summary
Modifier and TypeMethodDescriptionaddCommand
(CMD command) boolean
Execute the processing represented by thisChain
according to the following algorithm.
-
Method Details
-
addCommand
Add aCommand
to the list ofCommand
s that will be called in turn when thisChain
'sexecute()
method is called. Onceexecute()
has been called at least once, it is no longer possible to add additionalCommand
s; instead, an exception will be thrown.- Type Parameters:
CMD
- theCommand
type to be added in theChain
- Parameters:
command
- TheCommand
to be added- Throws:
IllegalArgumentException
- ifcommand
isnull
IllegalStateException
- if thisChain
has already been executed at least once, so no further configuration is allowed
-
execute
Execute the processing represented by thisChain
according to the following algorithm.- If there are no configured
Command
s in theChain
, returnfalse
. - Call the
execute()
method of eachCommand
configured on this chain, in the order they were added via calls to theaddCommand()
method, until the end of the configuredCommand
s is encountered, or until one of the executedCommand
s returnstrue
or throws an exception. - Walk backwards through the
Command
s whoseexecute()
methods, starting with the last one that was executed. If thisCommand
instance is also aFilter
, call itspostprocess()
method, discarding any exception that is thrown. - If the last
Command
whoseexecute()
method was called threw an exception, rethrow that exception. - Otherwise, return the value returned by the
execute()
method of the lastCommand
that was executed. This will betrue
if the lastCommand
indicated that processing of thisContext
has been completed, orfalse
if none of the calledCommand
s returnedtrue
.
- Specified by:
execute
in interfaceCommand<C extends Context>
- Parameters:
context
- TheContext
to be processed by thisChain
- Returns:
true
if the processing of thisContext
has been completed, orfalse
if the processing of thisContext
should be delegated to a subsequentCommand
in an enclosingChain
- Throws:
Exception
- if thrown by one of theCommand
s in thisChain
but not handled by apostprocess()
method of aFilter
IllegalArgumentException
- ifcontext
isnull
- If there are no configured
-