From 8c48b025410ca522f8c26a82358500567e5cab85 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Tue, 12 Jul 2016 10:32:09 +0000
Subject: [PATCH] dsreplication: code cleanup

---
 opendj-server-legacy/src/main/java/org/opends/server/tools/dsreplication/ReplicationCliMain.java    |   20 +-----
 opendj-server-legacy/src/main/java/org/opends/admin/ads/ReplicaDescriptor.java                      |   12 ++--
 opendj-server-legacy/src/main/java/org/opends/admin/ads/SuffixDescriptor.java                       |   16 +++++
 opendj-server-legacy/src/main/java/org/opends/quicksetup/installer/Installer.java                   |   24 ++++----
 opendj-server-legacy/src/main/java/org/opends/quicksetup/installer/ui/SuffixesToReplicatePanel.java |   14 ++--
 opendj-server-legacy/src/main/java/org/opends/quicksetup/installer/InstallerHelper.java             |   15 ----
 opendj-server-legacy/src/main/java/org/opends/admin/ads/ServerDescriptor.java                       |   65 ++++++++++-----------
 7 files changed, 79 insertions(+), 87 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/admin/ads/ReplicaDescriptor.java b/opendj-server-legacy/src/main/java/org/opends/admin/ads/ReplicaDescriptor.java
index 5b78e81..e160f10 100644
--- a/opendj-server-legacy/src/main/java/org/opends/admin/ads/ReplicaDescriptor.java
+++ b/opendj-server-legacy/src/main/java/org/opends/admin/ads/ReplicaDescriptor.java
@@ -30,7 +30,7 @@
   private int replicationId = -1;
   private int missingChanges = -1;
   private long ageOfOldestMissingChange = -1;
-  private String backendName;
+  private String backendId;
   private Set<String> objectClasses;
 
   /**
@@ -200,18 +200,18 @@
    * Returns the name of the backend where this replica is defined.
    * @return the name of the backend where this replica is defined.
    */
-  public String getBackendName()
+  public String getBackendId()
   {
-    return backendName;
+    return backendId;
   }
 
   /**
    * Sets the name of the backend where this replica is defined.
-   * @param backendName the name of the backend.
+   * @param backendId the name of the backend.
    */
-  public void setBackendName(String backendName)
+  public void setBackendId(String backendId)
   {
-    this.backendName = backendName;
+    this.backendId = backendId;
   }
 
   /**
diff --git a/opendj-server-legacy/src/main/java/org/opends/admin/ads/ServerDescriptor.java b/opendj-server-legacy/src/main/java/org/opends/admin/ads/ServerDescriptor.java
index b4a8ddd..ab7225e 100644
--- a/opendj-server-legacy/src/main/java/org/opends/admin/ads/ServerDescriptor.java
+++ b/opendj-server-legacy/src/main/java/org/opends/admin/ads/ServerDescriptor.java
@@ -19,7 +19,6 @@
 import static org.forgerock.opendj.ldap.SearchScope.*;
 import static org.forgerock.opendj.ldap.requests.Requests.*;
 import static org.opends.admin.ads.util.ConnectionUtils.*;
-import static org.opends.server.util.CollectionUtils.*;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -768,15 +767,15 @@
       {
         SearchResultEntry sr = entryReader.readEntry();
 
-        String id = firstValueAsString(sr, "ds-cfg-backend-id");
-        if (!isConfigBackend(id) || isSchemaBackend(id))
+        String backendId = firstValueAsString(sr, "ds-cfg-backend-id");
+        if (!isConfigBackend(backendId) || isSchemaBackend(backendId))
         {
           Set<DN> baseDns = asSetOfDN(sr, "ds-cfg-base-dn");
 
           Set<String> entries;
           if (cacheFilter.searchMonitoringInformation())
           {
-            entries = getBaseDNEntryCount(conn, id);
+            entries = getBaseDNEntryCount(conn, backendId);
           }
           else
           {
@@ -791,36 +790,11 @@
               ReplicaDescriptor replica = new ReplicaDescriptor();
               replica.setServer(desc);
               replica.setObjectClasses(asSetOfString(sr, ConfigConstants.ATTR_OBJECTCLASS));
-              replica.setBackendName(id);
+              replica.setBackendId(backendId);
+              replica.setSuffix(new SuffixDescriptor(baseDn, replica));
+              replica.setEntries(getNumberOfEntriesForBaseDn(entries, baseDn));
+
               replicas.add(replica);
-
-              SuffixDescriptor suffix = new SuffixDescriptor();
-              suffix.setDN(baseDn);
-              suffix.setReplicas(newHashSet(replica));
-              replica.setSuffix(suffix);
-
-              int nEntries = -1;
-              for (String s : entries)
-              {
-                int index = s.indexOf(" ");
-                if (index != -1)
-                {
-                  DN dn = DN.valueOf(s.substring(index + 1));
-                  if (baseDn.equals(dn))
-                  {
-                    try
-                    {
-                      nEntries = Integer.parseInt(s.substring(0, index));
-                    }
-                    catch (Throwable t)
-                    {
-                      /* Ignore */
-                    }
-                    break;
-                  }
-                }
-              }
-              replica.setEntries(nEntries);
             }
           }
           desc.setReplicas(replicas);
@@ -829,6 +803,31 @@
     }
   }
 
+  private static int getNumberOfEntriesForBaseDn(Set<String> entries, DN baseDn)
+  {
+    for (String s : entries)
+    {
+      int index = s.indexOf(" ");
+      if (index != -1)
+      {
+        DN dn = DN.valueOf(s.substring(index + 1));
+        if (baseDn.equals(dn))
+        {
+          try
+          {
+            return Integer.parseInt(s.substring(0, index));
+          }
+          catch (Throwable t)
+          {
+            /* Ignore */
+          }
+          break;
+        }
+      }
+    }
+    return -1;
+  }
+
   private static boolean isAddReplica(TopologyCacheFilter cacheFilter, DN baseDn)
   {
     if (cacheFilter.searchAllBaseDNs())
diff --git a/opendj-server-legacy/src/main/java/org/opends/admin/ads/SuffixDescriptor.java b/opendj-server-legacy/src/main/java/org/opends/admin/ads/SuffixDescriptor.java
index 1bd7b4e..abda665 100644
--- a/opendj-server-legacy/src/main/java/org/opends/admin/ads/SuffixDescriptor.java
+++ b/opendj-server-legacy/src/main/java/org/opends/admin/ads/SuffixDescriptor.java
@@ -16,6 +16,8 @@
  */
 package org.opends.admin.ads;
 
+import static java.util.Collections.*;
+
 import java.util.HashSet;
 import java.util.Set;
 
@@ -32,6 +34,20 @@
   private final Set<ReplicaDescriptor> replicas = new HashSet<>();
 
   /**
+   * Builds a new SuffixDescriptor.
+   *
+   * @param suffixDn
+   *          the suffix DN
+   * @param replica
+   *          the replica
+   */
+  public SuffixDescriptor(DN suffixDn, ReplicaDescriptor replica)
+  {
+    this.suffixDN = suffixDn;
+    setReplicas(singleton(replica));
+  }
+
+  /**
    * Returns the DN associated with this suffix descriptor.
    *
    * @return the DN associated with this suffix descriptor.
diff --git a/opendj-server-legacy/src/main/java/org/opends/quicksetup/installer/Installer.java b/opendj-server-legacy/src/main/java/org/opends/quicksetup/installer/Installer.java
index 3af5943..c0b1875 100644
--- a/opendj-server-legacy/src/main/java/org/opends/quicksetup/installer/Installer.java
+++ b/opendj-server-legacy/src/main/java/org/opends/quicksetup/installer/Installer.java
@@ -1866,7 +1866,7 @@
     notifyListeners(getFormattedWithPoints(INFO_PROGRESS_CREATING_REPLICATED_BACKENDS.get()));
 
     // The keys are the backend IDs and the values the list of base DNs.
-    final Map<String, Set<String>> hmBackendSuffix = new HashMap<>();
+    final Map<String, Set<DN>> hmBackendSuffix = new HashMap<>();
     final SuffixesToReplicateOptions suffixData = getUserData().getSuffixesToReplicateOptions();
     populateBackendsToCreate(hmBackendSuffix, suffixData.getSuffixes());
     createReplicatedBackends(hmBackendSuffix, suffixData.getSuffixBackendTypes());
@@ -1879,7 +1879,7 @@
    * configuration of the other server. The algorithm consists on putting the
    * remote servers in a list and pick the backend as they appear on the list.
    */
-  private void populateBackendsToCreate(Map<String, Set<String>> hmBackendSuffix, Set<SuffixDescriptor> suffixes)
+  private void populateBackendsToCreate(Map<String, Set<DN>> hmBackendSuffix, Set<SuffixDescriptor> suffixes)
   {
     Set<ServerDescriptor> serverList = getServerListFromSuffixes(suffixes);
     for (SuffixDescriptor suffix : suffixes)
@@ -1887,8 +1887,8 @@
       final ReplicaDescriptor replica = retrieveReplicaForSuffix(serverList, suffix);
       if (replica != null)
       {
-        final String backendNameKey = getOrAddBackend(hmBackendSuffix, replica.getBackendName());
-        hmBackendSuffix.get(backendNameKey).add(suffix.getDN().toString());
+        final String backendId = getOrAddBackend(hmBackendSuffix, replica.getBackendId());
+        hmBackendSuffix.get(backendId).add(suffix.getDN());
       }
     }
   }
@@ -1921,29 +1921,29 @@
     return null;
   }
 
-  private String getOrAddBackend(Map<String, Set<String>> hmBackendSuffix, String backendName)
+  private String getOrAddBackend(Map<String, Set<DN>> hmBackendSuffix, String backendId)
   {
     for (String storedBackend : hmBackendSuffix.keySet())
     {
-      if (storedBackend.equalsIgnoreCase(backendName))
+      if (storedBackend.equalsIgnoreCase(backendId))
       {
         return storedBackend;
       }
     }
-    hmBackendSuffix.put(backendName, new HashSet<String>());
-    return backendName;
+    hmBackendSuffix.put(backendId, new HashSet<DN>());
+    return backendId;
   }
 
-  private void createReplicatedBackends(final Map<String, Set<String>> hmBackendSuffix,
+  private void createReplicatedBackends(final Map<String, Set<DN>> hmBackendSuffix,
       final Map<String, BackendTypeUIAdapter> backendTypes) throws ApplicationException
   {
     try (ConnectionWrapper connection = createLocalConnection())
     {
       final InstallerHelper helper = new InstallerHelper();
-      for (String backendName : hmBackendSuffix.keySet())
+      for (String backendId : hmBackendSuffix.keySet())
       {
-        helper.createBackend(connection, backendName, hmBackendSuffix.get(backendName),
-            backendTypes.get(backendName).getBackend());
+        helper.createBackend(connection, backendId, hmBackendSuffix.get(backendId),
+            backendTypes.get(backendId).getBackend());
       }
     }
     catch (NamingException e)
diff --git a/opendj-server-legacy/src/main/java/org/opends/quicksetup/installer/InstallerHelper.java b/opendj-server-legacy/src/main/java/org/opends/quicksetup/installer/InstallerHelper.java
index 785b8de..4435a35 100644
--- a/opendj-server-legacy/src/main/java/org/opends/quicksetup/installer/InstallerHelper.java
+++ b/opendj-server-legacy/src/main/java/org/opends/quicksetup/installer/InstallerHelper.java
@@ -78,7 +78,6 @@
 import org.opends.server.tools.ConfigureDS;
 import org.opends.server.tools.ConfigureWindowsService;
 import org.opends.server.tools.JavaPropertiesTool;
-import org.opends.server.types.DirectoryException;
 import org.opends.server.types.LDIFExportConfig;
 import org.opends.server.types.OpenDsException;
 import org.opends.server.util.LDIFException;
@@ -333,7 +332,7 @@
    * @throws ApplicationException
    *           if something goes wrong.
    */
-  public void createBackend(ConnectionWrapper conn, String backendName, Set<String> baseDNs,
+  public void createBackend(ConnectionWrapper conn, String backendName, Set<DN> baseDNs,
       ManagedObjectDefinition<? extends BackendCfgClient, ? extends BackendCfg> backendType)
       throws ApplicationException
   {
@@ -342,7 +341,7 @@
       RootCfgClient root = conn.getRootConfiguration();
       BackendCfgClient backend = root.createBackend(backendType, backendName, null);
       backend.setEnabled(true);
-      backend.setBaseDN(toDNs(baseDNs));
+      backend.setBaseDN(baseDNs);
       backend.setBackendId(backendName);
       backend.setWritabilityMode(BackendCfgDefn.WritabilityMode.ENABLED);
       backend.commit();
@@ -354,16 +353,6 @@
     }
   }
 
-  private Set<DN> toDNs(Set<String> strings) throws DirectoryException
-  {
-    Set<DN> results = new HashSet<>();
-    for (String s : strings)
-    {
-      results.add(DN.valueOf(s));
-    }
-    return results;
-  }
-
   /**
    * Configures the replication on a given server.
    * @param conn the connection to the server where we want to configure
diff --git a/opendj-server-legacy/src/main/java/org/opends/quicksetup/installer/ui/SuffixesToReplicatePanel.java b/opendj-server-legacy/src/main/java/org/opends/quicksetup/installer/ui/SuffixesToReplicatePanel.java
index 9b00614..8d9b291 100644
--- a/opendj-server-legacy/src/main/java/org/opends/quicksetup/installer/ui/SuffixesToReplicatePanel.java
+++ b/opendj-server-legacy/src/main/java/org/opends/quicksetup/installer/ui/SuffixesToReplicatePanel.java
@@ -131,8 +131,8 @@
     final Map<String, BackendTypeUIAdapter> backendTypes = new HashMap<>();
     for (SuffixDescriptor suffix : getSelectedSuffixes())
     {
-      final String backendName = suffix.getReplicas().iterator().next().getBackendName();
-      backendTypes.put(backendName, (BackendTypeUIAdapter) backendTypeComboBoxes.get(backendName).getSelectedItem());
+      final String backendId = suffix.getReplicas().iterator().next().getBackendId();
+      backendTypes.put(backendId, (BackendTypeUIAdapter) backendTypeComboBoxes.get(backendId).getSelectedItem());
     }
     return backendTypes;
   }
@@ -299,12 +299,12 @@
     final Map<String, Set<SuffixDescriptor>> backendToSuffixes = new HashMap<>();
     for (SuffixDescriptor suffix : orderedSuffixes)
     {
-      final String backendName = suffix.getReplicas().iterator().next().getBackendName();
-      if (!backendToSuffixes.containsKey(backendName))
+      final String backendId = suffix.getReplicas().iterator().next().getBackendId();
+      if (!backendToSuffixes.containsKey(backendId))
       {
-        backendToSuffixes.put(backendName, new LinkedHashSet<SuffixDescriptor>());
+        backendToSuffixes.put(backendId, new LinkedHashSet<SuffixDescriptor>());
       }
-      backendToSuffixes.get(backendName).add(suffix);
+      backendToSuffixes.get(backendId).add(suffix);
     }
 
     return backendToSuffixes;
@@ -383,7 +383,7 @@
     backendTypeComboBox.setToolTipText(INFO_REPLICATED_SUFFIXES_BACKEND_TYPE_TOOLTIP.get().toString());
     final Set<String> objectClasses = backendData.getObjectClasses();
     backendTypeComboBox.setSelectedItem(getBackendTypeFromObjectClasses(objectClasses));
-    backendTypeComboBoxes.put(backendData.getBackendName(), backendTypeComboBox);
+    backendTypeComboBoxes.put(backendData.getBackendId(), backendTypeComboBox);
     checkBoxPanel.add(backendTypeComboBox, gbc);
     gbc.insets = SUFFIXES_TO_REPLICATE_INSETS;
   }
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/tools/dsreplication/ReplicationCliMain.java b/opendj-server-legacy/src/main/java/org/opends/server/tools/dsreplication/ReplicationCliMain.java
index f5a8666..91ecd91 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/tools/dsreplication/ReplicationCliMain.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/tools/dsreplication/ReplicationCliMain.java
@@ -21,7 +21,6 @@
 import static com.forgerock.opendj.cli.CommonArguments.*;
 import static com.forgerock.opendj.cli.Utils.*;
 import static com.forgerock.opendj.util.OperatingSystem.*;
-import static java.util.Collections.*;
 
 import static org.forgerock.opendj.ldap.SearchScope.*;
 import static org.forgerock.opendj.ldap.requests.Requests.*;
@@ -1693,22 +1692,11 @@
     {
       for (BaseDNDescriptor baseDN : backend.getBaseDns())
       {
-        SuffixDescriptor suffix = new SuffixDescriptor();
-        suffix.setDN(baseDN.getDn());
-
         ReplicaDescriptor replica = new ReplicaDescriptor();
-
-        if (baseDN.getType() == BaseDNDescriptor.Type.REPLICATED)
-        {
-          replica.setReplicationId(baseDN.getReplicaID());
-        }
-        else
-        {
-          replica.setReplicationId(-1);
-        }
-        replica.setBackendName(backend.getBackendID());
-        replica.setSuffix(suffix);
-        suffix.setReplicas(singleton(replica));
+        replica.setReplicationId(
+            baseDN.getType() == BaseDNDescriptor.Type.REPLICATED ? baseDN.getReplicaID() : -1);
+        replica.setBackendId(backend.getBackendID());
+        replica.setSuffix(new SuffixDescriptor(baseDN.getDn(), replica));
 
         replicas.add(replica);
       }

--
Gitblit v1.10.0