directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r371279 - in /directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common: IoService.java IoServiceProvider.java
Date Sun, 22 Jan 2006 12:57:24 GMT
Author: trustin
Date: Sun Jan 22 04:57:19 2006
New Revision: 371279

URL: http://svn.apache.org/viewcvs?rev=371279&view=rev
Log:
* Implemented IoService.unbindAll()
* Added getBoundAddresses()

Modified:
    directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/IoService.java
    directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/IoServiceProvider.java

Modified: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/IoService.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/IoService.java?rev=371279&r1=371278&r2=371279&view=diff
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/IoService.java
(original)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/IoService.java
Sun Jan 22 04:57:19 2006
@@ -19,9 +19,12 @@
 package org.apache.mina.common;
 
 import java.io.IOException;
+import java.net.BindException;
 import java.util.Collection;
 import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
@@ -34,15 +37,20 @@
 import org.apache.mina.util.IdentityHashSet;
 
 /**
- * TODO Document me
- * TODO Add UnsupportedAddressException
+ * Provides I/O services to users and manages {@link IoServiceProvider}s.
+ * This class is the first gateway you encounter when you implement your
+ * application using MINA.  This class provides general operations to activate
+ * or to deactivate server and client applications.
  * 
  * @author The Apache Directory Project (dev@directory.apache.org)
  * @version $Rev$, $Date$
+ * 
+ * @see IoServiceProvider
  */
 public class IoService
 {
     private static final Map providers = new HashMap();
+    private static final Set boundAddresses = new HashSet(); 
     private static ExceptionMonitor exceptionMonitor = new DefaultExceptionMonitor();
     
     static
@@ -53,7 +61,8 @@
     /**
      * Registers all {@link IoServiceProvider}s.
      */
-    private static void registerServiceProviders() {
+    private static void registerServiceProviders()
+    {
         for( Enumeration e = Service.providers( IoServiceProvider.class );
              e.hasMoreElements(); )
         {
@@ -62,6 +71,13 @@
         }
     }
     
+    /**
+     * Registers the specified <tt>provider</tt>.  You don't need to call this
+     * method to register the provider because {@link IoService} will discover
+     * the provider using Java SPI when it boots up.
+     * 
+     * @throws IllegalArgumentException if the specified provider is already registered
+     */
     public static void registerProvider( IoServiceProvider provider )
     {
         if( provider == null )
@@ -81,6 +97,11 @@
         }
     }
 
+    /**
+     * Deregisters the specified <tt>provider</tt>.
+     * 
+     * @return <tt>true</tt> if deregistered successfully
+     */
     public static boolean deregisterProvider( IoServiceProvider provider )
     {
         if( provider == null )
@@ -94,6 +115,9 @@
         }
     }
     
+    /**
+     * Returns the set of all registered {@link IoServiceProvider}s.
+     */
     public static Set getProviders()
     {
         synchronized( providers )
@@ -102,16 +126,32 @@
         }
     }
     
+    /**
+     * Returns the {@link IoServiceProvider} with the specified
+     * <tt>providerType</tt> and <tt>transportType</tt>.
+     * 
+     * @return <tt>null</tt> if not found
+     */
     public static IoServiceProvider getProvider( String providerType, String transportType
)
     {
     	return ( IoServiceProvider ) providers.get( getProviderKey( providerType, transportType
) );
     }
     
+    /**
+     * Returns the {@link IoServiceProvider} which is supposed to provide
+     * I/O service to the specified <tt>address</tt>.
+     * 
+     * @return <tt>null</tt> if not found
+     */
     public static IoServiceProvider getProvider( IoAddress address )
     {
         return ( IoServiceProvider ) providers.get( getProviderKey( address ) );
     }
     
+    /**
+     * Same with {@link #getProvider(IoAddress)} except that this method
+     * throws an exception instead of returning <tt>null</tt>.
+     */
     private static IoServiceProvider getProviderSafely( IoAddress address )
     {
         if( address == null )
@@ -127,16 +167,25 @@
     	throw new UnsupportedAddressException( address.toString() );
     }
     
+    /**
+     * Returns the key which is used to manage provider list.
+     */
 	private static String getProviderKey( IoServiceProvider provider )
     {
 		return getProviderKey( provider.getProviderType(), provider.getTransportType() );
 	}
     
+    /**
+     * Returns the key which is used to manage provider list.
+     */
     private static String getProviderKey( IoAddress address )
     {
         return getProviderKey( address.getProviderType(), address.getTransportType() );
     }
 	
+    /**
+     * Returns the key which is used to manage provider list.
+     */
 	private static String getProviderKey( String providerType, String transportType )
 	{
 		return providerType + ':' + transportType;
@@ -195,38 +244,80 @@
         {
             throw new NullPointerException( "handler" );
         }
-    	IoAcceptor acceptor = getProviderSafely( address ).getAcceptor();
-        if( filterChainBuilder == null )
+        if( address == null )
         {
-            filterChainBuilder = IoFilterChainBuilder.NOOP;
+            throw new NullPointerException( "address" );
+        }
+        
+        synchronized( boundAddresses )
+        {
+            if( boundAddresses.contains( address ) )
+            {
+                throw new BindException( "Address already in use: " + address );
+            }
+            
+            IoAcceptor acceptor = getProviderSafely( address ).getAcceptor();
+            if( filterChainBuilder == null )
+            {
+                filterChainBuilder = IoFilterChainBuilder.NOOP;
+            }
+            acceptor.bind(
+                    address, handler,
+                    createFilterChainBuilder( address, filterChainBuilder ) );
+            
+            boundAddresses.add( address );
         }
-    	acceptor.bind(
-                address, handler,
-                createFilterChainBuilder( address, filterChainBuilder ) );
     }
 
     /**
-     * Unbinds from the specified <code>address</code> and disconnects all clients
-     * connected there.
+     * Unbinds from the specified <code>address</code>.
      */
     public static void unbind( IoAddress address )
     {
-        IoAcceptor acceptor = getProviderSafely( address ).getAcceptor();
-        acceptor.unbind( address );
+        synchronized( boundAddresses )
+        {
+            if( !boundAddresses.contains( address ) )
+            {
+                return;
+            }
+
+            IoAcceptor acceptor = getProviderSafely( address ).getAcceptor();
+            acceptor.unbind( address );
+            
+            boundAddresses.remove( address );
+        }
     }
-    
+
     /**
-     * Unbinds from the specified <code>address</code> and disconnects all clients
-     * connected there.
+     * Unbinds from the specified <code>address</code>.
      */
     public static void unbind( String address )
     {
         unbind( createAddress( address ) );
     }
     
+    /**
+     * Unbinds all services.
+     */
     public static void unbindAll()
     {
-        // TODO implement me
+        // Get a copy to avoid ConcurrentModificationException
+        Set boundAddresses = getBoundAddresses();
+        for( Iterator i = boundAddresses.iterator(); i.hasNext(); )
+        {
+            unbind( ( IoAddress ) i.next() );
+        }
+    }
+    
+    /**
+     * Returns the {@link Set} of all bound {@link IoAddress}es.
+     */
+    public static Set getBoundAddresses()
+    {
+        synchronized( boundAddresses )
+        {
+            return new HashSet( boundAddresses );
+        }
     }
     
     /**

Modified: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/IoServiceProvider.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/IoServiceProvider.java?rev=371279&r1=371278&r2=371279&view=diff
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/IoServiceProvider.java
(original)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/IoServiceProvider.java
Sun Jan 22 04:57:19 2006
@@ -31,6 +31,13 @@
  * This interface is for transport type implementors only.  Ordinary users
  * don't need to implement this interface.
  * 
+ * <h3>Implementing New Transport Types</h3>
+ * MINA uses
+ * <a href="http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Service%20Provider">Java
SPI</a>
+ * to help users implement their favorite transport types easily.  All you need
+ * to do is to implement the {@link IoServiceProvider} and write the
+ * <tt>META-INF/services/org.apache.mina.common.IoServiceProvider</tt> file.
+ * 
  * @author The Apache Directory Project (dev@directory.apache.org)
  * @version $Rev$, $Date$
  */



Mime
View raw message