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

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

There is a newer version: 1.4
Show newest version
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.littleshoot.mina.handler.demux;

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

import org.littleshoot.mina.common.IoHandler;
import org.littleshoot.mina.common.IoHandlerAdapter;
import org.littleshoot.mina.common.IoSession;
import org.littleshoot.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 #addMessageHandler(Class, MessageHandler)} and * {@link #removeMessageHandler(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 #addMessageHandler(Class, MessageHandler)} and * {@link #removeMessageHandler(Class)} clear this cache. *

* * @author The Apache Directory Project ([email protected]) * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $ */ public class DemuxingIoHandler extends IoHandlerAdapter { private final Map findHandlerCache = new ConcurrentHashMap(); private final Map type2handler = new ConcurrentHashMap(); /** * Creates a new instance with no registered {@link MessageHandler}s. */ public DemuxingIoHandler() { } /** * 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 MessageHandler addMessageHandler(Class type, MessageHandler handler) { findHandlerCache.clear(); return type2handler.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 MessageHandler removeMessageHandler(Class type) { findHandlerCache.clear(); return type2handler.remove(type); } /** * Returns the {@link MessageHandler} which is registered to process * the specified type. */ @SuppressWarnings("unchecked") public MessageHandler getMessageHandler(Class type) { return type2handler.get(type); } /** * Returns the {@link Map} which contains all messageType-{@link MessageHandler} * pairs registered to this handler. */ public Map getMessageHandlerMap() { return Collections.unmodifiableMap(type2handler); } /** * Forwards the received events into the appropriate {@link MessageHandler} * which is registered by {@link #addMessageHandler(Class, MessageHandler)}. */ public void messageReceived(IoSession session, Object message) throws Exception { MessageHandler handler = findHandler(message.getClass()); if (handler != null) { handler.messageReceived(session, message); } else { throw new UnknownMessageTypeException( "No message handler found for message: " + message); } } protected MessageHandler findHandler(Class type) { return findHandler(type, null); } @SuppressWarnings("unchecked") private MessageHandler findHandler(Class type, Set triedClasses) { MessageHandler handler = null; if (triedClasses != null && triedClasses.contains(type)) return null; /* * Try the cache first. */ handler = findHandlerCache.get(type); if (handler != null) return handler; /* * Try the registered handlers for an immediate match. */ handler = type2handler.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 (int i = 0; i < interfaces.length; i++) { handler = findHandler(interfaces[i], 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(superclass); } /* * 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) findHandlerCache.put(type, handler); return handler; } }