From e60ac25c05d4474854317b71586f022bab93e0d8 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Mon, 07 Mar 2016 14:49:02 +0000
Subject: [PATCH] Prep work for OPENDJ-1342: align APIs for DNs: added DN.toUUID()

---
 opendj-server-legacy/src/main/java/org/opends/server/extensions/EntryUUIDVirtualAttributeProvider.java         |   21 +---------
 opendj-server-legacy/src/test/java/org/opends/server/extensions/EntryUUIDVirtualAttributeProviderTestCase.java |   15 ++-----
 opendj-server-legacy/src/main/java/org/opends/server/types/DN.java                                             |   12 ++++++
 opendj-server-legacy/src/main/java/org/opends/server/plugins/EntryUUIDPlugin.java                              |   32 +++-------------
 opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/EntryHistorical.java                   |    3 -
 5 files changed, 27 insertions(+), 56 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/extensions/EntryUUIDVirtualAttributeProvider.java b/opendj-server-legacy/src/main/java/org/opends/server/extensions/EntryUUIDVirtualAttributeProvider.java
index bd80515..2988c63 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/extensions/EntryUUIDVirtualAttributeProvider.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/extensions/EntryUUIDVirtualAttributeProvider.java
@@ -12,14 +12,13 @@
  * information: "Portions Copyright [year] [name of copyright owner]".
  *
  * Copyright 2008-2009 Sun Microsystems, Inc.
- * Portions Copyright 2012-2015 ForgeRock AS.
+ * Portions Copyright 2012-2016 ForgeRock AS.
  */
 package org.opends.server.extensions;
 
 import static org.opends.messages.ExtensionMessages.*;
 
 import java.util.List;
-import java.util.UUID;
 
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.opendj.ldap.ByteString;
@@ -44,9 +43,7 @@
 public class EntryUUIDVirtualAttributeProvider
        extends VirtualAttributeProvider<EntryUUIDVirtualAttributeCfg>
 {
-  /**
-   * Creates a new instance of this entryUUID virtual attribute provider.
-   */
+  /** Creates a new instance of this entryUUID virtual attribute provider. */
   public EntryUUIDVirtualAttributeProvider()
   {
     super();
@@ -55,23 +52,18 @@
     // initializeVirtualAttributeProvider method.
   }
 
-  /** {@inheritDoc} */
   @Override
   public boolean isMultiValued()
   {
     return false;
   }
 
-  /** {@inheritDoc} */
   @Override
   public Attribute getValues(Entry entry, VirtualAttributeRule rule)
   {
-    ByteString normDN = entry.getName().toNormalizedByteString();
-    String uuid = UUID.nameUUIDFromBytes(normDN.toByteArray()).toString();
-    return Attributes.create(rule.getAttributeType(), uuid);
+    return Attributes.create(rule.getAttributeType(), entry.getName().toUUID().toString());
   }
 
-  /** {@inheritDoc} */
   @Override
   public boolean hasValue(Entry entry, VirtualAttributeRule rule)
   {
@@ -79,7 +71,6 @@
     return true;
   }
 
-  /** {@inheritDoc} */
   @Override
   public ConditionResult matchesSubstring(Entry entry,
                                           VirtualAttributeRule rule,
@@ -91,7 +82,6 @@
     return ConditionResult.UNDEFINED;
   }
 
-  /** {@inheritDoc} */
   @Override
   public ConditionResult greaterThanOrEqualTo(Entry entry,
                               VirtualAttributeRule rule,
@@ -101,7 +91,6 @@
     return ConditionResult.UNDEFINED;
   }
 
-  /** {@inheritDoc} */
   @Override
   public ConditionResult lessThanOrEqualTo(Entry entry,
                               VirtualAttributeRule rule,
@@ -111,7 +100,6 @@
     return ConditionResult.UNDEFINED;
   }
 
-  /** {@inheritDoc} */
   @Override
   public ConditionResult approximatelyEqualTo(Entry entry,
                               VirtualAttributeRule rule,
@@ -121,7 +109,6 @@
     return ConditionResult.UNDEFINED;
   }
 
-  /** {@inheritDoc} */
   @Override
   public boolean isSearchable(VirtualAttributeRule rule,
                               SearchOperation searchOperation,
@@ -130,7 +117,6 @@
     return false;
   }
 
-  /** {@inheritDoc} */
   @Override
   public void processSearch(VirtualAttributeRule rule,
                             SearchOperation searchOperation)
@@ -142,4 +128,3 @@
     searchOperation.appendErrorMessage(message);
   }
 }
-
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/plugins/EntryUUIDPlugin.java b/opendj-server-legacy/src/main/java/org/opends/server/plugins/EntryUUIDPlugin.java
index e2e9b9f..923e63a 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/plugins/EntryUUIDPlugin.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/plugins/EntryUUIDPlugin.java
@@ -26,6 +26,7 @@
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.opendj.config.server.ConfigChangeResult;
 import org.forgerock.opendj.config.server.ConfigException;
+import org.forgerock.opendj.ldap.schema.AttributeType;
 import org.opends.server.admin.server.ConfigurationChangeListener;
 import org.opends.server.admin.std.meta.PluginCfgDefn;
 import org.opends.server.admin.std.server.EntryUUIDPluginCfg;
@@ -34,8 +35,10 @@
 import org.opends.server.api.plugin.PluginResult;
 import org.opends.server.api.plugin.PluginType;
 import org.opends.server.core.DirectoryServer;
-import org.forgerock.opendj.ldap.schema.AttributeType;
-import org.opends.server.types.*;
+import org.opends.server.types.Attribute;
+import org.opends.server.types.Attributes;
+import org.opends.server.types.Entry;
+import org.opends.server.types.LDIFImportConfig;
 import org.opends.server.types.operation.PreOperationAddOperation;
 
 /**
@@ -89,17 +92,12 @@
     }
   }
 
-
-
-  /** {@inheritDoc} */
   @Override
   public final void finalizePlugin()
   {
     currentConfig.removeEntryUUIDChangeListener(this);
   }
 
-
-
   @Override
   public final PluginResult.ImportLDIF
                doLDIFImport(LDIFImportConfig importConfig, Entry entry)
@@ -112,13 +110,10 @@
       return PluginResult.ImportLDIF.continueEntryProcessing();
     }
 
-
     // Construct a new UUID.  In order to make sure that UUIDs are consistent
     // when the same LDIF is generated on multiple servers, we'll base the UUID
     // on the byte representation of the normalized DN.
-    byte[] dnBytes = entry.getName().toNormalizedByteString().toByteArray();
-    UUID uuid = UUID.nameUUIDFromBytes(dnBytes);
-
+    UUID uuid = entry.getName().toUUID();
     uuidList = Attributes.createAsList(entryUUIDType, uuid.toString());
     entry.putAttribute(entryUUIDType, uuidList);
 
@@ -126,9 +121,6 @@
     return PluginResult.ImportLDIF.continueEntryProcessing();
   }
 
-
-
-  /** {@inheritDoc} */
   @Override
   public final PluginResult.PreOperation
                doPreOperation(PreOperationAddOperation addOperation)
@@ -144,7 +136,6 @@
       return PluginResult.PreOperation.continueOperationProcessing();
     }
 
-
     // Construct a new random UUID.
     UUID uuid = UUID.randomUUID();
     uuidList = Attributes.createAsList(entryUUIDType, uuid.toString());
@@ -154,9 +145,6 @@
     return PluginResult.PreOperation.continueOperationProcessing();
   }
 
-
-
-  /** {@inheritDoc} */
   @Override
   public boolean isConfigurationAcceptable(PluginCfg configuration,
                                            List<LocalizableMessage> unacceptableReasons)
@@ -165,9 +153,6 @@
     return isConfigurationChangeAcceptable(cfg, unacceptableReasons);
   }
 
-
-
-  /** {@inheritDoc} */
   @Override
   public boolean isConfigurationChangeAcceptable(
                       EntryUUIDPluginCfg configuration,
@@ -186,7 +171,6 @@
           // These are acceptable.
           break;
 
-
         default:
           unacceptableReasons.add(ERR_PLUGIN_ENTRYUUID_INVALID_PLUGIN_TYPE.get(pluginType));
           configAcceptable = false;
@@ -196,9 +180,6 @@
     return configAcceptable;
   }
 
-
-
-  /** {@inheritDoc} */
   @Override
   public ConfigChangeResult applyConfigurationChange(
                                  EntryUUIDPluginCfg configuration)
@@ -207,4 +188,3 @@
     return new ConfigChangeResult();
   }
 }
-
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/EntryHistorical.java b/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/EntryHistorical.java
index 175d126..85f509f 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/EntryHistorical.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/EntryHistorical.java
@@ -24,7 +24,6 @@
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
-import java.util.UUID;
 
 import org.forgerock.i18n.slf4j.LocalizedLogger;
 import org.forgerock.opendj.ldap.AttributeDescription;
@@ -764,6 +763,6 @@
           entryDN);
     }
 
-    return UUID.nameUUIDFromBytes(entryDN.toNormalizedByteString().toByteArray()).toString();
+    return entryDN.toUUID().toString();
   }
 }
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 a5004ee..9654771 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
@@ -27,6 +27,7 @@
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.UUID;
 
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
@@ -2546,4 +2547,15 @@
   {
     return Arrays.asList(rdnComponents).iterator();
   }
+
+  /**
+   * Returns a UUID whose content is based on the normalized content of this DN. Two equivalent DNs
+   * subject to the same schema will always yield the same UUID.
+   *
+   * @return the UUID representing this DN
+   */
+  public UUID toUUID()
+  {
+    return UUID.nameUUIDFromBytes(toNormalizedByteString().toByteArray());
+  }
 }
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/extensions/EntryUUIDVirtualAttributeProviderTestCase.java b/opendj-server-legacy/src/test/java/org/opends/server/extensions/EntryUUIDVirtualAttributeProviderTestCase.java
index 39d08a1..79fe468 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/extensions/EntryUUIDVirtualAttributeProviderTestCase.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/extensions/EntryUUIDVirtualAttributeProviderTestCase.java
@@ -18,7 +18,6 @@
 
 import java.util.Collections;
 import java.util.List;
-import java.util.UUID;
 
 import org.forgerock.opendj.ldap.ByteString;
 import org.forgerock.opendj.ldap.SearchScope;
@@ -42,7 +41,6 @@
 import static org.opends.server.protocols.internal.InternalClientConnection.*;
 import static org.opends.server.protocols.internal.Requests.*;
 import static org.opends.server.util.ServerConstants.*;
-import static org.opends.server.util.StaticUtils.*;
 import static org.testng.Assert.*;
 
 /**
@@ -112,7 +110,7 @@
   public void testGetEntry(DN entryDN)
          throws Exception
   {
-    String uuidString = UUID.nameUUIDFromBytes(entryDN.toNormalizedByteString().toByteArray()).toString();
+    String uuidString = entryDN.toUUID().toString();
 
     Entry e = DirectoryServer.getEntry(entryDN);
     assertNotNull(e);
@@ -141,8 +139,7 @@
   public void testGetUserEntry()
          throws Exception
   {
-    String uuidString =
-         UUID.nameUUIDFromBytes(getBytes("dc=example,dc=com")).toString();
+    String uuidString = DN.valueOf("dc=example,dc=com").toUUID().toString();
 
     TestCaseUtils.clearBackend("userRoot");
 
@@ -287,7 +284,7 @@
   public void testSearchEntryUUIDAttrInMatchingFilter(DN entryDN)
          throws Exception
   {
-    String uuidString = UUID.nameUUIDFromBytes(entryDN.toNormalizedByteString().toByteArray()).toString();
+    String uuidString = entryDN.toUUID().toString();
 
     final SearchRequest request = newSearchRequest(entryDN, SearchScope.BASE_OBJECT, "(entryUUID=" + uuidString + ")")
         .addAttribute("entryuuid");
@@ -299,8 +296,6 @@
     assertTrue(e.hasAttribute(entryUUIDType));
   }
 
-
-
   /**
    * Performs an internal search to retrieve the specified entry, ensuring that
    * no entries are returned when the entryUUID attribute is used in the search
@@ -395,7 +390,7 @@
   @Test
   public void testGetValues() throws Exception
   {
-    String uuidString = UUID.nameUUIDFromBytes(getBytes("o=test")).toString();
+    String uuidString = DN.valueOf("o=test").toUUID().toString();
 
     EntryUUIDVirtualAttributeProvider provider =
          new EntryUUIDVirtualAttributeProvider();
@@ -437,7 +432,7 @@
   @Test
   public void testHasMatchingValue() throws Exception
   {
-    String uuidString = UUID.nameUUIDFromBytes(getBytes("o=test")).toString();
+    String uuidString = DN.valueOf("o=test").toUUID().toString();
 
     EntryUUIDVirtualAttributeProvider provider =
          new EntryUUIDVirtualAttributeProvider();

--
Gitblit v1.10.0