Class DispatchCommand<C extends Context>

java.lang.Object
org.apache.commons.chain.generic.DispatchCommand<C>
Type Parameters:
C - Type of the context associated with this command
All Implemented Interfaces:
Command<C>

public abstract class DispatchCommand<C extends Context> extends Object implements Command<C>
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 Details

    • DEFAULT_SIGNATURE

      protected static final Class<?>[] DEFAULT_SIGNATURE
      The base implementation expects dispatch methods to take a Context as their only argument.
  • Constructor Details

    • DispatchCommand

      public DispatchCommand()
      The Default-Constructor for this class.
  • Method Details

    • execute

      public boolean execute(C context) throws Exception
      Look up the method specified by either "method" or "methodKey" and invoke it, returning a boolean value as interpreted by evaluateResult.
      Specified by:
      execute in interface Command<C extends Context>
      Parameters:
      context - The Context to be processed by this Command.
      Returns:
      the result of method being dispatched to.
      Throws:
      IllegalStateException - if neither 'method' nor 'methodKey' properties are defined
      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 an Error or other Throwable which is not an Exception.
    • extractMethod

      protected Method extractMethod(C context) throws NoSuchMethodException
      Extract the dispatch method. The base implementation uses the command's method 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 the methodKey.
      Parameters:
      context - The Context being processed by this Command.
      Returns:
      The method to execute
      Throws:
      NoSuchMethodException - if no method can be found under the specified name.
      NullPointerException - if no methodName cannot be determined
    • evaluateResult

      protected boolean evaluateResult(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 method execution
      Returns:
      The evaluated result/
    • getSignature

      protected Class<?>[] getSignature()
      Return a Class[] describing the expected signature of the method.
      Returns:
      The method signature.
    • getArguments

      protected Object[] getArguments(C 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 of getSignature().
      Parameters:
      context - The Context being processed by this Command.
      Returns:
      The method arguments.
    • getMethod

      public String getMethod()
      Return the method name.
      Returns:
      The method name.
    • getMethodKey

      public String getMethodKey()
      Return the Context key for the method name.
      Returns:
      The Context key for the method name.
    • setMethod

      public void setMethod(String method)
      Set the method name.
      Parameters:
      method - The method name.
    • setMethodKey

      public void setMethodKey(String methodKey)
      Set the Context key for the method name.
      Parameters:
      methodKey - The Context key for the method name.