From f6f67fe1cb1f1f08c392de2e4565ec1a89dd182a 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] Remove unnecessary calls to DirectoryServer.getAttributeTypeOrDefault(String).

---
 opendj-server-legacy/src/test/java/org/opends/server/protocols/ldap/TestLDAPConnectionHandler.java |    4 
 opendj-server-legacy/src/main/java/org/opends/server/core/PasswordPolicyState.java                 |   67 ++++++----------
 opendj-server-legacy/src/main/java/org/opends/server/types/DN.java                                 |  149 ++++++++++++-------------------------
 opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/LDAPReplicationDomain.java |    9 -
 4 files changed, 78 insertions(+), 151 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/core/PasswordPolicyState.java b/opendj-server-legacy/src/main/java/org/opends/server/core/PasswordPolicyState.java
index e0323fe..724d9ea 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/core/PasswordPolicyState.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/core/PasswordPolicyState.java
@@ -301,10 +301,9 @@
     if (passwordChangedTime < 0)
     {
       // Get the password changed time for the user.
-      AttributeType type = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_PWPOLICY_CHANGED_TIME_LC);
       try
       {
-        passwordChangedTime = getGeneralizedTime(userEntry, type);
+        passwordChangedTime = getGeneralizedTime0(userEntry, OP_ATTR_PWPOLICY_CHANGED_TIME_LC);
       }
       catch (DirectoryException e)
       {
@@ -319,10 +318,9 @@
       if (passwordChangedTime < 0)
       {
         // Get the time that the user's account was created.
-        AttributeType createTimeType = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_CREATE_TIMESTAMP_LC);
         try
         {
-          passwordChangedTime = getGeneralizedTime(userEntry, createTimeType);
+          passwordChangedTime = getGeneralizedTime0(userEntry, OP_ATTR_CREATE_TIMESTAMP_LC);
         }
         catch (DirectoryException e)
         {
@@ -351,6 +349,10 @@
   }
 
 
+  private long getGeneralizedTime0(Entry userEntry, String attrName) throws DirectoryException
+  {
+    return getGeneralizedTime(userEntry, DirectoryServer.getAttributeTypeOrDefault(attrName));
+  }
 
   /**
    * Retrieves the time that this password policy state object was created.
@@ -434,17 +436,15 @@
       logger.trace("Clearing password changed time for user %s", userDNString);
     }
 
-    AttributeType type = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_PWPOLICY_CHANGED_TIME_LC);
-    Attribute a = Attributes.empty(type);
+    Attribute a = Attributes.empty(OP_ATTR_PWPOLICY_CHANGED_TIME_LC);
     modifications.add(new Modification(ModificationType.REPLACE, a, true));
 
 
     // Fall back to using the entry creation time as the password changed time, if it's defined.
     // Otherwise, use a value of zero.
-    AttributeType createTimeType = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_CREATE_TIMESTAMP_LC);
     try
     {
-      passwordChangedTime = getGeneralizedTime(userEntry, createTimeType);
+      passwordChangedTime = getGeneralizedTime0(userEntry, OP_ATTR_CREATE_TIMESTAMP_LC);
       if (passwordChangedTime < 0)
       {
         passwordChangedTime = 0;
@@ -479,17 +479,15 @@
 
     this.isDisabled = ConditionResult.not(this.isDisabled);
 
-    AttributeType type = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_ACCOUNT_DISABLED);
-
     if (isDisabled)
     {
-      Attribute a = Attributes.create(type, String.valueOf(true));
+      Attribute a = Attributes.create(OP_ATTR_ACCOUNT_DISABLED, String.valueOf(true));
       modifications.add(new Modification(ModificationType.REPLACE, a, true));
     }
     else
     {
       // erase
-      modifications.add(new Modification(ModificationType.REPLACE, Attributes.empty(type), true));
+      modifications.add(new Modification(ModificationType.REPLACE, Attributes.empty(OP_ATTR_ACCOUNT_DISABLED), true));
     }
   }
 
@@ -512,10 +510,8 @@
       return isAccountExpired == ConditionResult.TRUE;
     }
 
-    AttributeType type = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_ACCOUNT_EXPIRATION_TIME);
-
     try {
-      accountExpirationTime = getGeneralizedTime(userEntry, type);
+      accountExpirationTime = getGeneralizedTime0(userEntry, OP_ATTR_ACCOUNT_EXPIRATION_TIME);
     }
     catch (Exception e)
     {
@@ -591,9 +587,8 @@
       }
 
       this.accountExpirationTime = accountExpirationTime;
-      AttributeType type = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_ACCOUNT_EXPIRATION_TIME);
 
-      Attribute a = Attributes.create(type, timeStr);
+      Attribute a = Attributes.create(OP_ATTR_ACCOUNT_EXPIRATION_TIME, timeStr);
       modifications.add(new Modification(ModificationType.REPLACE, a, true));
     }
   }
@@ -612,8 +607,8 @@
 
     accountExpirationTime = -1;
 
-    AttributeType type = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_ACCOUNT_EXPIRATION_TIME);
-    modifications.add(new Modification(ModificationType.REPLACE, Attributes.empty(type), true));
+    String attrName = OP_ATTR_ACCOUNT_EXPIRATION_TIME;
+    modifications.add(new Modification(ModificationType.REPLACE, Attributes.empty(attrName), true));
   }
 
 
@@ -768,10 +763,9 @@
       return;
     }
 
-    AttributeType type = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_PWPOLICY_FAILURE_TIME_LC);
     this.authFailureTimes = authFailureTimes;
 
-    AttributeBuilder builder = new AttributeBuilder(type);
+    AttributeBuilder builder = new AttributeBuilder(OP_ATTR_PWPOLICY_FAILURE_TIME_LC);
     long highestFailureTime = -1;
 
     for (long l : authFailureTimes)
@@ -1218,9 +1212,7 @@
 
     lastLoginTime = -1;
 
-    AttributeType type = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_LAST_LOGIN_TIME);
-
-    modifications.add(new Modification(ModificationType.REPLACE, Attributes.empty(type), true));
+    modifications.add(new Modification(ModificationType.REPLACE, Attributes.empty(OP_ATTR_LAST_LOGIN_TIME), true));
   }
 
 
@@ -1793,11 +1785,9 @@
       return requiredChangeTime;
     }
 
-    AttributeType type = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_PWPOLICY_CHANGED_BY_REQUIRED_TIME);
-
     try
     {
-      requiredChangeTime = getGeneralizedTime(userEntry, type);
+      requiredChangeTime = getGeneralizedTime0(userEntry, OP_ATTR_PWPOLICY_CHANGED_BY_REQUIRED_TIME);
     }
     catch (Exception e)
     {
@@ -1846,11 +1836,8 @@
     {
       this.requiredChangeTime = requiredChangeTime;
 
-      AttributeType type = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_PWPOLICY_CHANGED_BY_REQUIRED_TIME);
-
       String timeValue = GeneralizedTimeSyntax.format(requiredChangeTime);
-      Attribute a = Attributes.create(type, timeValue);
-
+      Attribute a = Attributes.create(OP_ATTR_PWPOLICY_CHANGED_BY_REQUIRED_TIME, timeValue);
       modifications.add(new Modification(ModificationType.REPLACE, a, true));
     }
   }
@@ -1870,8 +1857,8 @@
 
     this.requiredChangeTime = Long.MIN_VALUE;
 
-    AttributeType type = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_PWPOLICY_CHANGED_BY_REQUIRED_TIME);
-    modifications.add(new Modification(ModificationType.REPLACE, Attributes.empty(type), true));
+    String attrName = OP_ATTR_PWPOLICY_CHANGED_BY_REQUIRED_TIME;
+    modifications.add(new Modification(ModificationType.REPLACE, Attributes.empty(attrName), true));
   }
 
 
@@ -1885,10 +1872,9 @@
   {
     if (warnedTime == Long.MIN_VALUE)
     {
-      AttributeType type = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_PWPOLICY_WARNED_TIME);
       try
       {
-        warnedTime = getGeneralizedTime(userEntry, type);
+        warnedTime = getGeneralizedTime0(userEntry, OP_ATTR_PWPOLICY_WARNED_TIME);
       }
       catch (Exception e)
       {
@@ -1965,9 +1951,8 @@
     }
     warnedTime = -1;
 
-    AttributeType type = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_PWPOLICY_WARNED_TIME);
-    Attribute a = Attributes.empty(type);
-    modifications.add(new Modification(ModificationType.REPLACE, a, true));
+    String attrName = OP_ATTR_PWPOLICY_WARNED_TIME;
+    modifications.add(new Modification(ModificationType.REPLACE, Attributes.empty(attrName), true));
 
     if (logger.isTraceEnabled())
     {
@@ -2089,8 +2074,7 @@
 
     this.graceLoginTimes = graceLoginTimes;
 
-    AttributeType type = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_PWPOLICY_GRACE_LOGIN_TIME_LC);
-    AttributeBuilder builder = new AttributeBuilder(type);
+    AttributeBuilder builder = new AttributeBuilder(OP_ATTR_PWPOLICY_GRACE_LOGIN_TIME_LC);
     for (long l : graceLoginTimes)
     {
       builder.add(GeneralizedTimeSyntax.format(l));
@@ -2969,8 +2953,7 @@
       logger.trace("Clearing password history for user %s", userDNString);
     }
 
-    AttributeType type = DirectoryServer.getAttributeTypeOrDefault(OP_ATTR_PWPOLICY_HISTORY_LC);
-    modifications.add(new Modification(ModificationType.REPLACE, Attributes.empty(type), true));
+    modifications.add(new Modification(ModificationType.REPLACE, Attributes.empty(OP_ATTR_PWPOLICY_HISTORY_LC), true));
   }
 
 
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/LDAPReplicationDomain.java b/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/LDAPReplicationDomain.java
index 50244f1..368fe7e 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/LDAPReplicationDomain.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/LDAPReplicationDomain.java
@@ -2195,16 +2195,14 @@
         conn, nextOperationID(), nextMessageID(), new ArrayList<Control>(0),
         targetDN, newRDN, false, parentDN);
 
-    AttributeType attrType = DirectoryServer.getAttributeTypeOrDefault(DS_SYNC_CONFLICT);
     if (markConflict)
     {
-      Attribute attr =
-          Attributes.create(attrType, targetDN.toString());
+      Attribute attr = Attributes.create(DS_SYNC_CONFLICT, targetDN.toString());
       newOp.addModification(new Modification(ModificationType.REPLACE, attr));
     }
     else
     {
-      Attribute attr = Attributes.empty(attrType);
+      Attribute attr = Attributes.empty(DS_SYNC_CONFLICT);
       newOp.addModification(new Modification(ModificationType.DELETE, attr));
     }
 
@@ -3012,8 +3010,7 @@
   private void markConflictEntry(Operation op, DN currentDN, DN conflictDN)
   {
     // create new internal modify operation and run it.
-    AttributeType attrType = DirectoryServer.getAttributeTypeOrDefault(DS_SYNC_CONFLICT);
-    Attribute attr = Attributes.create(attrType, conflictDN.toString());
+    Attribute attr = Attributes.create(DS_SYNC_CONFLICT, conflictDN.toString());
     List<Modification> mods = newArrayList(new Modification(ModificationType.REPLACE, attr));
 
     ModifyOperation newOp = new ModifyOperationBasis(
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/types/DN.java b/opendj-server-legacy/src/main/java/org/opends/server/types/DN.java
index 494be1f..cf030bc 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/types/DN.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/types/DN.java
@@ -29,6 +29,7 @@
 import static org.forgerock.util.Reject.*;
 import static org.opends.messages.SchemaMessages.*;
 import static org.opends.server.config.ConfigConstants.*;
+import static org.opends.server.core.DirectoryServer.*;
 import static org.opends.server.util.StaticUtils.*;
 
 import java.io.Serializable;
@@ -645,11 +646,7 @@
       // component and return the DN.
       if (b == ' ')
       {
-        StringBuilder lowerName = new StringBuilder();
-        toLowerCase(attributeName, lowerName, true);
-        String attributeNameString = attributeName.toString();
-        AttributeType attrType = getAttributeType(lowerName.toString(), attributeNameString);
-        rdnComponents.add(new RDN(attrType, attributeNameString, ByteString.empty()));
+        rdnComponents.add(newRDN(attributeName, ByteString.empty()));
         return new DN(rdnComponents);
       }
 
@@ -660,21 +657,7 @@
 
 
       // Create the new RDN with the provided information.
-      StringBuilder lowerName = new StringBuilder();
-      toLowerCase(attributeName, lowerName, true);
-      AttributeType attrType = DirectoryServer.getAttributeTypeOrNull(lowerName.toString());
-      String attributeNameString = attributeName.toString();
-      if (attrType == null)
-      {
-        // This must be an attribute type that we don't know about.
-        // In that case, we'll create a new attribute using the
-        // default syntax.  If this is a problem, it will be caught
-        // later either by not finding the target entry or by not
-        // allowing the entry to be added.
-        attrType = DirectoryServer.getAttributeTypeOrDefault(attributeNameString);
-      }
-
-      RDN rdn = new RDN(attrType, attributeNameString, parsedValue);
+      RDN rdn = newRDN(attributeName, parsedValue);
 
 
       // Skip over any spaces that might be after the attribute value.
@@ -776,22 +759,7 @@
         // the RDN component and return the DN.
         if (b == ' ')
         {
-          lowerName = new StringBuilder();
-          toLowerCase(attributeName, lowerName, true);
-          attrType = DirectoryServer.getAttributeTypeOrNull(lowerName.toString());
-          attributeNameString = attributeName.toString();
-
-          if (attrType == null)
-          {
-            // This must be an attribute type that we don't know
-            // about.  In that case, we'll create a new attribute
-            // using the default syntax.  If this is a problem, it
-            // will be caught later either by not finding the target
-            // entry or by not allowing the entry to be added.
-            attrType = DirectoryServer.getAttributeTypeOrDefault(attributeNameString);
-          }
-
-          rdn.addValue(attrType, attributeNameString, ByteString.empty());
+          addValue(attributeName, rdn, ByteString.empty());
           rdnComponents.add(rdn);
           return new DN(rdnComponents);
         }
@@ -802,21 +770,7 @@
         parsedValue = parseAttributeValue(dnReader);
 
 
-        lowerName = new StringBuilder();
-        toLowerCase(attributeName, lowerName, true);
-        attrType = DirectoryServer.getAttributeTypeOrNull(lowerName.toString());
-        attributeNameString = attributeName.toString();
-        if (attrType == null)
-        {
-          // This must be an attribute type that we don't know about.
-          // In that case, we'll create a new attribute using the
-          // default syntax.  If this is a problem, it will be caught
-          // later either by not finding the target entry or by not
-          // allowing the entry to be added.
-          attrType = DirectoryServer.getAttributeTypeOrDefault(attributeNameString);
-        }
-
-        rdn.addValue(attrType, attributeNameString, parsedValue);
+        addValue(attributeName, rdn, parsedValue);
 
 
         // Skip over any spaces that might be after the attribute value.
@@ -857,6 +811,31 @@
     }
   }
 
+  private static RDN newRDN(ByteString attrName, ByteString value)
+  {
+    String lowerName = toLC(attrName);
+    String attributeNameString = attrName.toString();
+    AttributeType attrType = getAttributeTypeOrDefault(lowerName, attributeNameString);
+
+    return new RDN(attrType, attributeNameString, value);
+  }
+
+  private static void addValue(ByteString attributeName, RDN rdn, ByteString empty)
+  {
+    String lowerName = toLC(attributeName);
+    String attributeNameString = attributeName.toString();
+    AttributeType attrType = getAttributeTypeOrDefault(lowerName, attributeNameString);
+
+    rdn.addValue(attrType, attributeNameString, empty);
+  }
+
+  private static String toLC(ByteString attributeName)
+  {
+    StringBuilder lowerName = new StringBuilder();
+    toLowerCase(attributeName, lowerName, true);
+    return lowerName.toString();
+  }
+
   /**
    * Decodes the provided string as a DN.
    *
@@ -970,10 +949,7 @@
       // RDN component and return the DN.
       if (pos >= length)
       {
-        String        name      = attributeName.toString();
-        String        lowerName = toLowerCase(name);
-        AttributeType attrType = getAttributeType(lowerName, name);
-        rdnComponents.add(new RDN(attrType, name, ByteString.empty()));
+        rdnComponents.add(newRDN(attributeName, ByteString.empty()));
         return new DN(rdnComponents);
       }
 
@@ -984,10 +960,7 @@
 
 
       // Create the new RDN with the provided information.
-      String name            = attributeName.toString();
-      String lowerName       = toLowerCase(name);
-      AttributeType attrType = getAttributeType(lowerName, name);
-      RDN rdn = new RDN(attrType, name, parsedValue.toByteString());
+      RDN rdn = newRDN(attributeName, parsedValue.toByteString());
 
 
       // Skip over any spaces that might be after the attribute value.
@@ -1100,21 +1073,7 @@
         // the RDN component and return the DN.
         if (pos >= length)
         {
-          name      = attributeName.toString();
-          lowerName = toLowerCase(name);
-          attrType  = DirectoryServer.getAttributeTypeOrNull(lowerName);
-
-          if (attrType == null)
-          {
-            // This must be an attribute type that we don't know
-            // about.  In that case, we'll create a new attribute
-            // using the default syntax.  If this is a problem, it
-            // will be caught later either by not finding the target
-            // entry or by not allowing the entry to be added.
-            attrType = DirectoryServer.getAttributeTypeOrDefault(name);
-          }
-
-          rdn.addValue(attrType, name, ByteString.empty());
+          addValue(attributeName, rdn, ByteString.empty());
           rdnComponents.add(rdn);
           return new DN(rdnComponents);
         }
@@ -1126,20 +1085,7 @@
 
 
         // Create the new RDN with the provided information.
-        name      = attributeName.toString();
-        lowerName = toLowerCase(name);
-        attrType  = DirectoryServer.getAttributeTypeOrNull(lowerName);
-        if (attrType == null)
-        {
-          // This must be an attribute type that we don't know about.
-          // In that case, we'll create a new attribute using the
-          // default syntax.  If this is a problem, it will be caught
-          // later either by not finding the target entry or by not
-          // allowing the entry to be added.
-          attrType = DirectoryServer.getAttributeTypeOrDefault(name);
-        }
-
-        rdn.addValue(attrType, name, parsedValue.toByteString());
+        addValue(attributeName, rdn, parsedValue.toByteString());
 
 
         // Skip over any spaces that might be after the attribute value.
@@ -1181,19 +1127,22 @@
     }
   }
 
-  private static AttributeType getAttributeType(String lowerName, String name)
+  private static RDN newRDN(StringBuilder attributeName, ByteString value)
   {
-    AttributeType attrType = DirectoryServer.getAttributeTypeOrNull(lowerName);
-    if (attrType == null)
-    {
-      // This must be an attribute type that we don't know about.
-      // In that case, we'll create a new attribute using the
-      // default syntax.  If this is a problem, it will be caught
-      // later either by not finding the target entry or by not
-      // allowing the entry to be added.
-      attrType = DirectoryServer.getAttributeTypeOrDefault(name);
-    }
-    return attrType;
+    String        name      = attributeName.toString();
+    String        lowerName = toLowerCase(name);
+    AttributeType attrType = getAttributeTypeOrDefault(lowerName, name);
+
+    return new RDN(attrType, name, value);
+  }
+
+  private static void addValue(StringBuilder attributeName, RDN rdn, ByteString empty)
+  {
+    String name = attributeName.toString();
+    String lowerName = toLowerCase(name);
+    AttributeType attrType = getAttributeTypeOrDefault(lowerName, name);
+
+    rdn.addValue(attrType, name, empty);
   }
 
   /**
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/protocols/ldap/TestLDAPConnectionHandler.java b/opendj-server-legacy/src/test/java/org/opends/server/protocols/ldap/TestLDAPConnectionHandler.java
index 082e265..7b21726 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/protocols/ldap/TestLDAPConnectionHandler.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/protocols/ldap/TestLDAPConnectionHandler.java
@@ -121,9 +121,7 @@
     //Reset some things for the SSL handler
     Attribute useSSL=Attributes.create(ATTR_USE_SSL, String.valueOf(false));
     Attribute startTls=Attributes.create(ATTR_ALLOW_STARTTLS, String.valueOf(false));
-    AttributeType attrType=DirectoryServer.getAttributeTypeOrDefault(ATTR_LISTEN_PORT);
-    Attribute a=Attributes.empty(attrType);
-    LDAPHandlerEntry.removeAttribute(a, null);
+    LDAPHandlerEntry.removeAttribute(Attributes.empty(ATTR_LISTEN_PORT), null);
     LDAPHandlerEntry.removeAttribute(useSSL, null);
     LDAPHandlerEntry.removeAttribute(startTls, null);
     Attribute useSSL1=Attributes.create(ATTR_USE_SSL, String.valueOf(true));

--
Gitblit v1.10.0