From 0841dd13681c9543b393a18487ef7f259859eaf1 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Thu, 15 Oct 2015 19:38:35 +0000
Subject: [PATCH] Hide DirectoryServer.getDefaultAttributeType(String, Syntax) the best possible.

---
 opendj-server-legacy/src/test/java/org/opends/server/types/TestDN.java                            |    5 +
 opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java                    |   53 ++++++++++++-----
 opendj-server-legacy/src/main/java/org/opends/server/monitors/TraditionalWorkQueueMonitor.java    |    4 
 opendj-server-legacy/src/main/java/org/opends/server/extensions/DiskSpaceMonitor.java             |    9 +--
 opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java                  |   21 +-----
 opendj-server-legacy/src/main/java/org/opends/server/monitors/ParallelWorkQueueMonitor.java       |    4 
 opendj-server-legacy/src/test/java/org/opends/server/types/TestRDN.java                           |   10 +-
 opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/JEMonitor.java                  |    2 
 opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/DatabaseEnvironmentMonitor.java |   22 ++++--
 opendj-server-legacy/src/main/java/org/opends/server/config/ConfigEntry.java                      |   14 ++--
 10 files changed, 79 insertions(+), 65 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java
index 4cad0f0..4e4a2b0 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java
@@ -3628,14 +3628,8 @@
     {
       attrTypeSyntax = CoreSchema.getAttributeTypeDescriptionSyntax();
     }
-
-    AttributeType attributeAttrType = schema.getAttributeType(ATTR_ATTRIBUTE_TYPES_LC);
-    if (attributeAttrType == null)
-    {
-      attributeAttrType =
-           DirectoryServer.getDefaultAttributeType(ATTR_ATTRIBUTE_TYPES,
-                                                   attrTypeSyntax);
-    }
+    AttributeType attributeAttrType = DirectoryServer.getAttributeTypeOrDefault(
+        ATTR_ATTRIBUTE_TYPES_LC, ATTR_ATTRIBUTE_TYPES, attrTypeSyntax);
 
     // loop on the attribute types in the entry just received
     // and add them in the existing schema.
@@ -3720,15 +3714,8 @@
     {
       ocSyntax = CoreSchema.getObjectClassDescriptionSyntax();
     }
-
-    AttributeType objectclassAttrType =
-      schema.getAttributeType(ATTR_OBJECTCLASSES_LC);
-    if (objectclassAttrType == null)
-    {
-      objectclassAttrType =
-        DirectoryServer.getDefaultAttributeType(ATTR_OBJECTCLASSES,
-                                                ocSyntax);
-    }
+    AttributeType objectclassAttrType = DirectoryServer.getAttributeTypeOrDefault(
+        ATTR_OBJECTCLASSES_LC, ATTR_OBJECTCLASSES, ocSyntax);
 
     oidList.clear();
     List<Attribute> ocList = newSchemaEntry.getAttribute(objectclassAttrType);
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/DatabaseEnvironmentMonitor.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/DatabaseEnvironmentMonitor.java
index 4a59926..36f50a4 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/DatabaseEnvironmentMonitor.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/DatabaseEnvironmentMonitor.java
@@ -27,7 +27,12 @@
 package org.opends.server.backends.jeb;
 
 import java.lang.reflect.Method;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.forgerock.i18n.LocalizableMessage;
@@ -37,7 +42,12 @@
 import org.opends.server.admin.std.server.MonitorProviderCfg;
 import org.opends.server.api.MonitorProvider;
 import org.opends.server.core.DirectoryServer;
-import org.opends.server.types.*;
+import org.opends.server.types.Attribute;
+import org.opends.server.types.AttributeBuilder;
+import org.opends.server.types.AttributeType;
+import org.opends.server.types.Attributes;
+import org.opends.server.types.InitializationException;
+import org.opends.server.types.SearchFilter;
 import org.opends.server.util.TimeThread;
 
 import com.sleepycat.je.DatabaseException;
@@ -172,12 +182,8 @@
             Object statValue = method.invoke(stats);
 
             // Create an attribute from the statistic.
-            AttributeType attrType =
-                 DirectoryServer.getDefaultAttributeType(attrName,
-                                                         integerSyntax);
-            monitorAttrs.add(Attributes.create(attrType, String
-                .valueOf(statValue)));
-
+            AttributeType attrType = DirectoryServer.getAttributeTypeOrDefault(attrName, attrName, integerSyntax);
+            monitorAttrs.add(Attributes.create(attrType, String.valueOf(statValue)));
           } catch (Exception e)
           {
             logger.traceException(e);
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/JEMonitor.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/JEMonitor.java
index 1c3c568..ac3082a 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/JEMonitor.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/jeb/JEMonitor.java
@@ -111,7 +111,7 @@
       // Remove the 'get' from the method name and add the prefix.
       String attrName = attrPrefix + method.getName().substring(3);
 
-      AttributeType attrType = DirectoryServer.getDefaultAttributeType(attrName, integerSyntax);
+      AttributeType attrType = DirectoryServer.getAttributeTypeOrDefault(attrName, attrName, integerSyntax);
       monitorAttrs.add(Attributes.create(attrType, String.valueOf(method.invoke(stats))));
     }
     catch (Exception e)
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/config/ConfigEntry.java b/opendj-server-legacy/src/main/java/org/opends/server/config/ConfigEntry.java
index 1506225..2100e95 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/config/ConfigEntry.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/config/ConfigEntry.java
@@ -38,7 +38,12 @@
 import org.opends.server.api.ConfigChangeListener;
 import org.opends.server.api.ConfigDeleteListener;
 import org.opends.server.core.DirectoryServer;
-import org.opends.server.types.*;
+import org.opends.server.types.Attribute;
+import org.opends.server.types.AttributeBuilder;
+import org.opends.server.types.AttributeType;
+import org.opends.server.types.DN;
+import org.opends.server.types.Entry;
+import org.opends.server.types.ObjectClass;
 
 import static org.opends.messages.ConfigMessages.*;
 import static org.opends.server.config.ConfigConstants.*;
@@ -214,11 +219,8 @@
   public void putConfigAttribute(ConfigAttribute attribute)
   {
     String name = attribute.getName();
-    AttributeType attrType = DirectoryServer.getAttributeTypeOrNull(name.toLowerCase());
-    if (attrType == null)
-    {
-      attrType = DirectoryServer.getDefaultAttributeType(name, attribute.getSyntax());
-    }
+    AttributeType attrType = DirectoryServer.getAttributeTypeOrDefault(
+        name.toLowerCase(), name, attribute.getSyntax());
 
     List<Attribute> attrs = new ArrayList<>(2);
     AttributeBuilder builder = new AttributeBuilder(attrType, name);
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java b/opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java
index 8fd628c..a081055 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/core/DirectoryServer.java
@@ -2658,7 +2658,7 @@
    */
   public static AttributeType getAttributeTypeOrDefault(String lowerName)
   {
-    return getAttributeTypeOrDefault(lowerName, lowerName);
+    return getAttributeTypeOrDefault(lowerName, lowerName, getDefaultAttributeSyntax());
   }
 
   /**
@@ -2675,12 +2675,31 @@
    */
   public static AttributeType getAttributeTypeOrDefault(String lowerName, String upperName)
   {
-    AttributeType type = getAttributeTypeOrNull(lowerName);
-    if (type == null)
+    return getAttributeTypeOrDefault(lowerName, upperName, getDefaultAttributeSyntax());
+  }
+
+  /**
+   * Retrieves the attribute type for the provided lowercase name or OID. It will return a generated
+   * "default" version with the uppercase name or OID if the requested attribute type is not defined
+   * in the schema.
+   *
+   * @param lowerName
+   *          The lowercase name or OID for the attribute type to retrieve.
+   * @param upperName
+   *          The uppercase name or OID for the attribute type to generate.
+   * @param syntax
+   *          The syntax for the attribute type to generate.
+   * @return The requested attribute type, or a generated "default" version if there is no attribute
+   *         with the specified type defined in the server schema
+   */
+  public static AttributeType getAttributeTypeOrDefault(String lowerName, String upperName, Syntax syntax)
+  {
+    AttributeType attrType = getAttributeTypeOrNull(lowerName);
+    if (attrType == null)
     {
-      type = getDefaultAttributeType(upperName, getDefaultAttributeSyntax());
+      attrType = getDefaultAttributeType(upperName, syntax);
     }
-    return type;
+    return attrType;
   }
 
   /**
@@ -2750,11 +2769,7 @@
              "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 X-ORIGIN 'RFC 2256' )";
 
         directoryServer.objectClassAttributeType =
-             new AttributeType(definition, "objectClass",
-                               Collections.singleton("objectClass"),
-                               OBJECTCLASS_ATTRIBUTE_TYPE_OID, null, null,
-                               oidSyntax, AttributeUsage.USER_APPLICATIONS,
-                               false, false, false, false);
+            newAttributeType(definition, "objectClass", OBJECTCLASS_ATTRIBUTE_TYPE_OID, oidSyntax);
         try
         {
           directoryServer.schema.registerAttributeType(
@@ -2775,9 +2790,12 @@
    * Causes the Directory Server to construct a new attribute type definition
    * with the provided name and syntax.  This should only be used if there is no
    * real attribute type for the specified name.
+   * <p>
+   * TODO remove once we switch to the SDK Schema
+   * <p>
+   * FIXME move to {@link org.opends.server.types.Schema}?
    *
-   * @param  name    The name to use for the attribute type, as provided by the
-   *                 user.
+   * @param  name    The name to use for the attribute type, as provided by the user.
    * @param  syntax  The syntax to use for the attribute type.
    *
    * @return  The constructed attribute type definition.
@@ -2789,10 +2807,13 @@
                         syntax.getOID() + " )";
 
     // Temporary attribute types are immediately dirty.
-    return new AttributeType(definition, name, Collections.singleton(name),
-                             oid, null, null, syntax,
-                             AttributeUsage.USER_APPLICATIONS, false, false,
-                             false, false).setDirty();
+    return newAttributeType(definition, name, oid, syntax).setDirty();
+  }
+
+  private static AttributeType newAttributeType(String definition, String name, String oid, Syntax syntax)
+  {
+    return new AttributeType(definition, name, Collections.singleton(name), oid, null, null, syntax,
+        AttributeUsage.USER_APPLICATIONS, false, false, false, false);
   }
 
   /**
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/extensions/DiskSpaceMonitor.java b/opendj-server-legacy/src/main/java/org/opends/server/extensions/DiskSpaceMonitor.java
index 14d1c84..eecb244 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/extensions/DiskSpaceMonitor.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/extensions/DiskSpaceMonitor.java
@@ -29,10 +29,7 @@
 import static org.opends.messages.CoreMessages.*;
 import static org.opends.server.core.DirectoryServer.*;
 import static org.opends.server.util.CollectionUtils.*;
-import static org.opends.server.util.ServerConstants.ALERT_DESCRIPTION_DISK_FULL;
-import static org.opends.server.util.ServerConstants.ALERT_DESCRIPTION_DISK_SPACE_LOW;
-import static org.opends.server.util.ServerConstants.ALERT_TYPE_DISK_FULL;
-import static org.opends.server.util.ServerConstants.ALERT_TYPE_DISK_SPACE_LOW;
+import static org.opends.server.util.ServerConstants.*;
 
 import java.io.File;
 import java.io.IOException;
@@ -51,9 +48,9 @@
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
 import org.forgerock.opendj.config.server.ConfigException;
+import org.forgerock.opendj.ldap.schema.Syntax;
 import org.opends.server.admin.std.server.MonitorProviderCfg;
 import org.opends.server.api.AlertGenerator;
-import org.forgerock.opendj.ldap.schema.Syntax;
 import org.opends.server.api.DiskSpaceMonitorHandler;
 import org.opends.server.api.MonitorProvider;
 import org.opends.server.api.ServerShutdownListener;
@@ -147,7 +144,7 @@
 
     private Attribute attr(String name, Syntax syntax, Object value)
     {
-      AttributeType attrType = DirectoryServer.getDefaultAttributeType(name, syntax);
+      AttributeType attrType = DirectoryServer.getAttributeTypeOrDefault(name, name, syntax);
       return Attributes.create(attrType, String.valueOf(value));
     }
 
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/monitors/ParallelWorkQueueMonitor.java b/opendj-server-legacy/src/main/java/org/opends/server/monitors/ParallelWorkQueueMonitor.java
index e72e0a9..cdef8ed 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/monitors/ParallelWorkQueueMonitor.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/monitors/ParallelWorkQueueMonitor.java
@@ -31,9 +31,9 @@
 import java.util.ArrayList;
 import java.util.concurrent.TimeUnit;
 
+import org.forgerock.opendj.config.server.ConfigException;
 import org.opends.server.admin.std.server.MonitorProviderCfg;
 import org.opends.server.api.MonitorProvider;
-import org.forgerock.opendj.config.server.ConfigException;
 import org.opends.server.extensions.ParallelWorkQueue;
 import org.opends.server.types.Attribute;
 import org.opends.server.types.AttributeType;
@@ -166,7 +166,7 @@
 
   private void putAttribute(ArrayList<Attribute> monitorAttrs, String attrName, Object value)
   {
-    AttributeType attrType = getDefaultAttributeType(attrName, getDefaultIntegerSyntax());
+    AttributeType attrType = getAttributeTypeOrDefault(attrName, attrName, getDefaultIntegerSyntax());
     monitorAttrs.add(Attributes.create(attrType, String.valueOf(value)));
   }
 }
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/monitors/TraditionalWorkQueueMonitor.java b/opendj-server-legacy/src/main/java/org/opends/server/monitors/TraditionalWorkQueueMonitor.java
index 3dbeb48..82288f5 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/monitors/TraditionalWorkQueueMonitor.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/monitors/TraditionalWorkQueueMonitor.java
@@ -31,9 +31,9 @@
 import java.util.ArrayList;
 import java.util.concurrent.TimeUnit;
 
+import org.forgerock.opendj.config.server.ConfigException;
 import org.opends.server.admin.std.server.MonitorProviderCfg;
 import org.opends.server.api.MonitorProvider;
-import org.forgerock.opendj.config.server.ConfigException;
 import org.opends.server.extensions.TraditionalWorkQueue;
 import org.opends.server.types.Attribute;
 import org.opends.server.types.AttributeType;
@@ -173,7 +173,7 @@
 
   private void putAttribute(ArrayList<Attribute> monitorAttrs, String attrName, Object value)
   {
-    AttributeType attrType = getDefaultAttributeType(attrName, getDefaultIntegerSyntax());
+    AttributeType attrType = getAttributeTypeOrDefault(attrName, attrName, getDefaultIntegerSyntax());
     monitorAttrs.add(Attributes.create(attrType, String.valueOf(value)));
   }
 }
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/types/TestDN.java b/opendj-server-legacy/src/test/java/org/opends/server/types/TestDN.java
index 36c58f4..fdbbd2e 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/types/TestDN.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/types/TestDN.java
@@ -28,6 +28,7 @@
 
 import static org.assertj.core.api.Assertions.*;
 import static org.opends.server.TestCaseUtils.*;
+import static org.opends.server.core.DirectoryServer.*;
 import static org.testng.Assert.*;
 
 import java.util.ArrayList;
@@ -165,8 +166,8 @@
     // we'll start the server.
     TestCaseUtils.startServer();
 
-    AttributeType dummy = DirectoryServer.getDefaultAttributeType(
-        "x-test-integer-type", DirectoryServer.getDefaultIntegerSyntax());
+    String attrName = "x-test-integer-type";
+    AttributeType dummy = getAttributeTypeOrDefault(attrName, attrName, getDefaultIntegerSyntax());
     DirectoryServer.getSchema().registerAttributeType(dummy, true);
   }
 
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/types/TestRDN.java b/opendj-server-legacy/src/test/java/org/opends/server/types/TestRDN.java
index ac299f9..be21177 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/types/TestRDN.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/types/TestRDN.java
@@ -26,11 +26,12 @@
  */
 package org.opends.server.types;
 
-import java.util.ArrayList;
-
 import static org.opends.server.TestCaseUtils.*;
+import static org.opends.server.core.DirectoryServer.*;
 import static org.testng.Assert.*;
 
+import java.util.ArrayList;
+
 import org.forgerock.opendj.ldap.ByteString;
 import org.opends.server.TestCaseUtils;
 import org.opends.server.core.DirectoryServer;
@@ -70,9 +71,8 @@
     AT_DC = DirectoryServer.getAttributeTypeOrNull("dc");
     AT_CN = DirectoryServer.getAttributeTypeOrNull("cn");
 
-    AttributeType dummy = DirectoryServer.getDefaultAttributeType(
-        "x-test-integer-type", DirectoryServer
-            .getDefaultIntegerSyntax());
+    String attrName = "x-test-integer-type";
+    AttributeType dummy = getAttributeTypeOrDefault(attrName, attrName, getDefaultIntegerSyntax());
     DirectoryServer.getSchema().registerAttributeType(dummy, true);
 
     AV_DC_ORG = ByteString.valueOf("org");

--
Gitblit v1.10.0