directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r161439 - in directory/network/trunk/src: java/org/apache/mina/common/TransportType.java test/org/apache/mina/common/TransportTypeTest.java
Date Fri, 15 Apr 2005 11:24:47 GMT
Author: trustin
Date: Fri Apr 15 04:24:46 2005
New Revision: 161439

URL: http://svn.apache.org/viewcvs?view=rev&rev=161439
Log:
Resolving DIRMINA-16: Improve TransportType for permit to extend MINA supported transport
types.

Added:
    directory/network/trunk/src/test/org/apache/mina/common/TransportTypeTest.java   (with
props)
Modified:
    directory/network/trunk/src/java/org/apache/mina/common/TransportType.java

Modified: directory/network/trunk/src/java/org/apache/mina/common/TransportType.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/TransportType.java?view=diff&r1=161438&r2=161439
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/TransportType.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/common/TransportType.java Fri Apr 15
04:24:46 2005
@@ -21,88 +21,130 @@
 import java.io.InvalidObjectException;
 import java.io.ObjectStreamException;
 import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
 
 /**
- * Represents network transport types.  MINA provides three transport types:
+ * Represents network transport types.
+ * MINA provides three transport types by default:
  * <ul>
  *   <li>{@link #SOCKET} - TCP/IP</li>
  *   <li>{@link #DATAGRAM} - UDP/IP</li>
  *   <li>{@link #VM_PIPE} - in-VM pipe support (only available in protocol
  *       layer</li>
- * </ul> 
+ * </ul>
+ * <p>
+ * You can also create your own transport type.  Please refer to
+ * {@link #TransportType(String[], boolean)}.
  * 
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$
  */
-public class TransportType implements Serializable
+public final class TransportType implements Serializable
 {
     private static final long serialVersionUID = 3258132470497883447L;
+    
+    private static final Map name2type = new HashMap();
+    
+    private static void register( String[] names, TransportType type )
+    {
+        synchronized( name2type )
+        {
+            for( int i = names.length - 1; i >= 0; i -- )
+            {
+                if( name2type.containsKey( names[i] ) )
+                {
+                    throw new IllegalArgumentException(
+                            "Transport type name '" + names[i] + "' is already taken." );
+                }
+            }
+
+            for( int i = names.length - 1; i >= 0; i -- )
+            {
+                name2type.put( names[i].toUpperCase(), type );
+            }
+        }
+    }
 
     /**
-     * Transport type: TCP/IP (<code>SocketChannel</code>)
+     * Transport type: TCP/IP (Registry name: <tt>"SOCKET"</tt> or <tt>"TCP"</tt>)
      */
-    public static final TransportType SOCKET = new TransportType( "SOCKET",
-            false );
+    public static final TransportType SOCKET =
+        new TransportType( new String[] { "SOCKET", "TCP" }, false );
 
     /**
-     * Transport type: UDP/IP (<code>DatagramChannel</code>)
+     * Transport type: UDP/IP (Registry name: <tt>"DATAGRAM"</tt> or <tt>"UDP"</tt>)
      */
-    public static final TransportType DATAGRAM = new TransportType(
-            "DATAGRAM", true );
+    public static final TransportType DATAGRAM =
+        new TransportType( new String[] { "DATAGRAM", "UDP" }, true );
 
     /**
-     * Transport type: VM pipe (direct message exchange).
+     * Transport type: in-VM pipe (Registry name: <tt>"VM_PIPE"</tt>) 
      * Please refer to
      * <a href="../protocol/vmpipe/package-summary.htm"><tt>org.apache.mina.protocol.vmpipe</tt></a>
      * package.
      */
-    public static final TransportType VM_PIPE = new TransportType( "VM_PIPE",
-            false );
+    public static final TransportType VM_PIPE =
+        new TransportType( new String[] { "VM_PIPE" }, false );
+    
 
     /**
-     * Returns the transport type of the specified name.  Here are the list
-     * of available names:
-     * <ul>
-     *   <li><code>"socket"</code> or <code>"tcp"</code> returns
{@link #SOCKET}</li>
-     *   <li><code>"datagram"</code> or <code>"udp"</code>
returns {@link #DATAGRAM}</li>
-     *   <li><code>"vm_pipe"</code> returns {@link #VM_PIPE}</li>
-     * </ul>
+     * Returns the transport type of the specified name.
      * All names are case-insensitive.
      * 
      * @param name the name of the transport type
      * @return the transport type
      * @throws IllegalArgumentException if the specified name is not available.
      */
-    public static TransportType getInstance(String name)
+    public static TransportType getInstance( String name )
     {
-        if( "socket".equalsIgnoreCase(name) || "tcp".equalsIgnoreCase(name) )
+        TransportType type = (TransportType) name2type.get( name.toUpperCase() );
+        if( type != null )
         {
-            return SOCKET;
-        }
-
-        if( "datagram".equalsIgnoreCase(name) || "udp".equalsIgnoreCase(name) )
-        {
-            return DATAGRAM;
-        }
-        
-        if( "vm_pipe".equalsIgnoreCase(name) )
-        {
-            return VM_PIPE;
+            return type;
         }
         
         throw new IllegalArgumentException("Unknown transport type name: " + name);
     }
 
-    private final String strVal;
+    private final String[] names;
 
-    private final boolean stateless;
+    private final transient boolean stateless;
 
     /**
-     * Creates a new instance.
+     * Creates a new instance.  New transport type is automatically registered
+     * to internal registry so that you can look it up using {@link #getInstance(String)}.
+     * 
+     * @param names the name or aliases of this transport type
+     * @param stateless <tt>true</tt> if and only if this transport type is stateless
+     * 
+     * @throws IllegalArgumentException if <tt>names</tt> are already registered
or empty
      */
-    private TransportType( String strVal, boolean stateless )
+    public TransportType( String[] names, boolean stateless )
     {
-        this.strVal = strVal;
+        if( names == null )
+        {
+            throw new NullPointerException( "names" );
+        }
+        if( names.length == 0 )
+        {
+            throw new IllegalArgumentException( "names is empty" );
+        }
+
+        for( int i = 0; i < names.length; i ++ )
+        {
+            if( names[ i ] == null )
+            {
+                throw new NullPointerException( "strVals[" + i + "]" );
+            }
+            
+            names[ i ] = names[ i ].toUpperCase();
+        }
+
+        register( names, this );
+        this.names = names;
         this.stateless = stateless;
     }
 
@@ -114,22 +156,40 @@
     {
         return stateless;
     }
+    
+    /**
+     * Returns the known names of this transport type.
+     */
+    public Set getNames()
+    {
+        Set result = new TreeSet();
+        for( int i = names.length - 1; i >= 0; i -- )
+        {
+            result.add( names[ i ] );
+        }
+        
+        return result;
+    }
 
     public String toString()
     {
-        return strVal;
+        return names[0];
     }
     
     private Object readResolve() throws ObjectStreamException
     {
-        if( strVal.equals( SOCKET.toString() ) )
-            return SOCKET;
-        if( strVal.equals( DATAGRAM.toString() ) )
-            return DATAGRAM;
-        if( strVal.equals( VM_PIPE.toString() ) )
-            return VM_PIPE;
-        else
-            throw new InvalidObjectException( "Unknown transport type: "
-                    + this );
+        for( int i = names.length - 1; i >= 0; i -- )
+        {
+            try
+            {
+                return getInstance( names[ i ] );
+            }
+            catch( IllegalArgumentException e )
+            {
+                // ignore
+            }
+        }
+        
+        throw new InvalidObjectException( "Unknown transport type." );
     }
 }

Added: directory/network/trunk/src/test/org/apache/mina/common/TransportTypeTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/common/TransportTypeTest.java?view=auto&rev=161439
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/common/TransportTypeTest.java (added)
+++ directory/network/trunk/src/test/org/apache/mina/common/TransportTypeTest.java Fri Apr
15 04:24:46 2005
@@ -0,0 +1,68 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed 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.
+ *
+ */
+package org.apache.mina.common;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+/**
+ * Tests {@link TransportType}.
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class TransportTypeTest extends TestCase {
+
+    public static void main(String[] args) {
+        junit.textui.TestRunner.run(TransportTypeTest.class);
+    }
+    
+    public void testRegistration()
+    {
+        TransportType myType = new TransportType( new String[] { "a", "b", "c" }, true );
+        
+        Assert.assertSame( myType, TransportType.getInstance( "a" ) );
+        Assert.assertSame( myType, TransportType.getInstance( "A" ) );
+        Assert.assertSame( myType, TransportType.getInstance( "b" ) );
+        Assert.assertSame( myType, TransportType.getInstance( "B" ) );
+        Assert.assertSame( myType, TransportType.getInstance( "c" ) );
+        Assert.assertSame( myType, TransportType.getInstance( "C" ) );
+        try
+        {
+            TransportType.getInstance( "unknown" );
+            Assert.fail();
+        }
+        catch( IllegalArgumentException e )
+        {
+            // ignore
+        }
+        
+        try
+        {
+            new TransportType( new String[] { "A" }, false );
+            Assert.fail();
+        }
+        catch( IllegalArgumentException e )
+        {
+            // ignore
+        }
+    }
+
+}

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



Mime
View raw message