directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pro...@apache.org
Subject svn commit: r471608 - in /directory/branches/mina/1.2/core/src: main/java/org/apache/mina/common/ main/java/org/apache/mina/common/support/ main/java/org/apache/mina/transport/vmpipe/ test/java/org/apache/mina/common/support/
Date Mon, 06 Nov 2006 01:59:05 GMT
Author: proyal
Date: Sun Nov  5 17:59:03 2006
New Revision: 471608

URL: http://svn.apache.org/viewvc?view=rev&rev=471608
Log:
Minimize synchornization in the IoServiceListenerSupport implementation

Modified:
    directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/IoService.java
    directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/BaseIoService.java
    directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/DelegatedIoAcceptor.java
    directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/DelegatedIoConnector.java
    directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/IoServiceListenerSupport.java
    directory/branches/mina/1.2/core/src/main/java/org/apache/mina/transport/vmpipe/VmPipeConnector.java
    directory/branches/mina/1.2/core/src/test/java/org/apache/mina/common/support/IoServiceListenerSupportTest.java

Modified: directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/IoService.java
URL: http://svn.apache.org/viewvc/directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/IoService.java?view=diff&rev=471608&r1=471607&r2=471608
==============================================================================
--- directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/IoService.java (original)
+++ directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/IoService.java Sun
Nov  5 17:59:03 2006
@@ -6,16 +6,16 @@
  *  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. 
- *  
+ *  under the License.
+ *
  */
 package org.apache.mina.common;
 
@@ -26,7 +26,7 @@
 /**
  * Base interface for all {@link IoAcceptor}s and {@link IoConnector}s
  * that provide I/O service and manage {@link IoSession}s.
- * 
+ *
  * @author The Apache Directory Project (mina-dev@directory.apache.org)
  * @version $Rev$, $Date$
  */
@@ -37,49 +37,49 @@
      * this service.
      */
     void addListener( IoServiceListener listener );
-    
+
     /**
      * Removed an existing {@link IoServiceListener} that listens any events
      * related with this service.
      */
     void removeListener( IoServiceListener listener );
-    
+
     /**
      * Returns all {@link SocketAddress}es this service is managing.
      * If this service is an {@link IoAcceptor}, a set of bind addresses will
      * be returned.  If this service is an {@link IoConnector}, a set of remote
      * addresses will be returned.
      */
-    Set getManagedServiceAddresses();
-    
+    Set<SocketAddress> getManagedServiceAddresses();
+
     /**
      * Returns <tt>true</tt> if this service is managing the specified <tt>serviceAddress</tt>.
      * If this service is an {@link IoAcceptor}, <tt>serviceAddress</tt> is a
bind address.
      * If this service is an {@link IoConnector}, <tt>serviceAddress</tt> is
a remote address.
      */
     boolean isManaged( SocketAddress serviceAddress );
-    
+
     /**
      * Returns all sessions with the specified remote or local address,
      * which are currently managed by this service.
      * {@link IoAcceptor} will assume the specified <tt>address</tt> is a local
      * address, and {@link IoConnector} will assume it's a remote address.
-     * 
+     *
      * @param serviceAddress the address to return all sessions for.
      * @return the sessions. An empty collection if there's no session.
-     * @throws IllegalArgumentException if the specified <tt>address</tt> has

+     * @throws IllegalArgumentException if the specified <tt>address</tt> has
      *         not been bound.
      * @throws UnsupportedOperationException if this operation isn't supported
      *         for the particular transport type implemented by this {@link IoService}.
      */
-    Set getManagedSessions( SocketAddress serviceAddress );
+    Set<IoSession> getManagedSessions( SocketAddress serviceAddress );
 
     /**
      * Returns the default configuration which is used when you didn't specify
      * any configuration.
      */
     IoServiceConfig getDefaultConfig();
-    
+
     /**
      * Returns the global {@link IoFilterChainBuilder} which will modify the
      * {@link IoFilterChain} of all {@link IoSession}s which is managed
@@ -87,7 +87,7 @@
      * The default value is an empty {@link DefaultIoFilterChainBuilder}.
      */
     IoFilterChainBuilder getFilterChainBuilder();
-    
+
     /**
      * Sets the global {@link IoFilterChainBuilder} which will modify the
      * {@link IoFilterChain} of all {@link IoSession}s which is managed
@@ -96,7 +96,7 @@
      * an empty {@link DefaultIoFilterChainBuilder}.
      */
     void setFilterChainBuilder( IoFilterChainBuilder builder );
-    
+
     /**
      * A shortcut for <tt>( ( DefaultIoFilterChainBuilder ) </tt>{@link #getFilterChainBuilder()}<tt>
)</tt>.
      * Please note that the returned object is not a <b>real</b> {@link IoFilterChain}

Modified: directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/BaseIoService.java
URL: http://svn.apache.org/viewvc/directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/BaseIoService.java?view=diff&rev=471608&r1=471607&r2=471608
==============================================================================
--- directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/BaseIoService.java
(original)
+++ directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/BaseIoService.java
Sun Nov  5 17:59:03 2006
@@ -6,16 +6,16 @@
  *  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. 
- *  
+ *  under the License.
+ *
  */
 package org.apache.mina.common.support;
 
@@ -26,10 +26,11 @@
 import org.apache.mina.common.IoFilterChainBuilder;
 import org.apache.mina.common.IoService;
 import org.apache.mina.common.IoServiceListener;
+import org.apache.mina.common.IoSession;
 
 /**
  * Base implementation of {@link IoService}s.
- * 
+ *
  * @author The Apache Directory Project (mina-dev@directory.apache.org)
  * @version $Rev$, $Date$
  */
@@ -44,12 +45,12 @@
      * Maintains the {@link IoServiceListener}s of this service.
      */
     private final IoServiceListenerSupport listeners;
-    
+
     protected BaseIoService()
     {
         this.listeners = new IoServiceListenerSupport();
     }
-    
+
     public IoFilterChainBuilder getFilterChainBuilder()
     {
         return filterChainBuilder;
@@ -63,7 +64,7 @@
         }
         filterChainBuilder = builder;
     }
-    
+
     public DefaultIoFilterChainBuilder getFilterChain()
     {
         if( filterChainBuilder instanceof DefaultIoFilterChainBuilder )
@@ -76,23 +77,23 @@
                     "Current filter chain builder is not a DefaultIoFilterChainBuilder."
);
         }
     }
-    
+
     public void addListener( IoServiceListener listener )
     {
         getListeners().add( listener );
     }
-    
+
     public void removeListener( IoServiceListener listener )
     {
         getListeners().remove( listener );
     }
-    
-    public Set getManagedServiceAddresses()
+
+    public Set<SocketAddress> getManagedServiceAddresses()
     {
         return getListeners().getManagedServiceAddresses();
     }
 
-    public Set getManagedSessions( SocketAddress serviceAddress )
+    public Set<IoSession> getManagedSessions( SocketAddress serviceAddress )
     {
         return getListeners().getManagedSessions( serviceAddress );
     }

Modified: directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/DelegatedIoAcceptor.java
URL: http://svn.apache.org/viewvc/directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/DelegatedIoAcceptor.java?view=diff&rev=471608&r1=471607&r2=471608
==============================================================================
--- directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/DelegatedIoAcceptor.java
(original)
+++ directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/DelegatedIoAcceptor.java
Sun Nov  5 17:59:03 2006
@@ -6,16 +6,16 @@
  *  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. 
- *  
+ *  under the License.
+ *
  */
 package org.apache.mina.common.support;
 
@@ -33,21 +33,21 @@
 
 /**
  * A delegated {@link IoAcceptor} that wraps the other {@link IoAcceptor}.
- * 
+ *
  * @author The Apache Directory Project (mina-dev@directory.apache.org)
  * @version $Rev$, $Date$
  */
 public class DelegatedIoAcceptor implements IoAcceptor
 {
     protected IoAcceptor delegate;
-    
+
     /**
      * Creates a new instance.
      */
     protected DelegatedIoAcceptor()
     {
     }
-    
+
     /**
      * Sets the delegate.  This method should be invoked before any operations
      * is requested.
@@ -56,7 +56,7 @@
     {
         this.delegate = delegate;
     }
-    
+
     public void bind( SocketAddress address, IoHandler handler ) throws IOException
     {
         delegate.bind( address, handler );
@@ -71,23 +71,23 @@
     {
         delegate.unbind( address );
     }
-    
+
     public void unbindAll()
     {
         delegate.unbindAll();
     }
-    
+
     public boolean isManaged( SocketAddress address )
     {
         return delegate.isManaged( address );
     }
 
-    public Set getManagedServiceAddresses()
+    public Set<SocketAddress> getManagedServiceAddresses()
     {
         return delegate.getManagedServiceAddresses();
     }
 
-    public Set getManagedSessions( SocketAddress serviceAddress )
+    public Set<IoSession> getManagedSessions( SocketAddress serviceAddress )
     {
         return delegate.getManagedSessions( serviceAddress );
     }

Modified: directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/DelegatedIoConnector.java
URL: http://svn.apache.org/viewvc/directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/DelegatedIoConnector.java?view=diff&rev=471608&r1=471607&r2=471608
==============================================================================
--- directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/DelegatedIoConnector.java
(original)
+++ directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/DelegatedIoConnector.java
Sun Nov  5 17:59:03 2006
@@ -6,16 +6,16 @@
  *  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. 
- *  
+ *  under the License.
+ *
  */
 package org.apache.mina.common.support;
 
@@ -29,10 +29,11 @@
 import org.apache.mina.common.IoHandler;
 import org.apache.mina.common.IoServiceConfig;
 import org.apache.mina.common.IoServiceListener;
+import org.apache.mina.common.IoSession;
 
 /**
  * A delegated {@link IoConnector} that wraps the other {@link IoConnector}.
- * 
+ *
  * @author The Apache Directory Project (mina-dev@directory.apache.org)
  * @version $Rev$, $Date$
  */
@@ -55,7 +56,7 @@
     {
         this.delegate = delegate;
     }
-    
+
     public ConnectFuture connect( SocketAddress address, IoHandler handler )
     {
         return delegate.connect( address, handler );
@@ -82,17 +83,17 @@
     {
         return delegate.isManaged( serviceAddress );
     }
-    
-    public Set getManagedServiceAddresses()
+
+    public Set<SocketAddress> getManagedServiceAddresses()
     {
         return delegate.getManagedServiceAddresses();
     }
 
-    public Set getManagedSessions( SocketAddress serviceAddress )
+    public Set<IoSession> getManagedSessions( SocketAddress serviceAddress )
     {
         return delegate.getManagedSessions( serviceAddress );
     }
-    
+
     public IoServiceConfig getDefaultConfig()
     {
         return delegate.getDefaultConfig();

Modified: directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/IoServiceListenerSupport.java
URL: http://svn.apache.org/viewvc/directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/IoServiceListenerSupport.java?view=diff&rev=471608&r1=471607&r2=471608
==============================================================================
--- directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/IoServiceListenerSupport.java
(original)
+++ directory/branches/mina/1.2/core/src/main/java/org/apache/mina/common/support/IoServiceListenerSupport.java
Sun Nov  5 17:59:03 2006
@@ -6,28 +6,28 @@
  *  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. 
- *  
+ *  under the License.
+ *
  */
 package org.apache.mina.common.support;
 
 import java.net.SocketAddress;
-import java.util.ArrayList;
 import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.concurrent.CountDownLatch;
 
 import org.apache.mina.common.IoAcceptorConfig;
 import org.apache.mina.common.IoConnector;
@@ -38,12 +38,13 @@
 import org.apache.mina.common.IoServiceConfig;
 import org.apache.mina.common.IoServiceListener;
 import org.apache.mina.common.IoSession;
+import org.apache.mina.common.RuntimeIOException;
 import org.apache.mina.util.IdentityHashSet;
 
 /**
  * A helper which provides addition and removal of {@link IoServiceListener}s and firing
  * events.
- * 
+ *
  * @author The Apache Directory Project (mina-dev@directory.apache.org)
  * @version $Rev$, $Date$
  */
@@ -52,34 +53,32 @@
     /**
      * A list of {@link IoServiceListener}s.
      */
-    private final List listeners = new ArrayList();
+    private final List<IoServiceListener> listeners = new CopyOnWriteArrayList<IoServiceListener>();
 
     /**
      * Tracks managed <tt>serviceAddress</tt>es.
      */
-    private final Set managedServiceAddresses = new HashSet();
-    
+    private final Set<SocketAddress> managedServiceAddresses = new CopyOnWriteArraySet<SocketAddress>();
+
     /**
      * Tracks managed sesssions with <tt>serviceAddress</tt> as a key.
      */
-    private final Map managedSessions = new HashMap();
-    
+    private final ConcurrentMap<SocketAddress, Set<IoSession>> managedSessions
=
+        new ConcurrentHashMap<SocketAddress, Set<IoSession>>();
+
     /**
      * Creates a new instance.
      */
     public IoServiceListenerSupport()
     {
     }
-    
+
     /**
      * Adds a new listener.
      */
     public void add( IoServiceListener listener )
     {
-        synchronized( listeners )
-        {
-            listeners.add( listener );
-        }
+        listeners.add( listener );
     }
 
     /**
@@ -87,40 +86,31 @@
      */
     public void remove( IoServiceListener listener )
     {
-        synchronized( listeners )
-        {
-            listeners.remove( listener );
-        }
+        listeners.remove( listener );
     }
-    
-    public Set getManagedServiceAddresses()
+
+    public Set<SocketAddress> getManagedServiceAddresses()
     {
         return Collections.unmodifiableSet( managedServiceAddresses );
     }
 
     public boolean isManaged( SocketAddress serviceAddress )
     {
-        synchronized( managedServiceAddresses )
-        {
-            return managedServiceAddresses.contains( serviceAddress );
-        }
+        return managedServiceAddresses.contains( serviceAddress );
     }
 
-    public Set getManagedSessions( SocketAddress serviceAddress )
+    public Set<IoSession> getManagedSessions( SocketAddress serviceAddress )
     {
-        Set sessions;
-        synchronized( managedSessions )
+        Set<IoSession> sessions = managedSessions.get( serviceAddress );
+
+        if( null == sessions )
         {
-            sessions = ( Set ) managedSessions.get( serviceAddress );
-            if( sessions == null )
-            {
-                sessions = new IdentityHashSet();
-            }
+            return Collections.emptySet();
         }
-        
+
         synchronized( sessions )
         {
-            return new IdentityHashSet( sessions );
+            return new IdentityHashSet<IoSession>( sessions );
         }
     }
 
@@ -129,52 +119,38 @@
      * for all registered listeners.
      */
     public void fireServiceActivated(
-            IoService service, SocketAddress serviceAddress,
-            IoHandler handler, IoServiceConfig config )
+        IoService service, SocketAddress serviceAddress,
+        IoHandler handler, IoServiceConfig config )
     {
-        synchronized( managedServiceAddresses )
+        if( !managedServiceAddresses.add( serviceAddress ) )
         {
-            if( !managedServiceAddresses.add( serviceAddress ) )
-            {
-                return;
-            }
+            return;
         }
 
-        synchronized( listeners )
+        for( IoServiceListener listener : listeners )
         {
-            for( Iterator i = listeners.iterator(); i.hasNext(); )
-            {
-                ( ( IoServiceListener ) i.next() ).serviceActivated(
-                        service, serviceAddress, handler, config );
-            }
+            listener.serviceActivated( service, serviceAddress, handler, config );
         }
     }
-    
+
     /**
      * Calls {@link IoServiceListener#serviceDeactivated(IoService, SocketAddress, IoHandler,
IoServiceConfig)}
      * for all registered listeners.
      */
     public synchronized void fireServiceDeactivated(
-            IoService service, SocketAddress serviceAddress,
-            IoHandler handler, IoServiceConfig config )
+        IoService service, SocketAddress serviceAddress,
+        IoHandler handler, IoServiceConfig config )
     {
-        synchronized( managedServiceAddresses )
+        if( !managedServiceAddresses.remove( serviceAddress ) )
         {
-            if( !managedServiceAddresses.remove( serviceAddress ) )
-            {
-                return;
-            }
+            return;
         }
-        
+
         try
         {
-            synchronized( listeners )
+            for( IoServiceListener listener : listeners )
             {
-                for( Iterator i = listeners.iterator(); i.hasNext(); )
-                {
-                    ( ( IoServiceListener ) i.next() ).serviceDeactivated(
-                            service, serviceAddress, handler, config );
-                }
+                listener.serviceDeactivated( service, serviceAddress, handler, config );
             }
         }
         finally
@@ -182,103 +158,86 @@
             disconnectSessions( serviceAddress, config );
         }
     }
-    
-    
+
+
     /**
      * Calls {@link IoServiceListener#sessionCreated(IoSession)} for all registered listeners.
      */
     public void fireSessionCreated( IoSession session )
     {
         SocketAddress serviceAddress = session.getServiceAddress();
-        
+
         // Get the session set.
-        boolean firstSession = false;
-        Set sessions;
-        synchronized( managedSessions )
-        {
-            sessions = ( Set ) managedSessions.get( serviceAddress );
-            if( sessions == null )
-            {
-                sessions = new IdentityHashSet();
-                managedSessions.put( serviceAddress, sessions );
-                firstSession = true;
-            }
+        Set<IoSession> s = new IdentityHashSet<IoSession>();
+        Set<IoSession> sessions = managedSessions.putIfAbsent( serviceAddress, Collections.synchronizedSet(
s ) );
+        boolean firstSession;
+
+        if( null == sessions ) {
+            sessions = s;
+            firstSession = true;
+        } else {
+            firstSession = false;
         }
-        
+
         // If already registered, ignore.
-        synchronized( sessions )
+        if( !sessions.add( session ) )
         {
-            if ( !sessions.add( session ) )
-            {
-                return;
-            }
+            return;
         }
-        
+
         // If the first connector session, fire a virtual service activation event.
         if( session.getService() instanceof IoConnector && firstSession )
         {
             fireServiceActivated(
-                    session.getService(), session.getServiceAddress(),
-                    session.getHandler(), session.getServiceConfig() );
+                session.getService(), session.getServiceAddress(),
+                session.getHandler(), session.getServiceConfig() );
         }
 
         // Fire session events.
         session.getFilterChain().fireSessionCreated( session );
-        session.getFilterChain().fireSessionOpened( session);
-        
+        session.getFilterChain().fireSessionOpened( session );
+
         // Fire listener events.
-        synchronized( listeners )
+        for( IoServiceListener listener : listeners )
         {
-            for( Iterator i = listeners.iterator(); i.hasNext(); )
-            {
-                ( ( IoServiceListener ) i.next() ).sessionCreated( session );
-            }
+            listener.sessionCreated( session );
         }
     }
-    
+
     /**
      * Calls {@link IoServiceListener#sessionDestroyed(IoSession)} for all registered listeners.
      */
     public void fireSessionDestroyed( IoSession session )
     {
         SocketAddress serviceAddress = session.getServiceAddress();
-        
+
         // Get the session set.
-        Set sessions;
+        Set<IoSession> sessions = managedSessions.get( serviceAddress );
+        // Ignore if unknown.
+        if( sessions == null )
+        {
+            return;
+        }
+
+        sessions.remove( session );
+
         boolean lastSession = false;
-        synchronized( managedSessions )
+
+        // Try to remove the remaining empty session set after removal.
+        if( sessions.isEmpty() )
         {
-            sessions = ( Set ) managedSessions.get( serviceAddress );
-            // Ignore if unknown.
-            if( sessions == null )
-            {
-                return;
-            }
-            
-            // Try to remove the remaining empty seession set after removal.
-            synchronized( sessions )
-            {
-                sessions.remove( session );
-                if( sessions.isEmpty() )
-                {
-                    managedSessions.remove( serviceAddress );
-                    lastSession = true;
-                }
-            }
+            lastSession = managedSessions.remove( serviceAddress, sessions );
         }
-        
+
         // Fire session events.
         session.getFilterChain().fireSessionClosed( session );
-        
+
         // Fire listener events.
         try
         {
-            synchronized( listeners )
+            for( IoServiceListener listener : listeners )
             {
-                for( Iterator i = listeners.iterator(); i.hasNext(); )
-                {
-                    ( ( IoServiceListener ) i.next() ).sessionDestroyed( session );
-                }
+                listener.sessionDestroyed( session );
             }
         }
         finally
@@ -288,8 +247,8 @@
             if( session.getService() instanceof IoConnector && lastSession )
             {
                 fireServiceDeactivated(
-                        session.getService(), session.getServiceAddress(),
-                        session.getHandler(), session.getServiceConfig() );
+                    session.getService(), session.getServiceAddress(),
+                    session.getHandler(), session.getServiceConfig() );
             }
         }
     }
@@ -306,53 +265,32 @@
             return;
         }
 
-        Set sessions;
-        synchronized( managedSessions )
-        {
-            sessions = ( Set ) managedSessions.get( serviceAddress );
-        }
-        
-        if( sessions == null )
-        {
+        Set<IoSession> sessions = getManagedSessions( serviceAddress );
+
+        if( sessions.isEmpty() ) {
             return;
         }
 
-        final Object lock = new Object();
-        Set sessionsCopy;
-        
-        // Create a copy to avoid ConcurrentModificationException
-        synchronized( sessions )
-        {
-            sessionsCopy = new IdentityHashSet( sessions );
-        }
-        
-        for( Iterator i = sessionsCopy.iterator(); i.hasNext(); )
+        final CountDownLatch latch = new CountDownLatch( sessions.size() );
+
+        for( IoSession session : sessions )
         {
-            ( ( IoSession ) i.next() ).close().addListener( new IoFutureListener()
+            session.close().addListener( new IoFutureListener()
             {
                 public void operationComplete( IoFuture future )
                 {
-                    synchronized( lock )
-                    {
-                        lock.notifyAll();
-                    }
+                    latch.countDown();
                 }
             } );
         }
 
         try
         {
-            synchronized( lock )
-            {
-                while( !managedSessions.isEmpty() )
-                {
-                    lock.wait( 500 );
-                }
-            }
+            latch.await();
         }
-        catch( InterruptedException ie )
+        catch( InterruptedException e )
         {
-            // Ignored
+            throw new RuntimeIOException( e );
         }
     }
 }

Modified: directory/branches/mina/1.2/core/src/main/java/org/apache/mina/transport/vmpipe/VmPipeConnector.java
URL: http://svn.apache.org/viewvc/directory/branches/mina/1.2/core/src/main/java/org/apache/mina/transport/vmpipe/VmPipeConnector.java?view=diff&rev=471608&r1=471607&r2=471608
==============================================================================
--- directory/branches/mina/1.2/core/src/main/java/org/apache/mina/transport/vmpipe/VmPipeConnector.java
(original)
+++ directory/branches/mina/1.2/core/src/main/java/org/apache/mina/transport/vmpipe/VmPipeConnector.java
Sun Nov  5 17:59:03 2006
@@ -6,16 +6,16 @@
  *  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. 
- *  
+ *  under the License.
+ *
  */
 package org.apache.mina.transport.vmpipe;
 
@@ -41,7 +41,7 @@
 /**
  * Connects to {@link IoHandler}s which is bound on the specified
  * {@link VmPipeAddress}.
- * 
+ *
  * @author The Apache Directory Project (mina-dev@directory.apache.org)
  * @version $Rev$, $Date$
  */
@@ -83,7 +83,7 @@
             config = getDefaultConfig();
         }
 
-        VmPipe entry = ( VmPipe ) VmPipeAcceptor.boundHandlers.get( address );
+        VmPipe entry = VmPipeAcceptor.boundHandlers.get( address );
         if( entry == null )
         {
             return DefaultConnectFuture.newFailedFuture(
@@ -100,7 +100,7 @@
                     new AnonymousSocketAddress(),
                     handler,
                     entry );
-        
+
         // initialize connector session
         try
         {
@@ -118,7 +118,7 @@
         {
             future.setException( t );
         }
-        
+
         // initialize acceptor session
         VmPipeSessionImpl remoteSession = localSession.getRemoteSession();
         try
@@ -127,7 +127,7 @@
             entry.getAcceptor().getFilterChainBuilder().buildFilterChain( filterChain );
             entry.getConfig().getFilterChainBuilder().buildFilterChain( filterChain );
             entry.getConfig().getThreadModel().buildFilterChain( filterChain );
-            
+
             // The following sentences don't throw any exceptions.
             entry.getListeners().fireSessionCreated( remoteSession );
             VmPipeIdleStatusChecker.getInstance().addSession( remoteSession );
@@ -140,7 +140,7 @@
 
         return future;
     }
-    
+
     public IoServiceConfig getDefaultConfig()
     {
         return defaultConfig;

Modified: directory/branches/mina/1.2/core/src/test/java/org/apache/mina/common/support/IoServiceListenerSupportTest.java
URL: http://svn.apache.org/viewvc/directory/branches/mina/1.2/core/src/test/java/org/apache/mina/common/support/IoServiceListenerSupportTest.java?view=diff&rev=471608&r1=471607&r2=471608
==============================================================================
--- directory/branches/mina/1.2/core/src/test/java/org/apache/mina/common/support/IoServiceListenerSupportTest.java
(original)
+++ directory/branches/mina/1.2/core/src/test/java/org/apache/mina/common/support/IoServiceListenerSupportTest.java
Sun Nov  5 17:59:03 2006
@@ -6,16 +6,16 @@
  *  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. 
- *  
+ *  under the License.
+ *
  */
 package org.apache.mina.common.support;
 
@@ -39,9 +39,9 @@
 
 /**
  * Tests {@link IoServiceListenerSupport}.
- * 
+ *
  * @author The Apache Directory Project (mina-dev@directory.apache.org)
- * @version $Rev$, $Date$ 
+ * @version $Rev$, $Date$
  */
 public class IoServiceListenerSupportTest extends TestCase
 {
@@ -50,23 +50,23 @@
     public void testServiceLifecycle() throws Exception
     {
         IoServiceListenerSupport support = new IoServiceListenerSupport();
-    
+
         MockControl listenerControl = MockControl.createStrictControl( IoServiceListener.class
);
         IoServiceListener listener = ( IoServiceListener ) listenerControl.getMock();
-        
+
         // Test activation
         listener.serviceActivated( null, ADDRESS, null, null );
-        
+
         listenerControl.replay();
-        
+
         support.add( listener );
         support.fireServiceActivated( null, ADDRESS, null, null );
-        
+
         listenerControl.verify();
-        
+
         Assert.assertEquals( 1, support.getManagedServiceAddresses().size() );
         Assert.assertTrue( support.getManagedServiceAddresses().contains( ADDRESS ) );
-        
+
         // Test deactivation & other side effects
         listenerControl.reset();
         listener.serviceDeactivated( null, ADDRESS, null, null );
@@ -78,43 +78,43 @@
         support.fireServiceDeactivated( null, ADDRESS, null, null );
         //// Deactivate more than once
         support.fireServiceDeactivated( null, ADDRESS, null, null );
-        
+
         listenerControl.verify();
 
         Assert.assertEquals( 0, support.getManagedServiceAddresses().size() );
         Assert.assertFalse( support.getManagedServiceAddresses().contains( ADDRESS ) );
     }
-    
+
     public void testSessionLifecycle() throws Exception
     {
         IoServiceListenerSupport support = new IoServiceListenerSupport();
-    
+
         TestSession session = new TestSession( ADDRESS );
-        
+
         MockControl chainControl = MockControl.createStrictControl( IoFilterChain.class );
         IoFilterChain chain = ( IoFilterChain ) chainControl.getMock();
         session.setFilterChain( chain );
-        
+
         MockControl listenerControl = MockControl.createStrictControl( IoServiceListener.class
);
         IoServiceListener listener = ( IoServiceListener ) listenerControl.getMock();
-        
+
         // Test creation
         listener.sessionCreated( session );
         chain.fireSessionCreated( session );
         chain.fireSessionOpened( session);
-        
+
         listenerControl.replay();
         chainControl.replay();
-        
+
         support.add( listener );
         support.fireSessionCreated( session );
-        
+
         listenerControl.verify();
         chainControl.verify();
-        
+
         Assert.assertEquals( 1, support.getManagedSessions( ADDRESS ).size() );
         Assert.assertTrue( support.getManagedSessions( ADDRESS ).contains( session ) );
-        
+
         // Test destruction & other side effects
         listenerControl.reset();
         chainControl.reset();
@@ -128,18 +128,18 @@
         support.fireSessionDestroyed( session );
         //// Deactivate more than once
         support.fireSessionDestroyed( session );
-        
+
         listenerControl.verify();
 
         Assert.assertFalse( session.isClosing() );
         Assert.assertEquals( 0, support.getManagedSessions( ADDRESS ).size() );
         Assert.assertFalse( support.getManagedSessions( ADDRESS ).contains( session ) );
     }
-    
+
     public void testDisconnectOnUnbind() throws Exception
     {
         final IoServiceListenerSupport support = new IoServiceListenerSupport();
-    
+
         MockControl acceptorControl = MockControl.createStrictControl( IoAcceptor.class );
         IoAcceptor acceptor = ( IoAcceptor ) acceptorControl.getMock();
 
@@ -151,26 +151,26 @@
         MockControl chainControl = MockControl.createStrictControl( IoFilterChain.class );
         IoFilterChain chain = ( IoFilterChain ) chainControl.getMock();
         session.setFilterChain( chain );
-        
+
         MockControl listenerControl = MockControl.createStrictControl( IoServiceListener.class
);
         IoServiceListener listener = ( IoServiceListener ) listenerControl.getMock();
-        
+
         // Activate a service and create a session.
         listener.serviceActivated( acceptor, ADDRESS, null, config );
         listener.sessionCreated( session );
         chain.fireSessionCreated( session );
         chain.fireSessionOpened( session );
-        
+
         listenerControl.replay();
         chainControl.replay();
-        
+
         support.add( listener );
         support.fireServiceActivated( acceptor, ADDRESS, null, config );
         support.fireSessionCreated( session );
-        
+
         listenerControl.verify();
         chainControl.verify();
-        
+
         // Deactivate a service and make sure the session is closed & destroyed.
         listenerControl.reset();
         chainControl.reset();
@@ -187,6 +187,7 @@
         new Thread()
         {
             // Emulate I/O service
+            @Override
             public void run()
             {
                 try
@@ -195,13 +196,17 @@
                 }
                 catch( InterruptedException e )
                 {
+                    //noinspection CallToPrintStackTrace
                     e.printStackTrace();
                 }
                 support.fireSessionDestroyed( session );
             }
         }.start();
+
         support.fireServiceDeactivated( acceptor, ADDRESS, null, config );
-        
+
+        Thread.sleep(  1000 );
+
         listenerControl.verify();
         configControl.verify();
         chainControl.verify();
@@ -210,11 +215,11 @@
         Assert.assertEquals( 0, support.getManagedSessions( ADDRESS ).size() );
         Assert.assertFalse( support.getManagedSessions( ADDRESS ).contains( session ) );
     }
-    
+
     public void testConnectorActivation() throws Exception
     {
         IoServiceListenerSupport support = new IoServiceListenerSupport();
-    
+
         MockControl connectorControl = MockControl.createStrictControl( IoConnector.class
);
         IoConnector connector = ( IoConnector ) connectorControl.getMock();
 
@@ -226,58 +231,59 @@
 
         MockControl listenerControl = MockControl.createStrictControl( IoServiceListener.class
);
         IoServiceListener listener = ( IoServiceListener ) listenerControl.getMock();
-        
+
         // Creating a session should activate a service automatically.
         listener.serviceActivated( connector, ADDRESS, null, null );
         listener.sessionCreated( session );
         chain.fireSessionCreated( session );
         chain.fireSessionOpened( session );
-        
+
         listenerControl.replay();
         chainControl.replay();
-        
+
         support.add( listener );
         support.fireSessionCreated( session );
-        
+
         listenerControl.verify();
         chainControl.verify();
-        
+
         // Destroying a session should deactivate a service automatically.
         listenerControl.reset();
         chainControl.reset();
         listener.sessionDestroyed( session );
         chain.fireSessionClosed( session );
         listener.serviceDeactivated( connector, ADDRESS, null, null );
-        
+
         listenerControl.replay();
         chainControl.replay();
-        
+
         support.fireSessionDestroyed( session );
-        
+
         listenerControl.verify();
         chainControl.verify();
 
         Assert.assertEquals( 0, support.getManagedSessions( ADDRESS ).size() );
         Assert.assertFalse( support.getManagedSessions( ADDRESS ).contains( session ) );
     }
-    
+
     private static class TestSession extends BaseIoSession
     {
         private final IoService service;
         private final SocketAddress serviceAddress;
         private IoFilterChain filterChain;
-        
+
         TestSession( SocketAddress serviceAddress )
         {
             this( null, serviceAddress );
         }
-        
+
         TestSession( IoService service, SocketAddress serviceAddress )
         {
             this.service = service;
             this.serviceAddress = serviceAddress;
         }
 
+        @Override
         protected void updateTrafficMask()
         {
         }
@@ -291,7 +297,7 @@
         {
             return filterChain;
         }
-        
+
         public void setFilterChain( IoFilterChain filterChain )
         {
             this.filterChain = filterChain;



Mime
View raw message