From 3d858950f392bf1f59c2e6de2f8534f07f11fc6e Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Wed, 19 Nov 2014 09:01:26 +0000
Subject: [PATCH] BaseDnRegistry.java: Code cleanup

---
 opendj3-server-dev/src/server/org/opends/server/core/BaseDnRegistry.java |  164 ++++++++++++++++++++++--------------------------------
 1 files changed, 68 insertions(+), 96 deletions(-)

diff --git a/opendj3-server-dev/src/server/org/opends/server/core/BaseDnRegistry.java b/opendj3-server-dev/src/server/org/opends/server/core/BaseDnRegistry.java
index 94798d3..57522eb 100644
--- a/opendj3-server-dev/src/server/org/opends/server/core/BaseDnRegistry.java
+++ b/opendj3-server-dev/src/server/org/opends/server/core/BaseDnRegistry.java
@@ -27,59 +27,58 @@
 
 package org.opends.server.core;
 
-import org.opends.server.types.DN;
-import org.opends.server.types.DirectoryException;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.opendj.ldap.ResultCode;
 import org.opends.server.api.Backend;
-import static org.forgerock.util.Reject.ifNull;
-import org.forgerock.i18n.LocalizableMessage;
+import org.opends.server.types.DN;
+import org.opends.server.types.DirectoryException;
+
+import static org.forgerock.util.Reject.*;
 import static org.opends.messages.CoreMessages.*;
 
-import java.util.TreeMap;
-import java.util.List;
-import java.util.LinkedList;
-import java.util.Map;
-
 /**
- * Registry for maintaining the set of registered base DN's, assocated
- * backends and naming context information.
+ * Registry for maintaining the set of registered base DN's, associated backends
+ * and naming context information.
  */
 public class BaseDnRegistry {
 
-  // The set of base DNs registered with the server.
-  private TreeMap<DN,Backend> baseDNs;
+  /** The set of base DNs registered with the server. */
+  private final TreeMap<DN, Backend> baseDNs = new TreeMap<DN, Backend>();
 
-  // The set of private naming contexts registered with the server.
-  private TreeMap<DN,Backend> privateNamingContexts;
+  /** The set of private naming contexts registered with the server. */
+  private final TreeMap<DN, Backend> privateNamingContexts = new TreeMap<DN, Backend>();
 
-  // The set of public naming contexts registered with the server.
-  private TreeMap<DN,Backend> publicNamingContexts;
+  /** The set of public naming contexts registered with the server. */
+  private final TreeMap<DN, Backend> publicNamingContexts = new TreeMap<DN, Backend>();
 
-  // Indicates whether or not this base DN registry is in test mode.
-  // A registry instance that is in test mode will not modify backend
-  // objects referred to in the above maps.
+  /**
+   * Indicates whether or not this base DN registry is in test mode.
+   * A registry instance that is in test mode will not modify backend
+   * objects referred to in the above maps.
+   */
   private boolean testOnly;
 
   /**
    * Registers a base DN with this registry.
    *
    * @param  baseDN to register
-   * @param  backend with which the base DN is assocated
+   * @param  backend with which the base DN is associated
    * @param  isPrivate indicates whether or not this base DN is private
    * @return list of error messages generated by registering the base DN
    *         that should be logged if the changes to this registry are
    *         committed to the server
    * @throws DirectoryException if the base DN cannot be registered
    */
-  public List<LocalizableMessage> registerBaseDN(DN baseDN, Backend backend,
-                                      boolean isPrivate)
-          throws DirectoryException
+  public List<LocalizableMessage> registerBaseDN(DN baseDN, Backend<?> backend, boolean isPrivate)
+      throws DirectoryException
   {
-
-    List<LocalizableMessage> errors = new LinkedList<LocalizableMessage>();
-
     // Check to see if the base DN is already registered with the server.
-    Backend existingBackend = baseDNs.get(baseDN);
+    Backend<?> existingBackend = baseDNs.get(baseDN);
     if (existingBackend != null)
     {
       LocalizableMessage message = ERR_REGISTER_BASEDN_ALREADY_EXISTS.
@@ -94,7 +93,7 @@
     LinkedList<DN> otherBaseDNs = new LinkedList<DN>();
     for (DN dn : baseDNs.keySet())
     {
-      Backend b = baseDNs.get(dn);
+      Backend<?> b = baseDNs.get(dn);
       if (b.equals(backend))
       {
         otherBaseDNs.add(dn);
@@ -112,8 +111,8 @@
     // Check to see if the new base DN is subordinate to any other base DN
     // already defined.  If it is, then any other base DN(s) for the same
     // backend must also be subordinate to the same base DN.
-    Backend superiorBackend = null;
-    DN      superiorBaseDN        ;
+    Backend<?> superiorBackend = null;
+    DN      superiorBaseDN         ;
     DN      parentDN        = baseDN.parent();
     while (parentDN != null)
     {
@@ -138,24 +137,21 @@
       parentDN = parentDN.parent();
     }
 
-    if (superiorBackend == null)
+    if (superiorBackend == null && backend.getParentBackend() != null)
     {
-      if (backend.getParentBackend() != null)
-      {
-        LocalizableMessage message = ERR_REGISTER_BASEDN_NEW_BASE_NOT_SUBORDINATE.
-            get(baseDN, backend.getBackendID(), backend.getParentBackend().getBackendID());
-        throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
-      }
+      LocalizableMessage message = ERR_REGISTER_BASEDN_NEW_BASE_NOT_SUBORDINATE.
+          get(baseDN, backend.getBackendID(), backend.getParentBackend().getBackendID());
+      throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
     }
 
 
     // Check to see if the new base DN should be the superior base DN for any
     // other base DN(s) already defined.
-    LinkedList<Backend> subordinateBackends = new LinkedList<Backend>();
+    LinkedList<Backend<?>> subordinateBackends = new LinkedList<Backend<?>>();
     LinkedList<DN>      subordinateBaseDNs  = new LinkedList<DN>();
     for (DN dn : baseDNs.keySet())
     {
-      Backend b = baseDNs.get(dn);
+      Backend<?> b = baseDNs.get(dn);
       parentDN = dn.parent();
       while (parentDN != null)
       {
@@ -177,6 +173,7 @@
 
     // If we've gotten here, then the new base DN is acceptable.  If we should
     // actually apply the changes then do so now.
+    final List<LocalizableMessage> errors = new LinkedList<LocalizableMessage>();
 
     // Check to see if any of the registered backends already contain an
     // entry with the DN specified as the base DN.  This could happen if
@@ -187,13 +184,10 @@
     // being registered, but it's definitely important enough that we let
     // the administrator know about it and remind them that the existing
     // backend will need to be reinitialized.
-    if (superiorBackend != null)
+    if (superiorBackend != null && superiorBackend.entryExists(baseDN))
     {
-      if (superiorBackend.entryExists(baseDN))
-      {
-        errors.add(WARN_REGISTER_BASEDN_ENTRIES_IN_MULTIPLE_BACKENDS.
-            get(superiorBackend.getBackendID(), baseDN, backend.getBackendID()));
-      }
+      errors.add(WARN_REGISTER_BASEDN_ENTRIES_IN_MULTIPLE_BACKENDS.
+          get(superiorBackend.getBackendID(), baseDN, backend.getBackendID()));
     }
 
 
@@ -201,37 +195,31 @@
 
     if (superiorBackend == null)
     {
+      if (!testOnly)
+      {
+        backend.setPrivateBackend(isPrivate);
+      }
+
       if (isPrivate)
       {
-        if (!testOnly)
-        {
-          backend.setPrivateBackend(true);
-        }
         privateNamingContexts.put(baseDN, backend);
       }
       else
       {
-        if (!testOnly)
-        {
-          backend.setPrivateBackend(false);
-        }
         publicNamingContexts.put(baseDN, backend);
       }
     }
-    else if (otherBaseDNs.isEmpty())
+    else if (otherBaseDNs.isEmpty() && !testOnly)
     {
-      if (!testOnly)
-      {
-        backend.setParentBackend(superiorBackend);
-        superiorBackend.addSubordinateBackend(backend);
-      }
+      backend.setParentBackend(superiorBackend);
+      superiorBackend.addSubordinateBackend(backend);
     }
 
     if (!testOnly)
     {
-      for (Backend b : subordinateBackends)
+      for (Backend<?> b : subordinateBackends)
       {
-        Backend oldParentBackend = b.getParentBackend();
+        Backend<?> oldParentBackend = b.getParentBackend();
         if (oldParentBackend != null)
         {
           oldParentBackend.removeSubordinateBackend(b);
@@ -264,13 +252,11 @@
   public List<LocalizableMessage> deregisterBaseDN(DN baseDN)
          throws DirectoryException
   {
-    LinkedList<LocalizableMessage> errors = new LinkedList<LocalizableMessage>();
-
     ifNull(baseDN);
 
     // Make sure that the Directory Server actually contains a backend with
     // the specified base DN.
-    Backend backend = baseDNs.get(baseDN);
+    Backend<?> backend = baseDNs.get(baseDN);
     if (backend == null)
     {
       LocalizableMessage message =
@@ -281,11 +267,11 @@
 
     // Check to see if the backend has a parent backend, and whether it has
     // any subordinates with base DNs that are below the base DN to remove.
-    Backend             superiorBackend     = backend.getParentBackend();
-    LinkedList<Backend> subordinateBackends = new LinkedList<Backend>();
+    Backend<?>             superiorBackend     = backend.getParentBackend();
+    LinkedList<Backend<?>> subordinateBackends = new LinkedList<Backend<?>>();
     if (backend.getSubordinateBackends() != null)
     {
-      for (Backend b : backend.getSubordinateBackends())
+      for (Backend<?> b : backend.getSubordinateBackends())
       {
         for (DN dn : b.getBaseDNs())
         {
@@ -308,7 +294,7 @@
         continue;
       }
 
-      Backend b = baseDNs.get(dn);
+      Backend<?> b = baseDNs.get(dn);
       if (backend.equals(b))
       {
         otherBaseDNs.add(dn);
@@ -324,11 +310,12 @@
     publicNamingContexts.remove(baseDN);
     privateNamingContexts.remove(baseDN);
 
+    final LinkedList<LocalizableMessage> errors = new LinkedList<LocalizableMessage>();
     if (superiorBackend == null)
     {
       // If there were any subordinate backends, then all of their base DNs
       // will now be promoted to naming contexts.
-      for (Backend b : subordinateBackends)
+      for (Backend<?> b : subordinateBackends)
       {
         if (!testOnly)
         {
@@ -353,12 +340,9 @@
     {
       // If there are no other base DNs for the associated backend, then
       // remove this backend as a subordinate of the parent backend.
-      if (otherBaseDNs.isEmpty())
+      if (otherBaseDNs.isEmpty() && !testOnly)
       {
-        if (!testOnly)
-        {
-          superiorBackend.removeSubordinateBackend(backend);
-        }
+        superiorBackend.removeSubordinateBackend(backend);
       }
 
 
@@ -376,7 +360,7 @@
 
         if (!testOnly)
         {
-          for (Backend b : subordinateBackends)
+          for (Backend<?> b : subordinateBackends)
           {
             backend.removeSubordinateBackend(b);
             superiorBackend.addSubordinateBackend(b);
@@ -394,8 +378,7 @@
    */
   BaseDnRegistry()
   {
-    this(new TreeMap<DN,Backend>(), new TreeMap<DN,Backend>(),
-         new TreeMap<DN,Backend>(), false);
+    this(false);
   }
 
   /**
@@ -405,31 +388,22 @@
    */
   BaseDnRegistry copy()
   {
-    return new BaseDnRegistry(
-            new TreeMap<DN,Backend>(baseDNs),
-            new TreeMap<DN,Backend>(publicNamingContexts),
-            new TreeMap<DN,Backend>(privateNamingContexts),
-            true);
+    final BaseDnRegistry registry = new BaseDnRegistry(true);
+    registry.baseDNs.putAll(baseDNs);
+    registry.publicNamingContexts.putAll(publicNamingContexts);
+    registry.privateNamingContexts.putAll(privateNamingContexts);
+    return registry;
   }
 
 
   /**
    * Creates a parameterized instance.
    *
-   * @param baseDNs map
-   * @param publicNamingContexts map
-   * @param privateNamingContexts map
    * @param testOnly indicates whether this registry will be used for testing;
    *        when <code>true</code> this registry will not modify backends
    */
-  private BaseDnRegistry(TreeMap<DN, Backend> baseDNs,
-                         TreeMap<DN, Backend> publicNamingContexts,
-                         TreeMap<DN, Backend> privateNamingContexts,
-                         boolean testOnly)
+  private BaseDnRegistry(boolean testOnly)
   {
-    this.baseDNs = baseDNs;
-    this.publicNamingContexts = publicNamingContexts;
-    this.privateNamingContexts = privateNamingContexts;
     this.testOnly = testOnly;
   }
 
@@ -479,8 +453,7 @@
    */
   boolean containsNamingContext(DN dn)
   {
-    return (privateNamingContexts.containsKey(dn) ||
-            publicNamingContexts.containsKey(dn));
+    return privateNamingContexts.containsKey(dn) || publicNamingContexts.containsKey(dn);
   }
 
 
@@ -503,7 +476,6 @@
     {
       publicNamingContexts.clear();
     }
-
   }
 
 }

--
Gitblit v1.10.0