From 22c201d5b5c9234a0f353a9830ef729f2e395e0f Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Mon, 30 May 2016 10:43:10 +0000
Subject: [PATCH] OPENDJ-2987 SDK's Schema.getOIDForName() now matches what the server's schema was doing

---
 opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/Schema.java        |   39 ++++++++++++++-----
 opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaBuilder.java |   34 ----------------
 2 files changed, 30 insertions(+), 43 deletions(-)

diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/Schema.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/Schema.java
index ed15c90..cff4a05 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/Schema.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/Schema.java
@@ -17,6 +17,11 @@
  */
 package org.forgerock.opendj.ldap.schema;
 
+import static com.forgerock.opendj.ldap.CoreMessages.*;
+import static com.forgerock.opendj.util.StaticUtils.*;
+
+import static org.forgerock.opendj.ldap.AttributeDescription.*;
+
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedList;
@@ -45,9 +50,6 @@
 
 import com.forgerock.opendj.util.StaticUtils;
 
-import static org.forgerock.opendj.ldap.AttributeDescription.*;
-import static com.forgerock.opendj.ldap.CoreMessages.*;
-
 /**
  * This class defines a data structure that holds information about the
  * components of the LDAP schema. It includes the following kinds of elements:
@@ -418,7 +420,6 @@
         private final Map<String, ObjectClass> numericOID2ObjectClasses;
         private final Map<String, Syntax> numericOID2Syntaxes;
         private final Map<String, List<NameForm>> objectClass2NameForms;
-        private final Map<String, String> name2OIDs;
         private final List<LocalizableMessage> warnings;
         private final String schemaName;
         private final Options options;
@@ -448,7 +449,6 @@
                 final Map<String, List<DITStructureRule>> name2StructureRules,
                 final Map<String, List<NameForm>> objectClass2NameForms,
                 final Map<String, List<DITStructureRule>> nameForm2StructureRules,
-                final Map<String, String> name2OIDs,
                 final List<LocalizableMessage> warnings) {
             this.schemaName = schemaName;
             this.options = options;
@@ -471,7 +471,6 @@
             this.name2StructureRules = Collections.unmodifiableMap(name2StructureRules);
             this.objectClass2NameForms = Collections.unmodifiableMap(objectClass2NameForms);
             this.nameForm2StructureRules = Collections.unmodifiableMap(nameForm2StructureRules);
-            this.name2OIDs = Collections.unmodifiableMap(name2OIDs);
             this.warnings = Collections.unmodifiableList(warnings);
             this.strictSchema = new Schema(this);
             this.nonStrictSchema = new Schema(new NonStrictImpl(this));
@@ -504,11 +503,31 @@
 
         @Override
         public String getOIDForName(String lowerCaseName) {
-            final String oid = name2OIDs.get(lowerCaseName);
-            if (SchemaBuilder.AMBIGUOUS_OID.equals(oid)) {
-                throw new UnknownSchemaElementException(WARN_NAME_AMBIGUOUS.get(lowerCaseName));
+            final String oid = getOIDForName0(lowerCaseName);
+            return oid != null ? toLowerCase(oid) : null;
+        }
+
+        private String getOIDForName0(String lowerName) {
+            AttributeType attributeType = getAttributeType0(lowerName);
+            if (attributeType != null) {
+                return attributeType.getOID();
             }
-            return oid;
+            try {
+                return getObjectClass(lowerName).getOID();
+            } catch (UnknownSchemaElementException ignore) {
+                // try next schema element
+            }
+            try {
+                return getMatchingRule(lowerName).getOID();
+            } catch (UnknownSchemaElementException ignore) {
+                // try next schema element
+            }
+            try {
+                return getNameForm(lowerName).getOID();
+            } catch (UnknownSchemaElementException ignore) {
+                // try next schema element
+            }
+            return null;
         }
 
         @Override
diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaBuilder.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaBuilder.java
index f5d7301..9ec7161 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaBuilder.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaBuilder.java
@@ -139,7 +139,6 @@
     private Map<String, ObjectClass> numericOID2ObjectClasses;
     private Map<String, Syntax> numericOID2Syntaxes;
     private Map<String, List<NameForm>> objectClass2NameForms;
-    private Map<String, String> name2OIDs;
     private String schemaName;
     private List<LocalizableMessage> warnings;
     private Options options;
@@ -2054,7 +2053,7 @@
                         numericOID2ContentRules, id2StructureRules, name2MatchingRules,
                         name2MatchingRuleUses, name2AttributeTypes, name2ObjectClasses,
                         name2NameForms, name2ContentRules, name2StructureRules,
-                        objectClass2NameForms, nameForm2StructureRules, name2OIDs, warnings).asStrictSchema();
+                        objectClass2NameForms, nameForm2StructureRules, warnings).asStrictSchema();
         validate(schema);
 
         // Re-init this builder so that it can continue to be used afterwards.
@@ -2063,12 +2062,6 @@
         return schema;
     }
 
-    private void registerNameToOIDMapping(String name, String anOID) {
-        if (name2OIDs.put(name, anOID) != null) {
-            name2OIDs.put(name, AMBIGUOUS_OID);
-        }
-    }
-
     SchemaBuilder addAttributeType(final AttributeType attribute, final boolean overwrite) {
         AttributeType conflictingAttribute;
         if (numericOID2AttributeTypes.containsKey(attribute.getOID())) {
@@ -2365,7 +2358,6 @@
 
             objectClass2NameForms = new HashMap<>();
             nameForm2StructureRules = new HashMap<>();
-            name2OIDs = new HashMap<>();
             warnings = new LinkedList<>();
 
             if (copyOnWriteSchema != null) {
@@ -2528,7 +2520,6 @@
                 new Syntax[numericOID2Syntaxes.values().size()])) {
             try {
                 syntax.validate(schema, warnings);
-                registerNameToOIDMapping(syntax.getName(), syntax.getOID());
             } catch (final SchemaException e) {
                 removeSyntax(syntax);
                 warnings.add(ERR_SYNTAX_VALIDATION_FAIL
@@ -2540,9 +2531,6 @@
                 new MatchingRule[numericOID2MatchingRules.values().size()])) {
             try {
                 rule.validate(schema, warnings);
-                for (final String name : rule.getNames()) {
-                    registerNameToOIDMapping(StaticUtils.toLowerCase(name), rule.getOID());
-                }
             } catch (final SchemaException e) {
                 removeMatchingRule(rule);
                 warnings.add(ERR_MR_VALIDATION_FAIL.get(rule.toString(), e.getMessageObject()));
@@ -2560,12 +2548,6 @@
             removeAttributeType(attributeType);
         }
 
-        for (final AttributeType attributeType : numericOID2AttributeTypes.values()) {
-            for (final String name : attributeType.getNames()) {
-                registerNameToOIDMapping(StaticUtils.toLowerCase(name), attributeType.getOID());
-            }
-        }
-
         // Object classes need special processing because they have hierarchical
         // dependencies.
         final List<ObjectClass> invalidObjectClasses = new LinkedList<>();
@@ -2577,18 +2559,10 @@
             removeObjectClass(objectClass);
         }
 
-        for (final ObjectClass objectClass : numericOID2ObjectClasses.values()) {
-            for (final String name : objectClass.getNames()) {
-                registerNameToOIDMapping(StaticUtils.toLowerCase(name), objectClass.getOID());
-            }
-        }
         for (final MatchingRuleUse use : numericOID2MatchingRuleUses.values().toArray(
                 new MatchingRuleUse[numericOID2MatchingRuleUses.values().size()])) {
             try {
                 use.validate(schema);
-                for (final String name : use.getNames()) {
-                    registerNameToOIDMapping(StaticUtils.toLowerCase(name), use.getMatchingRuleOID());
-                }
             } catch (final SchemaException e) {
                 removeMatchingRuleUse(use);
                 warnings.add(ERR_MRU_VALIDATION_FAIL.get(use.toString(), e.getMessageObject()));
@@ -2612,9 +2586,6 @@
                 } else {
                     forms.add(form);
                 }
-                for (final String name : form.getNames()) {
-                    registerNameToOIDMapping(StaticUtils.toLowerCase(name), form.getOID());
-                }
             } catch (final SchemaException e) {
                 removeNameForm(form);
                 warnings.add(ERR_NAMEFORM_VALIDATION_FAIL
@@ -2626,9 +2597,6 @@
                 new DITContentRule[numericOID2ContentRules.values().size()])) {
             try {
                 rule.validate(schema, warnings);
-                for (final String name : rule.getNames()) {
-                    registerNameToOIDMapping(StaticUtils.toLowerCase(name), rule.getStructuralClassOID());
-                }
             } catch (final SchemaException e) {
                 removeDITContentRule(rule);
                 warnings.add(ERR_DCR_VALIDATION_FAIL.get(rule.toString(), e.getMessageObject()));

--
Gitblit v1.10.0