Class DispatchLookupCommand
- java.lang.Object
-
- org.apache.commons.chain.generic.LookupCommand
-
- org.apache.commons.chain.generic.DispatchLookupCommand
-
public class DispatchLookupCommand extends LookupCommand implements Filter
This command combines elements of the
LookupCommandwith theDispatchCommand. Look up a specifiedCommand(which could also be aChain) in aCatalog, and delegate execution to it. Introspection is used to lookup the appropriate method to delegate execution to. If the delegated-toCommandis also aFilter, itspostprocess()method will also be invoked at the appropriate time.The name of the
Commandcan be specified either directly (via thenameproperty) or indirectly (via thenameKeyproperty). Exactly one of these must be set.The name of the method to be called can be specified either directly (via the
methodproperty) or indirectly (via themethodKeyproperty). Exactly one of these must be set.If the
optionalproperty is set totrue, failure to find the specified command in the specified catalog will be silently ignored. Otherwise, a lookup failure will trigger anIllegalArgumentException.- Since:
- Chain 1.1
- Version:
- $Revision: 480477 $
- Author:
- Sean Schofield
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.Class[]DEFAULT_SIGNATUREThe base implementation expects dispatch methods to take aContextas their only argument.private java.lang.Stringmethodprivate java.lang.StringmethodKeyprivate java.util.WeakHashMapmethods-
Fields inherited from interface org.apache.commons.chain.Command
CONTINUE_PROCESSING, PROCESSING_COMPLETE
-
-
Constructor Summary
Constructors Constructor Description DispatchLookupCommand()Create an instance with an unspecifiedcatalogFactoryproperty.DispatchLookupCommand(CatalogFactory factory)Create an instance and initialize thecatalogFactoryproperty to givenfactory.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanexecute(Context context)Look up the specified command, and (if found) execute it.private java.lang.reflect.MethodextractMethod(Command command, Context context)Extract the dispatch method.protected java.lang.Object[]getArguments(Context context)Get the arguments to be passed into the dispatch method.java.lang.StringgetMethod()Return the method name.java.lang.StringgetMethodKey()Return the Context key for the method name.protected java.lang.Class[]getSignature()Return aClass[]describing the expected signature of the method.voidsetMethod(java.lang.String method)Set the method name.voidsetMethodKey(java.lang.String methodKey)Set the Context key for the method name.-
Methods inherited from class org.apache.commons.chain.generic.LookupCommand
getCatalog, getCatalogFactory, getCatalogName, getCommand, getCommandName, getName, getNameKey, isIgnoreExecuteResult, isIgnorePostprocessResult, isOptional, postprocess, setCatalogFactory, setCatalogName, setIgnoreExecuteResult, setIgnorePostprocessResult, setName, setNameKey, setOptional
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.commons.chain.Filter
postprocess
-
-
-
-
Field Detail
-
DEFAULT_SIGNATURE
private static final java.lang.Class[] DEFAULT_SIGNATURE
The base implementation expects dispatch methods to take aContextas their only argument.
-
methods
private java.util.WeakHashMap methods
-
method
private java.lang.String method
-
methodKey
private java.lang.String methodKey
-
-
Constructor Detail
-
DispatchLookupCommand
public DispatchLookupCommand()
Create an instance with an unspecifiedcatalogFactoryproperty. This property can be set later usingsetProperty, or if it is not set, the static singleton instance fromCatalogFactory.getInstance()will be used.
-
DispatchLookupCommand
public DispatchLookupCommand(CatalogFactory factory)
Create an instance and initialize thecatalogFactoryproperty to givenfactory.- Parameters:
factory- The Catalog Factory.
-
-
Method Detail
-
getMethod
public java.lang.String getMethod()
Return the method name.- Returns:
- The method name.
-
getMethodKey
public java.lang.String getMethodKey()
Return the Context key for the method name.- Returns:
- The Context key for the method name.
-
setMethod
public void setMethod(java.lang.String method)
Set the method name.- Parameters:
method- The method name.
-
setMethodKey
public void setMethodKey(java.lang.String methodKey)
Set the Context key for the method name.- Parameters:
methodKey- The Context key for the method name.
-
execute
public boolean execute(Context context) throws java.lang.Exception
Look up the specified command, and (if found) execute it.
- Specified by:
executein interfaceCommand- Overrides:
executein classLookupCommand- Parameters:
context- The context for this request- Returns:
- the result of executing the looked-up command's method, or
falseif no command is found. - Throws:
java.lang.Exception- if no suchCommandcan be found and theoptionalproperty is set tofalse
-
getSignature
protected java.lang.Class[] getSignature()
Return a
Class[]describing the expected signature of the method. The default is a signature that just accepts the command'sContext. The method can be overidden to provide a different method signature.- Returns:
- the expected method signature
-
getArguments
protected java.lang.Object[] getArguments(Context context)
Get the arguments to be passed into the dispatch method. Default implementation simply returns the context which was passed in, but subclasses could use this to wrap the context in some other type, or extract key values from the context to pass in. The length and types of values returned by this must coordinate with the return value ofgetSignature()- Parameters:
context- The context associated with the request- Returns:
- the method arguments to be used
-
extractMethod
private java.lang.reflect.Method extractMethod(Command command, Context context) throws java.lang.NoSuchMethodException
Extract the dispatch method. The base implementation uses the command'smethodproperty at the name of a method to look up, or, if that is not defined, uses themethodKeyto lookup the method name in the context.- Parameters:
command- The commmand that contains the method to be executed.context- The context associated with this request- Returns:
- the dispatch method
- Throws:
java.lang.NoSuchMethodException- if no method can be found under the specified name.java.lang.NullPointerException- if no methodName can be determined
-
-