From 110ff48cb0c18fb2bd636c49d16f9e64a7deae28 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Wed, 30 Mar 2016 10:14:13 +0000
Subject: [PATCH] Code cleanups

---
 opendj-server-legacy/src/main/java/org/opends/server/types/Entry.java                                              |  184 ++++++++++++++++++++++-----------------------
 opendj-server-legacy/src/main/java/org/opends/server/workflowelement/localbackend/LocalBackendModifyOperation.java |   16 ++-
 opendj-server-legacy/src/main/java/org/opends/server/util/LDIFReader.java                                          |   32 ++-----
 3 files changed, 109 insertions(+), 123 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/types/Entry.java b/opendj-server-legacy/src/main/java/org/opends/server/types/Entry.java
index bc97bc0..55cc922 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/types/Entry.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/types/Entry.java
@@ -1050,10 +1050,11 @@
    */
   public void incrementAttribute(Attribute attribute) throws DirectoryException
   {
-    Attribute a = getExactAttribute(attribute.getAttributeDescription());
+    AttributeDescription attrDesc = attribute.getAttributeDescription();
+    Attribute a = getExactAttribute(attrDesc);
     if (a == null)
     {
-      LocalizableMessage message = ERR_ENTRY_INCREMENT_NO_SUCH_ATTRIBUTE.get(attribute.getAttributeDescription());
+      LocalizableMessage message = ERR_ENTRY_INCREMENT_NO_SUCH_ATTRIBUTE.get(attrDesc);
       throw new DirectoryException(ResultCode.NO_SUCH_ATTRIBUTE, message);
     }
 
@@ -1061,25 +1062,24 @@
     Iterator<ByteString> i = attribute.iterator();
     if (!i.hasNext())
     {
-      LocalizableMessage message = ERR_ENTRY_INCREMENT_INVALID_VALUE_COUNT.get(attribute.getAttributeDescription());
+      LocalizableMessage message = ERR_ENTRY_INCREMENT_INVALID_VALUE_COUNT.get(attrDesc);
       throw new DirectoryException(ResultCode.CONSTRAINT_VIOLATION, message);
     }
 
     String incrementValue = i.next().toString();
-    long increment = parseLong(incrementValue, attribute);
+    long increment = parseLong(incrementValue, attrDesc);
 
     if (i.hasNext())
     {
-      LocalizableMessage message = ERR_ENTRY_INCREMENT_INVALID_VALUE_COUNT.get(attribute.getAttributeDescription());
+      LocalizableMessage message = ERR_ENTRY_INCREMENT_INVALID_VALUE_COUNT.get(attrDesc);
       throw new DirectoryException(ResultCode.CONSTRAINT_VIOLATION, message);
     }
 
     // Increment each attribute value by the specified amount.
     AttributeBuilder builder = new AttributeBuilder(a.getAttributeDescription());
-
     for (ByteString v : a)
     {
-      long currentValue = parseLong(v.toString(), attribute);
+      long currentValue = parseLong(v.toString(), attrDesc);
       long newValue = currentValue + increment;
       builder.add(String.valueOf(newValue));
     }
@@ -1087,7 +1087,7 @@
     replaceAttribute(builder.toAttribute());
   }
 
-  private long parseLong(String value, Attribute attribute) throws DirectoryException
+  private long parseLong(String value, AttributeDescription attrDesc) throws DirectoryException
   {
     try
     {
@@ -1095,12 +1095,11 @@
     }
     catch (NumberFormatException e)
     {
-      LocalizableMessage message = ERR_ENTRY_INCREMENT_CANNOT_PARSE_AS_INT.get(attribute.getAttributeDescription());
+      LocalizableMessage message = ERR_ENTRY_INCREMENT_CANNOT_PARSE_AS_INT.get(attrDesc);
       throw new DirectoryException(ResultCode.CONSTRAINT_VIOLATION, message);
     }
   }
 
-
   /**
    * Removes all instances of the specified attribute type from this
    * entry, including any instances with options. If the provided
@@ -1329,9 +1328,7 @@
   public void applyModification(Modification mod, boolean relaxConstraints)
          throws DirectoryException
   {
-    Attribute     a = mod.getAttribute();
-    AttributeType t = a.getAttributeDescription().getAttributeType();
-
+    AttributeType t = mod.getAttribute().getAttributeDescription().getAttributeType();
     if (t.isObjectClass())
     {
       applyModificationToObjectclass(mod, relaxConstraints);
@@ -1355,6 +1352,7 @@
       ocs.put(oc, ocName);
     }
 
+    AttributeDescription attrDesc = a.getAttributeDescription();
     switch (mod.getModificationType().asEnum())
     {
     case ADD:
@@ -1364,7 +1362,7 @@
         {
           if (!relaxConstraints)
           {
-            LocalizableMessage message = ERR_ENTRY_DUPLICATE_VALUES.get(a.getAttributeDescription());
+            LocalizableMessage message = ERR_ENTRY_DUPLICATE_VALUES.get(attrDesc);
             throw new DirectoryException(ATTRIBUTE_OR_VALUE_EXISTS, message);
           }
         }
@@ -1381,7 +1379,7 @@
       {
         if (objectClasses.remove(oc) == null && !relaxConstraints)
         {
-          LocalizableMessage message = ERR_ENTRY_NO_SUCH_VALUE.get(a.getAttributeDescription());
+          LocalizableMessage message = ERR_ENTRY_NO_SUCH_VALUE.get(attrDesc);
           throw new DirectoryException(NO_SUCH_ATTRIBUTE, message);
         }
       }
@@ -3526,11 +3524,12 @@
         }
         // Decode the attribute.
         Attribute a = config.getCompressedSchema().decodeAttribute(entryBuffer);
-        List<Attribute> attrList = attributes.get(a.getAttributeDescription().getAttributeType());
+        AttributeType attrType = a.getAttributeDescription().getAttributeType();
+        List<Attribute> attrList = attributes.get(attrType);
         if (attrList == null)
         {
           attrList = new ArrayList<>(1);
-          attributes.put(a.getAttributeDescription().getAttributeType(), attrList);
+          attributes.put(attrType, attrList);
         }
         attrList.add(a);
       }
@@ -4523,93 +4522,88 @@
       {
         continue;
       }
-      else
+
+      // If a non-default attribute name was provided or if the
+      // attribute has options then we will need to rebuild the
+      // attribute so that it contains the user-requested names and options.
+      final AttributeType subAttrType = subAttrDesc.getAttributeType();
+
+      if ((attrName != null && !attrName.equals(subAttrDesc.getNameOrOID()))
+          || attrDesc.hasOptions())
       {
-        // If a non-default attribute name was provided or if the
-        // attribute has options then we will need to rebuild the
-        // attribute so that it contains the user-requested names and options.
-        final AttributeType subAttrType = subAttrDesc.getAttributeType();
-
-        if ((attrName != null && !attrName.equals(subAttrDesc.getNameOrOID()))
-            || attrDesc.hasOptions())
+        // We want to use the user-provided name only if this attribute has
+        // the same type as the requested type. This might not be the case for
+        // sub-types e.g. requesting "name" and getting back "cn" - we don't
+        // want to rename "name" to "cn".
+        AttributeType attrType = attrDesc.getAttributeType();
+        AttributeDescription newAttrDesc;
+        if (attrName == null || !subAttrType.equals(attrType))
         {
-          // We want to use the user-provided name only if this attribute has
-          // the same type as the requested type. This might not be the case for
-          // sub-types e.g. requesting "name" and getting back "cn" - we don't
-          // want to rename "name" to "cn".
-          AttributeType attrType = attrDesc.getAttributeType();
-          AttributeDescription newAttrDesc;
-          if (attrName == null || !subAttrType.equals(attrType))
-          {
-            newAttrDesc = AttributeDescription.create(subAttrDesc.getNameOrOID(), subAttrDesc.getAttributeType());
-          }
-          else
-          {
-            newAttrDesc = AttributeDescription.create(attrName, subAttrDesc.getAttributeType());
-          }
-          AttributeBuilder builder = new AttributeBuilder(newAttrDesc);
-
-          builder.setOptions(attrDesc.getOptions());
-
-          // Now add in remaining options from original attribute
-          // (this will not overwrite options already present).
-          builder.setOptions(subAttrDesc.getOptions());
-
-          if (!omitValues)
-          {
-            builder.addAll(attribute);
-          }
-
-          attribute = builder.toAttribute();
-        }
-        else if (omitValues)
-        {
-          attribute = Attributes.empty(attribute);
-        }
-
-        // Now put the attribute into the destination map.
-        // Be careful of duplicates.
-        List<Attribute> attrList = destMap.get(subAttrType);
-
-        if (attrList == null)
-        {
-          // Assume that they'll all go in the one list. This isn't
-          // always the case, for example if the list contains sub-types.
-          attrList = new ArrayList<>(sourceList.size());
-          attrList.add(attribute);
-          destMap.put(subAttrType, attrList);
+          newAttrDesc = AttributeDescription.create(subAttrDesc.getNameOrOID(), subAttrDesc.getAttributeType());
         }
         else
         {
-          // The attribute may have already been put in the list.
-          //
-          // This may occur in two cases:
-          //
-          // 1) The attribute is identified by more than one attribute
-          //    type description in the attribute list (e.g. in a wildcard).
-          //
-          // 2) The attribute has both a real and virtual component.
-          //
-          boolean found = false;
-          for (int i = 0; i < attrList.size(); i++)
-          {
-            Attribute otherAttribute = attrList.get(i);
-            if (otherAttribute.getAttributeDescription().equals(subAttrDesc))
-            {
-              // Assume that wildcards appear first in an attribute
-              // list with more specific attribute names afterwards:
-              // let the attribute name and options from the later
-              // attribute take preference.
-              attrList.set(i, Attributes.merge(attribute, otherAttribute));
-              found = true;
-            }
-          }
+          newAttrDesc = AttributeDescription.create(attrName, subAttrDesc.getAttributeType());
+        }
 
-          if (!found)
+        AttributeBuilder builder = new AttributeBuilder(newAttrDesc);
+        builder.setOptions(attrDesc.getOptions());
+        // Now add in remaining options from original attribute
+        // (this will not overwrite options already present).
+        builder.setOptions(subAttrDesc.getOptions());
+        if (!omitValues)
+        {
+          builder.addAll(attribute);
+        }
+        attribute = builder.toAttribute();
+      }
+      else if (omitValues)
+      {
+        attribute = Attributes.empty(attribute);
+      }
+
+      // Now put the attribute into the destination map.
+      // Be careful of duplicates.
+      List<Attribute> attrList = destMap.get(subAttrType);
+
+      if (attrList == null)
+      {
+        // Assume that they'll all go in the one list. This isn't
+        // always the case, for example if the list contains sub-types.
+        attrList = new ArrayList<>(sourceList.size());
+        attrList.add(attribute);
+        destMap.put(subAttrType, attrList);
+      }
+      else
+      {
+        // The attribute may have already been put in the list.
+        //
+        // This may occur in two cases:
+        //
+        // 1) The attribute is identified by more than one attribute
+        //    type description in the attribute list (e.g. in a wildcard).
+        //
+        // 2) The attribute has both a real and virtual component.
+        //
+        boolean found = false;
+        for (int i = 0; i < attrList.size(); i++)
+        {
+          Attribute otherAttribute = attrList.get(i);
+          if (otherAttribute.getAttributeDescription().equals(subAttrDesc))
           {
-            attrList.add(attribute);
+            // Assume that wildcards appear first in an attribute
+            // list with more specific attribute names afterwards:
+            // let the attribute name and options from the later
+            // attribute take preference.
+            attrList.set(i, Attributes.merge(attribute, otherAttribute));
+            found = true;
           }
         }
+
+        if (!found)
+        {
+          attrList.add(attribute);
+        }
       }
     }
   }
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/util/LDIFReader.java b/opendj-server-legacy/src/main/java/org/opends/server/util/LDIFReader.java
index 38a0a5b..bb47ed2 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/util/LDIFReader.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/util/LDIFReader.java
@@ -770,25 +770,20 @@
   {
     // Parse the attribute type description.
     int colonPos = parseColonPosition(lines, line);
-    String attrDescr = line.substring(0, colonPos);
-    final AttributeDescription attrDesc = parseAttrDescription(attrDescr);
+    String attrDescStr = line.substring(0, colonPos);
+    final AttributeDescription attrDesc = parseAttrDescription(attrDescStr);
     final AttributeType attrType = attrDesc.getAttributeType();
-    final String attrName = attrType.getNameOrOID();
 
     // Now parse the attribute value.
-    ByteString value = parseSingleValue(lines, line, entryDN, colonPos, attrName);
+    ByteString value = parseSingleValue(lines, line, entryDN, colonPos, attrDescStr);
 
     // See if this is an objectclass or an attribute.  Then get the
     // corresponding definition and add the value to the appropriate hash.
-    if (attrName.equalsIgnoreCase("objectclass"))
+    if (attrType.isObjectClass())
     {
       if (! importConfig.includeObjectClasses())
       {
-        if (logger.isTraceEnabled())
-        {
-          logger.trace("Skipping objectclass %s for entry %s due to " +
-              "the import configuration.", value, entryDN);
-        }
+        logger.trace("Skipping objectclass %s for entry %s due to the import configuration.", value, entryDN);
         return;
       }
 
@@ -814,21 +809,16 @@
     {
       if (! importConfig.includeAttribute(attrType))
       {
-        if (logger.isTraceEnabled())
-        {
-          logger.trace("Skipping attribute %s for entry %s due to the " +
-              "import configuration.", attrName, entryDN);
-        }
+        logger.trace("Skipping attribute %s for entry %s due to the import configuration.", attrDescStr, entryDN);
         return;
       }
 
        //The attribute is not being ignored so check for binary option.
       if (checkSchema
           && !attrType.getSyntax().isBEREncodingRequired()
- && attrDesc.hasOption("binary"))
+          && attrDesc.hasOption("binary"))
       {
-        LocalizableMessage message = ERR_LDIF_INVALID_ATTR_OPTION.get(
-          entryDN, lastEntryLineNumber, attrName);
+        LocalizableMessage message = ERR_LDIF_INVALID_ATTR_OPTION.get(entryDN, lastEntryLineNumber, attrDescStr);
         logToRejectWriter(lines, message);
         throw new LDIFException(message, lastEntryLineNumber,true);
       }
@@ -839,7 +829,7 @@
         if (! attrType.getSyntax().valueIsAcceptable(value, invalidReason))
         {
           LocalizableMessage message = WARN_LDIF_VALUE_VIOLATES_SYNTAX.get(
-              entryDN, lastEntryLineNumber, value, attrName, invalidReason);
+              entryDN, lastEntryLineNumber, value, attrDescStr, invalidReason);
           if (DirectoryServer.getSyntaxEnforcementPolicy() == AcceptRejectWarn.WARN)
           {
             logger.error(message);
@@ -881,14 +871,14 @@
           if (!a.add(attributeValue) && checkSchema)
           {
               LocalizableMessage message = WARN_LDIF_DUPLICATE_ATTR.get(
-                  entryDN, lastEntryLineNumber, attrName, value);
+                  entryDN, lastEntryLineNumber, attrDescStr, value);
               logToRejectWriter(lines, message);
             throw new LDIFException(message, lastEntryLineNumber, true);
           }
           if (attrType.isSingleValue() && a.size() > 1 && checkSchema)
           {
             LocalizableMessage message = ERR_LDIF_MULTIPLE_VALUES_FOR_SINGLE_VALUED_ATTR
-                    .get(entryDN, lastEntryLineNumber, attrName);
+                    .get(entryDN, lastEntryLineNumber, attrDescStr);
             logToRejectWriter(lines, message);
             throw new LDIFException(message, lastEntryLineNumber, true);
           }
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/workflowelement/localbackend/LocalBackendModifyOperation.java b/opendj-server-legacy/src/main/java/org/opends/server/workflowelement/localbackend/LocalBackendModifyOperation.java
index 993626a..34dc518 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/workflowelement/localbackend/LocalBackendModifyOperation.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/workflowelement/localbackend/LocalBackendModifyOperation.java
@@ -1015,7 +1015,8 @@
       numPasswords = 0;
     }
 
-    AttributeBuilder builder = new AttributeBuilder(pwAttr.getAttributeDescription());
+    AttributeDescription pwdAttrDesc = pwAttr.getAttributeDescription();
+    AttributeBuilder builder = new AttributeBuilder(pwdAttrDesc);
     for (ByteString v : pwAttr)
     {
       if (pwPolicyState.passwordIsPreEncoded(v))
@@ -1029,7 +1030,7 @@
 
         // We still need to check if the pre-encoded password matches
         // an existing value, to decrease the number of passwords.
-        List<Attribute> attrList = currentEntry.getAttribute(pwAttr.getAttributeDescription().getAttributeType());
+        List<Attribute> attrList = currentEntry.getAttribute(pwdAttrDesc.getAttributeType());
         if (attrList.isEmpty())
         {
           throw new DirectoryException(ResultCode.NO_SUCH_ATTRIBUTE, ERR_MODIFY_NO_EXISTING_VALUES.get());
@@ -1042,7 +1043,7 @@
       }
       else
       {
-        List<Attribute> attrList = currentEntry.getAttribute(pwAttr.getAttributeDescription().getAttributeType());
+        List<Attribute> attrList = currentEntry.getAttribute(pwdAttrDesc.getAttributeType());
         if (attrList.isEmpty())
         {
           throw new DirectoryException(ResultCode.NO_SUCH_ATTRIBUTE, ERR_MODIFY_NO_EXISTING_VALUES.get());
@@ -1341,7 +1342,9 @@
 
     // If the attribute to be replaced is the object class attribute
     // then make sure that all the object classes are known and not obsoleted.
-    if (attr.getAttributeDescription().getAttributeType().isObjectClass())
+    AttributeDescription attrDesc = attr.getAttributeDescription();
+    AttributeType t = attrDesc.getAttributeType();
+    if (t.isObjectClass())
     {
       validateObjectClasses(attr);
     }
@@ -1350,14 +1353,13 @@
     modifiedEntry.replaceAttribute(attr);
 
     // Make sure that the RDN attribute value(s) has not been removed.
-    AttributeType t = attr.getAttributeDescription().getAttributeType();
     RDN rdn = modifiedEntry.getName().rdn();
     if (rdn != null
         && rdn.hasAttributeType(t)
-        && !modifiedEntry.hasValue(attr.getAttributeDescription(), rdn.getAttributeValue(t)))
+        && !modifiedEntry.hasValue(attrDesc, rdn.getAttributeValue(t)))
     {
       throw newDirectoryException(modifiedEntry, ResultCode.NOT_ALLOWED_ON_RDN,
-          ERR_MODIFY_DELETE_RDN_ATTR.get(entryDN, attr.getAttributeDescription()));
+          ERR_MODIFY_DELETE_RDN_ATTR.get(entryDN, attrDesc));
     }
   }
 

--
Gitblit v1.10.0