Package org.apache.commons.chain.generic
Class DispatchCommand
- java.lang.Object
-
- org.apache.commons.chain.generic.DispatchCommand
-
- All Implemented Interfaces:
Command
public abstract class DispatchCommand extends java.lang.Object implements Command
An abstract base command which uses introspection to look up a method to execute. For use by developers who prefer to group related functionality into a single class rather than an inheritance family.- Since:
- Chain 1.1
-
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.Class[]
DEFAULT_SIGNATURE
The base implementation expects dispatch methods to take aContext
as their only argument.private java.lang.String
method
Method nameprivate java.lang.String
methodKey
Method keyprivate java.util.Map
methods
Cache of methods-
Fields inherited from interface org.apache.commons.chain.Command
CONTINUE_PROCESSING, PROCESSING_COMPLETE
-
-
Constructor Summary
Constructors Constructor Description DispatchCommand()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected boolean
evaluateResult(java.lang.Object o)
Evaluate the result of the method invocation as a boolean value.boolean
execute(Context context)
Look up the method specified by either "method" or "methodKey" and invoke it, returning a boolean value as interpreted byevaluateResult
.protected java.lang.reflect.Method
extractMethod(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.String
getMethod()
Return the method name.java.lang.String
getMethodKey()
Return the Context key for the method name.protected java.lang.Class[]
getSignature()
Return aClass[]
describing the expected signature of the method.void
setMethod(java.lang.String method)
Set the method name.void
setMethodKey(java.lang.String methodKey)
Set the Context key for the method name.
-
-
-
Field Detail
-
methods
private java.util.Map methods
Cache of methods
-
method
private java.lang.String method
Method name
-
methodKey
private java.lang.String methodKey
Method key
-
DEFAULT_SIGNATURE
protected static final java.lang.Class[] DEFAULT_SIGNATURE
The base implementation expects dispatch methods to take aContext
as their only argument.
-
-
Method Detail
-
execute
public boolean execute(Context context) throws java.lang.Exception
Look up the method specified by either "method" or "methodKey" and invoke it, returning a boolean value as interpreted byevaluateResult
.- Specified by:
execute
in interfaceCommand
- Parameters:
context
- The Context to be processed by this Command.- Returns:
- the result of method being dispatched to.
- Throws:
java.lang.IllegalStateException
- if neither 'method' nor 'methodKey' properties are definedjava.lang.Exception
- if any is thrown by the invocation. Note that if invoking the method results in an InvocationTargetException, the cause of that exception is thrown instead of the exception itself, unless the cause is anError
or otherThrowable
which is not anException
.
-
extractMethod
protected java.lang.reflect.Method extractMethod(Context context) throws java.lang.NoSuchMethodException
Extract the dispatch method. The base implementation uses the command'smethod
property as the name of a method to look up, or, if that is not defined, looks up the the method name in the Context using themethodKey
.- Parameters:
context
- The Context being processed by this Command.- Returns:
- The method to execute
- Throws:
java.lang.NoSuchMethodException
- if no method can be found under the specified name.java.lang.NullPointerException
- if no methodName cannot be determined
-
evaluateResult
protected boolean evaluateResult(java.lang.Object o)
Evaluate the result of the method invocation as a boolean value. Base implementation expects that the invoked method returns boolean true/false, but subclasses might implement other interpretations.- Parameters:
o
- The result of the methid execution- Returns:
- The evaluated result/
-
getSignature
protected java.lang.Class[] getSignature()
Return aClass[]
describing the expected signature of the method.- Returns:
- The 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 being processed by this Command.- Returns:
- The method arguments.
-
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.
-
-