org.apache.mina.handler.demux
Class DemuxingIoHandler

java.lang.Object
  extended by org.apache.mina.core.service.IoHandlerAdapter
      extended by org.apache.mina.handler.demux.DemuxingIoHandler
All Implemented Interfaces:
IoHandler

public class DemuxingIoHandler
extends IoHandlerAdapter

A IoHandler that demuxes messageReceived events to the appropriate MessageHandler.

You can freely register and deregister MessageHandlers using addReceivedMessageHandler(Class, MessageHandler) and removeReceivedMessageHandler(Class).

When message is received through a call to messageReceived(IoSession, Object) the class of the message object will be used to find a MessageHandler for that particular message type. If no MessageHandler instance can be found for the immediate class (i.e. message.getClass()) the interfaces implemented by the immediate class will be searched in depth-first order. If no match can be found for any of the interfaces the search will be repeated recursively for the superclass of the immediate class (i.e. message.getClass().getSuperclass()).

Consider the following type hierarchy (Cx are classes while Ix are interfaces):

     C3 - I7 - I9
      |    |   /\
      |   I8  I3 I4
      |
     C2 - I5 - I6
      |
     C1 - I1 - I2 - I4
      |         |
      |        I3
    Object
 
When message is of type C3 this hierarchy will be searched in the following order: C3, I7, I8, I9, I3, I4, C2, I5, I6, C1, I1, I2, I3, I4, Object.

For efficiency searches will be cached. Calls to addReceivedMessageHandler(Class, MessageHandler) and removeReceivedMessageHandler(Class) clear this cache.

Author:
Apache MINA Project

Constructor Summary
DemuxingIoHandler()
          Creates a new instance with no registered MessageHandlers.
 
Method Summary
<E extends java.lang.Throwable>
ExceptionHandler<? super E>
addExceptionHandler(java.lang.Class<E> type, ExceptionHandler<? super E> handler)
          Registers a MessageHandler that receives the messages of the specified type.
<E> MessageHandler<? super E>
addReceivedMessageHandler(java.lang.Class<E> type, MessageHandler<? super E> handler)
          Registers a MessageHandler that handles the received messages of the specified type.
<E> MessageHandler<? super E>
addSentMessageHandler(java.lang.Class<E> type, MessageHandler<? super E> handler)
          Registers a MessageHandler that handles the sent messages of the specified type.
 void exceptionCaught(IoSession session, java.lang.Throwable cause)
          Invoked when any exception is thrown by user IoHandler implementation or by MINA.
protected  ExceptionHandler<java.lang.Throwable> findExceptionHandler(java.lang.Class<? extends java.lang.Throwable> type)
           
protected  MessageHandler<java.lang.Object> findReceivedMessageHandler(java.lang.Class<?> type)
           
protected  MessageHandler<java.lang.Object> findSentMessageHandler(java.lang.Class<?> type)
           
 java.util.Map<java.lang.Class<?>,ExceptionHandler<?>> getExceptionHandlerMap()
          Returns the Map which contains all messageType-MessageHandler pairs registered to this handler.
<E> MessageHandler<? super E>
getMessageHandler(java.lang.Class<E> type)
          Returns the MessageHandler which is registered to process the specified type.
 java.util.Map<java.lang.Class<?>,MessageHandler<?>> getReceivedMessageHandlerMap()
          Returns the Map which contains all messageType-MessageHandler pairs registered to this handler for received messages.
 java.util.Map<java.lang.Class<?>,MessageHandler<?>> getSentMessageHandlerMap()
          Returns the Map which contains all messageType-MessageHandler pairs registered to this handler for sent messages.
 void messageReceived(IoSession session, java.lang.Object message)
          Forwards the received events into the appropriate MessageHandler which is registered by addReceivedMessageHandler(Class, MessageHandler).
 void messageSent(IoSession session, java.lang.Object message)
          Invoked when a message written by IoSession.write(Object) is sent out.
<E extends java.lang.Throwable>
ExceptionHandler<? super E>
removeExceptionHandler(java.lang.Class<E> type)
          Deregisters a MessageHandler that receives the messages of the specified type.
<E> MessageHandler<? super E>
removeReceivedMessageHandler(java.lang.Class<E> type)
          Deregisters a MessageHandler that handles the received messages of the specified type.
<E> MessageHandler<? super E>
removeSentMessageHandler(java.lang.Class<E> type)
          Deregisters a MessageHandler that handles the sent messages of the specified type.
 
Methods inherited from class org.apache.mina.core.service.IoHandlerAdapter
sessionClosed, sessionCreated, sessionIdle, sessionOpened
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DemuxingIoHandler

public DemuxingIoHandler()
Creates a new instance with no registered MessageHandlers.

Method Detail

addReceivedMessageHandler

public <E> MessageHandler<? super E> addReceivedMessageHandler(java.lang.Class<E> type,
                                                               MessageHandler<? super E> handler)
Registers a MessageHandler that handles the received messages of the specified type.

Returns:
the old handler if there is already a registered handler for the specified type. null otherwise.

removeReceivedMessageHandler

public <E> MessageHandler<? super E> removeReceivedMessageHandler(java.lang.Class<E> type)
Deregisters a MessageHandler that handles the received messages of the specified type.

Returns:
the removed handler if successfully removed. null otherwise.

addSentMessageHandler

public <E> MessageHandler<? super E> addSentMessageHandler(java.lang.Class<E> type,
                                                           MessageHandler<? super E> handler)
Registers a MessageHandler that handles the sent messages of the specified type.

Returns:
the old handler if there is already a registered handler for the specified type. null otherwise.

removeSentMessageHandler

public <E> MessageHandler<? super E> removeSentMessageHandler(java.lang.Class<E> type)
Deregisters a MessageHandler that handles the sent messages of the specified type.

Returns:
the removed handler if successfully removed. null otherwise.

addExceptionHandler

public <E extends java.lang.Throwable> ExceptionHandler<? super E> addExceptionHandler(java.lang.Class<E> type,
                                                                                       ExceptionHandler<? super E> handler)
Registers a MessageHandler that receives the messages of the specified type.

Returns:
the old handler if there is already a registered handler for the specified type. null otherwise.

removeExceptionHandler

public <E extends java.lang.Throwable> ExceptionHandler<? super E> removeExceptionHandler(java.lang.Class<E> type)
Deregisters a MessageHandler that receives the messages of the specified type.

Returns:
the removed handler if successfully removed. null otherwise.

getMessageHandler

public <E> MessageHandler<? super E> getMessageHandler(java.lang.Class<E> type)
Returns the MessageHandler which is registered to process the specified type.


getReceivedMessageHandlerMap

public java.util.Map<java.lang.Class<?>,MessageHandler<?>> getReceivedMessageHandlerMap()
Returns the Map which contains all messageType-MessageHandler pairs registered to this handler for received messages.


getSentMessageHandlerMap

public java.util.Map<java.lang.Class<?>,MessageHandler<?>> getSentMessageHandlerMap()
Returns the Map which contains all messageType-MessageHandler pairs registered to this handler for sent messages.


getExceptionHandlerMap

public java.util.Map<java.lang.Class<?>,ExceptionHandler<?>> getExceptionHandlerMap()
Returns the Map which contains all messageType-MessageHandler pairs registered to this handler.


messageReceived

public void messageReceived(IoSession session,
                            java.lang.Object message)
                     throws java.lang.Exception
Forwards the received events into the appropriate MessageHandler which is registered by addReceivedMessageHandler(Class, MessageHandler). Warning ! If you are to overload this method, be aware that you _must_ call the messageHandler in your own method, otherwise it won't be called.

Specified by:
messageReceived in interface IoHandler
Overrides:
messageReceived in class IoHandlerAdapter
Throws:
java.lang.Exception

messageSent

public void messageSent(IoSession session,
                        java.lang.Object message)
                 throws java.lang.Exception
Invoked when a message written by IoSession.write(Object) is sent out. Warning ! If you are to overload this method, be aware that you _must_ call the messageHandler in your own method, otherwise it won't be called.

Specified by:
messageSent in interface IoHandler
Overrides:
messageSent in class IoHandlerAdapter
Throws:
java.lang.Exception

exceptionCaught

public void exceptionCaught(IoSession session,
                            java.lang.Throwable cause)
                     throws java.lang.Exception
Invoked when any exception is thrown by user IoHandler implementation or by MINA. If cause is an instance of IOException, MINA will close the connection automatically. Warning ! If you are to overload this method, be aware that you _must_ call the messageHandler in your own method, otherwise it won't be called.

Specified by:
exceptionCaught in interface IoHandler
Overrides:
exceptionCaught in class IoHandlerAdapter
Throws:
java.lang.Exception

findReceivedMessageHandler

protected MessageHandler<java.lang.Object> findReceivedMessageHandler(java.lang.Class<?> type)

findSentMessageHandler

protected MessageHandler<java.lang.Object> findSentMessageHandler(java.lang.Class<?> type)

findExceptionHandler

protected ExceptionHandler<java.lang.Throwable> findExceptionHandler(java.lang.Class<? extends java.lang.Throwable> type)


Copyright © 2004-2011 Apache MINA Project. All Rights Reserved.