Class ChainedInvocation
- java.lang.Object
-
- net.sf.jguiraffe.di.impl.ChainedInvocation
-
- All Implemented Interfaces:
Invokable
public class ChainedInvocation extends Object implements Invokable
A special implementation of the
Invokableinterface that allows aggregating an arbitrary number ofInvokableobjects to a kind of script.The idea behind this class is that other objects implementing the
Invokableinterface 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 specialDependency, which can be created by calling thegetChainDependency()method.The typical life-cycle of an instance of this class is as follows:
- An instance is created using the default constructor.
- The
Invokableobjects to be executed are added using theaddInvokable()methods. Note that the order of these calls is important; the added objects are invoked in exactly the same order. - After adding all
Invokableobjects theinvoke()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.
- Version:
- $Id: ChainedInvocation.java 207 2012-02-09 07:30:13Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Constructor Description ChainedInvocation()Creates a new instance ofChainedInvocation.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddInvokable(Invokable inv)Adds the specifiedInvokableobject to this object.voidaddInvokable(Invokable inv, String result)Adds the specifiedInvokableobject to this object and initializes its result variable.voidaddInvokable(Invokable inv, String result, String source)Adds the specifiedInvokableobject to this object and initializes its result variable and its source object.DependencygetChainDependency(String name)Returns a specialDependencyfor a local variable that is used during the execution of aChainedInvocation.List<Invokable>getInvokables()Returns a list with theInvokableobjects that have already been added to this chain.List<Dependency>getParameterDependencies()Returns a list of the dependencies required for this invocation.StringgetResultVariableName()Returns the name of the result variable.ObjectgetVariable(String name)Returns the name of the variable with the given name.BeanProvidergetVariableBean(String name)Returns aBeanProviderfor the local variable with the given name.Set<String>getVariableNames()Returns a set with the names of the currently defined local variables.Objectinvoke(DependencyProvider depProvider, Object target)Performs the invocation represented by this class.booleanisClearVariables()Returns a flag whether all local variables are to be cleared for each new invocation.voidsetClearVariables(boolean clearVariables)Sets the value of theclear variablesflag.voidsetResultVariableName(String resultVariableName)Sets the name of the result variable.voidsetVariable(String name, Object value)Sets the value of the specified variable.intsize()Returns the number ofInvokableobjects contained in this chain.StringtoString()Returns a string representation for this object.
-
-
-
Method Detail
-
addInvokable
public void addInvokable(Invokable inv)
Adds the specifiedInvokableobject to this object. It will become part of the invocation sequence.- Parameters:
inv- the object to be added (must not be null)- Throws:
IllegalArgumentException- if the passed inInvokableobject is null
-
addInvokable
public void addInvokable(Invokable inv, String result)
Adds the specifiedInvokableobject 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.- Parameters:
inv- the object to be added (must not be null)result- the name of the result variable- Throws:
IllegalArgumentException- if the passed inInvokableobject is null
-
addInvokable
public void addInvokable(Invokable inv, String result, String source)
Adds the specifiedInvokableobject 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.- Parameters:
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 invocation- Throws:
IllegalArgumentException- if the passed inInvokableobject is null
-
getInvokables
public List<Invokable> getInvokables()
Returns a list with theInvokableobjects 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.- Returns:
- a list with the children of this chain
-
size
public int size()
Returns the number ofInvokableobjects contained in this chain.- Returns:
- the size of this chain
-
getChainDependency
public Dependency getChainDependency(String name)
Returns a specialDependencyfor a local variable that is used during the execution of aChainedInvocation. Local variables are created by specifying result names forInvokables 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.- Parameters:
name- the name of the local variable to be accessed (must not be null)- Returns:
- the dependency for accessing the specified local variable
- Throws:
IllegalArgumentException- if the name is null
-
getVariableBean
public BeanProvider getVariableBean(String name)
Returns aBeanProviderfor 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.- Parameters:
name- the name of the local variable to be accessed (must not be null)- Returns:
- a
BeanProviderwrapping this local variable - Throws:
IllegalArgumentException- if the name is null- Since:
- 1.1
-
getVariableNames
public Set<String> getVariableNames()
Returns a set with the names of the currently defined local variables. These names can be passed to thegetVariable()method for querying the current values of these variables.- Returns:
- a set with the names of the currently existing local variables
-
getVariable
public Object getVariable(String name)
Returns the name of the variable with the given name. If there is no variable with this name, an exception is thrown. (We consider an access to an undefined variable an error in the invocation chain.)- Parameters:
name- the name of the variable to be queried- Returns:
- the value of this variable
- Throws:
InjectionException- if the variable cannot be found
-
setVariable
public void setVariable(String name, Object value)
Sets the value of the specified variable. If the variable does not exist yet, it is created. A value of null removes the variable.- Parameters:
name- the name of the variable (must not be null)value- the new value of the variable- Throws:
IllegalArgumentException- if the name is null
-
isClearVariables
public boolean isClearVariables()
Returns a flag whether all local variables are to be cleared for each new invocation.- Returns:
- the clear variables flag
-
setClearVariables
public void setClearVariables(boolean clearVariables)
Sets the value of theclear variablesflag. 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 thesetVariable()method, it will be necessary to disable this flag; otherwise these variables will also get lost.- Parameters:
clearVariables- the new value of the flag
-
getResultVariableName
public String getResultVariableName()
Returns the name of the result variable.- Returns:
- the name of the result variable
- Since:
- 1.1
-
setResultVariableName
public void setResultVariableName(String resultVariableName)
Sets the name of the result variable. If this property is set, theinvoke()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.- Parameters:
resultVariableName- the name of the result variable- Since:
- 1.1
-
getParameterDependencies
public List<Dependency> getParameterDependencies()
Returns a list of the dependencies required for this invocation. This implementation creates a union of the dependencies of all containedInvokableobjects.- Specified by:
getParameterDependenciesin interfaceInvokable- Returns:
- a list with the dependencies
-
invoke
public Object invoke(DependencyProvider depProvider, Object target)
Performs the invocation represented by this class. This implementation will invoke all containedInvokableobjects.- Specified by:
invokein interfaceInvokable- Parameters:
depProvider- the dependency providertarget- the target object- Returns:
- the result of the invocation
- Throws:
InjectionException- if an error occursIllegalArgumentException- if the dependency provider is null or a required target object is undefined
-
-