com.thetransactioncompany.jsonrpc2.server
Class Dispatcher

java.lang.Object
  extended by com.thetransactioncompany.jsonrpc2.server.Dispatcher

public class Dispatcher
extends java.lang.Object

Dispatcher for JSON-RPC 2.0 requests and notifications.

Use the register() methods to add a request or notification handler for an RPC method.

Use the dispatch() methods to have an incoming request or notification processed by the matching handler.

The reportProcTime() method enables reporting of request processing time (in microseconds) by appending a non-standard "xProcTime" attribute to the resulting JSON-RPC 2.0 response message.

Example:

 { 
   "result"    : "xyz",
   "id"        : 1,
   "jsonrpc"   : "2.0",
   "xProcTime" : "189 us"
 }
 

Version:
1.5 (2011-07-15)
Author:
Vladimir Dzhuvinov

Constructor Summary
Dispatcher()
          Creates a new dispatcher with no registered handlers.
 
Method Summary
 void dispatch(com.thetransactioncompany.jsonrpc2.JSONRPC2Notification notification, MessageContext notificationCtx)
          Dispatches the specified JSON-RPC 2.0 notification to the appropriate handler for processing.
 com.thetransactioncompany.jsonrpc2.JSONRPC2Response dispatch(com.thetransactioncompany.jsonrpc2.JSONRPC2Request request, MessageContext requestCtx)
          Dispatches the specified JSON-RPC 2.0 request to the appropriate handler for processing and returns the response.
 NotificationHandler getNotificationHandler(java.lang.String notificationName)
          Gets the handler for the specified JSON-RPC 2.0 notification name.
 RequestHandler getRequestHandler(java.lang.String requestName)
          Gets the handler for the specified JSON-RPC 2.0 request name.
 java.lang.String[] handledNotifications()
          Returns the registered notification names.
 java.lang.String[] handledRequests()
          Returns the registered request names.
 void register(NotificationHandler handler)
          Registers a new JSON-RPC 2.0 notification handler.
 void register(RequestHandler handler)
          Registers a new JSON-RPC 2.0 request handler.
 void reportProcTime(boolean enable)
          Controls reporting of request processing time by appending a non-standard "xProcTime" attribute to the JSON-RPC 2.0 response.
 boolean reportsProcTime()
          Returns true if reporting of request processing time is enabled.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Dispatcher

public Dispatcher()
Creates a new dispatcher with no registered handlers.

Method Detail

register

public void register(RequestHandler handler)
Registers a new JSON-RPC 2.0 request handler.

Parameters:
handler - The request handler to register.
Throws:
java.lang.IllegalArgumentException - On attempting to register a handler that duplicates an existing request name.

register

public void register(NotificationHandler handler)
Registers a new JSON-RPC 2.0 notification handler.

Parameters:
handler - The notification handler to register.
Throws:
java.lang.IllegalArgumentException - On attempting to register a handler that duplicates an existing notification name.

handledRequests

public java.lang.String[] handledRequests()
Returns the registered request names.

Returns:
The request names.

handledNotifications

public java.lang.String[] handledNotifications()
Returns the registered notification names.

Returns:
The notification names.

getRequestHandler

public RequestHandler getRequestHandler(java.lang.String requestName)
Gets the handler for the specified JSON-RPC 2.0 request name.

Parameters:
requestName - The request name to lookup.
Returns:
The corresponding request handler or null if none was found.

getNotificationHandler

public NotificationHandler getNotificationHandler(java.lang.String notificationName)
Gets the handler for the specified JSON-RPC 2.0 notification name.

Parameters:
notificationName - The notification name to lookup.
Returns:
The corresponding notification handler or null if none was found.

dispatch

public com.thetransactioncompany.jsonrpc2.JSONRPC2Response dispatch(com.thetransactioncompany.jsonrpc2.JSONRPC2Request request,
                                                                    MessageContext requestCtx)
Dispatches the specified JSON-RPC 2.0 request to the appropriate handler for processing and returns the response.

Parameters:
request - The JSON-RPC 2.0 request to dispatch for processing.
requestCtx - Context information about the request, may be null if undefined.
Returns:
The response, which may indicate a processing error, such as METHOD_NOT_FOUND.

dispatch

public void dispatch(com.thetransactioncompany.jsonrpc2.JSONRPC2Notification notification,
                     MessageContext notificationCtx)
Dispatches the specified JSON-RPC 2.0 notification to the appropriate handler for processing.

Note that JSON-RPC 2.0 notifications don't produce a response!

Parameters:
notification - The JSON-RPC 2.0 notification to dispatch for processing.
notificationCtx - Context information about the notification, may be null if undefined.

reportProcTime

public void reportProcTime(boolean enable)
Controls reporting of request processing time by appending a non-standard "xProcTime" attribute to the JSON-RPC 2.0 response. Reporting is disabled by default.

Parameters:
enable - true to enable proccessing time reporting, false to disable it.

reportsProcTime

public boolean reportsProcTime()
Returns true if reporting of request processing time is enabled. See the reportProcTime description for more information.

Returns:
true if reporting of request processing time is enabled, else false.


Copyright © 2010 - 2012 Vladimir Dzhuvinov. All Rights Reserved.