o synchronized the normalize method
o changed the nomalized flag to AtomicBoolean


git-svn-id: https://svn.apache.org/repos/asf/directory/shared/branches/shared-dnfactory-experiment@981283 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ldap/src/main/java/org/apache/directory/shared/ldap/name/DN.java b/ldap/src/main/java/org/apache/directory/shared/ldap/name/DN.java
index 64f9aff..be73470 100644
--- a/ldap/src/main/java/org/apache/directory/shared/ldap/name/DN.java
+++ b/ldap/src/main/java/org/apache/directory/shared/ldap/name/DN.java
@@ -32,6 +32,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import javax.naming.InvalidNameException;
 import javax.naming.Name;
@@ -88,7 +89,7 @@
     public static final int EQUAL = 0;
 
     /** A flag used to tell if the DN has been normalized */
-    private boolean normalized;
+    private AtomicBoolean normalized;
 
     // ~ Static fields/initializers
     // -----------------------------------------------------------------
@@ -134,7 +135,7 @@
         this.schemaManager = schemaManger;
         upName = "";
         normName = "";
-        normalized = true;
+        normalized = new AtomicBoolean( true );
     }
 
     
@@ -175,6 +176,8 @@
 
         toUpName();
         
+        normalized = new AtomicBoolean();
+        
         if( schemaManager != null )
         {
             normalize( schemaManager.getNormalizerMapping() );
@@ -182,7 +185,7 @@
         else
         {
             normalizeInternal();
-            normalized = false;
+            normalized.set( false );
         }
     }
 
@@ -217,6 +220,8 @@
             DnParser.parseInternal( upName, rdns );
         }
         
+        normalized = new AtomicBoolean();
+        
         if( schemaManager != null )
         {
             this.schemaManager = schemaManager;
@@ -224,7 +229,7 @@
         }
         else
         {
-            normalized = false;
+            normalized.set( false );
 
             // Stores the representations of a DN : internal (as a string and as a
             // byte[]) and external.
@@ -304,6 +309,8 @@
             throw new LdapInvalidDnException( ResultCodeEnum.INVALID_DN_SYNTAX, I18n.err( I18n.ERR_04202 ) );
         }
 
+        normalized = new AtomicBoolean();
+        
         // Stores the representations of a DN : internal (as a string and as a
         // byte[]) and external.
         upName = sb.toString();
@@ -313,11 +320,10 @@
         {
             this.schemaManager = schemaManager;
             normalize( schemaManager.getNormalizerMapping() );
-            normalized = true;
         }
         else
         {
-            normalized = false;
+            normalized.set( false );
             normalizeInternal();
         }
     }
@@ -333,7 +339,7 @@
      */
     DN( String upName, String normName, byte[] bytes )
     {
-        normalized = true;
+        normalized = new AtomicBoolean( true );
         this.upName = upName;
         this.normName = normName;
         this.bytes = bytes;
@@ -372,13 +378,13 @@
             this.normName = rdn.getNormName();
             this.upName = rdn.getName();
             this.bytes = StringTools.getBytesUtf8( normName );
-            normalized = true;
+            normalized = new AtomicBoolean( true );
         }
         else
         {
             normalizeInternal();
             toUpName();
-            normalized = false;
+            normalized = new AtomicBoolean( false );
         }
     }
 
@@ -414,7 +420,7 @@
         }
         
         newDn.normalizeInternal();
-        newDn.normalized = true;
+        newDn.normalized.set( true );
         
         return newDn;
     }
@@ -1582,7 +1588,7 @@
 
         dn.normalizeInternal();
 
-        dn.normalized = true;
+        dn.normalized.set( true );
         return dn;
     }
 
@@ -1611,25 +1617,35 @@
             return this;
         }
 
-        if ( size() == 0 )
+        if( normalized.get() )
         {
-            normalized = true;
+           return this; 
+        }
+        
+        synchronized ( this )
+        {
+            if ( size() == 0 )
+            {
+                normalized.set( true );
+                return this;
+            }
+            
+            Enumeration<RDN> localRdns = getAllRdn();
+            
+            // Loop on all RDNs
+            while ( localRdns.hasMoreElements() )
+            {
+                RDN rdn = localRdns.nextElement();
+                
+                rdn.normalize( oidsMap );
+            }
+            
+            normalizeInternal();
+            
+            normalized.set( true );
+            
             return this;
         }
-
-        Enumeration<RDN> localRdns = getAllRdn();
-
-        // Loop on all RDNs
-        while ( localRdns.hasMoreElements() )
-        {
-            RDN rdn = localRdns.nextElement();
-            
-            rdn.normalize( oidsMap );
-        }
-
-        normalizeInternal();
-        normalized = true;
-        return this;
     }
 
 
@@ -1653,7 +1669,7 @@
      */
     public boolean isNormalized()
     {
-        return normalized;
+        return normalized.get();
     }
 
 
@@ -1745,7 +1761,7 @@
         }
         
         // A serialized DN is always normalized.
-        normalized = true;
+        normalized.set( true );
             
         // Should we read the byte[] ???
         bytes = StringTools.getBytesUtf8( upName );