You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2009/08/23 21:32:07 UTC

svn commit: r807007 - /directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/SchemaObjectRegistry.java

Author: elecharny
Date: Sun Aug 23 19:32:07 2009
New Revision: 807007

URL: http://svn.apache.org/viewvc?rev=807007&view=rev
Log:
Added the new base for all the registries

Modified:
    directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/SchemaObjectRegistry.java

Modified: directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/SchemaObjectRegistry.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/SchemaObjectRegistry.java?rev=807007&r1=807006&r2=807007&view=diff
==============================================================================
--- directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/SchemaObjectRegistry.java (original)
+++ directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/SchemaObjectRegistry.java Sun Aug 23 19:32:07 2009
@@ -21,10 +21,16 @@
 
 
 import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import javax.naming.NamingException;
 
+import org.apache.directory.shared.asn1.primitives.OID;
 import org.apache.directory.shared.ldap.schema.SchemaObject;
+import org.apache.directory.shared.ldap.schema.SchemaObjectType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 /**
@@ -33,16 +39,48 @@
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public interface SchemaObjectRegistry<T extends SchemaObject> extends Iterable<T>
+public class SchemaObjectRegistry<T extends SchemaObject, U extends SchemaObject> implements Iterable<T>
 {
+    /** static class logger */
+    private static final Logger LOG = LoggerFactory.getLogger( SchemaObjectRegistry.class );
+
+    /** A speedup for debug */
+    private static final boolean DEBUG = LOG.isDebugEnabled();
+    
+    /** a map of Normalizers looked up by OID or Name */
+    protected final Map<String, T> byOid;
+    
+    /** maps an OID to a SchemaObject Description */
+    private final Map<String, U> oidToDescription;
+
+    
+    /** The SchemaObject type */
+    protected SchemaObjectType type;
+    
+    
+    /**
+     * Creates a new SchemaObjectRegistry instance.
+     */
+    protected SchemaObjectRegistry( SchemaObjectType schemaObjectType )
+    {
+        byOid = new ConcurrentHashMap<String, T>();
+        oidToDescription = new ConcurrentHashMap<String, U>();
+        type = schemaObjectType;
+    }
+    
+    
     /**
-     * Checks to see if an SchemaObject exists in the registry.
+     * Checks to see if an SchemaObject exists in the registry, by its
+     * OID or name. 
      * 
-     * @param id the object identifier or name of the SchemaObject
+     * @param oid the object identifier or name of the SchemaObject
      * @return true if a SchemaObject definition exists for the oid, false
      * otherwise
      */
-    boolean contains( String id );
+    public boolean contains( String oid )
+    {
+        return byOid.containsKey( oid );
+    }
     
     
     /**
@@ -52,15 +90,68 @@
      * @return the schema name
      * @throws NamingException if the schema object does not exist
      */
-    String getSchemaName( String id ) throws NamingException;
+    public String getSchemaName( String oid ) throws NamingException
+    {
+        if ( ! OID.isOID( oid ) )
+        {
+            String msg = "Looks like the arg is not a numeric OID";
+            LOG.warn( msg );
+            throw new NamingException( msg );
+        }
+        
+        SchemaObject schemaObject = byOid.get( oid );
+
+        if ( schemaObject != null )
+        {
+            return schemaObject.getSchemaName();
+        }
+        
+        String msg = "OID " + oid + " not found in oid to schema name map!";
+        LOG.warn( msg );
+        throw new NamingException( msg );
+    }
 
     
     /**
+     * Modify all the SchemaObject using a schemaName when this name changes.
+     *
+     * @param originalSchemaName The original Schema name
+     * @param newSchemaName The new Schema name
+     */
+    public void renameSchema( String originalSchemaName, String newSchemaName )
+    {
+        // Loop on all the SchemaObjects stored and remove those associated
+        // with the give schemaName
+        for ( T schemaObject : this )
+        {
+            if ( originalSchemaName.equalsIgnoreCase( schemaObject.getSchemaName() ) )
+            {
+                schemaObject.setSchemaName( newSchemaName );
+                SchemaObject description = oidToDescription.get( schemaObject.getOid() );
+                
+                if ( description != null )
+                {
+                    description.setSchemaName( newSchemaName );
+                }
+
+                if ( DEBUG )
+                {
+                    LOG.debug( "Renamed {} schemaName to {}", schemaObject, newSchemaName );
+                }
+            }
+        }
+    }
+    
+    
+    /**
      * Gets an iterator over the registered schema objects in the registry.
      *
      * @return an Iterator of homogeneous schema objects
      */
-    Iterator<T> iterator();
+    public Iterator<T> iterator()
+    {
+        return byOid.values().iterator();
+    }
 
     
     /**
@@ -68,34 +159,170 @@
      *
      * @return an Iterator of OIDs
      */
-    Iterator<String> oidsIterator();
+    public Iterator<String> oidsIterator()
+    {
+        return byOid.keySet().iterator();
+    }
+
+    
+    /**
+     * Gets an iterator over the registered descriptions in the registry.
+     *
+     * @return an Iterator of descriptions
+     */
+    public Iterator<U> descriptionsIterator()
+    {
+        return oidToDescription.values().iterator();
+    }
 
     
     /**
      * Looks up a SchemaObject by its unique Object Identifier or by name.
      *
-     * @param id the object identifier or name
+     * @param oid the object identifier or name
      * @return the SchemaObject instance for the id
      * @throws NamingException if the SchemaObject does not exist
      */
-    T lookup( String id ) throws NamingException;
+    public T lookup( String oid ) throws NamingException
+    {
+        T schemaObject = byOid.get( oid );
+
+        if ( schemaObject == null )
+        {
+            String msg = type.name() + " for OID " + oid + " does not exist!";
+            LOG.debug( msg );
+            throw new NamingException( msg );
+        }
+
+        if ( DEBUG )
+        {
+            LOG.debug( "Found {} with oid: {}", schemaObject, oid );
+        }
+        
+        return schemaObject;
+    }
+    
+    
+    /**
+     * Registers a new SchemaObject with this registry.
+     *
+     * @param schemaObject the SchemaObject to register
+     * @throws NamingException if the SchemaObject is already registered or
+     * the registration operation is not supported
+     */
+    public void register( T schemaObject ) throws NamingException
+    {
+        String oid = schemaObject.getOid();
+        
+        if ( byOid.containsKey( oid ) )
+        {
+            String msg = type.name() + " with OID " + oid + " already registered!";
+            LOG.warn( msg );
+            throw new NamingException( msg );
+        }
+
+        byOid.put( oid, schemaObject );
+        
+        if ( LOG.isDebugEnabled() )
+        {
+            LOG.debug( "registered {} for OID {}", schemaObject, oid );
+        }
+    }
 
 
     /**
      * Registers a new SchemaObject with this registry.
      *
+     * @param schemaObjectDescription The SchemaObject description to register
      * @param schemaObject the SchemaObject to register
      * @throws NamingException if the SchemaObject is already registered or
      * the registration operation is not supported
      */
-    void register( T schemaObject ) throws NamingException;
+    public void register( U schemaObjectDescription, T schemaObject ) throws NamingException
+    {
+        String oid = schemaObject.getOid();
+        
+        if ( byOid.containsKey( oid ) )
+        {
+            String msg = type.name() + " already registered for OID " + oid;
+            LOG.warn( msg );
+            throw new NamingException( msg );
+        }
+
+        byOid.put( oid, schemaObject );
+        
+        // Register the name -> schemaObject couples.
+        // We will also add the oid -> schemaObject into this map
+        if ( schemaObject.getNames() != null )
+        {
+            for ( String name : schemaObject.getNames() )
+            {
+                byOid.put( name, schemaObject );
+            }
+        }
+        
+        // And register the description too
+        oidToDescription.put( oid, schemaObjectDescription );
+        
+        if ( DEBUG )
+        {
+            LOG.debug( "registered {} with oid: {}", type.name(), oid );
+        }
+    }
 
 
     /**
-     * Removes the SchemaObject registered with this registry.
+     * Removes the SchemaObject registered with this registry, using its
+     * numeric OID.
      * 
      * @param numericOid the numeric identifier
      * @throws NamingException if the numeric identifier is invalid
      */
-    void unregister( String numericOid ) throws NamingException;
+    public void unregister( String numericOid ) throws NamingException
+    {
+        if ( !OID.isOID( numericOid ) )
+        {
+            String msg = "OID " + numericOid + " is not a numeric OID";
+            LOG.error( msg );
+            throw new NamingException( msg );
+        }
+
+        SchemaObject schemaObject = byOid.remove( numericOid );
+
+        if ( DEBUG )
+        {
+            LOG.debug( "Removed {} with oid {} from the registry", schemaObject, numericOid );
+        }
+    }
+    
+    
+    /**
+     * Unregisters all syntaxCheckers defined for a specific schema from
+     * this registry.
+     * 
+     * @param schemaName the name of the schema whose syntaxCheckers will be removed
+     */
+    public void unregisterSchemaElements( String schemaName )
+    {
+        if ( schemaName == null )
+        {
+            return;
+        }
+        
+        // Loop on all the SchemaObjects stored and remove those associated
+        // with the give schemaName
+        for ( T schemaObject : this )
+        {
+            if ( schemaName.equalsIgnoreCase( schemaObject.getSchemaName() ) )
+            {
+                String oid = schemaObject.getOid();
+                SchemaObject removed = byOid.remove( oid );
+
+                if ( DEBUG )
+                {
+                    LOG.debug( "Removed {} with oid {} from the registry", removed, oid );
+                }
+            }
+        }
+    }
 }