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

svn commit: r368990 - in /directory/trunks/apacheds: core-unit/src/test/java/org/apache/ldap/server/jndi/ReferralTest.java core/src/main/java/org/apache/ldap/server/referral/ReferralService.java

Author: akarasulu
Date: Fri Jan 13 21:56:52 2006
New Revision: 368990

URL: http://svn.apache.org/viewcvs?rev=368990&view=rev
Log:
changes ...

 o fixed a couple bugs where cache updates occured before confirming successful
   operation completion
 o added modify handing and tests for both modify overloads in the interceptor
 o did some refactoring in interceptor to make sure we reuse some code
 o also refactored in test case to reuse code that checks for correct results


Modified:
    directory/trunks/apacheds/core-unit/src/test/java/org/apache/ldap/server/jndi/ReferralTest.java
    directory/trunks/apacheds/core/src/main/java/org/apache/ldap/server/referral/ReferralService.java

Modified: directory/trunks/apacheds/core-unit/src/test/java/org/apache/ldap/server/jndi/ReferralTest.java
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/core-unit/src/test/java/org/apache/ldap/server/jndi/ReferralTest.java?rev=368990&r1=368989&r2=368990&view=diff
==============================================================================
--- directory/trunks/apacheds/core-unit/src/test/java/org/apache/ldap/server/jndi/ReferralTest.java (original)
+++ directory/trunks/apacheds/core-unit/src/test/java/org/apache/ldap/server/jndi/ReferralTest.java Fri Jan 13 21:56:52 2006
@@ -27,7 +27,10 @@
 import javax.naming.NamingException;
 import javax.naming.ReferralException;
 import javax.naming.directory.Attributes;
+import javax.naming.directory.BasicAttribute;
 import javax.naming.directory.BasicAttributes;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.ModificationItem;
 import javax.naming.ldap.InitialLdapContext;
 import javax.naming.ldap.LdapContext;
 
@@ -158,6 +161,29 @@
     }
 
     
+    public void checkAncestorReferrals( ReferralException e ) throws Exception
+    {
+        assertEquals( "ldap://fermi:10389", e.getReferralInfo() );
+        assertTrue( e.skipReferral() );
+        assertEquals( "ldap://hertz:10389/cn=alex karasulu,ou=apache,ou=users,dc=example,dc=com", 
+            e.getReferralInfo() );
+        assertTrue( e.skipReferral() );
+        assertEquals( "ldap://maxwell:10389", e.getReferralInfo() );
+        assertFalse( e.skipReferral() );
+    }
+
+    
+    public void checkParentReferrals( ReferralException e ) throws Exception
+    {
+        assertEquals( "ldap://fermi:10389", e.getReferralInfo() );
+        assertTrue( e.skipReferral() );
+        assertEquals( "ldap://hertz:10389/cn=alex karasulu,ou=users,dc=example,dc=com", e.getReferralInfo() );
+        assertTrue( e.skipReferral() );
+        assertEquals( "ldap://maxwell:10389", e.getReferralInfo() );
+        assertFalse( e.skipReferral() );
+    }
+    
+    
     /**
      * Checks for correct core behavoir when Context.REFERRAL is set to <b>throw</b>
      * for an add operation with the parent context being a referral.
@@ -184,12 +210,7 @@
         }
         catch( ReferralException e )
         {
-            assertEquals( "ldap://fermi:10389", e.getReferralInfo() );
-            assertTrue( e.skipReferral() );
-            assertEquals( "ldap://hertz:10389/cn=alex karasulu,ou=users,dc=example,dc=com", e.getReferralInfo() );
-            assertTrue( e.skipReferral() );
-            assertEquals( "ldap://maxwell:10389", e.getReferralInfo() );
-            assertFalse( e.skipReferral() );
+            checkParentReferrals( e );
         }
     }
     
@@ -220,13 +241,7 @@
         }
         catch( ReferralException e )
         {
-            assertEquals( "ldap://fermi:10389", e.getReferralInfo() );
-            assertTrue( e.skipReferral() );
-            assertEquals( "ldap://hertz:10389/cn=alex karasulu,ou=apache,ou=users,dc=example,dc=com", 
-                e.getReferralInfo() );
-            assertTrue( e.skipReferral() );
-            assertEquals( "ldap://maxwell:10389", e.getReferralInfo() );
-            assertFalse( e.skipReferral() );
+            checkAncestorReferrals( e );
         }
     }
     
@@ -253,12 +268,7 @@
         }
         catch( ReferralException e )
         {
-            assertEquals( "ldap://fermi:10389", e.getReferralInfo() );
-            assertTrue( e.skipReferral() );
-            assertEquals( "ldap://hertz:10389/cn=alex karasulu,ou=users,dc=example,dc=com", e.getReferralInfo() );
-            assertTrue( e.skipReferral() );
-            assertEquals( "ldap://maxwell:10389", e.getReferralInfo() );
-            assertFalse( e.skipReferral() );
+            checkParentReferrals( e );
         }
     }
     
@@ -285,13 +295,7 @@
         }
         catch( ReferralException e )
         {
-            assertEquals( "ldap://fermi:10389", e.getReferralInfo() );
-            assertTrue( e.skipReferral() );
-            assertEquals( "ldap://hertz:10389/cn=alex karasulu,ou=apache,ou=users,dc=example,dc=com", 
-                e.getReferralInfo() );
-            assertTrue( e.skipReferral() );
-            assertEquals( "ldap://maxwell:10389", e.getReferralInfo() );
-            assertFalse( e.skipReferral() );
+            checkAncestorReferrals( e );
         }
     }
     
@@ -327,12 +331,7 @@
         }
         catch( ReferralException e )
         {
-            assertEquals( "ldap://fermi:10389", e.getReferralInfo() );
-            assertTrue( e.skipReferral() );
-            assertEquals( "ldap://hertz:10389/cn=alex karasulu,ou=users,dc=example,dc=com", e.getReferralInfo() );
-            assertTrue( e.skipReferral() );
-            assertEquals( "ldap://maxwell:10389", e.getReferralInfo() );
-            assertFalse( e.skipReferral() );
+            checkParentReferrals( e );
         }
     }
     
@@ -368,13 +367,123 @@
         }
         catch( ReferralException e )
         {
-            assertEquals( "ldap://fermi:10389", e.getReferralInfo() );
-            assertTrue( e.skipReferral() );
-            assertEquals( "ldap://hertz:10389/cn=alex karasulu,ou=apache,ou=users,dc=example,dc=com", 
-                e.getReferralInfo() );
-            assertTrue( e.skipReferral() );
-            assertEquals( "ldap://maxwell:10389", e.getReferralInfo() );
-            assertFalse( e.skipReferral() );
+            checkAncestorReferrals( e );
+        }
+    }
+    
+    
+    /**
+     * Checks for correct core behavoir when Context.REFERRAL is set to <b>throw</b>
+     * for a modify operation with the parent context being a referral.
+     * 
+     * @throws Exception if something goes wrong.
+     */
+    public void testModifyWithReferralParent() throws Exception
+    {
+        // -------------------------------------------------------------------
+        // Attempt to modify the attributes of an entry below the referral 
+        // parent.  We should encounter referral errors with referral setting 
+        // set to throw.
+        // -------------------------------------------------------------------
+
+        td.refCtx.addToEnvironment( Context.REFERRAL, "throw" );
+        try 
+        {
+            td.refCtx.modifyAttributes( "cn=alex karasulu", DirContext.ADD_ATTRIBUTE, 
+                new BasicAttributes( "description", "just some text", true ) );
+            fail( "Should fail here throwing a ReferralException" );
+        }
+        catch( ReferralException e )
+        {
+            checkParentReferrals( e );
+        }
+    }
+    
+    
+    /**
+     * Checks for correct core behavoir when Context.REFERRAL is set to <b>throw</b>
+     * for a modify operation with an ancestor context being a referral.
+     * 
+     * @throws Exception if something goes wrong.
+     */
+    public void testModifyWithReferralAncestor() throws Exception
+    {
+        // -------------------------------------------------------------------
+        // Attempt to modify the attributes of an entry below the referral 
+        // ancestor. We should encounter referral errors when referral setting 
+        // is set to throw.
+        // -------------------------------------------------------------------
+
+        td.refCtx.addToEnvironment( Context.REFERRAL, "throw" );
+        try 
+        {
+            td.refCtx.modifyAttributes( "cn=alex karasulu,ou=apache", DirContext.ADD_ATTRIBUTE, 
+                new BasicAttributes( "description", "just some text", true ) );
+            fail( "Should fail here throwing a ReferralException" );
+        }
+        catch( ReferralException e )
+        {
+            checkAncestorReferrals( e );
+        }
+    }
+
+
+    
+    
+    /**
+     * Checks for correct core behavoir when Context.REFERRAL is set to <b>throw</b>
+     * for a modify operation with the parent context being a referral.
+     * 
+     * @throws Exception if something goes wrong.
+     */
+    public void testModifyWithReferralParent2() throws Exception
+    {
+        // -------------------------------------------------------------------
+        // Attempt to modify the attributes of an entry below the referral 
+        // parent.  We should encounter referral errors with referral setting 
+        // set to throw.
+        // -------------------------------------------------------------------
+
+        td.refCtx.addToEnvironment( Context.REFERRAL, "throw" );
+        try 
+        {
+            ModificationItem[] mods = new ModificationItem[] { new ModificationItem( 
+                DirContext.ADD_ATTRIBUTE, new BasicAttribute( "description", "just some text" ) ) };
+            td.refCtx.modifyAttributes( "cn=alex karasulu", mods );
+            fail( "Should fail here throwing a ReferralException" );
+        }
+        catch( ReferralException e )
+        {
+            checkParentReferrals( e );
+        }
+    }
+    
+    
+    /**
+     * Checks for correct core behavoir when Context.REFERRAL is set to <b>throw</b>
+     * for a modify operation with an ancestor context being a referral.
+     * 
+     * @throws Exception if something goes wrong.
+     */
+    public void testModifyWithReferralAncestor2() throws Exception
+    {
+        // -------------------------------------------------------------------
+        // Attempt to modify the attributes of an entry below the referral 
+        // ancestor. We should encounter referral errors when referral setting 
+        // is set to throw.
+        // -------------------------------------------------------------------
+
+        td.refCtx.addToEnvironment( Context.REFERRAL, "throw" );
+        try 
+        {
+            ModificationItem[] mods = new ModificationItem[] { new ModificationItem( 
+                DirContext.ADD_ATTRIBUTE, new BasicAttribute( "description", "just some text" ) ) };
+            td.refCtx.modifyAttributes( "cn=alex karasulu,ou=apache", mods );
+            fail( "Should fail here throwing a ReferralException" );
+        }
+        catch( ReferralException e )
+        {
+            checkAncestorReferrals( e );
         }
     }
 }

Modified: directory/trunks/apacheds/core/src/main/java/org/apache/ldap/server/referral/ReferralService.java
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/core/src/main/java/org/apache/ldap/server/referral/ReferralService.java?rev=368990&r1=368989&r2=368990&view=diff
==============================================================================
--- directory/trunks/apacheds/core/src/main/java/org/apache/ldap/server/referral/ReferralService.java (original)
+++ directory/trunks/apacheds/core/src/main/java/org/apache/ldap/server/referral/ReferralService.java Fri Jan 13 21:56:52 2006
@@ -244,16 +244,14 @@
         ServerLdapContext caller = ( ServerLdapContext ) invocation.getCaller();
         String refval = ( String ) caller.getEnvironment().get( Context.REFERRAL );
 
-        // handle updating the lut
-        if ( isReferral( entry ) ) 
-        {
-            lut.referralAdded( normName );
-        }
-        
         // handle a normal add without following referrals
         if ( refval == null || refval.equals( IGNORE ) )
         {
             next.add( upName, normName, entry );
+            if ( isReferral( entry ) ) 
+            {
+                lut.referralAdded( normName );
+            }
             return;
         }
 
@@ -263,6 +261,10 @@
             if ( farthest == null ) 
             {
                 next.add( upName, normName, entry );
+                if ( isReferral( entry ) ) 
+                {
+                    lut.referralAdded( normName );
+                }
                 return;
             }
             
@@ -327,16 +329,14 @@
         ServerLdapContext caller = ( ServerLdapContext ) invocation.getCaller();
         String refval = ( String ) caller.getEnvironment().get( Context.REFERRAL );
 
-        // handle updating the lut
-        if ( lut.isReferral( normName ) ) 
-        {
-            lut.referralDeleted( normName );
-        }
-        
         // handle a normal delete without following referrals
         if ( refval == null || refval.equals( IGNORE ) )
         {
             next.delete( normName );
+            if ( lut.isReferral( normName ) ) 
+            {
+                lut.referralDeleted( normName );
+            }
             return;
         }
         
@@ -346,6 +346,10 @@
             if ( farthest == null ) 
             {
                 next.delete( normName );
+                if ( lut.isReferral( normName ) ) 
+                {
+                    lut.referralDeleted( normName );
+                }
                 return;
             }
             
@@ -409,10 +413,8 @@
     }
     
 
-    public void modify( NextInterceptor next, Name name, int modOp, Attributes mods ) throws NamingException
+    private void checkModify( Name name, int modOp, Attributes mods ) throws NamingException
     {
-        next.modify( name, modOp, mods );
-        
         // -------------------------------------------------------------------
         // Check and update lut if we change the objectClass 
         // -------------------------------------------------------------------
@@ -469,10 +471,49 @@
     }
     
     
-    public void modify( NextInterceptor next, Name name, ModificationItem[] mods ) throws NamingException
+    public void modify( NextInterceptor next, Name name, int modOp, Attributes mods ) throws NamingException
+    {
+        Invocation invocation = InvocationStack.getInstance().peek();
+        ServerLdapContext caller = ( ServerLdapContext ) invocation.getCaller();
+        String refval = ( String ) caller.getEnvironment().get( Context.REFERRAL );
+
+        // handle a normal modify without following referrals
+        if ( refval == null || refval.equals( IGNORE ) )
+        {
+            next.modify( name, modOp, mods );
+            checkModify( name, modOp, mods );
+            return;
+        }
+
+        if ( refval.equals( THROW ) )
+        {
+            Name farthest = lut.getFarthestReferralAncestor( name );
+            if ( farthest == null ) 
+            {
+                next.modify( name, modOp, mods );
+                checkModify( name, modOp, mods );
+                return;
+            }
+            
+            Attributes referral = invocation.getProxy().lookup( farthest, DirectoryPartitionNexusProxy.LOOKUP_BYPASS );
+            Attribute refs = referral.get( REF_ATTR );
+            doReferralException( farthest, name, refs );
+        }
+        else if ( refval.equals( FOLLOW ) )
+        {
+            throw new NotImplementedException( FOLLOW + " referral handling mode not implemented" );
+        }
+        else
+        {
+            throw new LdapNamingException( "Undefined value for " + Context.REFERRAL  + " key: " 
+                + refval, ResultCodeEnum.OTHER );
+        }
+    }
+    
+    
+    private void checkModify( Name name, ModificationItem[] mods ) throws NamingException
     {
         boolean isTargetReferral = lut.isReferral( name );
-        next.modify( name, mods );
 
         // -------------------------------------------------------------------
         // Check and update lut if we change the objectClass 
@@ -530,6 +571,46 @@
 
                 break;
             }
+        }
+    }
+    
+    
+    public void modify( NextInterceptor next, Name name, ModificationItem[] mods ) throws NamingException
+    {
+        Invocation invocation = InvocationStack.getInstance().peek();
+        ServerLdapContext caller = ( ServerLdapContext ) invocation.getCaller();
+        String refval = ( String ) caller.getEnvironment().get( Context.REFERRAL );
+
+        // handle a normal modify without following referrals
+        if ( refval == null || refval.equals( IGNORE ) )
+        {
+            next.modify( name, mods );
+            checkModify( name, mods );
+            return;
+        }
+
+        if ( refval.equals( THROW ) )
+        {
+            Name farthest = lut.getFarthestReferralAncestor( name );
+            if ( farthest == null ) 
+            {
+                next.modify( name, mods );
+                checkModify( name, mods );
+                return;
+            }
+            
+            Attributes referral = invocation.getProxy().lookup( farthest, DirectoryPartitionNexusProxy.LOOKUP_BYPASS );
+            Attribute refs = referral.get( REF_ATTR );
+            doReferralException( farthest, name, refs );
+        }
+        else if ( refval.equals( FOLLOW ) )
+        {
+            throw new NotImplementedException( FOLLOW + " referral handling mode not implemented" );
+        }
+        else
+        {
+            throw new LdapNamingException( "Undefined value for " + Context.REFERRAL  + " key: " 
+                + refval, ResultCodeEnum.OTHER );
         }
     }