directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r122732 - in incubator/directory/network/trunk/mina/src: examples/org/apache/mina/examples/reverser java/org/apache/mina/protocol java/org/apache/mina/util
Date Sun, 19 Dec 2004 02:39:55 GMT
Author: trustin
Date: Sat Dec 18 18:39:54 2004
New Revision: 122732

URL: http://svn.apache.org/viewcvs?view=rev&rev=122732
Log:
 * Added the same filter feature with I/O layer to Protocol layer (IoAdapter)
Added:
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ProtocolHandlerFilterManager.java
  (contents, props changed)
Modified:
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/Main.java
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSession.java

Modified: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/Main.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/Main.java?view=diff&rev=122732&p1=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/Main.java&r1=122731&p2=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/Main.java&r2=122732
==============================================================================
--- incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/Main.java
(original)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/Main.java
Sat Dec 18 18:39:54 2004
@@ -38,7 +38,7 @@
     {
         Acceptor acceptor = new TcpAcceptor();
         acceptor.bind( new InetSocketAddress( PORT ),
-                       IoAdapter.adapt( new ReverseProtocolProvider() ) );
+                       new IoAdapter().adapt( new ReverseProtocolProvider() ) );
         System.out.println( "Listening on port " + PORT );
     }
 }

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java?view=diff&rev=122732&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java&r1=122731&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java&r2=122732
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java
Sat Dec 18 18:39:54 2004
@@ -20,13 +20,13 @@
 
 import java.net.SocketAddress;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.mina.core.*;
-import org.apache.mina.io.ReadBuffer;
-import org.apache.mina.io.IoSession;
+import org.apache.mina.core.IdleStatus;
+import org.apache.mina.core.SessionConfig;
 import org.apache.mina.io.IoHandler;
+import org.apache.mina.io.IoSession;
+import org.apache.mina.io.ReadBuffer;
 import org.apache.mina.io.WriteBuffer;
+import org.apache.mina.util.ProtocolHandlerFilterManager;
 import org.apache.mina.util.Queue;
 
 /**
@@ -37,24 +37,39 @@
  */
 public class IoAdapter
 {
-    private static final Log log = LogFactory.getLog( IoAdapter.class );
+    private final ProtocolHandlerFilterManager filterManager = new ProtocolHandlerFilterManager();
+
+    public IoAdapter()
+    {
+    }
+
+    public void addFilter( int priority, ProtocolHandlerFilter filter )
+    {
+        filterManager.addFilter( priority, filter );
+    }
 
-    public static IoHandler adapt( ProtocolProvider protocolProvider )
+    public void removeFilter( ProtocolHandlerFilter filter )
+    {
+        filterManager.removeFilter( filter );
+    }
+
+    public IoHandler adapt( ProtocolProvider protocolProvider )
     {
         return new SessionHandlerAdapter( protocolProvider );
     }
 
-    private static class SessionHandlerAdapter implements IoHandler
+    private class SessionHandlerAdapter implements IoHandler
     {
         private final ProtocolEncoder encoder;
-        
+
         private final ProtocolDecoder decoder;
 
         private final ProtocolHandler handler;
 
         public SessionHandlerAdapter( ProtocolProvider protocolProvider )
         {
-            ProtocolCodecFactory codecFactory = protocolProvider.getCodecFactory();
+            ProtocolCodecFactory codecFactory = protocolProvider
+                    .getCodecFactory();
             this.encoder = codecFactory.newEncoder();
             this.decoder = codecFactory.newDecoder();
             this.handler = protocolProvider.getHandler();
@@ -64,30 +79,31 @@
         {
             ProtocolSession psession = new ProtocolSessionImpl( session, this );
             session.setAttachment( psession );
-            fireSessionOpened( psession );
+            filterManager.fireSessionOpened( psession );
         }
 
         public void sessionClosed( IoSession session )
         {
-            fireSessionClosed( ( ProtocolSession ) session.getAttachment() );
+            filterManager.fireSessionClosed( ( ProtocolSession ) session
+                    .getAttachment() );
         }
 
         public void sessionIdle( IoSession session, IdleStatus status )
         {
-            fireSessionIdle( ( ProtocolSession ) session.getAttachment(),
-                             status );
+            filterManager.fireSessionIdle( ( ProtocolSession ) session
+                    .getAttachment(), status );
         }
 
         public void exceptionCaught( IoSession session, Throwable cause )
         {
-            fireExceptionCaught( ( ProtocolSession ) session.getAttachment(),
-                                 cause );
+            filterManager.fireExceptionCaught( ( ProtocolSession ) session
+                    .getAttachment(), cause );
         }
 
         public void dataRead( IoSession session, int readBytes )
         {
             ProtocolSession psession = ( ProtocolSession ) session
-                                                                  .getAttachment();
+                    .getAttachment();
             ReadBuffer in = session.getReadBuffer();
             int sizeBefore;
             int sizeAfter;
@@ -113,7 +129,7 @@
 
                     if( result != null )
                     {
-                        fireMessageReceived( psession, result );
+                        filterManager.fireMessageReceived( psession, result );
                     }
                     else
                     {
@@ -124,7 +140,7 @@
             }
             catch( Throwable t )
             {
-                fireExceptionCaught( psession, t );
+                filterManager.fireExceptionCaught( psession, t );
             }
         }
 
@@ -135,14 +151,14 @@
 
         public void markerReleased( IoSession session, Object marker )
         {
-            fireMessageSent( ( ProtocolSession ) session.getAttachment(),
-                             marker );
+            filterManager.fireMessageSent( ( ProtocolSession ) session
+                    .getAttachment(), marker );
         }
 
         private void write( IoSession session )
         {
             ProtocolSessionImpl psession = ( ProtocolSessionImpl ) session
-                                                                          .getAttachment();
+                    .getAttachment();
             Queue writeQueue = psession.writeQueue;
 
             if( writeQueue.isEmpty() )
@@ -172,82 +188,7 @@
             }
             catch( Throwable t )
             {
-                fireExceptionCaught( psession, t );
-            }
-        }
-
-        private void fireSessionOpened( ProtocolSession session )
-        {
-            try
-            {
-                handler.sessionOpened( session );
-            }
-            catch( Throwable t )
-            {
-                fireExceptionCaught( session, t );
-            }
-        }
-
-        private void fireSessionClosed( ProtocolSession session )
-        {
-            try
-            {
-                handler.sessionClosed( session );
-            }
-            catch( Throwable t )
-            {
-                fireExceptionCaught( session, t );
-            }
-        }
-
-        private void fireSessionIdle( ProtocolSession session,
-                                     IdleStatus idleStatus )
-        {
-            try
-            {
-                handler.sessionIdle( session, idleStatus );
-            }
-            catch( Throwable t )
-            {
-                fireExceptionCaught( session, t );
-            }
-        }
-
-        private void fireMessageReceived( ProtocolSession session,
-                                         Object message )
-        {
-            try
-            {
-                handler.messageReceived( session, message );
-            }
-            catch( Throwable t )
-            {
-                fireExceptionCaught( session, t );
-            }
-        }
-
-        private void fireMessageSent( ProtocolSession session, Object message )
-        {
-            try
-            {
-                handler.messageSent( session, message );
-            }
-            catch( Throwable t )
-            {
-                fireExceptionCaught( session, t );
-            }
-        }
-
-        private void fireExceptionCaught( ProtocolSession session,
-                                         Throwable cause )
-        {
-            try
-            {
-                handler.exceptionCaught( session, cause );
-            }
-            catch( Throwable t )
-            {
-                log.error( "Exception from excaptionCaught.", t );
+                filterManager.fireExceptionCaught( psession, t );
             }
         }
     }
@@ -267,6 +208,11 @@
         {
             this.session = session;
             this.adapter = adapter;
+        }
+
+        public ProtocolHandler getHandler()
+        {
+            return adapter.handler;
         }
 
         public void close()

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSession.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSession.java?view=diff&rev=122732&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSession.java&r1=122731&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSession.java&r2=122732
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSession.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSession.java
Sat Dec 18 18:39:54 2004
@@ -31,6 +31,8 @@
  */
 public interface ProtocolSession
 {
+    ProtocolHandler getHandler();
+   
     void close();
 
     Object getAttachment();

Added: incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ProtocolHandlerFilterManager.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ProtocolHandlerFilterManager.java?view=auto&rev=122732
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ProtocolHandlerFilterManager.java
Sat Dec 18 18:39:54 2004
@@ -0,0 +1,320 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.util;
+
+import org.apache.mina.core.IdleStatus;
+import org.apache.mina.protocol.ProtocolHandler;
+import org.apache.mina.protocol.ProtocolHandlerFilter;
+import org.apache.mina.protocol.ProtocolSession;
+
+/**
+ * TODO Document me.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class ProtocolHandlerFilterManager
+{
+    private static final ProtocolHandlerFilter FINAL_FILTER = new ProtocolHandlerFilter()
+    {
+
+        public void sessionOpened( ProtocolHandler nextHandler,
+                                  ProtocolSession session )
+        {
+            session.getHandler().sessionOpened( session );
+        }
+
+        public void sessionClosed( ProtocolHandler nextHandler,
+                                  ProtocolSession session )
+        {
+            session.getHandler().sessionClosed( session );
+        }
+
+        public void sessionIdle( ProtocolHandler nextHandler,
+                                ProtocolSession session, IdleStatus status )
+        {
+            session.getHandler().sessionIdle( session, status );
+        }
+
+        public void exceptionCaught( ProtocolHandler nextHandler,
+                                    ProtocolSession session, Throwable cause )
+        {
+            session.getHandler().exceptionCaught( session, cause );
+        }
+
+        public void messageReceived( ProtocolHandler nextHandler,
+                                    ProtocolSession session, Object message )
+        {
+            session.getHandler().messageReceived( session, message );
+        }
+
+        public void messageSent( ProtocolHandler nextHandler,
+                                ProtocolSession session, Object message )
+        {
+            session.getHandler().messageSent( session, message );
+        }
+    };
+
+    private Entry head = new Entry( null, Integer.MIN_VALUE, FINAL_FILTER );
+
+    public ProtocolHandlerFilterManager()
+    {
+    }
+
+    public synchronized void addFilter( int priority, ProtocolHandlerFilter filter )
+    {
+        Entry e = head;
+        Entry prevEntry = null;
+        for( ;; )
+        {
+            if( e.nextEntry == null )
+            {
+                Entry newEntry = new Entry( e, priority, filter );
+                if( prevEntry == null )
+                {
+                    head = newEntry;
+                }
+                else
+                {
+                    prevEntry.nextEntry = newEntry;
+                }
+                break;
+            }
+            else if( e.priority < priority )
+            {
+                Entry newEntry = new Entry( e, priority, filter );
+                if( prevEntry == null )
+                {
+                    head = newEntry;
+                }
+                else
+                {
+                    prevEntry.nextEntry = newEntry;
+                }
+                break;
+            }
+            prevEntry = e;
+            e = e.nextEntry;
+        }
+    }
+
+    public synchronized void removeFilter( ProtocolHandlerFilter filter )
+    {
+        Entry e = head;
+        Entry prevEntry = null;
+        for( ;; )
+        {
+            if( e.nextEntry == null )
+            {
+                break;
+            }
+            else if( e.filter == filter )
+            {
+                if( prevEntry == null )
+                {
+                    // e is head
+                    head = e.nextEntry;
+                }
+                else
+                {
+                    prevEntry.nextEntry = e.nextEntry;
+                }
+                break;
+            }
+            prevEntry = e;
+            e = e.nextEntry;
+        }
+    }
+
+    public void fireSessionOpened( ProtocolSession session )
+    {
+        Entry head = this.head;
+        try
+        {
+            head.filter.sessionOpened( head.nextHandler, session );
+        }
+        catch( Throwable e )
+        {
+            fireExceptionCaught( session, e );
+        }
+    }
+
+    public void fireSessionClosed( ProtocolSession session )
+    {
+        Entry head = this.head;
+        try
+        {
+            head.filter.sessionClosed( head.nextHandler, session );
+        }
+        catch( Throwable e )
+        {
+            fireExceptionCaught( session, e );
+        }
+    }
+
+    public void fireSessionIdle( ProtocolSession session, IdleStatus status )
+    {
+        Entry head = this.head;
+        try
+        {
+            head.filter.sessionIdle( head.nextHandler, session, status );
+        }
+        catch( Throwable e )
+        {
+            fireExceptionCaught( session, e );
+        }
+    }
+
+    public void fireMessageSent( ProtocolSession session, Object message )
+    {
+        Entry head = this.head;
+        try
+        {
+            head.filter.messageSent( head.nextHandler, session, message );
+        }
+        catch( Throwable e )
+        {
+            fireExceptionCaught( session, e );
+        }
+    }
+
+    public void fireMessageReceived( ProtocolSession session, Object message )
+    {
+        Entry head = this.head;
+        try
+        {
+            head.filter.messageReceived( head.nextHandler, session, message );
+        }
+        catch( Throwable e )
+        {
+            fireExceptionCaught( session, e );
+        }
+    }
+
+    public void fireExceptionCaught( ProtocolSession session, Throwable cause )
+    {
+        Entry head = this.head;
+        try
+        {
+            head.filter.exceptionCaught( head.nextHandler, session, cause );
+        }
+        catch( Throwable e )
+        {
+            e.printStackTrace();
+        }
+    }
+
+    private static class Entry
+    {
+        private Entry nextEntry;
+
+        private final int priority;
+
+        private final ProtocolHandlerFilter filter;
+
+        private final ProtocolHandler nextHandler;
+
+        private Entry( Entry nextEntry, int priority, ProtocolHandlerFilter filter )
+        {
+            if( filter == null )
+                throw new NullPointerException( "filter" );
+            this.nextEntry = nextEntry;
+            this.priority = priority;
+            this.filter = filter;
+            this.nextHandler = new ProtocolHandler()
+            {
+
+                public void sessionOpened( ProtocolSession session )
+                {
+                    try
+                    {
+                        Entry.this.nextEntry.filter
+                                .sessionOpened(
+                                                Entry.this.nextEntry.nextHandler,
+                                                session );
+                    }
+                    catch( Throwable e )
+                    {
+                        exceptionCaught( session, e );
+                    }
+                }
+
+                public void sessionClosed( ProtocolSession session )
+                {
+                    try
+                    {
+                        Entry.this.nextEntry.filter
+                                .sessionClosed(
+                                                Entry.this.nextEntry.nextHandler,
+                                                session );
+                    }
+                    catch( Throwable e )
+                    {
+                        exceptionCaught( session, e );
+                    }
+                }
+
+                public void sessionIdle( ProtocolSession session, IdleStatus status )
+                {
+                    try
+                    {
+                        Entry.this.nextEntry.filter
+                                .sessionIdle(
+                                              Entry.this.nextEntry.nextHandler,
+                                              session, status );
+                    }
+                    catch( Throwable e )
+                    {
+                        exceptionCaught( session, e );
+                    }
+                }
+
+                public void exceptionCaught( ProtocolSession session, Throwable cause )
+                {
+                    try
+                    {
+                        Entry.this.nextEntry.filter
+                                .exceptionCaught(
+                                                  Entry.this.nextEntry.nextHandler,
+                                                  session, cause );
+                    }
+                    catch( Throwable e )
+                    {
+                        e.printStackTrace();
+                    }
+                }
+
+                public void messageReceived( ProtocolSession session, Object message )
+                {
+                    try
+                    {
+                        Entry.this.nextEntry.filter
+                                .messageReceived(
+                                              Entry.this.nextEntry.nextHandler,
+                                              session, message );
+                    }
+                    catch( Throwable e )
+                    {
+                        exceptionCaught( session, e );
+                    }
+                }
+
+                public void messageSent( ProtocolSession session, Object message )
+                {
+                    try
+                    {
+                        Entry.this.nextEntry.filter
+                                .messageSent(
+                                              Entry.this.nextEntry.nextHandler,
+                                              session, message );
+                    }
+                    catch( Throwable e )
+                    {
+                        exceptionCaught( session, e );
+                    }
+                }
+            };
+        }
+    }
+}
\ No newline at end of file

Mime
View raw message