directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1071031 - in /directory: apacheds/branches/m1/core-api/src/main/java/org/apache/directory/server/core/ apacheds/branches/m1/core/src/main/java/org/apache/directory/server/core/ shared/branches/m1/integ/src/test/java/org/apache/directory/sh...
Date Tue, 15 Feb 2011 20:09:59 GMT
Author: elecharny
Date: Tue Feb 15 20:09:59 2011
New Revision: 1071031

URL: http://svn.apache.org/viewvc?rev=1071031&view=rev
Log:
o The Dn class is not anymore implementing the Cloneable, Serialiable and Comparable interfaces
o Updated the Dn Javadoc
o Renamed the Dn.clone() method to copy()
o Added a addInternal package protected method for deserialization
o Added two methods in DnSerializer that take no Input and Output Stream (they are created
internally)
o Removed the Dn.compateTo() method, it's useless
o Fixed the tests and the server accordingly to those modifications

Modified:
    directory/apacheds/branches/m1/core-api/src/main/java/org/apache/directory/server/core/LdapPrincipal.java
    directory/apacheds/branches/m1/core/src/main/java/org/apache/directory/server/core/DefaultDnFactory.java
    directory/shared/branches/m1/integ/src/test/java/org/apache/directory/shared/ldap/name/DnTest.java
    directory/shared/branches/m1/integ/src/test/java/org/apache/directory/shared/ldap/name/MultiThreadedTest.java
    directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/entry/DefaultEntry.java
    directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/name/Dn.java
    directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/name/DnSerializer.java
    directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/schema/comparators/DnComparator.java
    directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/schema/comparators/UniqueMemberComparator.java

Modified: directory/apacheds/branches/m1/core-api/src/main/java/org/apache/directory/server/core/LdapPrincipal.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/m1/core-api/src/main/java/org/apache/directory/server/core/LdapPrincipal.java?rev=1071031&r1=1071030&r2=1071031&view=diff
==============================================================================
--- directory/apacheds/branches/m1/core-api/src/main/java/org/apache/directory/server/core/LdapPrincipal.java
(original)
+++ directory/apacheds/branches/m1/core-api/src/main/java/org/apache/directory/server/core/LdapPrincipal.java
Tue Feb 15 20:09:59 2011
@@ -29,6 +29,7 @@ import java.security.Principal;
 import org.apache.directory.server.i18n.I18n;
 import org.apache.directory.shared.ldap.model.constants.AuthenticationLevel;
 import org.apache.directory.shared.ldap.model.name.Dn;
+import org.apache.directory.shared.ldap.model.name.DnSerializer;
 import org.apache.directory.shared.util.Strings;
 
 
@@ -203,7 +204,7 @@ public final class LdapPrincipal impleme
     public void readExternal( ObjectInput in ) throws IOException , ClassNotFoundException
     {
         // Read the name
-        dn = (Dn)in.readObject();
+        dn = DnSerializer.deserialize( in );
         
         // read the authentication level
         int level = in.readInt();
@@ -226,11 +227,11 @@ public final class LdapPrincipal impleme
         // Write the name
         if ( dn == null )
         {
-            out.writeObject( Dn.EMPTY_DN );
+            DnSerializer.serialize( Dn.EMPTY_DN, out );
         }
         else
         {
-            out.writeObject( dn );
+            DnSerializer.serialize( dn, out );
         }
         
         // write the authentication level

Modified: directory/apacheds/branches/m1/core/src/main/java/org/apache/directory/server/core/DefaultDnFactory.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/m1/core/src/main/java/org/apache/directory/server/core/DefaultDnFactory.java?rev=1071031&r1=1071030&r2=1071031&view=diff
==============================================================================
--- directory/apacheds/branches/m1/core/src/main/java/org/apache/directory/server/core/DefaultDnFactory.java
(original)
+++ directory/apacheds/branches/m1/core/src/main/java/org/apache/directory/server/core/DefaultDnFactory.java
Tue Feb 15 20:09:59 2011
@@ -91,7 +91,7 @@ public class DefaultDnFactory implements
 
             if ( dnCacheEntry != null )
             {
-                cachedDn = (Dn) dnCacheEntry.getValue();
+                cachedDn = (Dn) dnCacheEntry.getObjectValue();
             }
         }
 

Modified: directory/shared/branches/m1/integ/src/test/java/org/apache/directory/shared/ldap/name/DnTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/m1/integ/src/test/java/org/apache/directory/shared/ldap/name/DnTest.java?rev=1071031&r1=1071030&r2=1071031&view=diff
==============================================================================
--- directory/shared/branches/m1/integ/src/test/java/org/apache/directory/shared/ldap/name/DnTest.java
(original)
+++ directory/shared/branches/m1/integ/src/test/java/org/apache/directory/shared/ldap/name/DnTest.java
Tue Feb 15 20:09:59 2011
@@ -31,13 +31,9 @@ import static org.junit.Assert.fail;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
-import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collections;
-import java.util.Comparator;
 import java.util.List;
 
 import javax.naming.InvalidNameException;
@@ -1329,7 +1325,7 @@ public class DnTest
     // CLONE Operation
     /**
      * test a clone operation on a empty Dn
-     */
+     *
     @Test
     public void testDnCloneEmpty()
     {
@@ -1342,7 +1338,7 @@ public class DnTest
 
     /**
      * test a clone operation on a simple Dn
-     */
+     *
     @Test
     public void testDnCloneSimple() throws LdapException
     {
@@ -1357,7 +1353,7 @@ public class DnTest
 
     /**
      * test a clone operation on a complex Dn
-     */
+     *
     @Test
     public void testDnCloneComplex() throws LdapException
     {
@@ -1500,13 +1496,14 @@ public class DnTest
     }
 
 
+    
     @Test
     public void testAttributeComparisonIsCaseInSensitive() throws Exception
     {
         Dn name1 = new Dn( "cn=HomeDir" );
         Dn name2 = new Dn( "CN=HomeDir" );
 
-        assertEquals( 0, name1.compareTo( name2 ) );
+        assertEquals( name1, name2 );
     }
 
 
@@ -1516,7 +1513,7 @@ public class DnTest
         Dn name1 = new Dn( "cn=HomeDir+cn=WorkDir" );
         Dn name2 = new Dn( "cn=HomeDir+CN=WorkDir" );
 
-        assertEquals( 0, name1.compareTo( name2 ) );
+        assertEquals( name1, name2 );
     }
 
 
@@ -1527,7 +1524,7 @@ public class DnTest
         Dn name1 = new Dn( "cn=HomeDir+cn=WorkDir" );
         Dn name2 = new Dn( "cn=WorkDir+cn=HomeDir" );
 
-        assertEquals( 0, name1.compareTo( name2 ) );
+        assertEquals( name1, name2 );
     }
 
 
@@ -1538,7 +1535,7 @@ public class DnTest
         Dn name1 = new Dn( "cn= HomeDir+cn=Workdir" );
         Dn name2 = new Dn( "cn = Work+cn=HomeDir" );
 
-        assertEquals( 1, name1.compareTo( name2 ) );
+        assertNotSame( name1, name2 );
     }
 
 
@@ -1592,18 +1589,18 @@ public class DnTest
         Dn name4 = new Dn( "cn=Website,cn=John,ou=Marketing,ou=West" );
         Dn name5 = new Dn( "cn=Airline,cn=John,ou=Marketing,ou=West" );
 
-        assertTrue( name0.compareTo( copy ) == 0 );
-        assertTrue( name0.compareTo( name1 ) < 0 );
-        assertTrue( name0.compareTo( name2 ) < 0 );
-        assertTrue( name1.compareTo( name2 ) < 0 );
-        assertTrue( name2.compareTo( name1 ) > 0 );
-        assertTrue( name2.compareTo( name0 ) > 0 );
-        assertTrue( name2.compareTo( name3 ) < 0 );
-        assertTrue( name2.compareTo( name4 ) < 0 );
-        assertTrue( name3.compareTo( name4 ) < 0 );
-        assertTrue( name3.compareTo( name5 ) > 0 );
-        assertTrue( name4.compareTo( name5 ) > 0 );
-        assertTrue( name2.compareTo( name5 ) < 0 );
+        assertEquals( name0, copy );
+        assertTrue( name0.isParentOf( name1 ) );
+        assertTrue( name0.isParentOf( name2 ) );
+        assertTrue( name1.isParentOf( name2 ) );
+        assertTrue( name2.isChildOf( name1 ) );
+        assertTrue( name2.isChildOf( name0 ) );
+        assertNotSame( name2, name3 );
+        assertNotSame( name2, name4 );
+        assertNotSame( name3, name4 );
+        assertNotSame( name3, name5 );
+        assertNotSame( name4, name5 );
+        assertNotSame( name2, name5 );
     }
 
 
@@ -1623,7 +1620,7 @@ public class DnTest
      *
      * @throws Exception
      *             if anything goes wrong.
-     */
+     *
     @Test
     public void testClone() throws Exception
     {
@@ -1634,81 +1631,6 @@ public class DnTest
 
 
     /**
-     * Class to test for compareTo
-     *
-     * @throws Exception
-     *             if anything goes wrong.
-     */
-    @Test
-    public void testCompareTo() throws Exception
-    {
-        Dn name0 = new Dn( "ou=Marketing,ou=East" );
-        Dn copy = new Dn( "ou=Marketing,ou=East" );
-        Dn name1 = new Dn( "cn=John,ou=Marketing,ou=East" );
-        Dn name2 = new Dn( "cn=HomeDir,cn=John,ou=Marketing,ou=East" );
-        Dn name3 = new Dn( "cn=HomeDir,cn=John,ou=Marketing,ou=West" );
-        Dn name4 = new Dn( "cn=Website,cn=John,ou=Marketing,ou=West" );
-        Dn name5 = new Dn( "cn=Airline,cn=John,ou=Marketing,ou=West" );
-
-        assertTrue( name0.compareTo( copy ) == 0 );
-        assertTrue( name0.compareTo( name1 ) < 0 );
-        assertTrue( name0.compareTo( name2 ) < 0 );
-        assertTrue( name1.compareTo( name2 ) < 0 );
-        assertTrue( name2.compareTo( name1 ) > 0 );
-        assertTrue( name2.compareTo( name0 ) > 0 );
-        assertTrue( name2.compareTo( name3 ) < 0 );
-        assertTrue( name2.compareTo( name4 ) < 0 );
-        assertTrue( name3.compareTo( name4 ) < 0 );
-        assertTrue( name3.compareTo( name5 ) > 0 );
-        assertTrue( name4.compareTo( name5 ) > 0 );
-        assertTrue( name2.compareTo( name5 ) < 0 );
-
-        List<Dn> list = new ArrayList<Dn>();
-
-        Comparator<Dn> comparator = new Comparator<Dn>()
-        {
-            public int compare( Dn obj1, Dn obj2 )
-            {
-                Dn n1 = obj1;
-                Dn n2 = obj2;
-                return n1.compareTo( n2 );
-            }
-
-
-            public boolean equals( Object obj )
-            {
-                return super.equals( obj );
-            }
-
-
-            /**
-             * Compute the instance's hash code
-             * @return the instance's hash code
-             */
-            public int hashCode()
-            {
-                return super.hashCode();
-            }
-        };
-
-        list.add( name0 );
-        list.add( name1 );
-        list.add( name2 );
-        list.add( name3 );
-        list.add( name4 );
-        list.add( name5 );
-        Collections.sort( list, comparator );
-
-        assertEquals( name0, list.get( 0 ) );
-        assertEquals( name1, list.get( 1 ) );
-        assertEquals( name2, list.get( 2 ) );
-        assertEquals( name5, list.get( 3 ) );
-        assertEquals( name3, list.get( 4 ) );
-        assertEquals( name4, list.get( 5 ) );
-    }
-
-
-    /**
      * Class to test for size
      *
      * @throws Exception
@@ -3069,74 +2991,6 @@ public class DnTest
     }
 
 
-    private ByteArrayOutputStream serializeDN( Dn dn ) throws IOException
-    {
-        ObjectOutputStream oOut = null;
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-
-        try
-        {
-            oOut = new ObjectOutputStream( out );
-            oOut.writeObject( dn );
-        }
-        catch ( IOException ioe )
-        {
-            throw ioe;
-        }
-        finally
-        {
-            try
-            {
-                if ( oOut != null )
-                {
-                    oOut.flush();
-                    oOut.close();
-                }
-            }
-            catch ( IOException ioe )
-            {
-                throw ioe;
-            }
-        }
-
-        return out;
-    }
-
-
-    private Dn deserializeDN( ByteArrayOutputStream out ) throws IOException, ClassNotFoundException
-    {
-        ObjectInputStream oIn = null;
-        ByteArrayInputStream in = new ByteArrayInputStream( out.toByteArray() );
-
-        try
-        {
-            oIn = new ObjectInputStream( in );
-
-            Dn dn = (Dn) oIn.readObject();
-
-            return dn;
-        }
-        catch ( IOException ioe )
-        {
-            throw ioe;
-        }
-        finally
-        {
-            try
-            {
-                if ( oIn != null )
-                {
-                    oIn.close();
-                }
-            }
-            catch ( IOException ioe )
-            {
-                throw ioe;
-            }
-        }
-    }
-
-
     /**
      * Test the serialization of a Dn
      *
@@ -3148,7 +3002,7 @@ public class DnTest
         Dn dn = new Dn( "ou= Some   People   + dc=  And   Some anImAls,dc = eXample,dc= cOm"
);
         dn.normalize( schemaManager );
 
-        assertEquals( dn, deserializeDN( serializeDN( dn ) ) );
+        assertEquals( dn, DnSerializer.deserialize( DnSerializer.serialize( dn ) ) );
     }
 
 
@@ -3157,7 +3011,7 @@ public class DnTest
     {
         Dn dn = Dn.EMPTY_DN;
 
-        assertEquals( dn, deserializeDN( serializeDN( dn ) ) );
+        assertEquals( dn, DnSerializer.deserialize( DnSerializer.serialize( dn ) ) );
     }
 
 
@@ -3172,13 +3026,8 @@ public class DnTest
         Dn dn = new Dn( "ou= Some   People   + dc=  And   Some anImAls,dc = eXample,dc= cOm"
);
         dn.normalize( schemaManager );
 
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        ObjectOutputStream out = new ObjectOutputStream( baos );
-
-        DnSerializer.serialize( dn, out );
-        out.flush();
+        byte[] data = DnSerializer.serialize( dn );
 
-        byte[] data = baos.toByteArray();
         ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( data ) );
 
         assertEquals( dn, DnSerializer.deserialize( in ) );
@@ -3249,7 +3098,7 @@ public class DnTest
         ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( data ) );
 
         assertEquals( dn, DnSerializer.deserialize(in) );
-        assertEquals( dn, deserializeDN( serializeDN( dn ) ) );
+        assertEquals( dn, DnSerializer.deserialize( DnSerializer.serialize( dn ) ) );
     }
 
 

Modified: directory/shared/branches/m1/integ/src/test/java/org/apache/directory/shared/ldap/name/MultiThreadedTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/m1/integ/src/test/java/org/apache/directory/shared/ldap/name/MultiThreadedTest.java?rev=1071031&r1=1071030&r2=1071031&view=diff
==============================================================================
--- directory/shared/branches/m1/integ/src/test/java/org/apache/directory/shared/ldap/name/MultiThreadedTest.java
(original)
+++ directory/shared/branches/m1/integ/src/test/java/org/apache/directory/shared/ldap/name/MultiThreadedTest.java
Tue Feb 15 20:09:59 2011
@@ -140,8 +140,8 @@ public class MultiThreadedTest
         assertEquals( 0, sharedRdn.compareTo( referenceRdn ) );
 
         sharedDn.normalize( schemaManager );
-        assertEquals( 0, referenceDn.compareTo( sharedDn ) );
-        assertEquals( 0, sharedDn.compareTo( referenceDn ) );
+        assertEquals( referenceDn, sharedDn );
+        assertEquals( sharedDn, referenceDn );
     }
 
 }

Modified: directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/entry/DefaultEntry.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/entry/DefaultEntry.java?rev=1071031&r1=1071030&r2=1071031&view=diff
==============================================================================
--- directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/entry/DefaultEntry.java
(original)
+++ directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/entry/DefaultEntry.java
Tue Feb 15 20:09:59 2011
@@ -35,13 +35,14 @@ import org.apache.directory.shared.i18n.
 import org.apache.directory.shared.ldap.model.constants.SchemaConstants;
 import org.apache.directory.shared.ldap.model.exception.LdapException;
 import org.apache.directory.shared.ldap.model.name.Dn;
+import org.apache.directory.shared.ldap.model.name.DnSerializer;
 import org.apache.directory.shared.ldap.model.name.Rdn;
 import org.apache.directory.shared.ldap.model.name.RdnSerializer;
-import org.apache.directory.shared.util.exception.NotImplementedException;
 import org.apache.directory.shared.ldap.model.schema.AttributeType;
 import org.apache.directory.shared.ldap.model.schema.SchemaManager;
 import org.apache.directory.shared.util.Strings;
 import org.apache.directory.shared.util.Unicode;
+import org.apache.directory.shared.util.exception.NotImplementedException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -2485,12 +2486,12 @@ public class DefaultEntry implements Ent
         if ( dn == null )
         {
             // Write an empty Dn
-            out.writeObject( Dn.EMPTY_DN );
+            DnSerializer.serialize( Dn.EMPTY_DN, out );
         }
         else
         {
             // Write the Dn
-            out.writeObject( dn );
+            DnSerializer.serialize( dn, out );
         }
 
         // Then the attributes.
@@ -2514,7 +2515,7 @@ public class DefaultEntry implements Ent
     public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException
     {
         // Read the Dn
-        dn = (Dn) in.readObject();
+        dn = DnSerializer.deserialize( in );
 
         // Read the number of attributes
         int nbAttributes = in.readInt();

Modified: directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/name/Dn.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/name/Dn.java?rev=1071031&r1=1071030&r2=1071031&view=diff
==============================================================================
--- directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/name/Dn.java
(original)
+++ directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/name/Dn.java
Tue Feb 15 20:09:59 2011
@@ -21,7 +21,6 @@
 package org.apache.directory.shared.ldap.model.name;
 
 
-import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -60,7 +59,7 @@ import org.slf4j.LoggerFactory;
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public class Dn implements Cloneable, Serializable, Comparable<Dn>, Iterable<Rdn>
+public final class Dn implements Iterable<Rdn>
 {
     /** The LoggerFactory used by this class */
     protected static final Logger LOG = LoggerFactory.getLogger( Dn.class );
@@ -83,8 +82,6 @@ public class Dn implements Cloneable, Se
     /** A flag used to tell if the Dn has been normalized */
     private AtomicBoolean normalized;
 
-    // ~ Static fields/initializers
-    // -----------------------------------------------------------------
     /**
      *  The RDNs that are elements of the Dn
      * NOTE THAT THESE ARE IN THE OPPOSITE ORDER FROM THAT IMPLIED BY THE JAVADOC!
@@ -158,9 +155,6 @@ public class Dn implements Cloneable, Se
     }
 
 
-    // ~ Methods
-    // ------------------------------------------------------------------------------------
-
     /**
      * Construct an empty Dn object
      */
@@ -185,7 +179,10 @@ public class Dn implements Cloneable, Se
 
 
     /**
-     * @see #Dn( SchemaManager, String )
+     * Creates a new DN from the given String
+     *
+     * @param upName The String that contains the Dn
+     * @throws LdapInvalidNameException if the String does not contain a valid Dn.
      */
     public Dn( String upName ) throws LdapInvalidDnException
     {
@@ -194,16 +191,10 @@ public class Dn implements Cloneable, Se
 
 
     /**
-     * Parse a String and checks that it is a valid Dn <br>
-     * <p>
-     * &lt;distinguishedName&gt; ::= &lt;name&gt; | e <br>
-     * &lt;name&gt; ::= &lt;name-component&gt; &lt;name-components&gt;
<br>
-     * &lt;name-components&gt; ::= &lt;spaces&gt; &lt;separator&gt;
-     * &lt;spaces&gt; &lt;name-component&gt; &lt;name-components&gt;
| e <br>
-     * </p>
+     * Creates a new Schema aware DN from the given String
      *
-     * @param upName The String that contains the Dn.
      * @param schemaManager the schema manager (optional)
+     * @param upName The String that contains the Dn
      * @throws LdapInvalidNameException if the String does not contain a valid Dn.
      */
     public Dn( SchemaManager schemaManager, String upName ) throws LdapInvalidDnException
@@ -234,7 +225,26 @@ public class Dn implements Cloneable, Se
 
 
     /**
-     * @see #Dn(SchemaManager, String...)
+     * Creates a new instance of Dn, using varargs to declare the RDNs. Each
+     * String is either a full Rdn, or a couple of AttributeType DI and a value.
+     * If the String contains a '=' symbol, the the constructor will assume that
+     * the String arg contains afull Rdn, otherwise, it will consider that the
+     * following arg is the value.<br/>
+     * The created Dn is Schema aware.
+     * <br/><br/>
+     * An example of usage would be :
+     * <pre>
+     * String exampleName = "example";
+     * String baseDn = "dc=apache,dc=org";
+     *
+     * Dn dn = new Dn( DefaultSchemaManager.INSTANCE,
+     *     "cn=Test",
+     *     "ou", exampleName,
+     *     baseDn);
+     * </pre>
+     * @param schemaManager the schema manager
+     * @param upRdns
+     * @throws LdapInvalidDnException
      */
     public Dn(String... upRdns) throws LdapInvalidDnException
     {
@@ -247,9 +257,9 @@ public class Dn implements Cloneable, Se
      * String is either a full Rdn, or a couple of AttributeType DI and a value.
      * If the String contains a '=' symbol, the the constructor will assume that
      * the String arg contains afull Rdn, otherwise, it will consider that the
-     * following arg is the value.
+     * following arg is the value.<br/>
      * The created Dn is Schema aware.
-     * <br/>
+     * <br/><br/>
      * An example of usage would be :
      * <pre>
      * String exampleName = "example";
@@ -260,7 +270,6 @@ public class Dn implements Cloneable, Se
      *     "ou", exampleName,
      *     baseDn);
      * </pre>
-     *
      * @param schemaManager the schema manager
      * @param upRdns
      * @throws LdapInvalidDnException
@@ -342,10 +351,9 @@ public class Dn implements Cloneable, Se
 
 
     /**
+     * Creates a Dn from a list of Rdns.
      *
-     * Creates a Dn by based on the given Rdn.
-     *
-     * @param rdn the Rdn to be used in the Dn
+     * @param rdns the list of Rdns to be used for the Dn
      */
     public Dn( Rdn... rdns )
     {
@@ -366,10 +374,10 @@ public class Dn implements Cloneable, Se
 
 
     /**
+     * Creates a Schema aware Dn from a list of Rdns.
      *
-     * Creates a Dn by based on the given Rdn.
-     *
-     * @param rdn the Rdn to be used in the Dn
+     *  @param schemaManager The SchemaManager to use
+     * @param rdns the list of Rdns to be used for the Dn
      */
     public Dn( SchemaManager schemaManager, Rdn... rdns )
     {
@@ -1068,7 +1076,7 @@ public class Dn implements Cloneable, Se
             return this;
         }
 
-        Dn clonedDn = clone();
+        Dn clonedDn = copy();
 
         // Concatenate the rdns
         clonedDn.rdns.addAll( clonedDn.size() - posn, dn.rdns );
@@ -1109,7 +1117,7 @@ public class Dn implements Cloneable, Se
             return this;
         }
 
-        Dn clonedDn = clone();
+        Dn clonedDn = copy();
 
         // Concatenate the rdns
         clonedDn.rdns.addAll( clonedDn.size() - posn, dn.rdns );
@@ -1153,7 +1161,7 @@ public class Dn implements Cloneable, Se
             return this;
         }
 
-        Dn clonedDn = clone();
+        Dn clonedDn = copy();
 
         // We have to parse the nameComponent which is given as an argument
         Rdn newRdn = new Rdn( comp, schemaManager );
@@ -1185,7 +1193,7 @@ public class Dn implements Cloneable, Se
      */
     public Dn add( Rdn newRdn )
     {
-        Dn clonedDn = clone();
+        Dn clonedDn = copy();
 
         clonedDn.rdns.add( 0, newRdn.clone() );
         clonedDn.normalized.getAndSet( false );
@@ -1237,7 +1245,7 @@ public class Dn implements Cloneable, Se
         // We have to parse the nameComponent which is given as an argument
         Rdn newRdn = new Rdn( comp );
 
-        Dn clonedDn = clone();
+        Dn clonedDn = copy();
 
         int realPos = clonedDn.size() - posn;
         clonedDn.rdns.add( realPos, newRdn );
@@ -1273,7 +1281,7 @@ public class Dn implements Cloneable, Se
         // We have to parse the nameComponent which is given as an argument
         Rdn newRdn = rdn.clone();
 
-        Dn clonedDn = clone();
+        Dn clonedDn = copy();
 
         int realPos = clonedDn.size() - posn;
         clonedDn.rdns.add( realPos, newRdn );
@@ -1284,6 +1292,31 @@ public class Dn implements Cloneable, Se
 
 
     /**
+     * used only for deserialization.
+     * 
+     * {@inheritDoc}
+     */
+    /* No qualifier */ Dn addInternal( int posn, Rdn rdn )
+    {
+        if ( ( posn < 0 ) || ( posn > size() ) )
+        {
+            String message = I18n.err( I18n.ERR_04206, posn, rdns.size() );
+            LOG.error( message );
+            throw new ArrayIndexOutOfBoundsException( message );
+        }
+
+        // We have to parse the nameComponent which is given as an argument
+        Rdn newRdn = rdn.clone();
+
+        int realPos = size() - posn;
+        rdns.add( realPos, newRdn );
+        toUpName();
+
+        return this;
+    }
+
+
+    /**
      * {@inheritDoc}
      */
     public Dn remove( int posn ) throws LdapInvalidDnException
@@ -1300,7 +1333,7 @@ public class Dn implements Cloneable, Se
             throw new ArrayIndexOutOfBoundsException( message );
         }
 
-        Dn clonedDn = clone();
+        Dn clonedDn = copy();
         clonedDn._removeChild( posn );
 
         return clonedDn;
@@ -1342,27 +1375,19 @@ public class Dn implements Cloneable, Se
     /**
      * {@inheritDoc}
      */
-    @Override
-    public Dn clone()
+    //@Override
+    private Dn copy()
     {
-        try
-        {
-            Dn dn = (Dn) super.clone();
-            dn.normalized = new AtomicBoolean( normalized.get() );
-            dn.rdns = new ArrayList<Rdn>();
+        Dn dn = new Dn( schemaManager );
+        dn.normalized = new AtomicBoolean( normalized.get() );
+        dn.rdns = new ArrayList<Rdn>();
 
-            for ( Rdn rdn : rdns )
-            {
-                dn.rdns.add( rdn.clone() );
-            }
-
-            return dn;
-        }
-        catch ( CloneNotSupportedException cnse )
+        for ( Rdn rdn : rdns )
         {
-            LOG.error( I18n.err( I18n.ERR_04207 ) );
-            throw new Error( I18n.err( I18n.ERR_04208 ) );
+            dn.rdns.add( rdn.clone() );
         }
+
+        return dn;
     }
 
 
@@ -1404,32 +1429,6 @@ public class Dn implements Cloneable, Se
     }
 
 
-    /**
-     * {@inheritDoc}
-     */
-    public int compareTo( Dn dn )
-    {
-        if ( dn.size() != size() )
-        {
-            return size() - dn.size();
-        }
-
-        for ( int i = rdns.size(); i > 0; i-- )
-        {
-            Rdn rdn1 = rdns.get( i - 1 );
-            Rdn rdn2 = dn.rdns.get( i - 1 );
-            int res = rdn1.compareTo( rdn2 );
-
-            if ( res != 0 )
-            {
-                return res;
-            }
-        }
-
-        return EQUAL;
-    }
-
-
     private static Ava atavOidToName( Ava atav, Map<String, OidNormalizer> oidsMap
)
         throws LdapInvalidDnException
     {
@@ -1658,7 +1657,24 @@ public class Dn implements Cloneable, Se
 
 
     /**
-     * {@inheritDoc}
+     * Iterate over the inner Rdn. The Rdn are returned from 
+     * the rightmost to the leftmost. For instance, the following code :<br/>
+     * <pre>
+     * Dn dn = new Dn( "sn=test, dc=apache, dc=org );
+     * 
+     * for ( Rdn rdn : dn )
+     * {
+     *     System.out.println( rdn.toString() );
+     * }
+     * </pre>
+     * <br/>
+     * will produce this output : <br/>
+     * <pre>
+     * dc=org
+     * dc=apache
+     * sn=test
+     * </pre>
+     * 
      */
     public Iterator<Rdn> iterator()
     {

Modified: directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/name/DnSerializer.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/name/DnSerializer.java?rev=1071031&r1=1071030&r2=1071031&view=diff
==============================================================================
--- directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/name/DnSerializer.java
(original)
+++ directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/name/DnSerializer.java
Tue Feb 15 20:09:59 2011
@@ -19,9 +19,13 @@
  */
 package org.apache.directory.shared.ldap.model.name;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.ObjectInput;
+import java.io.ObjectInputStream;
 import java.io.ObjectOutput;
+import java.io.ObjectOutputStream;
 
 import org.apache.directory.shared.i18n.I18n;
 import org.apache.directory.shared.util.Strings;
@@ -62,6 +66,36 @@ public final class DnSerializer
      * <li>call the Rdn write method</li>
      *
      * @param dn The Dn to serialize
+     * @return a byte[] containing the serialized DN
+     * @throws IOException If we can't write in this stream
+     */
+    public static byte[] serialize( Dn dn ) throws IOException
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+        
+        serialize( dn, out );
+        
+        out.flush();
+        
+        return baos.toByteArray();
+    }
+
+
+    /**
+     * Serialize a Dn
+     *
+     * We have to store a Dn data efficiently. Here is the structure :
+     *
+     * <li>upName</li> The User provided Dn<p>
+     * <li>normName</li> May be null if the normName is equivalent to
+     * the upName<p>
+     * <li>rdns</li> The rdn's List.<p>
+     *
+     * for each rdn :
+     * <li>call the Rdn write method</li>
+     *
+     * @param dn The Dn to serialize
      * @param out the stream in which the Dn will be serialized
      * @throws IOException If we can't write in this stream
      */
@@ -117,6 +151,26 @@ public final class DnSerializer
      * read is exposed in the {@link DnSerializer#serialize(Dn, ObjectOutput)}
      * method<p>
      *
+     * @param in The input bytes from which the Dn is read
+     * @return a deserialized Dn
+     * @throws IOException If the stream can't be read
+     */
+    public static Dn deserialize( byte[] bytes ) throws IOException
+    {
+        ByteArrayInputStream bais = new ByteArrayInputStream( bytes );
+        ObjectInputStream in = new ObjectInputStream( bais );
+    
+        return deserialize( in );
+    }
+    
+
+    /**
+     * Deserialize a Dn
+     *
+     * We read back the data to create a new Dn. The structure
+     * read is exposed in the {@link DnSerializer#serialize(Dn, ObjectOutput)}
+     * method<p>
+     *
      * @param in The input stream from which the Dn is read
      * @return a deserialized Dn
      * @throws IOException If the stream can't be read
@@ -147,7 +201,7 @@ public final class DnSerializer
         for ( int i = 0; i < nbRdns; i++ )
         {
             Rdn rdn = RdnSerializer.deserialize( in );
-            dn = dn.add( 0, rdn );
+            dn = dn.addInternal( 0, rdn );
         }
 
         return dn;

Modified: directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/schema/comparators/DnComparator.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/schema/comparators/DnComparator.java?rev=1071031&r1=1071030&r2=1071031&view=diff
==============================================================================
--- directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/schema/comparators/DnComparator.java
(original)
+++ directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/schema/comparators/DnComparator.java
Tue Feb 15 20:09:59 2011
@@ -64,7 +64,14 @@ public class DnComparator extends LdapCo
             return -1;
         }
         
-        return dn0.compareTo( dn1 );
+        if ( dn0.equals( dn1 ) )
+        {
+            return 0;
+        }
+        else
+        {
+            return -1;
+        }
     }
 
 

Modified: directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/schema/comparators/UniqueMemberComparator.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/schema/comparators/UniqueMemberComparator.java?rev=1071031&r1=1071030&r2=1071031&view=diff
==============================================================================
--- directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/schema/comparators/UniqueMemberComparator.java
(original)
+++ directory/shared/branches/m1/ldap-model/src/main/java/org/apache/directory/shared/ldap/model/schema/comparators/UniqueMemberComparator.java
Tue Feb 15 20:09:59 2011
@@ -57,17 +57,27 @@ public class UniqueMemberComparator exte
     /**
      * Implementation of the Compare method
      */
-    public int compare( String dnstr0, String dnstr1 )
+    public int compare( String dnstr1, String dnstr2 )
     {
-        int dash0 = dnstr0.lastIndexOf( '#' );
         int dash1 = dnstr1.lastIndexOf( '#' );
+        int dash2 = dnstr2.lastIndexOf( '#' );
 
-        if ( ( dash0 == -1 ) && ( dash1 == -1 ) )
+        if ( ( dash1 == -1 ) && ( dash2 == -1 ) )
         {
             // no UID part
             try
             {
-                return getDn( dnstr0 ).compareTo( getDn( dnstr1 ) );
+                Dn dn1 = getDn( dnstr1 );
+                Dn dn2 = getDn( dnstr2 );
+                
+                if ( dn1.equals( dn2 ) )
+                {
+                    return 0;
+                }
+                else
+                {
+                    return -1;
+                }
             }
             catch ( LdapInvalidDnException ne )
             {
@@ -77,33 +87,33 @@ public class UniqueMemberComparator exte
         else
         {
             // Now, check that we don't have another '#'
-            if ( dnstr0.indexOf( '#' ) != dash0 )
+            if ( dnstr1.indexOf( '#' ) != dash1 )
             {
                 // Yes, we have one : this is not allowed, it should have been
                 // escaped.
                 return -1;
             }
 
-            if ( dnstr1.indexOf( '#' ) != dash0 )
+            if ( dnstr2.indexOf( '#' ) != dash1 )
             {
                 // Yes, we have one : this is not allowed, it should have been
                 // escaped.
                 return 1;
             }
 
-            Dn dn0 = null;
             Dn dn1 = null;
+            Dn dn2 = null;
 
             // This is an UID if the '#' is immediatly
             // followed by a BitString, except if the '#' is
             // on the last position
-            String uid0 = dnstr0.substring( dash0 + 1 );
+            String uid1 = dnstr1.substring( dash1 + 1 );
 
-            if ( dash0 > 0 )
+            if ( dash1 > 0 )
             {
                 try
                 {
-                    dn0 = new Dn( dnstr0.substring( 0, dash0 ) );
+                    dn1 = new Dn( dnstr1.substring( 0, dash1 ) );
                 }
                 catch ( LdapException ne )
                 {
@@ -118,13 +128,13 @@ public class UniqueMemberComparator exte
             // This is an UID if the '#' is immediatly
             // followed by a BitString, except if the '#' is
             // on the last position
-            String uid1 = dnstr1.substring( dash1 + 1 );
+            String uid2 = dnstr2.substring( dash2 + 1 );
 
-            if ( dash1 > 0 )
+            if ( dash2 > 0 )
             {
                 try
                 {
-                    dn1 = new Dn( dnstr0.substring( 0, dash1 ) );
+                    dn2 = new Dn( dnstr1.substring( 0, dash2 ) );
                 }
                 catch ( LdapException ne )
                 {
@@ -136,14 +146,12 @@ public class UniqueMemberComparator exte
                 return 1;
             }
 
-            int dnComp = dn0.compareTo( dn1 );
-
-            if ( dnComp != 0 )
+            if ( dn1.equals( dn2 ) )
             {
-                return dnComp;
+                return uid1.compareTo( uid2 );
             }
 
-            return uid0.compareTo( uid1 );
+            return -1;
         }
     }
 



Mime
View raw message