kiwi.util
Class CommandDispatcher

java.lang.Object
  |
  +--kiwi.util.CommandDispatcher

public class CommandDispatcher
extends 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().

Version:
1.0 (05/98)
Author:
Mark Lindner, PING Software Group

Constructor Summary
CommandDispatcher(CommandProcessor processor)
          Construct a new CommandDispatcher for the given helper class.
 
Method Summary
 void dispatch(String s)
          Dispatch a command string to the appropriate processing method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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(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.