org.apache.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.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 super E> addReceivedMessageHandler(Class type,
MessageHandler super E> handler) {
receivedMessageHandlerCache.clear();
return (MessageHandler super E>) 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 super E> removeReceivedMessageHandler(Class type) {
receivedMessageHandlerCache.clear();
return (MessageHandler super E>) 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 super E> addSentMessageHandler(Class type,
MessageHandler super E> handler) {
sentMessageHandlerCache.clear();
return (MessageHandler super E>) 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 super E> removeSentMessageHandler(Class type) {
sentMessageHandlerCache.clear();
return (MessageHandler super E>) 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 super E> addExceptionHandler(
Class type, ExceptionHandler super E> handler) {
exceptionHandlerCache.clear();
return (ExceptionHandler super E>) 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 super E>
removeExceptionHandler(Class type) {
exceptionHandlerCache.clear();
return (ExceptionHandler super E>) exceptionHandlers.remove(type);
}
/**
* Returns the {@link MessageHandler} which is registered to process
* the specified type
.
*/
@SuppressWarnings("unchecked")
public MessageHandler super E> getMessageHandler(Class type) {
return (MessageHandler super E>) 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