directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r280453 - in /directory/apacheds/trunk/core/src: main/java/org/apache/ldap/server/event/ main/java/org/apache/ldap/server/jndi/ test/org/apache/ldap/server/event/
Date Tue, 13 Sep 2005 00:14:24 GMT
Author: akarasulu
Date: Mon Sep 12 17:14:16 2005
New Revision: 280453

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

 o fixed bug reported by Enrique Rodriguez in DIREVE-247 here
   http://issues.apache.org/jira/browse/DIREVE-247
 o add test case to make sure listeners could be deregistered
 o now using context to determine which listener, context record to remove 
   rather than removing all registrations of the listener


Added:
    directory/apacheds/trunk/core/src/test/org/apache/ldap/server/event/
    directory/apacheds/trunk/core/src/test/org/apache/ldap/server/event/EventServiceTest.java
  (with props)
Modified:
    directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/event/EventService.java
    directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/jndi/ContextPartitionNexusProxy.java
    directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/jndi/ServerContext.java
    directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/jndi/ServerDirContext.java

Modified: directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/event/EventService.java
URL: http://svn.apache.org/viewcvs/directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/event/EventService.java?rev=280453&r1=280452&r2=280453&view=diff
==============================================================================
--- directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/event/EventService.java
(original)
+++ directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/event/EventService.java
Mon Sep 12 17:14:16 2005
@@ -102,9 +102,38 @@
     }
 
 
-    public void removeNamingListener( NamingListener namingListener )
+    public void removeNamingListener( EventContext ctx, NamingListener namingListener )
     {
-        sources.remove( sources.get( namingListener ) );
+        Object obj = sources.get( namingListener );
+
+        if ( obj == null )
+        {
+            return;
+        }
+
+        if ( obj instanceof EventSourceRecord )
+        {
+            sources.remove( namingListener );
+        }
+        else if ( obj instanceof List )
+        {
+            List list = ( List ) obj;
+
+            for ( int ii = 0; ii < list.size(); ii++ )
+            {
+                EventSourceRecord rec = ( EventSourceRecord ) list.get( ii );
+                if ( rec.getEventContext() == ctx )
+                {
+                    list.remove( ii );
+                }
+            }
+
+            if ( list.isEmpty() )
+            {
+                sources.remove( namingListener );
+            }
+        }
+
     }
 
 

Modified: directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/jndi/ContextPartitionNexusProxy.java
URL: http://svn.apache.org/viewcvs/directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/jndi/ContextPartitionNexusProxy.java?rev=280453&r1=280452&r2=280453&view=diff
==============================================================================
--- directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/jndi/ContextPartitionNexusProxy.java
(original)
+++ directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/jndi/ContextPartitionNexusProxy.java
Mon Sep 12 17:14:16 2005
@@ -439,10 +439,10 @@
     }
 
 
-    public void removeNamingListener( NamingListener namingListener ) throws NamingException
+    public void removeNamingListener( EventContext ctx, NamingListener namingListener ) throws
NamingException
     {
         InterceptorChain chain = this.configuration.getInterceptorChain();
         EventService interceptor = ( EventService ) chain.get( "eventService" );
-        interceptor.removeNamingListener( namingListener );
+        interceptor.removeNamingListener( ctx, namingListener );
     }
 }

Modified: directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/jndi/ServerContext.java
URL: http://svn.apache.org/viewcvs/directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/jndi/ServerContext.java?rev=280453&r1=280452&r2=280453&view=diff
==============================================================================
--- directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/jndi/ServerContext.java
(original)
+++ directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/jndi/ServerContext.java
Mon Sep 12 17:14:16 2005
@@ -19,6 +19,9 @@
 
 import java.io.Serializable;
 import java.util.Hashtable;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Iterator;
 
 import javax.naming.ConfigurationException;
 import javax.naming.Context;
@@ -71,6 +74,9 @@
     /** The distinguished name of this Context */
     private final LdapName dn;
 
+    /** The set of registered NamingListeners */
+    private final Set listeners = new HashSet();
+
     /** The Principal associated with this context */
     private LdapPrincipal principal;
 
@@ -222,7 +228,12 @@
      */
     public void close() throws NamingException
     {
-        // Does nothing yet?
+        Iterator list = listeners.iterator();
+        while ( list.hasNext() )
+        {
+            ( ( ContextPartitionNexusProxy ) this.nexusProxy )
+                    .removeNamingListener( this, ( NamingListener ) list.next() );
+        }
     }
 
 
@@ -772,6 +783,7 @@
         controls.setSearchScope( scope );
         ( ( ContextPartitionNexusProxy ) this.nexusProxy )
                 .addNamingListener( this, buildTarget( name ), filter, controls, namingListener
);
+        listeners.add( namingListener );
     }
 
 
@@ -783,13 +795,25 @@
 
     public void removeNamingListener( NamingListener namingListener ) throws NamingException
     {
-        ( ( ContextPartitionNexusProxy ) this.nexusProxy ).removeNamingListener( namingListener
);
+        ( ( ContextPartitionNexusProxy ) this.nexusProxy ).removeNamingListener( this, namingListener
);
+        listeners.remove( namingListener );
     }
 
 
     public boolean targetMustExist() throws NamingException
     {
         return false;
+    }
+
+
+    /**
+     * Allows subclasses to register and unregister listeners.
+     *
+     * @return the set of listeners used for tracking registered name listeners.
+     */
+    protected Set getListeners()
+    {
+        return listeners;
     }
 
 

Modified: directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/jndi/ServerDirContext.java
URL: http://svn.apache.org/viewcvs/directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/jndi/ServerDirContext.java?rev=280453&r1=280452&r2=280453&view=diff
==============================================================================
--- directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/jndi/ServerDirContext.java
(original)
+++ directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/jndi/ServerDirContext.java
Mon Sep 12 17:14:16 2005
@@ -670,7 +670,8 @@
 
     FilterParserImpl filterParser = new FilterParserImpl();
 
-    public void addNamingListener( Name name, String filterStr, SearchControls searchControls,
NamingListener namingListener )
+    public void addNamingListener( Name name, String filterStr, SearchControls searchControls,
+                                   NamingListener namingListener )
             throws NamingException
     {
         ExprNode filter = null;
@@ -688,6 +689,7 @@
 
         ( ( ContextPartitionNexusProxy ) getNexusProxy() )
                 .addNamingListener( this, buildTarget( name ), filter, searchControls, namingListener
);
+        getListeners().add( namingListener );
     }
 
 

Added: directory/apacheds/trunk/core/src/test/org/apache/ldap/server/event/EventServiceTest.java
URL: http://svn.apache.org/viewcvs/directory/apacheds/trunk/core/src/test/org/apache/ldap/server/event/EventServiceTest.java?rev=280453&view=auto
==============================================================================
--- directory/apacheds/trunk/core/src/test/org/apache/ldap/server/event/EventServiceTest.java
(added)
+++ directory/apacheds/trunk/core/src/test/org/apache/ldap/server/event/EventServiceTest.java
Mon Sep 12 17:14:16 2005
@@ -0,0 +1,164 @@
+/*
+ *   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.ldap.server.event;
+
+
+import org.apache.ldap.server.AbstractAdminTestCase;
+
+import javax.naming.NamingException;
+import javax.naming.directory.*;
+import javax.naming.event.*;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.EventObject;
+
+
+/**
+ * Test cases for the event service.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class EventServiceTest extends AbstractAdminTestCase
+{
+    /**
+     * Test to make sure NamingListener's are no longer registered
+     * after they are removed via the EventContex.removeNamingListener method.
+     */
+    public void testRemoveNamingListener() throws NamingException
+    {
+        TestListener listener = new TestListener();
+        EventDirContext ctx = ( EventDirContext ) super.sysRoot.lookup( "" );
+        ctx.addNamingListener( "", SearchControls.SUBTREE_SCOPE, listener );
+        Attributes testEntry = new BasicAttributes( "ou", "testentry", true );
+        Attribute objectClass = new BasicAttribute( "objectClass", "top" );
+        objectClass.add( "organizationalUnit" );
+        testEntry.put( objectClass );
+        ctx.createSubcontext( "ou=testentry", testEntry );
+
+        assertEquals( 1, listener.getEventRecords().size() );
+        EventRecord rec = ( EventRecord ) listener.getEventRecords().get( 0 );
+        assertEquals( "objectAdded", rec.method );
+        assertEquals( ctx, rec.event.getSource() );
+
+        ctx.removeNamingListener( listener );
+        ctx.destroySubcontext( "ou=testentry" );
+
+        assertEquals( 1, listener.getEventRecords().size() );
+        rec = ( EventRecord ) listener.getEventRecords().get( 0 );
+        assertEquals( "objectAdded", rec.method );
+        assertEquals( ctx, rec.event.getSource() );
+
+        // readd the entry once again just to make sure
+        ctx.createSubcontext( "ou=testentry", testEntry );
+        assertEquals( 1, listener.getEventRecords().size() );
+        rec = ( EventRecord ) listener.getEventRecords().get( 0 );
+        assertEquals( "objectAdded", rec.method );
+        assertEquals( ctx, rec.event.getSource() );
+    }
+
+
+    /**
+     * Test to make sure NamingListener's are no longer registered
+     * after the context used for registration is closed.
+     */
+    public void testContextClose() throws NamingException
+    {
+        TestListener listener = new TestListener();
+        EventDirContext ctx = ( EventDirContext ) super.sysRoot.lookup( "" );
+        ctx.addNamingListener( "", SearchControls.SUBTREE_SCOPE, listener );
+        Attributes testEntry = new BasicAttributes( "ou", "testentry", true );
+        Attribute objectClass = new BasicAttribute( "objectClass", "top" );
+        objectClass.add( "organizationalUnit" );
+        testEntry.put( objectClass );
+        ctx.createSubcontext( "ou=testentry", testEntry );
+
+        assertEquals( 1, listener.getEventRecords().size() );
+        EventRecord rec = ( EventRecord ) listener.getEventRecords().get( 0 );
+        assertEquals( "objectAdded", rec.method );
+        assertEquals( ctx, rec.event.getSource() );
+
+        ctx.close();
+        ctx = ( EventDirContext ) super.sysRoot.lookup( "" );
+        ctx.destroySubcontext( "ou=testentry" );
+
+        assertEquals( 1, listener.getEventRecords().size() );
+        rec = ( EventRecord ) listener.getEventRecords().get( 0 );
+        assertEquals( "objectAdded", rec.method );
+
+        // readd the entry once again just to make sure
+        ctx.createSubcontext( "ou=testentry", testEntry );
+        assertEquals( 1, listener.getEventRecords().size() );
+        rec = ( EventRecord ) listener.getEventRecords().get( 0 );
+        assertEquals( "objectAdded", rec.method );
+    }
+
+
+    public class TestListener implements ObjectChangeListener, NamespaceChangeListener
+    {
+        List events = new ArrayList();
+
+
+        public List getEventRecords()
+        {
+            return events;
+        }
+
+
+        public void objectChanged( NamingEvent event )
+        {
+            events.add( new EventRecord( "objectChanged", event ) );
+        }
+
+
+        public void namingExceptionThrown( NamingExceptionEvent event )
+        {
+            events.add( new EventRecord( "namingExceptionThrown", event ) );
+        }
+
+
+        public void objectAdded( NamingEvent event )
+        {
+            events.add( new EventRecord( "objectAdded", event ) );
+        }
+
+
+        public void objectRemoved( NamingEvent event )
+        {
+            events.add( new EventRecord( "objectRemoved", event ) );
+        }
+
+
+        public void objectRenamed( NamingEvent event )
+        {
+            events.add( new EventRecord( "objectRenamed", event ) );
+        }
+    }
+
+
+    public class EventRecord
+    {
+        String method;
+        EventObject event;
+
+        EventRecord( String method, EventObject event )
+        {
+            this.method = method;
+            this.event = event;
+        }
+    }
+}

Propchange: directory/apacheds/trunk/core/src/test/org/apache/ldap/server/event/EventServiceTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message