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/24 13:33:56 UTC

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

Author: elecharny
Date: Mon Aug 24 11:33:55 2009
New Revision: 807154

URL: http://svn.apache.org/viewvc?rev=807154&view=rev
Log:
A new version of the OidRegistry

Added:
    directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/OidRegistry.java
      - copied, changed from r806632, directory/apacheds/branches/apacheds-schema/schema-registries/src/main/java/org/apache/directory/server/schema/registries/OidRegistry.java

Copied: directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/OidRegistry.java (from r806632, directory/apacheds/branches/apacheds-schema/schema-registries/src/main/java/org/apache/directory/server/schema/registries/OidRegistry.java)
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/OidRegistry.java?p2=directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/OidRegistry.java&p1=directory/apacheds/branches/apacheds-schema/schema-registries/src/main/java/org/apache/directory/server/schema/registries/OidRegistry.java&r1=806632&r2=807154&rev=807154&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/schema-registries/src/main/java/org/apache/directory/server/schema/registries/OidRegistry.java (original)
+++ directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/schema/registries/OidRegistry.java Mon Aug 24 11:33:55 2009
@@ -17,42 +17,52 @@
  *  under the License. 
  *  
  */
-package org.apache.directory.server.schema.registries;
+package org.apache.directory.shared.ldap.schema.registries;
 
 
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 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.util.ArrayUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 
 /**
- * Object identifier registry.
+ * Object identifier registry. It stores the OIDs for AT, OC, MR, LS, MRU, DSR, DCR and NF.
+ * An OID is unique, and associated with a SO.
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$
  */
-public interface OidRegistry
+public class OidRegistry
 {
-    /**
-     * Gets the object identifier for a common name or returns the argument
-     * as-is if it is an object identifier.
-     * 
-     * @param name the name to lookup an OID for
-     * @return the OID string associated with a name
-     * @throws NamingException if name does not map to an OID
-     */
-    String getOid( String name ) throws NamingException;
+    /** static class logger */
+    private static final Logger LOG = LoggerFactory.getLogger( OidRegistry.class );
 
+    /** Speedup for DEBUG mode */
+    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
 
+    /** Maps OID to a SchemaObject */
+    private Map<String, SchemaObject> byOid = new ConcurrentHashMap<String, SchemaObject>();
+    
     /**
-     * Checks to see if an identifier, oid or name exists within this registry.
-     *
-     * @param id the oid or name to look for
-     * @return true if the id exists false otherwise
+     * Tells if the given OID is present on this registry
+     * 
+     * @param OID The OID to lookup
+     * @return true if the OID alreadyexists
      */
-    boolean hasOid( String id );
+    public boolean hasOid( String oid )
+    {
+        return byOid.containsKey( oid );
+    }
 
 
     /**
@@ -63,7 +73,45 @@
      * @return the primary name
      * @throws NamingException if oid does not exist
      */
-    String getPrimaryName( String oid ) throws NamingException;
+    public String getPrimaryName( String oid ) throws NamingException
+    {
+        SchemaObject schemaObject = byOid.get( oid );
+        
+        if ( schemaObject != null )
+        {
+            return schemaObject.getName();
+        }
+        else
+        {
+            String msg = "OID '" + oid + "' was not found within the OID registry";
+            LOG.error( msg );
+            throw new NamingException( msg );
+        }
+    }
+
+
+    /**
+     * Gets the SchemaObject associated with an OID. 
+     * 
+     * @param oid the object identifier
+     * @return the associated SchemaObject
+     * @throws NamingException if oid does not exist
+     */
+    public SchemaObject getSchemaObject( String oid ) throws NamingException
+    {
+        SchemaObject schemaObject = byOid.get( oid );
+        
+        if ( schemaObject != null )
+        {
+            return schemaObject;
+        }
+        else
+        {
+            String msg = "There is no SchemaObject associated with OID '" + oid + "'";
+            LOG.error( msg );
+            throw new NamingException( msg );
+        }
+    }
 
 
     /**
@@ -74,11 +122,31 @@
      * name certain things within the server internally.  If there is more than
      * one name then the first name is taken to be the primary.
      * 
+     * @param type The SchemaObjectType the oid belongs to
      * @param oid the OID for which we return the set of common names
      * @return a sorted set of names
      * @throws NamingException if oid does not exist
      */
-    List<String> getNameSet( String oid ) throws NamingException;
+    public List<String> getNameSet( String oid ) throws NamingException
+    {
+        SchemaObject schemaObject = byOid.get( oid );
+
+        if ( null == schemaObject )
+        {
+            String msg = "OID '" + oid + "' was not found within the OID registry";
+            LOG.error( msg );
+            throw new NamingException( msg );
+        }
+
+        List<String> names = schemaObject.getNames();
+        
+        if ( IS_DEBUG )
+        {
+            LOG.debug( "looked up names '{}' for OID '{}'", ArrayUtils.toString( names ), oid );
+        }
+        
+        return names;
+    }
 
 
     /**
@@ -86,37 +154,84 @@
      * 
      * @return all the OIDs registered
      */
-    Iterator<String> list();
+    public Iterator<String> iteratorOids()
+    {
+        return Collections.unmodifiableSet( byOid.keySet() ).iterator();
+    }
 
 
     /**
-     * Adds an OID name pair to the registry.
+     * Lists all the SchemaObjects within the registry.  This may be a really big list.
      * 
-     * @param name the name to associate with the OID
-     * @param oid the OID to add or associate a new name with
+     * @return all the SchemaObject registered
      */
-    void register( String name, String oid ) throws NamingException;
+    public Iterator<SchemaObject> iterator()
+    {
+        return byOid.values().iterator();
+    }
 
 
     /**
-     * Get the map of all the oids by their name
-     * @return The Map that contains all the oids
-     */
-    public Map<String,String> getOidByName();
-
-
-    /**
-     * Get the map of all the oids by their name
-     * @return The Map that contains all the oids
+     * Adds an OID name pair to the registry.
+     * 
+     * @param type The SchemaObjectType the oid belongs to
+     * @param oid the OID to add or associate a new name with
      */
-    public Map<String,List<String>> getNameByOid();
+    public void register( SchemaObject schemaObject ) throws NamingException
+    {
+        if ( schemaObject == null )
+        {
+            String message = "Cannot register a Null SchemaObject !";
+        
+            LOG.debug( message );
+            throw new NamingException( message );
+        }
+        
+        String oid = schemaObject.getOid();
+        
+        if ( !OID.isOID( oid ) )
+        {
+            String message = "The given SchemaObject does not have a valid OID";
+            
+            LOG.debug( message );
+            throw new NamingException( message );
+        }
+        
+        /*
+         * Update OID Map if it does not already exist
+         */
+        if ( byOid.containsKey( oid ) )
+        {
+            String message = "Therer is already a SchemaObject for OID " + oid;
+            LOG.info( message );
+            return;
+        }
+        else
+        {
+            byOid.put( oid, schemaObject );
+
+            if ( IS_DEBUG )
+            {
+                LOG.debug( "registed SchemaObject '" + schemaObject + "' with OID: " + oid );
+            }
+        }
+    }
 
 
     /**
      * Removes an oid from this registry.
      *
-     * @param numericOid the numeric identifier for the object
+     * @param oid the numeric identifier for the object
      * @throws NamingException if the identifier is not numeric
      */
-    void unregister( String numericOid ) throws NamingException;
+    public void unregister( String oid ) throws NamingException
+    {
+        // Removes the <OID, names> from the byOID map
+        SchemaObject removed = byOid.remove( oid );
+        
+        if ( IS_DEBUG )
+        {
+            LOG.debug( "Unregisted SchemaObject '{}' with OID: {}", removed, oid );
+        }
+    }
 }