org.littleshoot.mina.handler.demux.DemuxingIoHandler Maven / Gradle / Ivy
/*
* 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 super E> addMessageHandler(Class type,
MessageHandler super E> 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 super E> 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 super E> 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