directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r345612 - in /directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common: name/DnParser.java schema/DnComparator.java
Date Sat, 19 Nov 2005 00:43:09 GMT
Author: elecharny
Date: Fri Nov 18 16:42:21 2005
New Revision: 345612

URL: http://svn.apache.org/viewcvs?rev=345612&view=rev
Log:
Added a brutal and rude synchronization to avoid a race condition in DnParser usage.

At least, it corrects the problem, but the server is now 3 times slower. 

Modified:
    directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/name/DnParser.java
    directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/schema/DnComparator.java

Modified: directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/name/DnParser.java
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/name/DnParser.java?rev=345612&r1=345611&r2=345612&view=diff
==============================================================================
--- directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/name/DnParser.java
(original)
+++ directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/name/DnParser.java
Fri Nov 18 16:42:21 2005
@@ -58,6 +58,7 @@
 
     private ReusableAntlrValueLexer valueLexer;
 
+    private static final Object parserMutex = new Object();
 
     /**
      * Creates a regular non normalizing name parser.
@@ -67,6 +68,7 @@
     public DnParser() throws NamingException
     {
         this.m_isNormalizing = false ;
+        
         try
         {
             init() ;
@@ -102,8 +104,12 @@
             ne.setRootCause( e );
             throw ne;
         }
-        this.m_isNormalizing = true ;
-        this.m_parser.setNormalizer( a_normalizer ) ;
+        
+        synchronized ( parserMutex )
+        {
+            this.m_isNormalizing = true ;
+            this.m_parser.setNormalizer( a_normalizer ) ;
+        }
     }
 
 
@@ -125,26 +131,29 @@
      */
     private void init() throws IOException
     {
-        this.m_selector = new TokenStreamSelector() ;
-
-        // Create lexers and add them to the selector.
-        typeLexer = new ReusableAntlrTypeLexer( new StringReader( "" ) );
-        this.m_selector.addInputStream( typeLexer, ReusableAntlrTypeLexer.LEXER_KEY );
-        valueLexer = new ReusableAntlrValueLexer( typeLexer.getInputState() );
-        this.m_selector.addInputStream( valueLexer, ReusableAntlrValueLexer.LEXER_KEY );
-
-        // Set selector on lexers, select initial lexer and initalize parser
-        typeLexer.setSelector( this.m_selector ) ;
-        valueLexer.setSelector( this.m_selector ) ;
-        this.m_selector.select( ReusableAntlrTypeLexer.LEXER_KEY );
-        this.m_parser = new ReusableAntlrNameParser( m_selector );
+        synchronized ( parserMutex )
+        {
+            this.m_selector = new TokenStreamSelector() ;
+    
+            // Create lexers and add them to the selector.
+            typeLexer = new ReusableAntlrTypeLexer( new StringReader( "" ) );
+            this.m_selector.addInputStream( typeLexer, ReusableAntlrTypeLexer.LEXER_KEY );
+            valueLexer = new ReusableAntlrValueLexer( typeLexer.getInputState() );
+            this.m_selector.addInputStream( valueLexer, ReusableAntlrValueLexer.LEXER_KEY
);
+    
+            // Set selector on lexers, select initial lexer and initalize parser
+            typeLexer.setSelector( this.m_selector ) ;
+            valueLexer.setSelector( this.m_selector ) ;
+            this.m_selector.select( ReusableAntlrTypeLexer.LEXER_KEY );
+            this.m_parser = new ReusableAntlrNameParser( m_selector );
+        }
     }
 
 
     /**
      * Resets the parser and lexers to be reused with new input
      */
-    private synchronized void reset( String name )     
+    private void reset( String name )     
     {
         this.typeLexer.prepareNextInput( new StringReader( name + "#\n" ) );
         this.valueLexer.prepareNextInput( typeLexer.getInputState() );
@@ -161,7 +170,7 @@
      * @throws NamingException if a_name is invalid or the parsers plumbing 
      *     breaks
      */
-    public synchronized Name parse( String name, LdapName emptyName ) throws NamingException
+    public Name parse( String name, LdapName emptyName ) throws NamingException
     {
         if ( log.isDebugEnabled() )
         {
@@ -178,13 +187,19 @@
         {
             if ( null == emptyName )
             {
-                reset( name );
-                emptyName = new LdapName( m_parser.name() ) ;
+                synchronized ( parserMutex )
+                {
+                    reset( name );
+                    emptyName = new LdapName( m_parser.name() ) ;
+                }
             }
             else 
             {
-                reset( name );
-                emptyName.setList( m_parser.name() ) ;
+                synchronized ( parserMutex )
+                {
+                    reset( name );
+                    emptyName.setList( m_parser.name() ) ;
+                }
             }
         }
         catch ( RecognitionException e )

Modified: directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/schema/DnComparator.java
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/schema/DnComparator.java?rev=345612&r1=345611&r2=345612&view=diff
==============================================================================
--- directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/schema/DnComparator.java
(original)
+++ directory/shared/ldap/trunk/common/src/main/java/org/apache/ldap/common/schema/DnComparator.java
Fri Nov 18 16:42:21 2005
@@ -41,6 +41,8 @@
     /** The parser used to parse DN Strings */
     private NameParser parser ;
     
+    private static final Object parserMutex = new Object();
+    
     
     // ------------------------------------------------------------------------
     // C O N S T R U C T O R S
@@ -53,7 +55,10 @@
      */
     public DnComparator() throws NamingException
     {
-        parser = new DnParser() ;
+        synchronized ( parserMutex )
+        {
+            parser = new DnParser() ;
+        }
     }
     
     
@@ -64,7 +69,10 @@
      */
     public DnComparator( NameComponentNormalizer normalizer ) throws NamingException
     {
-        parser = new DnParser( normalizer ) ;
+        synchronized ( parserMutex )
+        {
+            parser = new DnParser( normalizer ) ;
+        }
     }
     
     
@@ -97,19 +105,22 @@
         	{
 	            try
 	            {
-	                dn1 = parser.parse( ( String ) obj1 ) ;
+                    synchronized ( parserMutex )
+                    {
+                        dn1 = parser.parse( ( String ) obj1 ) ;
+                    }
 	            }
 	            catch ( NamingException ne )
 	            {
 	                throw new IllegalArgumentException( 
-	                    "first argument was not a distinguished name" ) ;
+	                    "first argument (" + obj1 + ") was not a distinguished name" ) ;
 	            }
         	}
         }
         else
         {
             throw new IllegalArgumentException( 
-                "first argument was not a distinguished name" ) ;
+                "first argument (" + obj1 + ") was not a Name or a String" ) ;
         }
 
         // Figure out how to compose the Name for the second object
@@ -121,7 +132,10 @@
         {
             try
             {
-                dn2 = parser.parse( ( String ) obj2 ) ;
+                synchronized ( parserMutex )
+                {
+                    dn2 = parser.parse( ( String ) obj2 ) ;
+                }
             }
             catch ( NamingException ne )
             {



Mime
View raw message