throw new InvalidConfigurationException("Can not accept a callback listener since there are no " +
"ServerInvocationHandlers registered. Please add via xml configuration " +
"or via the Connector's addInvocationHandler() method.");
}
InvokerCallbackHandler callbackHandler = getCallbackHandler(invocation);
handler.addListener(callbackHandler);
}
else if(InternalInvocation.REMOVELISTENER.equals(methodName))
{
ServerInvokerCallbackHandler callbackHandler = removeCallbackHandler(invocation);
if(callbackHandler != null)
{
if(handler == null)
{
throw new InvalidConfigurationException("Can not remove a callback listener since there are no " +
"ServerInvocationHandlers registered. Please add via xml configuration " +
"or via the Connector's addInvocationHandler() method.");
}
handler.removeListener(callbackHandler);
if(log.isTraceEnabled())
{
log.trace("ServerInvoker (" + this + ") removing server callback handler " + callbackHandler + ".");
}
callbackHandler.destroy();
}
else
{
String sessionId = ServerInvokerCallbackHandler.getId(invocation);
throw new RuntimeException("Can not remove callback listener from target server with id of " + sessionId + " as it does not exist as a registered callback listener.");
}
}
else if(InternalInvocation.GETCALLBACKS.equals(methodName))
{
ServerInvokerCallbackHandler callbackHandler = getCallbackHandler(invocation);
if(log.isTraceEnabled())
{
log.trace("ServerInvoker (" + this + ") getting callbacks for callback handler " + callbackHandler + ".");
}
result = callbackHandler.getCallbacks();
}
else if(InternalInvocation.ADDCLIENTLISTENER.equals(methodName))
{
String sessionId = ServerInvokerCallbackHandler.getId(invocation);
Object[] params = param.getParameters();
// the only elements should be the callback handler and possibly the callback handle object
if(params == null || params.length < 0 || params.length > 3)
{
log.error("Recieved addClientListener InternalInvocation, but getParameters() " +
"returned: " + params);
throw new RuntimeException("InvokerCallbackHandler and callback handle object (optional) must be supplied as the only " +
"parameter objects within the InternalInvocation when " +
"calling addClientListener.");
}
InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) params[0];
Object callbackHandleObject = params[1];
CallbackContainer callbackContainer = new CallbackContainer(callbackHandler, callbackHandleObject);
clientCallbackListener.put(sessionId, callbackContainer);
log.debug("ServerInvoker (" + this + ") added client callback handler " + callbackHandler + " with session id of " + sessionId +
" and callback handle object of " + callbackHandleObject + ".");
}
else if(InternalInvocation.REMOVECLIENTLISTENER.equals(methodName))
{
String sessionId = ServerInvokerCallbackHandler.getId(invocation);
log.debug("ServerInvoker (" + this + ") removing client callback handler with session id of " + sessionId + ".");
Object cbo = clientCallbackListener.remove(sessionId);
if(cbo == null)
{
throw new RuntimeException("Can not remove callback listener from callback server with id of " + sessionId + " as it does not exist as a registered callback listener.");
}
}
else if(InternalInvocation.HANDLECALLBACK.equals(methodName))
{
String sessionId = ServerInvokerCallbackHandler.getId(invocation);
if(log.isTraceEnabled())
{
log.trace("ServerInvoker (" + this + ") is being asked to deliver callback on client callback handler with session id of " + sessionId + ".");
}
CallbackContainer callbackContainer = (CallbackContainer) clientCallbackListener.get(sessionId);
if(callbackContainer != null && callbackContainer.getCallbackHandler() != null)
{
Object[] params = param.getParameters();
Callback callbackRequest = (Callback) params[0];
Map callbackHandleObject = callbackRequest.getReturnPayload();
if(callbackHandleObject == null)
{
callbackHandleObject = new HashMap();
}
callbackHandleObject.put(Callback.CALLBACK_HANDLE_OBJECT_KEY, callbackContainer.getCallbackHandleObject());
callbackRequest.setReturnPayload(callbackHandleObject);
InvokerCallbackHandler callbackHandler = callbackContainer.getCallbackHandler();
callbackHandler.handleCallback(callbackRequest);
}
else
{
log.error("Could not find callback handler to call upon for handleCallback " +
"where session id equals " + sessionId);