public class MessageDispatcher extends Object
Constructor and Description |
---|
MessageDispatcher(TypeResolver resolver)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
addClass(Class targetClass)
Perform the Java reflection operations needed to do JSON message
dispatch on a given Java class.
|
void |
dispatchMessage(Deliverer from,
DispatchTarget target,
JSONObject message)
Dispatch a received JSON message by invoking the appropriate JSON method
on the appropriate object with the parameters from the message.
|
public MessageDispatcher(TypeResolver resolver)
resolver
- Type resolver for the type tags of JSON encoded message
parameter objects.public void addClass(Class targetClass)
JSONMethod
annotation.
JSON message handler methods must have public scope, a return type of
void, and a least one parameter, the first of which must have a type
assignable to a variable of type Deliverer
.
The name of the method is the name of the JSON message verb that the
method handles. The value of the attached JSONMethod
annotation
is an array of Strings, one for each method parameter except the initial
Deliverer
parameter. These strings will be the names of JSON
message parameters and will be mapped one-to-one to the corresponding
parameters of the method itself when it is invoked to handle a JSON
message.
If a method is annotated JSONMethod
but does not follow these
rules, no dispatch information will be recorded for that method and an
error message will be logged.targetClass
- Class to compute method dispatch information for.JSONSetupError
- if an annotated method breaks the rules for a
JSON method.public void dispatchMessage(Deliverer from, DispatchTarget target, JSONObject message) throws MessageHandlerException
SourceRetargeter
, then
'from' is replaced with result of calling its findEffectiveSource()
method.
Second, if 'target' is an instance of MessageRetargeter
, then
'target' is replaced with the result of calling its findActualTarget()
method. This
step is repeated as many times as necessary until 'target' is no longer
an instance of MessageRetargeter
.
If 'target' has a method with the same name as the message verb in
'message' and which matches the message handler signature pattern as
described in the description of the addClass()
method,
then this method is invoked to handle the message and the message
dispatch operation is complete. Note: for this to work, the 'target's
class must have previously been inserted into this dispatcher using the
addClass()
method.
If the previous step failed to located a message handler method, but
'target' is an instance of DefaultDispatchTarget
, then its
handleMessage()
method is
invoked to handle the message and the message dispatch operation is
complete. Otherwise a MessageHandlerException
is thrown.from
- The source from whom the message was allegedly received.target
- The object to which the message is addressed.message
- The message itself.MessageHandlerException
- if there was some kind of problem
handling the message.Copyright © 2017. All rights reserved.