public class CommandDispatcher extends Object
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()
.
Modifier and Type | Field and Description |
---|---|
private static Class[] |
argtypes |
private static Class[] |
argtypes2 |
private Hashtable |
cmds |
private CommandProcessor |
processor |
private static Class[] |
stringConstructor |
Constructor and Description |
---|
CommandDispatcher(CommandProcessor processor)
Construct a new
CommandDispatcher for the given helper
class. |
Modifier and Type | Method and Description |
---|---|
void |
dispatch(String s)
Dispatch a command string to the appropriate processing method.
|
private boolean |
isSupported(Class type) |
private Class |
wrapperClass(Class clazz) |
private CommandProcessor processor
private static final Class[] stringConstructor
public CommandDispatcher(CommandProcessor processor)
CommandDispatcher
for the given helper
class.processor
- The helper class that serves as the command processor
and error handler.public void dispatch(String s)
s
- The command string to be parsed and passed to its associated
command processing method.private Class wrapperClass(Class clazz)
private boolean isSupported(Class type)
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses