public class ChainedInvocation extends Object implements Invokable
A special implementation of the Invokable
interface that
allows aggregating an arbitrary number of Invokable
objects to
a kind of script.
The idea behind this class is that other objects implementing the
Invokable
interface can be added. They can then be executed en
bloc. In addition to that a rudimentary support for variables is available:
the result of an invocation can be assigned to a named variable; later this
variable can be accessed again using a special
Dependency
, which can be created by calling the
getChainDependency()
method.
The typical life-cycle of an instance of this class is as follows:
Invokable
objects to be executed are added using the
addInvokable()
methods. Note that the order of these calls is
important; the added objects are invoked in exactly the same order.Invokable
objects the
invoke()
method can be called. It triggers all contained
objects.This class provides some methods for accessing and manipulating local variables. So it is possible to list all currently existing variables, query their values, and even modify them. However these features are mainly intended for debugging purposes rather than for implementing additional scripting logic.
Note: This class is not thread-safe. It is intended to be created and initialized by a single thread and then be passed to a complex bean provider with initialization support. After that no more invocations should be added. Because the local variables of a currently executed script are internally stored, it is especially not possible to have multiple concurrent invocations.
Constructor and Description |
---|
ChainedInvocation()
Creates a new instance of
ChainedInvocation . |
Modifier and Type | Method and Description |
---|---|
void |
addInvokable(Invokable inv)
Adds the specified
Invokable object to this object. |
void |
addInvokable(Invokable inv,
String result)
Adds the specified
Invokable object to this object and
initializes its result variable. |
void |
addInvokable(Invokable inv,
String result,
String source)
Adds the specified
Invokable object to this object and
initializes its result variable and its source object. |
Dependency |
getChainDependency(String name)
Returns a special
Dependency for a local variable that is
used during the execution of a ChainedInvocation . |
List<Invokable> |
getInvokables()
Returns a list with the
Invokable objects that have
already been added to this chain. |
List<Dependency> |
getParameterDependencies()
Returns a list of the dependencies required for this invocation.
|
String |
getResultVariableName()
Returns the name of the result variable.
|
Object |
getVariable(String name)
Returns the name of the variable with the given name.
|
BeanProvider |
getVariableBean(String name)
Returns a
BeanProvider for the local variable with the given
name. |
Set<String> |
getVariableNames()
Returns a set with the names of the currently defined local variables.
|
Object |
invoke(DependencyProvider depProvider,
Object target)
Performs the invocation represented by this class.
|
boolean |
isClearVariables()
Returns a flag whether all local variables are to be cleared for each new
invocation.
|
void |
setClearVariables(boolean clearVariables)
Sets the value of the
clear variables flag. |
void |
setResultVariableName(String resultVariableName)
Sets the name of the result variable.
|
void |
setVariable(String name,
Object value)
Sets the value of the specified variable.
|
int |
size()
Returns the number of
Invokable objects contained in this
chain. |
String |
toString()
Returns a string representation for this object.
|
public ChainedInvocation()
ChainedInvocation
.public void addInvokable(Invokable inv)
Invokable
object to this object. It
will become part of the invocation sequence.inv
- the object to be added (must not be null)IllegalArgumentException
- if the passed in Invokable
object is nullpublic void addInvokable(Invokable inv, String result)
Invokable
object to this object and
initializes its result variable. It will become part of the invocation
sequence. The result of its invocation will be stored in a local variable
with the given name.inv
- the object to be added (must not be null)result
- the name of the result variableIllegalArgumentException
- if the passed in Invokable
object is nullpublic void addInvokable(Invokable inv, String result, String source)
Invokable
object to this object and
initializes its result variable and its source object. It will become
part of the invocation sequence. The result of its invocation will be
stored in a local variable with the given name. The invocation is not
performed on the current target object, but on the object stored in the
local variable with the given source name. This makes it possible to
manipulate other objects than the current main target.inv
- the object to be added (must not be null)result
- the name of the result variablesource
- the name of the variable, which contains the target object
for this invocationIllegalArgumentException
- if the passed in Invokable
object is nullpublic List<Invokable> getInvokables()
Invokable
objects that have
already been added to this chain. Manipulations of this list do not
affect this object. It is empty if nothing has been added yet.public int size()
Invokable
objects contained in this
chain.public Dependency getChainDependency(String name)
Dependency
for a local variable that is
used during the execution of a ChainedInvocation
. Local
variables are created by specifying result names for
Invokable
s when they are added to the chain: the result
of this invocation will then be stored in a variable with this name. If
this variable later needs to be used for another invocation (e.g. as the
parameter of a method call), it can be accessed using such a dependency.name
- the name of the local variable to be accessed (must not be
null)IllegalArgumentException
- if the name is nullpublic BeanProvider getVariableBean(String name)
BeanProvider
for the local variable with the given
name. Using this method, variables created during script execution can be
accessed as beans and thus can act as providers for other tags.name
- the name of the local variable to be accessed (must not be
null)BeanProvider
wrapping this local variableIllegalArgumentException
- if the name is nullpublic Set<String> getVariableNames()
getVariable()
method for
querying the current values of these variables.public Object getVariable(String name)
name
- the name of the variable to be queriedInjectionException
- if the variable cannot be foundpublic void setVariable(String name, Object value)
name
- the name of the variable (must not be null)value
- the new value of the variableIllegalArgumentException
- if the name is nullpublic boolean isClearVariables()
public void setClearVariables(boolean clearVariables)
clear variables
flag. If this flag
is set to true (which is the default value), the storage for
local variables is cleared at the beginning of an invocation. This
ensures that values generated by earlier invocations do not affect the
current invocation. If variables have been set manually using the
setVariable()
method, it will be necessary to disable this
flag; otherwise these variables will also get lost.clearVariables
- the new value of the flagpublic String getResultVariableName()
public void setResultVariableName(String resultVariableName)
invoke()
method will not return the passed in target object, but
the object referenced by this variable. This is useful if the script
generates a result.resultVariableName
- the name of the result variablepublic List<Dependency> getParameterDependencies()
Invokable
objects.getParameterDependencies
in interface Invokable
public Object invoke(DependencyProvider depProvider, Object target)
Invokable
objects.invoke
in interface Invokable
depProvider
- the dependency providertarget
- the target objectInjectionException
- if an error occursIllegalArgumentException
- if the dependency provider is null
or a required target object is undefinedCopyright © 2016 The JGUIraffe Team. All rights reserved.