directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r157372 - directory/network/mina/trunk/src/java/org/apache/mina/protocol/DemuxingProtocolHandler.java
Date Mon, 14 Mar 2005 03:45:18 GMT
Author: trustin
Date: Sun Mar 13 19:45:17 2005
New Revision: 157372

URL: http://svn.apache.org/viewcvs?view=rev&rev=157372
Log:
Added: DemuxingProtocolHandler

Added:
    directory/network/mina/trunk/src/java/org/apache/mina/protocol/DemuxingProtocolHandler.java
  (with props)

Added: directory/network/mina/trunk/src/java/org/apache/mina/protocol/DemuxingProtocolHandler.java
URL: http://svn.apache.org/viewcvs/directory/network/mina/trunk/src/java/org/apache/mina/protocol/DemuxingProtocolHandler.java?view=auto&rev=157372
==============================================================================
--- directory/network/mina/trunk/src/java/org/apache/mina/protocol/DemuxingProtocolHandler.java
(added)
+++ directory/network/mina/trunk/src/java/org/apache/mina/protocol/DemuxingProtocolHandler.java
Sun Mar 13 19:45:17 2005
@@ -0,0 +1,178 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.protocol;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * A {@link ProtocolHandler} that demuxes <code>messageReceived</code> events
+ * to the appropriate {@link MessageHandler}.
+ * 
+ * You can freely register and deregister {@link MessageHandler}s using
+ * {@link #registerMessageType(Class, MessageHandler)} and
+ * {@link #deregisterMessageType(Class)}.
+ * 
+ * @author The Apache Directory Project
+ * @version $Rev$, $Date$
+ */
+public abstract class DemuxingProtocolHandler implements ProtocolHandler
+{
+    private final Map type2handler = new HashMap();
+
+    /**
+     * Creates a new instance with no registered {@link MessageHandler}s.
+     */
+    protected DemuxingProtocolHandler()
+    {
+    }
+
+    /**
+     * Registers a {@link MessageHandler} that receives the messages of
+     * the specified <code>type</code>.
+     */
+    protected void registerMessageType( Class type, MessageHandler handler )
+    {
+        synchronized( type2handler )
+        {
+            type2handler.put( type, handler );
+        }
+    }
+
+    /**
+     * Deregisters a {@link MessageHandler} that receives the messages of
+     * the specified <code>type</code>.
+     */
+    protected void deregisterMessageType( Class clazz )
+    {
+        synchronized( type2handler )
+        {
+            type2handler.remove( clazz );
+        }
+    }
+
+    /**
+     * Forwards the received events into the appropriate {@link MessageHandler}
+     * which is registered by {@link #registerMessageType(Class, MessageHandler)}.
+     */
+    public void messageReceived( ProtocolSession session, Object message )
+    {
+        MessageHandler handler = getHandler( message.getClass() );
+        if( handler != null )
+        {
+            handler.messageReceived( session, message );
+        }
+        else
+        {
+            throw new UnknownMessageTypeException(
+                    "No message handler found for message: " + message );
+        }
+    }
+
+    private MessageHandler getHandler( Class type )
+    {
+        MessageHandler handler = ( MessageHandler ) type2handler.get( type );
+        if( handler == null )
+        {
+            handler = getHandler( type, new HashSet() );
+        }
+
+        return handler;
+    }
+
+    private MessageHandler getHandler( Class type, Set triedClassNames )
+    {
+        MessageHandler handler;
+
+        String typeName = type.getName();
+        if( triedClassNames.contains( typeName ) )
+            return null;
+        triedClassNames.add( typeName );
+
+        handler = ( MessageHandler ) type2handler.get( typeName );
+        if( handler == null )
+        {
+            handler = getHandler( type, triedClassNames );
+            if( handler != null )
+                return handler;
+
+            Class[] interfaces = type.getInterfaces();
+            for( int i = 0; i < interfaces.length; i ++ )
+            {
+                handler = getHandler( interfaces[ i ], triedClassNames );
+                if( handler != null )
+                    return handler;
+            }
+
+            return null;
+        }
+        else
+            return handler;
+    }
+
+    /**
+     * A handler interface that {@link DemuxingProtocolHandler} forwards
+     * <code>messageReceived</code> events to.  You have to register your
+     * handler with the type of message you want to get notified using
+     * {@link DemuxingProtocolHandler#registerMessageType(Class, MessageHandler)}.
+     * 
+     * @author The Apache Directory Project
+     * @version $Rev$, $Date$
+     */
+    public interface MessageHandler
+    {
+        /**
+         * A {@link MessageHandler} that does nothing.  This is usefule when
+         * you want to ignore messages of the specific type silently.
+         */
+        static MessageHandler NOOP = new MessageHandler()
+        {
+            public void messageReceived( ProtocolSession session, Object message )
+            {
+            }
+        };
+        
+        /**
+         * Invoked when the specific type of message is received from the
+         * specified <code>session</code>.
+         */
+        void messageReceived( ProtocolSession session, Object message );
+    }
+
+    /**
+     * An exception that is thrown when {@link DemuxingProtocolHandler}
+     * cannot find any {@link MessageHandler}s associated with the specific
+     * message type.  You have to use
+     * {@link DemuxingProtocolHandler#registerMessageType(Class, MessageHandler)}
+     * to associate a message type and a message handler. 
+     * 
+     * @author The Apache Directory Project
+     * @version $Rev$, $Date$
+     */
+    public class UnknownMessageTypeException extends RuntimeException
+    {
+        private static final long serialVersionUID = 3257290227428047158L;
+
+        public UnknownMessageTypeException()
+        {
+        }
+
+        public UnknownMessageTypeException( String message, Throwable cause )
+        {
+            super( message, cause );
+        }
+
+        public UnknownMessageTypeException( String message )
+        {
+            super( message );
+        }
+
+        public UnknownMessageTypeException( Throwable cause )
+        {
+            super( cause );
+        }
+    }
+}

Propchange: directory/network/mina/trunk/src/java/org/apache/mina/protocol/DemuxingProtocolHandler.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision



Mime
View raw message