You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2006/01/22 08:39:06 UTC

svn commit: r371226 - in /directory/sandbox/trustin/mina-spi/core/src: main/java/org/apache/mina/common/IoAddress.java main/java/org/apache/mina/common/support/UnmodifiableProperties.java test/java/org/apache/mina/common/IoAddressTest.java

Author: trustin
Date: Sat Jan 21 23:38:54 2006
New Revision: 371226

URL: http://svn.apache.org/viewcvs?rev=371226&view=rev
Log:
* Added properties property to IoAddress

Added:
    directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/UnmodifiableProperties.java   (with props)
Modified:
    directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/IoAddress.java
    directory/sandbox/trustin/mina-spi/core/src/test/java/org/apache/mina/common/IoAddressTest.java

Modified: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/IoAddress.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/IoAddress.java?rev=371226&r1=371225&r2=371226&view=diff
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/IoAddress.java (original)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/IoAddress.java Sat Jan 21 23:38:54 2006
@@ -18,20 +18,35 @@
  */
 package org.apache.mina.common;
 
+import java.io.Serializable;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.util.Properties;
+
+import org.apache.mina.common.support.UnmodifiableProperties;
+
 /**
  * Represents the address of the location where I/O occurs.
  *
  * @author The Apache Directory Project (dev@directory.apache.org)
  * @version $Rev$, $Date$
  */
-public class IoAddress implements Comparable
+public class IoAddress implements Comparable, Serializable
 {
+    private static final long serialVersionUID = -8612622458934483901L;
+
     private final String providerType;
     private final String transportType;
     private final String address;
+    private final Properties properties;
     
     public IoAddress( String providerType, String transportType, String address )
     {
+        this( providerType, transportType, address, null );
+    }
+
+    public IoAddress( String providerType, String transportType, String address, Properties properties )
+    {
         if( providerType == null )
         {
             throw new NullPointerException( "providerType" );
@@ -44,6 +59,14 @@
         {
             throw new NullPointerException( "address" );
         }
+        if( properties == null )
+        {
+            properties = new UnmodifiableProperties();
+        }
+        else if( !( properties instanceof UnmodifiableProperties ) )
+        {
+            properties = new UnmodifiableProperties( ( Properties ) properties.clone() );
+        }
         
         providerType = providerType.trim().toLowerCase();
         transportType = transportType.trim().toLowerCase();
@@ -61,6 +84,7 @@
         this.providerType = providerType;
         this.transportType = transportType;
         this.address = address;
+        this.properties = properties;
     }
     
     public IoAddress( String uri )
@@ -81,7 +105,17 @@
         transportType = uri.substring( startPos, colonPos ).trim().toLowerCase();
         startPos = colonPos + 1;
         
-        address = uri.substring( startPos ).trim();
+        int questionPos = uri.lastIndexOf( '?' );
+        if( questionPos < startPos )
+        {
+            address = uri.substring( startPos ).trim();
+            properties = new UnmodifiableProperties();
+        }
+        else
+        {
+            address = uri.substring( startPos, questionPos );
+            properties = parseProperties( uri.substring( questionPos + 1 ) );
+        }
     }
 
     private void checkColonPos( String uri, int colonIdx )
@@ -92,6 +126,40 @@
         }
     }
     
+    private Properties parseProperties( String query )
+    {
+        Properties ret = new Properties();
+        String[] props = query.split( "&" );
+        try
+        {
+            for( int i = props.length - 1; i >= 0; i-- )
+            {
+                String prop = props[ i ];
+                int equalsPos = prop.indexOf( '=' );
+                if( equalsPos < 0 )
+                {
+                    ret.setProperty(
+                            URLDecoder.decode( prop, "ISO-8859-1" ).toLowerCase(),
+                            "true" );
+                }
+                else
+                {
+                    ret.setProperty(
+                            URLDecoder.decode(
+                                    prop.substring( 0, equalsPos ), "ISO-8859-1" ).toLowerCase(),
+                            URLDecoder.decode(
+                                    prop.substring( equalsPos + 1), "ISO-8859-1" ) );
+                }
+            }
+        }
+        catch( UnsupportedEncodingException e )
+        {
+            throw ( InternalError ) new InternalError().initCause( e );
+        }
+        
+        return new UnmodifiableProperties( ret );
+    }
+    
     public String getProviderType()
     {
         return providerType;
@@ -107,6 +175,11 @@
         return address;
     }
     
+    public Properties getProperties()
+    {
+        return properties;
+    }
+    
     public String toString()
     {
         return providerType + ':' + transportType + ':' + address;
@@ -138,5 +211,11 @@
     public int compareTo( Object o )
     {
         return this.toString().compareTo( ( ( IoAddress ) o ).toString() );
+    }
+    
+    public Object clone()
+    {
+        return new IoAddress(
+                providerType, transportType, address, properties );
     }
 }

Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/UnmodifiableProperties.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/UnmodifiableProperties.java?rev=371226&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/UnmodifiableProperties.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/UnmodifiableProperties.java Sat Jan 21 23:38:54 2006
@@ -0,0 +1,214 @@
+/*
+ *   @(#) $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.support;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.InvalidPropertiesFormatException;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Unmodifiable version of {@link Properties}.
+ *
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public final class UnmodifiableProperties extends Properties
+{
+    private static final long serialVersionUID = 4951867421321628797L;
+    
+    private final Properties props;
+
+    public UnmodifiableProperties()
+    {
+        this.props = new Properties();
+    }
+
+    public UnmodifiableProperties( Properties props )
+    {
+        super();
+        this.props = props;
+    }
+
+    public Object setProperty( String key, String value )
+    {
+        throw new UnsupportedOperationException();
+    }
+
+    public void clear()
+    {
+        throw new UnsupportedOperationException();
+    }
+
+    public Set entrySet()
+    {
+        return Collections.unmodifiableSet( props.entrySet() );
+    }
+
+    public Set keySet()
+    {
+        return Collections.unmodifiableSet( props.keySet() );
+    }
+
+    public Object put( Object arg0, Object arg1 )
+    {
+        throw new UnsupportedOperationException();
+    }
+
+    public void putAll( Map arg0 )
+    {
+        throw new UnsupportedOperationException();
+    }
+
+    public Object remove( Object key )
+    {
+        throw new UnsupportedOperationException();
+    }
+
+    public Collection values()
+    {
+        return Collections.unmodifiableCollection( props.values() );
+    }
+    
+    public Object clone()
+    {
+        return new UnmodifiableProperties( props );
+    }
+
+    public String getProperty( String key, String defaultValue )
+    {
+        return props.getProperty( key, defaultValue );
+    }
+
+    public String getProperty( String key )
+    {
+        return props.getProperty( key );
+    }
+
+    public void list( PrintStream out )
+    {
+        props.list( out );
+    }
+
+    public void list( PrintWriter out )
+    {
+        props.list( out );
+    }
+
+    public void load( InputStream inStream ) throws IOException
+    {
+        props.load( inStream );
+    }
+
+    public void loadFromXML( InputStream in ) throws IOException, InvalidPropertiesFormatException
+    {
+        props.loadFromXML( in );
+    }
+
+    public Enumeration propertyNames()
+    {
+        return props.propertyNames();
+    }
+
+    /**
+     * @deprecated
+     */
+    public void save( OutputStream out, String comments )
+    {
+        props.save( out, comments );
+    }
+
+    public void store( OutputStream out, String comments ) throws IOException
+    {
+        props.store( out, comments );
+    }
+
+    public void storeToXML( OutputStream os, String comment, String encoding ) throws IOException
+    {
+        props.storeToXML( os, comment, encoding );
+    }
+
+    public void storeToXML( OutputStream os, String comment ) throws IOException
+    {
+        props.storeToXML( os, comment );
+    }
+
+    public boolean contains( Object value )
+    {
+        return props.contains( value );
+    }
+
+    public boolean containsKey( Object key )
+    {
+        return props.containsKey( key );
+    }
+
+    public boolean containsValue( Object value )
+    {
+        return props.containsValue( value );
+    }
+
+    public Enumeration elements()
+    {
+        return props.elements();
+    }
+
+    public boolean equals( Object o )
+    {
+        return props.equals( o );
+    }
+
+    public Object get( Object key )
+    {
+        return props.get( key );
+    }
+
+    public int hashCode()
+    {
+        return props.hashCode();
+    }
+
+    public boolean isEmpty()
+    {
+        return props.isEmpty();
+    }
+
+    public Enumeration keys()
+    {
+        return props.keys();
+    }
+
+    public int size()
+    {
+        return props.size();
+    }
+
+    public String toString()
+    {
+        return props.toString();
+    }
+}

Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/UnmodifiableProperties.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Modified: directory/sandbox/trustin/mina-spi/core/src/test/java/org/apache/mina/common/IoAddressTest.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/test/java/org/apache/mina/common/IoAddressTest.java?rev=371226&r1=371225&r2=371226&view=diff
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/test/java/org/apache/mina/common/IoAddressTest.java (original)
+++ directory/sandbox/trustin/mina-spi/core/src/test/java/org/apache/mina/common/IoAddressTest.java Sat Jan 21 23:38:54 2006
@@ -69,10 +69,19 @@
     
     public void testConstructor2() throws Exception
     {
-        IoAddress uri = new IoAddress( "nio:socket:192.168.0.1:8080" );
+        IoAddress uri;
+        
+        uri = new IoAddress( "nio:socket:192.168.0.1:8080" );
         Assert.assertEquals( "nio", uri.getProviderType() );
         Assert.assertEquals( "socket", uri.getTransportType() );
         Assert.assertEquals( "192.168.0.1:8080", uri.getAddress() );
+        
+        uri = new IoAddress( "nio:socket:192.168.0.1:8080?reuseAddr&receiveBufferSize=4096" );
+        Assert.assertEquals( "nio", uri.getProviderType() );
+        Assert.assertEquals( "socket", uri.getTransportType() );
+        Assert.assertEquals( "192.168.0.1:8080", uri.getAddress() );
+        Assert.assertEquals( 2, uri.getProperties().size() );
+        Assert.assertEquals( "true", uri.getProperties().getProperty( "reuseaddr" ) );
+        Assert.assertEquals( "4096", uri.getProperties().getProperty( "receivebuffersize" ) );
     }
-
 }