kiwi.util
Class CommandDispatcher

java.lang.Object
  extended bykiwi.util.CommandDispatcher

public class CommandDispatcher
extends java.lang.Object

A class that makes use of the reflection API to implement a generic, reusable, and flexible command processor.

The CommandDispatcher analyzes a helper class (which implements the CommandProcessor interface), searching for methods whose names begin with the prefix cmd_. This information is used to build a command dictionary that maps command names to their associated processing methods.

The general idea is that the CommandDispatcher will be fed command strings that consist of a command name followed by a whitespace- separated list of arguments. This string is tokenized into words. The first word is the command name and is used to look up an associated method in the command dictionary. For example, if the command is hello, the associated method in the helper class would be cmd_hello(). If a matching method is not found, the helper object is messaged with an unknownCommandError().

If the command is found, then the remaining tokens are counted to determine the number of arguments that the command is being invoked with. Some commands may have more than one form, or may have optional arguments. In this case, the associated command processing method in the helper class may be overloaded to handle all of the separate (legal) forms. The argument count is used to select an appropriate command handler. Note that no two command processing methods of the same name may have the same number of arguments, as this would produce a parsing ambiguity. Thus it is not valid to have void cmd_hello(String s, int i) and void cmd_hello(int a, int b). If an appriate method cannot be found, the helper object is messaged with an argumentCountError().

The arguments to the command (which are string tokens) are cast to the appropriate data types for the method. Thus if the command processor method for hello is declared as void cmd_hello(String s, int i) , then the first argument is left as a string and the second argument is converted to an Integer object. The following object types are supported for arguments: String, int, Integer, boolean, Boolean, short, Short, long, Long, float, Float, double, Double. If an error occurrs during argument conversion, the helper object is messaged with an arugmentFormatError().

Once argument conversion is complete, the command processor method in the helper object is invoked with the argument list. If an error occurrs during the invocation, the helper object is messaged with an invocationError().

Author:
Mark Lindner, PING Software Group

Field Summary
private static java.lang.Class[] argtypes
           
private static java.lang.Class[] argtypes2
           
private  java.util.Hashtable cmds
           
private  CommandProcessor processor
           
private static java.lang.Class[] stringConstructor
           
 
Constructor Summary
CommandDispatcher(CommandProcessor processor)
          Construct a new CommandDispatcher for the given helper class.
 
Method Summary
 void dispatch(java.lang.String s)
          Dispatch a command string to the appropriate processing method.
private  boolean isSupported(java.lang.Class type)
           
private  java.lang.Class wrapperClass(java.lang.Class clazz)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

processor

private CommandProcessor processor

cmds

private java.util.Hashtable cmds

argtypes

private static final java.lang.Class[] argtypes

argtypes2

private static final java.lang.Class[] argtypes2

stringConstructor

private static final java.lang.Class[] stringConstructor
Constructor Detail

CommandDispatcher

public CommandDispatcher(CommandProcessor processor)
Construct a new CommandDispatcher for the given helper class.

Parameters:
processor - The helper class that serves as the command processor and error handler.
Method Detail

dispatch

public void dispatch(java.lang.String s)
Dispatch a command string to the appropriate processing method.

Parameters:
s - The command string to be parsed and passed to its associated command processing method.

wrapperClass

private java.lang.Class wrapperClass(java.lang.Class clazz)

isSupported

private boolean isSupported(java.lang.Class type)