Author: trustin
Date: Mon Nov 12 20:14:12 2007
New Revision: 594424

URL: http://svn.apache.org/viewvc?rev=594424&view=rev
Log:
Related issue: DIRMINA-372 (Generics support for IoHandler)
* in org.apache.mina.handler.demux package...
** Added ExceptionHandler
** Added DemuxingIoHandler.add/remove/find ExceptionHandler(...)
** Added MessageHandler.messageSent()

Added:
    
mina/trunk/core/src/main/java/org/apache/mina/handler/demux/ExceptionHandler.java
   (with props)
Modified:
    
mina/trunk/core/src/main/java/org/apache/mina/handler/demux/DemuxingIoHandler.java
    
mina/trunk/core/src/main/java/org/apache/mina/handler/demux/MessageHandler.java

Modified: 
mina/trunk/core/src/main/java/org/apache/mina/handler/demux/DemuxingIoHandler.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/handler/demux/DemuxingIoHandler.java?rev=594424&r1=594423&r2=594424&view=diff
==============================================================================
--- 
mina/trunk/core/src/main/java/org/apache/mina/handler/demux/DemuxingIoHandler.java
 (original)
+++ 
mina/trunk/core/src/main/java/org/apache/mina/handler/demux/DemuxingIoHandler.java
 Mon Nov 12 20:14:12 2007
@@ -78,11 +78,18 @@
  * @version $Rev$, $Date$
  */
 public class DemuxingIoHandler extends IoHandlerAdapter {
-    @SuppressWarnings("unchecked")
-    private final Map<Class<?>, MessageHandler> findHandlerCache = new 
ConcurrentHashMap<Class<?>, MessageHandler>();
+    
+    private final Map<Class<?>, MessageHandler<?>> messageHandlerCache =
+        new ConcurrentHashMap<Class<?>, MessageHandler<?>>();
 
-    @SuppressWarnings("unchecked")
-    private final Map<Class<?>, MessageHandler> type2handler = new 
ConcurrentHashMap<Class<?>, MessageHandler>();
+    private final Map<Class<?>, MessageHandler<?>> messageHandlers =
+        new ConcurrentHashMap<Class<?>, MessageHandler<?>>();
+
+    private final Map<Class<?>, ExceptionHandler<?>> exceptionHandlerCache =
+        new ConcurrentHashMap<Class<?>, ExceptionHandler<?>>();
+
+    private final Map<Class<?>, ExceptionHandler<?>> exceptionHandlers =
+        new ConcurrentHashMap<Class<?>, ExceptionHandler<?>>();
 
     /**
      * Creates a new instance with no registered [EMAIL PROTECTED] 
MessageHandler}s.
@@ -100,8 +107,8 @@
     @SuppressWarnings("unchecked")
     public <E> MessageHandler<? super E> addMessageHandler(Class<E> type,
             MessageHandler<? super E> handler) {
-        findHandlerCache.clear();
-        return type2handler.put(type, handler);
+        messageHandlerCache.clear();
+        return (MessageHandler<? super E>) messageHandlers.put(type, handler);
     }
 
     /**
@@ -112,8 +119,36 @@
      */
     @SuppressWarnings("unchecked")
     public <E> MessageHandler<? super E> removeMessageHandler(Class<E> type) {
-        findHandlerCache.clear();
-        return type2handler.remove(type);
+        messageHandlerCache.clear();
+        return (MessageHandler<? super E>) messageHandlers.remove(type);
+    }
+
+    /**
+     * Registers a [EMAIL PROTECTED] MessageHandler} that receives the 
messages of
+     * the specified <code>type</code>.
+     *
+     * @return the old handler if there is already a registered handler for
+     *         the specified <tt>type</tt>.  <tt>null</tt> otherwise.
+     */
+    @SuppressWarnings("unchecked")
+    public <E extends Throwable> 
+    ExceptionHandler<? super E> addExceptionHandler(
+            Class<E> type, ExceptionHandler<? super E> handler) {
+        exceptionHandlerCache.clear();
+        return (ExceptionHandler<? super E>) exceptionHandlers.put(type, 
handler);
+    }
+
+    /**
+     * Deregisters a [EMAIL PROTECTED] MessageHandler} that receives the 
messages of
+     * the specified <code>type</code>.
+     *
+     * @return the removed handler if successfully removed.  <tt>null</tt> 
otherwise.
+     */
+    @SuppressWarnings("unchecked")
+    public <E extends Throwable> ExceptionHandler<? super E>
+    removeExceptionHandler(Class<E> type) {
+        exceptionHandlerCache.clear();
+        return (ExceptionHandler<? super E>) exceptionHandlers.remove(type);
     }
 
     /**
@@ -122,16 +157,23 @@
      */
     @SuppressWarnings("unchecked")
     public <E> MessageHandler<? super E> getMessageHandler(Class<E> type) {
-        return type2handler.get(type);
+        return (MessageHandler<? super E>) messageHandlers.get(type);
     }
 
     /**
      * Returns the [EMAIL PROTECTED] Map} which contains all [EMAIL PROTECTED] 
MessageHandler}
      * pairs registered to this handler.
      */
-    @SuppressWarnings("unchecked")
-    public Map<Class<?>, MessageHandler> getMessageHandlerMap() {
-        return Collections.unmodifiableMap(type2handler);
+    public Map<Class<?>, MessageHandler<?>> getMessageHandlerMap() {
+        return Collections.unmodifiableMap(messageHandlers);
+    }
+
+    /**
+     * Returns the [EMAIL PROTECTED] Map} which contains all [EMAIL PROTECTED] 
MessageHandler}
+     * pairs registered to this handler.
+     */
+    public Map<Class<?>, ExceptionHandler<?>> getExceptionHandlerMap() {
+        return Collections.unmodifiableMap(exceptionHandlers);
     }
 
     /**
@@ -141,7 +183,7 @@
     @Override
     public void messageReceived(IoSession session, Object message)
             throws Exception {
-        MessageHandler<Object> handler = findHandler(message.getClass());
+        MessageHandler<Object> handler = 
findMessageHandler(message.getClass());
         if (handler != null) {
             handler.messageReceived(session, message);
         } else {
@@ -150,14 +192,58 @@
         }
     }
 
-    protected MessageHandler<Object> findHandler(Class<?> type) {
-        return findHandler(type, null);
+    @Override
+    public void messageSent(IoSession session, Object message) throws 
Exception {
+        MessageHandler<Object> handler = 
findMessageHandler(message.getClass());
+        if (handler != null) {
+            handler.messageSent(session, message);
+        } else {
+            throw new UnknownMessageTypeException(
+                    "No handler found for message: " + 
message.getClass().getName());
+        }
+    }
+
+    @Override
+    public void exceptionCaught(IoSession session, Throwable cause) throws 
Exception {
+        ExceptionHandler<Throwable> handler = 
findExceptionHandler(cause.getClass());
+        if (handler != null) {
+            handler.exceptionCaught(session, cause);
+        } else {
+            throw new UnknownMessageTypeException(
+                    "No handler found for exception: " + 
cause.getClass().getName());
+        }
+    }
+
+    protected MessageHandler<Object> findMessageHandler(Class<?> type) {
+        return findMessageHandler(type, null);
+    }
+
+    protected ExceptionHandler<Throwable> findExceptionHandler(Class<? extends 
Throwable> type) {
+        return findExceptionHandler(type, null);
     }
 
     @SuppressWarnings("unchecked")
-    private MessageHandler<Object> findHandler(Class type,
-            Set<Class> triedClasses) {
-        MessageHandler handler = null;
+    private MessageHandler<Object> findMessageHandler(
+            Class type, Set<Class> triedClasses) {
+        
+        return (MessageHandler<Object>) findHandler(
+                messageHandlers, messageHandlerCache, type, triedClasses);
+    }
+
+    @SuppressWarnings("unchecked")
+    private ExceptionHandler<Throwable> findExceptionHandler(
+            Class type, Set<Class> triedClasses) {
+        
+        return (ExceptionHandler<Throwable>) findHandler(
+                exceptionHandlers, exceptionHandlerCache, type, triedClasses);
+    }
+
+    @SuppressWarnings("unchecked")
+    private Object findHandler(
+            Map handlers, Map handlerCache,
+            Class type, Set<Class> triedClasses) {
+
+        Object handler = null;
 
         if (triedClasses != null && triedClasses.contains(type)) {
             return null;
@@ -166,7 +252,7 @@
         /*
          * Try the cache first.
          */
-        handler = findHandlerCache.get(type);
+        handler = handlerCache.get(type);
         if (handler != null) {
             return handler;
         }
@@ -174,7 +260,7 @@
         /*
          * Try the registered handlers for an immediate match.
          */
-        handler = type2handler.get(type);
+        handler = handlers.get(type);
 
         if (handler == null) {
             /*
@@ -188,7 +274,7 @@
 
             Class[] interfaces = type.getInterfaces();
             for (Class element : interfaces) {
-                handler = findHandler(element, triedClasses);
+                handler = findHandler(handlers, handlerCache, element, 
triedClasses);
                 if (handler != null) {
                     break;
                 }
@@ -200,10 +286,9 @@
              * No match in type's interfaces could be found. Search the
              * superclass.
              */
-
             Class superclass = type.getSuperclass();
             if (superclass != null) {
-                handler = findHandler(superclass);
+                handler = findHandler(handlers, handlerCache, superclass, 
null);
             }
         }
 
@@ -213,7 +298,7 @@
          * led to a match will be cached along with the immediate message type.
          */
         if (handler != null) {
-            findHandlerCache.put(type, handler);
+            handlerCache.put(type, handler);
         }
 
         return handler;

Added: 
mina/trunk/core/src/main/java/org/apache/mina/handler/demux/ExceptionHandler.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/handler/demux/ExceptionHandler.java?rev=594424&view=auto
==============================================================================
--- 
mina/trunk/core/src/main/java/org/apache/mina/handler/demux/ExceptionHandler.java
 (added)
+++ 
mina/trunk/core/src/main/java/org/apache/mina/handler/demux/ExceptionHandler.java
 Mon Nov 12 20:14:12 2007
@@ -0,0 +1,47 @@
+/*
+ *  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 org.apache.mina.common.IoSession;
+
+/**
+ * A handler interface that [EMAIL PROTECTED] DemuxingIoHandler} forwards
+ * <code>exceptionCaught</code> events to.  You have to register your
+ * handler with the type of exception you want to get notified using
+ * [EMAIL PROTECTED] DemuxingIoHandler#addExceptionHandler(Class, 
ExceptionHandler)}.
+ *
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ * @version $Rev$, $Date$
+ */
+public interface ExceptionHandler<E extends Throwable> {
+    /**
+     * A [EMAIL PROTECTED] ExceptionHandler} that does nothing.  This is 
usefule when
+     * you want to ignore messages of the specific type silently.
+     */
+    static ExceptionHandler<Throwable> NOOP = new 
ExceptionHandler<Throwable>() {
+        public void exceptionCaught(IoSession session, Throwable cause) {}
+    };
+
+    /**
+     * Invoked when the specific type of exception is caught from the
+     * specified <code>session</code>.
+     */
+    void exceptionCaught(IoSession session, E cause) throws Exception;
+}
\ No newline at end of file

Propchange: 
mina/trunk/core/src/main/java/org/apache/mina/handler/demux/ExceptionHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
mina/trunk/core/src/main/java/org/apache/mina/handler/demux/ExceptionHandler.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: 
mina/trunk/core/src/main/java/org/apache/mina/handler/demux/MessageHandler.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/handler/demux/MessageHandler.java?rev=594424&r1=594423&r2=594424&view=diff
==============================================================================
--- 
mina/trunk/core/src/main/java/org/apache/mina/handler/demux/MessageHandler.java 
(original)
+++ 
mina/trunk/core/src/main/java/org/apache/mina/handler/demux/MessageHandler.java 
Mon Nov 12 20:14:12 2007
@@ -36,8 +36,8 @@
      * you want to ignore messages of the specific type silently.
      */
     static MessageHandler<Object> NOOP = new MessageHandler<Object>() {
-        public void messageReceived(IoSession session, Object message) {
-        }
+        public void messageReceived(IoSession session, Object message) {}
+        public void messageSent(IoSession session, Object message) {}
     };
 
     /**
@@ -45,4 +45,10 @@
      * specified <code>session</code>.
      */
     void messageReceived(IoSession session, E message) throws Exception;
+    
+    /**
+     * Invoked when the specific type of message is received from the
+     * specified <code>session</code>.
+     */
+    void messageSent(IoSession session, E message) throws Exception;
 }


Reply via email to