All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.mina.handler.demux.DemuxingIoHandler Maven / Gradle / Ivy

/**
 * Copyright 2007-2015, Kaazing Corporation. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.mina.handler.demux;

import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.session.UnknownMessageTypeException;
import org.apache.mina.util.IdentityHashSet;

/**
 * A {@link IoHandler} that demuxes messageReceived events
 * to the appropriate {@link MessageHandler}.
 * 

* You can freely register and deregister {@link MessageHandler}s using * {@link #addReceivedMessageHandler(Class, MessageHandler)} and * {@link #removeReceivedMessageHandler(Class)}. *

*

* When message is received through a call to * {@link #messageReceived(IoSession, Object)} the class of the * message object will be used to find a {@link MessageHandler} for * that particular message type. If no {@link 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 * {@link #addReceivedMessageHandler(Class, MessageHandler)} and * {@link #removeReceivedMessageHandler(Class)} clear this cache. *

* * @author Apache MINA Project */ public class DemuxingIoHandler extends IoHandlerAdapter { private final Map, MessageHandler> receivedMessageHandlerCache = new ConcurrentHashMap, MessageHandler>(); private final Map, MessageHandler> receivedMessageHandlers = new ConcurrentHashMap, MessageHandler>(); private final Map, MessageHandler> sentMessageHandlerCache = new ConcurrentHashMap, MessageHandler>(); private final Map, MessageHandler> sentMessageHandlers = new ConcurrentHashMap, MessageHandler>(); private final Map, ExceptionHandler> exceptionHandlerCache = new ConcurrentHashMap, ExceptionHandler>(); private final Map, ExceptionHandler> exceptionHandlers = new ConcurrentHashMap, ExceptionHandler>(); /** * Creates a new instance with no registered {@link MessageHandler}s. */ public DemuxingIoHandler() { // Do nothing } /** * Registers a {@link MessageHandler} that handles the received messages of * the specified type. * * @return the old handler if there is already a registered handler for * the specified type. null otherwise. */ @SuppressWarnings("unchecked") public MessageHandler addReceivedMessageHandler(Class type, MessageHandler handler) { receivedMessageHandlerCache.clear(); return (MessageHandler) receivedMessageHandlers.put(type, handler); } /** * Deregisters a {@link MessageHandler} that handles the received messages * of the specified type. * * @return the removed handler if successfully removed. null otherwise. */ @SuppressWarnings("unchecked") public MessageHandler removeReceivedMessageHandler(Class type) { receivedMessageHandlerCache.clear(); return (MessageHandler) receivedMessageHandlers.remove(type); } /** * Registers a {@link MessageHandler} that handles the sent messages of the * specified type. * * @return the old handler if there is already a registered handler for * the specified type. null otherwise. */ @SuppressWarnings("unchecked") public MessageHandler addSentMessageHandler(Class type, MessageHandler handler) { sentMessageHandlerCache.clear(); return (MessageHandler) sentMessageHandlers.put(type, handler); } /** * Deregisters a {@link MessageHandler} that handles the sent messages of * the specified type. * * @return the removed handler if successfully removed. null otherwise. */ @SuppressWarnings("unchecked") public MessageHandler removeSentMessageHandler(Class type) { sentMessageHandlerCache.clear(); return (MessageHandler) sentMessageHandlers.remove(type); } /** * Registers a {@link MessageHandler} that receives the messages of * the specified type. * * @return the old handler if there is already a registered handler for * the specified type. null otherwise. */ @SuppressWarnings("unchecked") public ExceptionHandler addExceptionHandler( Class type, ExceptionHandler handler) { exceptionHandlerCache.clear(); return (ExceptionHandler) exceptionHandlers.put(type, handler); } /** * Deregisters a {@link MessageHandler} that receives the messages of * the specified type. * * @return the removed handler if successfully removed. null otherwise. */ @SuppressWarnings("unchecked") public ExceptionHandler removeExceptionHandler(Class type) { exceptionHandlerCache.clear(); return (ExceptionHandler) exceptionHandlers.remove(type); } /** * Returns the {@link MessageHandler} which is registered to process * the specified type. */ @SuppressWarnings("unchecked") public MessageHandler getMessageHandler(Class type) { return (MessageHandler) receivedMessageHandlers.get(type); } /** * Returns the {@link Map} which contains all messageType-{@link MessageHandler} * pairs registered to this handler for received messages. */ public Map, MessageHandler> getReceivedMessageHandlerMap() { return Collections.unmodifiableMap(receivedMessageHandlers); } /** * Returns the {@link Map} which contains all messageType-{@link MessageHandler} * pairs registered to this handler for sent messages. */ public Map, MessageHandler> getSentMessageHandlerMap() { return Collections.unmodifiableMap(sentMessageHandlers); } /** * Returns the {@link Map} which contains all messageType-{@link MessageHandler} * pairs registered to this handler. */ public Map, ExceptionHandler> getExceptionHandlerMap() { return Collections.unmodifiableMap(exceptionHandlers); } /** * Forwards the received events into the appropriate {@link MessageHandler} * which is registered by {@link #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. */ @Override public void messageReceived(IoSession session, Object message) throws Exception { MessageHandler handler = findReceivedMessageHandler(message.getClass()); if (handler != null) { handler.handleMessage(session, message); } else { throw new UnknownMessageTypeException( "No message handler found for message type: " + message.getClass().getSimpleName()); } } /** * 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. */ @Override public void messageSent(IoSession session, Object message) throws Exception { MessageHandler handler = findSentMessageHandler(message.getClass()); if (handler != null) { handler.handleMessage(session, message); } else { throw new UnknownMessageTypeException( "No handler found for message type: " + message.getClass().getSimpleName()); } } /** * 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. */ @Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { ExceptionHandler handler = findExceptionHandler(cause.getClass()); if (handler != null) { handler.exceptionCaught(session, cause); } else { throw new UnknownMessageTypeException( "No handler found for exception type: " + cause.getClass().getSimpleName()); } } protected MessageHandler findReceivedMessageHandler(Class type) { return findReceivedMessageHandler(type, null); } protected MessageHandler findSentMessageHandler(Class type) { return findSentMessageHandler(type, null); } protected ExceptionHandler findExceptionHandler(Class type) { return findExceptionHandler(type, null); } @SuppressWarnings("unchecked") private MessageHandler findReceivedMessageHandler( Class type, Set triedClasses) { return (MessageHandler) findHandler( receivedMessageHandlers, receivedMessageHandlerCache, type, triedClasses); } @SuppressWarnings("unchecked") private MessageHandler findSentMessageHandler( Class type, Set triedClasses) { return (MessageHandler) findHandler( sentMessageHandlers, sentMessageHandlerCache, type, triedClasses); } @SuppressWarnings("unchecked") private ExceptionHandler findExceptionHandler( Class type, Set triedClasses) { return (ExceptionHandler) findHandler( exceptionHandlers, exceptionHandlerCache, type, triedClasses); } @SuppressWarnings("unchecked") private Object findHandler( Map handlers, Map handlerCache, Class type, Set triedClasses) { Object handler = null; if (triedClasses != null && triedClasses.contains(type)) { return null; } /* * Try the cache first. */ handler = handlerCache.get(type); if (handler != null) { return handler; } /* * Try the registered handlers for an immediate match. */ handler = handlers.get(type); if (handler == null) { /* * No immediate match could be found. Search the type's interfaces. */ if (triedClasses == null) { triedClasses = new IdentityHashSet(); } triedClasses.add(type); Class[] interfaces = type.getInterfaces(); for (Class element : interfaces) { handler = findHandler(handlers, handlerCache, element, triedClasses); if (handler != null) { break; } } } if (handler == null) { /* * No match in type's interfaces could be found. Search the * superclass. */ Class superclass = type.getSuperclass(); if (superclass != null) { handler = findHandler(handlers, handlerCache, superclass, null); } } /* * Make sure the handler is added to the cache. By updating the cache * here all the types (superclasses and interfaces) in the path which * led to a match will be cached along with the immediate message type. */ if (handler != null) { handlerCache.put(type, handler); } return handler; } }