You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by er...@apache.org on 2006/05/03 23:43:37 UTC

svn commit: r399448 - in /directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/trigger: TriggerService.java TriggerSpecCache.java

Author: ersiner
Date: Wed May  3 14:43:34 2006
New Revision: 399448

URL: http://svn.apache.org/viewcvs?rev=399448&view=rev
Log:
Implemented a Trigger Specification Cache. Needs testing tough.

Added:
    directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/trigger/TriggerSpecCache.java
Modified:
    directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/trigger/TriggerService.java

Modified: directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/trigger/TriggerService.java
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/trigger/TriggerService.java?rev=399448&r1=399447&r2=399448&view=diff
==============================================================================
--- directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/trigger/TriggerService.java (original)
+++ directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/trigger/TriggerService.java Wed May  3 14:43:34 2006
@@ -60,6 +60,8 @@
     private static final String TRIGGER_SUBENTRIES_ATTR = "triggerSubentries";
 
 
+    /** a triggerSpecCache that responds to add, delete, and modify attempts */
+    private TriggerSpecCache triggerSpecCache;
     /** a normalizing Trigger Specification parser */
     private TriggerSpecificationParser triggerParser;
     /** a normalizing Distinguished Name parser */
@@ -82,6 +84,7 @@
     public void init( DirectoryServiceConfiguration dirServCfg, InterceptorConfiguration intCfg ) throws NamingException
     {
         super.init( dirServCfg, intCfg );
+        triggerSpecCache = new TriggerSpecCache( dirServCfg );
         attrRegistry = dirServCfg.getGlobalRegistries().getAttributeTypeRegistry();
         triggerParser = new TriggerSpecificationParser( new ConcreteNameComponentNormalizer( attrRegistry ) );
         dnParser = new DnParser( new ConcreteNameComponentNormalizer( attrRegistry ) );

Added: directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/trigger/TriggerSpecCache.java
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/trigger/TriggerSpecCache.java?rev=399448&view=auto
==============================================================================
--- directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/trigger/TriggerSpecCache.java (added)
+++ directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/trigger/TriggerSpecCache.java Wed May  3 14:43:34 2006
@@ -0,0 +1,238 @@
+/*
+ *   Copyright 2006 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.directory.server.core.trigger;
+
+
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.naming.Name;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.ModificationItem;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.SearchResult;
+
+import org.apache.directory.server.core.DirectoryServiceConfiguration;
+import org.apache.directory.server.core.partition.DirectoryPartitionNexus;
+import org.apache.directory.server.core.schema.AttributeTypeRegistry;
+import org.apache.directory.server.core.schema.ConcreteNameComponentNormalizer;
+import org.apache.directory.shared.ldap.filter.ExprNode;
+import org.apache.directory.shared.ldap.filter.SimpleNode;
+import org.apache.directory.shared.ldap.name.DnParser;
+import org.apache.directory.shared.ldap.name.LdapName;
+import org.apache.directory.shared.ldap.name.NameComponentNormalizer;
+import org.apache.directory.shared.ldap.trigger.TriggerSpecification;
+import org.apache.directory.shared.ldap.trigger.TriggerSpecificationParser;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * A cache for Trigger Specifications which responds to specific events to
+ * perform cache house keeping as trigger subentries are added, deleted
+ * and modified.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev:$
+ */
+public class TriggerSpecCache
+{
+    /** the attribute id for prescriptive trigger: prescriptiveTrigger */
+    private static final String PRESCRIPTIVE_TRIGGER_ATTR = "prescriptiveTrigger";
+    /** the attribute id for an object class: objectClass */
+    private static final String OC_ATTR = "objectClass";
+    /** the object class for trigger subentries: triggerSubentry */
+    private static final String TRIGGER_SUBENTRY_OC = "triggerSubentry";
+
+    /** the logger for this class */
+    private static final Logger log = LoggerFactory.getLogger( TriggerSpecCache.class );
+
+    /** cloned startup environment properties we use for subentry searching */
+    private final Hashtable env;
+    /** a map of strings to TriggerSpecification collections */
+    private final Map triggerSpecs = new HashMap();
+    /** a handle on the partition nexus */
+    private final DirectoryPartitionNexus nexus;
+    /** a normalizing TriggerSpecification parser */
+    private final TriggerSpecificationParser triggerSpecParser;
+    /** a normalizing DN parser */
+    private final DnParser dnParser;
+
+
+    /**
+     * Creates a TriggerSpecification cache.
+     *
+     * @param dirServCfg the context factory configuration for the server
+     */
+    public TriggerSpecCache( DirectoryServiceConfiguration dirServCfg ) throws NamingException
+    {
+        this.nexus = dirServCfg.getPartitionNexus();
+        AttributeTypeRegistry registry = dirServCfg.getGlobalRegistries().getAttributeTypeRegistry();
+        NameComponentNormalizer ncn = new ConcreteNameComponentNormalizer( registry );
+        triggerSpecParser = new TriggerSpecificationParser( ncn );
+        dnParser = new DnParser( ncn );
+        env = ( Hashtable ) dirServCfg.getEnvironment().clone();
+        initialize();
+    }
+
+
+    private void initialize() throws NamingException
+    {
+        // search all naming contexts for trigger subentenries
+        // generate TriggerSpecification arrays for each subentry
+        // add that subentry to the hash
+        Iterator suffixes = nexus.listSuffixes( true );
+        while ( suffixes.hasNext() )
+        {
+            String suffix = ( String ) suffixes.next();
+            Name baseDn = new LdapName( suffix );
+            ExprNode filter = new SimpleNode( OC_ATTR, TRIGGER_SUBENTRY_OC, SimpleNode.EQUALITY );
+            SearchControls ctls = new SearchControls();
+            ctls.setSearchScope( SearchControls.SUBTREE_SCOPE );
+            NamingEnumeration results = nexus.search( baseDn, env, filter, ctls );
+            while ( results.hasMore() )
+            {
+                SearchResult result = ( SearchResult ) results.next();
+                String subentryDn = result.getName();
+                Attribute triggerSpec = result.getAttributes().get( PRESCRIPTIVE_TRIGGER_ATTR );
+                if ( triggerSpec == null )
+                {
+                    log.warn( "Found triggerSubentry '" + subentryDn + "' without any " + PRESCRIPTIVE_TRIGGER_ATTR );
+                    continue;
+                }
+
+                Name normSubentryName = dnParser.parse( subentryDn );
+                subentryAdded( normSubentryName, result.getAttributes() );
+            }
+            results.close();
+        }
+    }
+
+
+    private boolean hasPrescriptiveTrigger( Attributes entry ) throws NamingException
+    {
+        // only do something if the entry contains prescriptiveTrigger
+        Attribute aci = entry.get( PRESCRIPTIVE_TRIGGER_ATTR );        
+        if ( aci == null )
+        {
+            return false;
+        }
+        return true;
+    }
+
+
+    public void subentryAdded( Name normName, Attributes entry ) throws NamingException
+    {
+        // only do something if the entry contains prescriptiveTrigger
+        Attribute triggerSpec = entry.get( PRESCRIPTIVE_TRIGGER_ATTR );
+        if ( !hasPrescriptiveTrigger( entry ) )
+        {
+            return;
+        }
+
+        List subentryTriggerSpecs = new ArrayList();
+        for ( int ii = 0; ii < triggerSpec.size(); ii++ )
+        {
+            TriggerSpecification item = null;
+
+            try
+            {
+                item = triggerSpecParser.parse( ( String ) triggerSpec.get( ii ) );
+            }
+            catch ( ParseException e )
+            {
+                String msg = "TriggerSpecification parser failure on '" + item + "'. Cannnot add Trigger Specificaitons to TriggerSpecCache.";
+                log.warn( msg, e );
+            }
+
+            subentryTriggerSpecs.add( item );
+        }
+        triggerSpecs.put( normName.toString(), subentryTriggerSpecs );
+    }
+
+
+    public void subentryDeleted( Name normName, Attributes entry ) throws NamingException
+    {
+        if ( !hasPrescriptiveTrigger( entry ) )
+        {
+            return;
+        }
+
+        triggerSpecs.remove( normName.toString() );
+    }
+
+
+    public void subentryModified( Name normName, ModificationItem[] mods, Attributes entry ) throws NamingException
+    {
+        if ( !hasPrescriptiveTrigger( entry ) )
+        {
+            return;
+        }
+
+        boolean isTriggerSpecModified = false;
+        for ( int ii = 0; ii < mods.length; ii++ )
+        {
+            isTriggerSpecModified |= mods[ii].getAttribute().contains( PRESCRIPTIVE_TRIGGER_ATTR );
+        }
+        if ( isTriggerSpecModified )
+        {
+            subentryDeleted( normName, entry );
+            subentryAdded( normName, entry );
+        }
+    }
+
+
+    public void subentryModified( Name normName, int modOp, Attributes mods, Attributes entry ) throws NamingException
+    {
+        if ( !hasPrescriptiveTrigger( entry ) )
+        {
+            return;
+        }
+
+        if ( mods.get( PRESCRIPTIVE_TRIGGER_ATTR ) != null )
+        {
+            subentryDeleted( normName, entry );
+            subentryAdded( normName, entry );
+        }
+    }
+
+
+    public List getSubentryTriggerSpecs( String subentryDn )
+    {
+        List subentryTriggerSpecs = ( List ) triggerSpecs.get( subentryDn );
+        if ( subentryTriggerSpecs == null )
+        {
+            return Collections.EMPTY_LIST;
+        }
+        return Collections.unmodifiableList( subentryTriggerSpecs );
+    }
+
+
+    public void subentryRenamed( Name oldName, Name newName )
+    {
+        triggerSpecs.put( newName.toString(), triggerSpecs.remove( oldName.toString() ) );
+    }
+}