directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pro...@apache.org
Subject svn commit: r398024 - in /directory/trunks/mina/core/src/main/java/org/apache/mina/common/support: AbstractIoFilterChain.java IoFilterLifeCycleManager.java ReferenceCountingIoFilterWrapper.java
Date Fri, 28 Apr 2006 21:45:59 GMT
Author: proyal
Date: Fri Apr 28 14:45:56 2006
New Revision: 398024

URL: http://svn.apache.org/viewcvs?rev=398024&view=rev
Log:
DIRMINA-185 - Reference-counting based init/destroy of IoFilters has been moved into a wrapper
class that can be used when users desire that behavior. By default, MINA will not init/destroy
IoFilter instances.

Added:
    directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/ReferenceCountingIoFilterWrapper.java
  (contents, props changed)
      - copied, changed from r397283, directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/IoFilterLifeCycleManager.java
Removed:
    directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/IoFilterLifeCycleManager.java
Modified:
    directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/AbstractIoFilterChain.java

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/AbstractIoFilterChain.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/AbstractIoFilterChain.java?rev=398024&r1=398023&r2=398024&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/AbstractIoFilterChain.java
(original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/AbstractIoFilterChain.java
Fri Apr 28 14:45:56 2006
@@ -18,23 +18,23 @@
  */
 package org.apache.mina.common.support;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
 import org.apache.mina.common.IdleStatus;
 import org.apache.mina.common.IoFilter;
+import org.apache.mina.common.IoFilter.NextFilter;
+import org.apache.mina.common.IoFilter.WriteRequest;
 import org.apache.mina.common.IoFilterAdapter;
 import org.apache.mina.common.IoFilterChain;
 import org.apache.mina.common.IoFilterLifeCycleException;
 import org.apache.mina.common.IoSession;
-import org.apache.mina.common.IoFilter.NextFilter;
-import org.apache.mina.common.IoFilter.WriteRequest;
 import org.apache.mina.util.ByteBufferUtil;
 import org.apache.mina.util.SessionLog;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
 /**
  * An abstract implementation of {@link IoFilterChain} that provides
  * common operations for developers to implement their own transport layer.
@@ -42,9 +42,9 @@
  * The only method a developer should implement is
  * {@link #doWrite(IoSession, IoFilter.WriteRequest)}.  This method is invoked
  * when filter chain is evaluated for
- * {@link IoFilter#filterWrite(NextFilter, IoSession, IoFilter.WriteRequest)} and 
+ * {@link IoFilter#filterWrite(NextFilter, IoSession, IoFilter.WriteRequest)} and
  * finally to be written out.
- * 
+ *
  * @author The Apache Directory Project (mina-dev@directory.apache.org)
  * @version $Rev$, $Date$
  */
@@ -53,7 +53,7 @@
     private final IoSession session;
 
     private final Map name2entry = new HashMap();
-    
+
     private final EntryImpl head;
     private final EntryImpl tail;
 
@@ -63,13 +63,13 @@
         {
             throw new NullPointerException( "session" );
         }
-        
+
         this.session = session;
         head = new EntryImpl( null, null, "head", createHeadFilter() );
         tail = new EntryImpl( head, null, "tail", createTailFilter() );
         head.nextEntry = tail;
     }
-    
+
     /**
      * Override this method to create custom head of this filter chain.
      */
@@ -115,7 +115,7 @@
             {
                 nextFilter.messageSent( session, message );
             }
-            
+
             public void filterWrite( NextFilter nextFilter, IoSession session,
                                      WriteRequest writeRequest ) throws Exception
             {
@@ -126,7 +126,7 @@
                 else
                 {
                     throw new IllegalStateException(
-                            "Write requests must be transformed to " + 
+                            "Write requests must be transformed to " +
                             session.getTransportType().getEnvelopeType() +
                             ": " + writeRequest );
                 }
@@ -138,7 +138,7 @@
             }
         };
     }
-    
+
     /**
      * Override this method to create custom head of this filter chain.
      */
@@ -218,12 +218,12 @@
             }
         };
     }
-    
+
     public IoSession getSession()
     {
         return session;
     }
-    
+
     public Entry getEntry( String name )
     {
         Entry e = ( Entry ) name2entry.get( name );
@@ -233,7 +233,7 @@
         }
         return e;
     }
-    
+
     public IoFilter get( String name )
     {
         Entry e = getEntry( name );
@@ -241,10 +241,10 @@
         {
             return null;
         }
-        
+
         return e.getFilter();
     }
-    
+
     public NextFilter getNextFilter( String name )
     {
         Entry e = getEntry( name );
@@ -252,10 +252,10 @@
         {
             return null;
         }
-        
+
         return e.getNextFilter();
     }
-    
+
     public synchronized void addFirst( String name,
                                        IoFilter filter )
     {
@@ -308,58 +308,55 @@
     {
         EntryImpl newEntry = new EntryImpl( prevEntry, prevEntry.nextEntry, name, filter
);
 
-        IoFilterLifeCycleManager lifeCycleManager = IoFilterLifeCycleManager.getInstance();
-        
-        synchronized( lifeCycleManager )
-        {
-            lifeCycleManager.callInitIfNecessary( filter );
 
-            try
-            {
-                lifeCycleManager.callOnPreAdd( this, name, filter, newEntry.getNextFilter()
);
-            }
-            finally
-            {
-                lifeCycleManager.callDestroyIfNecessary( filter );
-            }
+        try
+        {
+            filter.onPreAdd( this, name, newEntry.getNextFilter() );
+        }
+        catch( Exception e )
+        {
+            throw new IoFilterLifeCycleException( "onPreAdd(): " + name + ':' + filter +
" in " + getSession(), e );
+        }
 
-            prevEntry.nextEntry.prevEntry = newEntry;
-            prevEntry.nextEntry = newEntry;
-            name2entry.put( name, newEntry );
+        prevEntry.nextEntry.prevEntry = newEntry;
+        prevEntry.nextEntry = newEntry;
+        name2entry.put( name, newEntry );
 
-            try
-            {
-                lifeCycleManager.callOnPostAdd( this, name, filter, newEntry.getNextFilter()
);
-            }
-            catch( IoFilterLifeCycleException e )
-            {
-                deregister0( newEntry );
-                throw e;
-            }
-            finally
-            {
-                lifeCycleManager.callDestroyIfNecessary( filter );
-            }
+        try
+        {
+            filter.onPostAdd( this, name, newEntry.getNextFilter() );
+        }
+        catch( Exception e )
+        {
+            deregister0( newEntry );
+            throw new IoFilterLifeCycleException( "onPostAdd(): " + name + ':' + filter +
" in " + getSession(), e );
         }
     }
-    
+
     private void deregister( EntryImpl entry )
     {
         IoFilter filter = entry.getFilter();
-        IoFilterLifeCycleManager lifeCycleManager = IoFilterLifeCycleManager.getInstance();
-        
-        lifeCycleManager.callOnPreRemove( this, entry.getName(), filter, entry.getNextFilter()
);
-        
+
+        try
+        {
+            filter.onPreRemove( this, entry.getName(), entry.getNextFilter() );
+        }
+        catch( Exception e )
+        {
+            throw new IoFilterLifeCycleException( "onPreRemove(): " + entry.getName() + ':'
+ filter
+                                                  + " in " + getSession(), e );
+        }
+
         deregister0( entry );
 
         try
         {
-            lifeCycleManager.callOnPostRemove(
-                    this, entry.getName(), filter, entry.getNextFilter() );
+            filter.onPostRemove( this, entry.getName(), entry.getNextFilter() );
         }
-        finally
+        catch( Exception e )
         {
-            lifeCycleManager.callDestroyIfNecessary( filter );
+            throw new IoFilterLifeCycleException( "onPostRemove(): " + entry.getName() +
':' + filter
+                                                  + " in " + getSession(), e );
         }
     }
 
@@ -449,7 +446,7 @@
         {
             exceptionCaught( session, t );
         }
-        
+
         // And start the chain.
         Entry head = this.head;
         callNextSessionClosed(head, session);
@@ -461,7 +458,7 @@
         try
         {
             entry.getFilter().sessionClosed( entry.getNextFilter(), session );
-                
+
         }
         catch( Throwable e )
         {
@@ -537,7 +534,7 @@
 
     private void callNextMessageSent( Entry entry,
                                       IoSession session,
-                                      Object message ) 
+                                      Object message )
     {
         try
         {
@@ -570,7 +567,7 @@
                     "Unexpected exception from exceptionCaught handler.", e );
         }
     }
-    
+
     public void filterWrite( IoSession session, WriteRequest writeRequest )
     {
         Entry tail = this.tail;
@@ -634,7 +631,7 @@
         }
         return list;
     }
-    
+
     public boolean contains( String name )
     {
         return getEntry( name ) != null;
@@ -672,9 +669,9 @@
     {
         StringBuffer buf = new StringBuffer();
         buf.append( "{ " );
-        
+
         boolean empty = true;
-        
+
         EntryImpl e = head.nextEntry;
         while( e != tail )
         {
@@ -686,7 +683,7 @@
             {
                 empty = false;
             }
-            
+
             buf.append( '(' );
             buf.append( e.getName() );
             buf.append( ':' );
@@ -700,13 +697,13 @@
         {
             buf.append( "empty" );
         }
-        
+
         buf.append( " }" );
-        
+
         return buf.toString();
     }
 
-    
+
     protected void finalize() throws Throwable
     {
         try
@@ -720,7 +717,7 @@
     }
 
     protected abstract void doWrite( IoSession session, WriteRequest writeRequest ) throws
Exception;
-    
+
     protected abstract void doClose( IoSession session ) throws Exception;
 
     private class EntryImpl implements Entry
@@ -730,11 +727,11 @@
         private EntryImpl nextEntry;
 
         private final String name;
-        
+
         private final IoFilter filter;
 
         private final NextFilter nextFilter;
-        
+
         private EntryImpl( EntryImpl prevEntry, EntryImpl nextEntry,
                        String name, IoFilter filter )
         {
@@ -746,7 +743,7 @@
             {
                 throw new NullPointerException( "name" );
             }
-            
+
             this.prevEntry = prevEntry;
             this.nextEntry = nextEntry;
             this.name = name;
@@ -795,7 +792,7 @@
                     Entry nextEntry = EntryImpl.this.nextEntry;
                     callNextMessageSent( nextEntry, session, message );
                 }
-                
+
                 public void filterWrite( IoSession session, WriteRequest writeRequest )
                 {
                     Entry nextEntry = EntryImpl.this.prevEntry;

Copied: directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/ReferenceCountingIoFilterWrapper.java
(from r397283, directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/IoFilterLifeCycleManager.java)
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/ReferenceCountingIoFilterWrapper.java?p2=directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/ReferenceCountingIoFilterWrapper.java&p1=directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/IoFilterLifeCycleManager.java&r1=397283&r2=398024&rev=398024&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/IoFilterLifeCycleManager.java
(original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/ReferenceCountingIoFilterWrapper.java
Fri Apr 28 14:45:56 2006
@@ -18,183 +18,114 @@
  */
 package org.apache.mina.common.support;
 
-import java.util.IdentityHashMap;
-import java.util.Map;
-
+import org.apache.mina.common.IdleStatus;
 import org.apache.mina.common.IoFilter;
 import org.apache.mina.common.IoFilterChain;
-import org.apache.mina.common.IoFilterLifeCycleException;
-import org.apache.mina.common.IoFilter.NextFilter;
+import org.apache.mina.common.IoSession;
 
 /**
- * Manages the life cycle of {@link IoFilter}s globally.
+ * An {@link IoFilter}s wrapper that keeps track of the number of usages of this filter and
will call init/destroy
+ * when the filter is not in use.
  *
  * @author The Apache Directory Project (mina-dev@directory.apache.org)
  * @version $Rev$, $Date$
  */
-public class IoFilterLifeCycleManager
+public class ReferenceCountingIoFilterWrapper implements IoFilter
 {
-    private static final IoFilterLifeCycleManager INSTANCE = new IoFilterLifeCycleManager();
-    
-    public static IoFilterLifeCycleManager getInstance()
+    private final IoFilter filter;
+    private int count = 0;
+
+    public ReferenceCountingIoFilterWrapper( IoFilter filter )
     {
-        return INSTANCE;
+        this.filter = filter;
     }
-    
-    private Map counts = new IdentityHashMap();
-    
-    private IoFilterLifeCycleManager()
+
+    public void init() throws Exception
     {
+        // no-op, will init on-demand in pre-add if count == 0
     }
-    
-    public synchronized void callInitIfNecessary( IoFilter filter )
+
+    public void destroy() throws Exception
     {
-        ReferenceCount count = ( ReferenceCount ) counts.get( filter );
-        if( count == null )
-        {
-            count = new ReferenceCount();
-            counts.put( filter, count );
-            try
-            {
-                filter.init();
-            }
-            catch( Throwable t )
-            {
-                throw new IoFilterLifeCycleException(
-                        "init(): " + filter, t );
-            }
-        }
+        //no-op, will destroy on-demand in post-remove if count == 0
     }
-    
-    public synchronized void callOnPreAdd( IoFilterChain chain, String name, IoFilter filter,
NextFilter nextFilter )
+
+    public synchronized void onPreAdd( IoFilterChain parent, String name, NextFilter nextFilter
) throws Exception
     {
-        ReferenceCount count = ( ReferenceCount ) counts.get( filter );
-        if( count == null )
+        if( 0 == count )
         {
-            throw new IllegalStateException();
-        }
-        
-        count.increase();
+            filter.init();
 
-        try
-        {
-            filter.onPreAdd( chain, name, nextFilter );
-        }
-        catch( Throwable t )
-        {
-            throw new IoFilterLifeCycleException(
-                    "onPreAdd(): " + name + ':' + filter + " in " +
-                    chain.getSession(), t );
+            ++count;
         }
+
+        filter.onPreAdd( parent, name, nextFilter );
     }
 
-    public synchronized void callOnPreRemove( IoFilterChain chain, String name, IoFilter
filter, NextFilter nextFilter )
+    public synchronized void onPostRemove( IoFilterChain parent, String name, NextFilter
nextFilter ) throws Exception
     {
-        ReferenceCount count = ( ReferenceCount ) counts.get( filter );
-        if( count == null || count.get() == 0 )
-        {
-            return;
-        }
+        filter.onPostRemove( parent, name, nextFilter );
 
-        try
-        {
-            filter.onPreRemove( chain, name, nextFilter);
-        }
-        catch( Throwable t )
-        {
-            throw new IoFilterLifeCycleException(
-                    "onPreRemove(): " + name + ':' + filter + " in " +
-                    chain.getSession(), t );
+        --count;
+
+        if( 0 == count ) {
+            filter.destroy();
         }
     }
-    
-    public synchronized void callOnPostAdd( IoFilterChain chain, String name, IoFilter filter,
NextFilter nextFilter )
+
+    public void exceptionCaught( NextFilter nextFilter, IoSession session, Throwable cause
) throws Exception
     {
-        ReferenceCount count = ( ReferenceCount ) counts.get( filter );
-        if( count == null )
-        {
-            throw new IllegalStateException();
-        }
-        
-        try
-        {
-            filter.onPostAdd( chain, name, nextFilter );
-        }
-        catch( Throwable t )
-        {
-            // Revert back the reference count.
-            count.decrease();
+        filter.exceptionCaught( nextFilter, session, cause );
+    }
 
-            throw new IoFilterLifeCycleException(
-                    "onPostAdd(): " + name + ':' + filter + " in " +
-                    chain.getSession(), t );
-        }
+    public void filterClose( NextFilter nextFilter, IoSession session ) throws Exception
+    {
+        filter.filterClose( nextFilter, session );
     }
 
-    public synchronized void callOnPostRemove( IoFilterChain chain, String name, IoFilter
filter, NextFilter nextFilter )
+    public void filterWrite( NextFilter nextFilter, IoSession session, WriteRequest writeRequest
)
+        throws Exception
     {
-        ReferenceCount count = ( ReferenceCount ) counts.get( filter );
-        if( count == null || count.get() == 0 )
-        {
-            return;
-        }
+        filter.filterWrite( nextFilter, session, writeRequest );
+    }
 
-        try
-        {
-            filter.onPostRemove( chain, name, nextFilter);
-        }
-        catch( Throwable t )
-        {
-            throw new IoFilterLifeCycleException(
-                    "onPostRemove(): " + name + ':' + filter + " in " +
-                    chain.getSession(), t );
-        }
-        finally
-        {
-            count.decrease();
-        }
+    public void messageReceived( NextFilter nextFilter, IoSession session, Object message
) throws Exception
+    {
+        filter.messageReceived( nextFilter, session, message );
     }
 
-    public synchronized void callDestroyIfNecessary( IoFilter filter )
+    public void messageSent( NextFilter nextFilter, IoSession session, Object message ) throws
Exception
     {
-        ReferenceCount count = ( ReferenceCount ) counts.get( filter );
-        if( count == null )
-        {
-            return;
-        }
-        
-        if( count.get() == 0 )
-        {
-            counts.remove( filter );
-            try
-            {
-                filter.destroy();
-            }
-            catch( Throwable t2 )
-            {
-                throw new IoFilterLifeCycleException( "Failed to destroy: " + filter, t2
);
-            }
-        }
+        filter.messageSent( nextFilter, session, message );
     }
-    
-    /** Maintains the reference count of an {@link IoFilter}. */ 
-    private static class ReferenceCount
-    {
-        private int count;
-        
-        public int get()
-        {
-            return count;
-        }
-        
-        public int increase()
-        {
-            return count ++;
-        }
-        
-        public int decrease()
-        {
-            return -- count;
-        }
+
+    public void onPostAdd( IoFilterChain parent, String name, NextFilter nextFilter ) throws
Exception
+    {
+        filter.onPostAdd( parent, name, nextFilter );
+    }
+
+    public void onPreRemove( IoFilterChain parent, String name, NextFilter nextFilter ) throws
Exception
+    {
+        filter.onPreRemove( parent, name, nextFilter );
+    }
+
+    public void sessionClosed( NextFilter nextFilter, IoSession session ) throws Exception
+    {
+        filter.sessionClosed( nextFilter, session );
+    }
+
+    public void sessionCreated( NextFilter nextFilter, IoSession session ) throws Exception
+    {
+        filter.sessionCreated( nextFilter, session );
+    }
+
+    public void sessionIdle( NextFilter nextFilter, IoSession session, IdleStatus status
) throws Exception
+    {
+        filter.sessionIdle( nextFilter, session, status );
+    }
+
+    public void sessionOpened( NextFilter nextFilter, IoSession session ) throws Exception
+    {
+        filter.sessionOpened( nextFilter, session );
     }
 }

Propchange: directory/trunks/mina/core/src/main/java/org/apache/mina/common/support/ReferenceCountingIoFilterWrapper.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision



Mime
View raw message