From 8d0bcc4cfb85138b7455afae55174c8583a97bcf Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Tue, 16 Dec 2008 15:08:30 +0000
Subject: [PATCH] Fix for issue 3668 (Control Panel does not display correctly connection handlers' listen addresses) Use a comparator of InetAdress in the admin framework to sort the addresses.

---
 opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ControlPanelInfo.java            |    4 
 opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigFromFile.java                   |   10 
 opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ConnectionHandlerDescriptor.java |   11 +
 opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigReader.java                     |   13 +
 opends/src/guitools/org/opends/guitools/controlpanel/datamodel/CustomSearchResult.java          |   61 ++++++++
 opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ConnectionHandlerTableModel.java |    2 
 opends/src/guitools/org/opends/guitools/controlpanel/ui/StatusGenericPanel.java                 |    2 
 opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigFromDirContext.java             |  260 ++++++++++++++++++++----------------
 8 files changed, 236 insertions(+), 127 deletions(-)

diff --git a/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ConnectionHandlerDescriptor.java b/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ConnectionHandlerDescriptor.java
index a8ade36..487f481 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ConnectionHandlerDescriptor.java
+++ b/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ConnectionHandlerDescriptor.java
@@ -35,6 +35,7 @@
 import java.util.TreeSet;
 
 import org.opends.messages.Message;
+import org.opends.server.admin.std.meta.AdministrationConnectorCfgDefn;
 
 /**
  * This class is used to represent a Listener and is aimed to be used by the
@@ -131,7 +132,9 @@
   }
 
   private State state;
-  private SortedSet<InetAddress> addresses = new TreeSet<InetAddress>();
+  private SortedSet<InetAddress> addresses = new TreeSet<InetAddress>(
+      AdministrationConnectorCfgDefn.getInstance().
+      getListenAddressPropertyDefinition());
   private int port;
   private Protocol protocol;
   private String toString;
@@ -140,7 +143,7 @@
   private int hashCode;
 
   /**
-   * Constructor for thid class.
+   * Constructor for the connection handler..
    * @param addresses the list of InetAdresses of the listener.
    * @param port the port of the connection handler.
    * @param protocol the protocol of the listener.
@@ -173,7 +176,7 @@
    */
   public SortedSet<InetAddress> getAddresses()
   {
-    return new TreeSet<InetAddress>(addresses);
+    return addresses;
   }
 
   /**
@@ -222,7 +225,7 @@
     }
     else if (o instanceof ConnectionHandlerDescriptor)
     {
-      equals = hashCode() == o.hashCode();
+      equals = toString.equals(o.toString());
     }
     return equals;
   }
diff --git a/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ConnectionHandlerTableModel.java b/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ConnectionHandlerTableModel.java
index 9bcf880..9d287e2 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ConnectionHandlerTableModel.java
+++ b/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ConnectionHandlerTableModel.java
@@ -283,7 +283,7 @@
         {
           buf.append("<br>");
         }
-        buf.append(address);
+        buf.append(address.getCanonicalHostName());
         added = true;
         if (desc.getPort() > 0)
         {
diff --git a/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ControlPanelInfo.java b/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ControlPanelInfo.java
index 6424630..641525f 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ControlPanelInfo.java
+++ b/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ControlPanelInfo.java
@@ -925,8 +925,8 @@
       if (port > 0) {
         InetAddress address = addresses.first();
         url = "ldaps://" +
-          ConnectionUtils.getHostNameForLdapUrl(address.toString()) + ":" +
-          port;
+          ConnectionUtils.getHostNameForLdapUrl(address.getHostAddress()) + ":"
+          + port;
       }
     }
     return url;
diff --git a/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/CustomSearchResult.java b/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/CustomSearchResult.java
index 5d3fa2e..1bf445c 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/CustomSearchResult.java
+++ b/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/CustomSearchResult.java
@@ -57,6 +57,8 @@
   private String dn;
   private Map<String, Set<Object>> attributes;
   private SortedSet<String> attrNames;
+  private String toString;
+  private int hashCode;
 
   /**
    * Constructor of an empty search result.  This constructor is used by the
@@ -69,6 +71,8 @@
     this.dn = dn;
     attributes = new HashMap<String, Set<Object>>();
     attrNames = new TreeSet<String>();
+    toString = calculateToString();
+    hashCode = calculateHashCode();
   }
 
   /**
@@ -134,6 +138,8 @@
         attributes.put(name.toLowerCase(), values);
       }
     }
+    toString = calculateToString();
+    hashCode = calculateHashCode();
   }
 
   /**
@@ -178,8 +184,49 @@
   /**
    * {@inheritDoc}
    */
+  public boolean equals(Object o)
+  {
+    boolean equals = false;
+    if (o != null)
+    {
+      equals = o == this;
+      if (!equals && (o instanceof CustomSearchResult))
+      {
+        CustomSearchResult sr = (CustomSearchResult)o;
+        equals = getDN().equals(sr.getDN());
+        if (equals)
+        {
+          equals = getAttributeNames().equals(sr.getAttributeNames());
+          if (equals)
+          {
+            for (String attrName : getAttributeNames())
+            {
+              equals = getAttributeValues(attrName).equals(
+                  sr.getAttributeValues(attrName));
+              if (!equals)
+              {
+                break;
+              }
+            }
+          }
+        }
+      }
+    }
+    return equals;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
   public String toString() {
-    return "dn: "+dn+"\nattributes: "+attributes;
+    return toString;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  public int hashCode() {
+    return hashCode;
   }
 
   /**
@@ -192,5 +239,17 @@
     attrNames.add(attrName);
     attrName = attrName.toLowerCase();
     attributes.put(attrName, values);
+    toString = calculateToString();
+    hashCode = calculateHashCode();
+  }
+
+  private String calculateToString()
+  {
+    return "dn: "+dn+"\nattributes: "+attributes;
+  }
+
+  private int calculateHashCode()
+  {
+    return 23 + toString.hashCode();
   }
 }
diff --git a/opends/src/guitools/org/opends/guitools/controlpanel/ui/StatusGenericPanel.java b/opends/src/guitools/org/opends/guitools/controlpanel/ui/StatusGenericPanel.java
index 0e4bcda..4cc9613 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/ui/StatusGenericPanel.java
+++ b/opends/src/guitools/org/opends/guitools/controlpanel/ui/StatusGenericPanel.java
@@ -686,7 +686,7 @@
      * Constructor.
      * @param combo the combo box.
      */
-    IgnoreItemListener(JComboBox combo)
+    public IgnoreItemListener(JComboBox combo)
     {
       this.combo = combo;
       selectedItem = combo.getSelectedItem();
diff --git a/opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigFromDirContext.java b/opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigFromDirContext.java
index 6ecae6c..a34eaa9 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigFromDirContext.java
+++ b/opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigFromDirContext.java
@@ -328,8 +328,6 @@
         ex.add(oe);
       }
 
-      updateMonitorInformation(ctx, bs, ex);
-
       try
       {
         readSchema();
@@ -349,14 +347,150 @@
     {
       LOG.log(Level.WARNING, "Error reading configuration: "+oe, oe);
     }
-    exceptions = Collections.unmodifiableList(ex);
     administrativeUsers = Collections.unmodifiableSet(as);
     listeners = Collections.unmodifiableSet(ls);
     backends = Collections.unmodifiableSet(bs);
+    try
+    {
+      updateMonitorInformation(ctx, ex);
+    }
+    catch (Throwable t)
+    {
+      OnlineUpdateException oupe = new OnlineUpdateException(
+          ERR_READING_CONFIG_LDAP.get(t.toString()), t);
+      ex.add(oupe);
+    }
+    exceptions = Collections.unmodifiableList(ex);
+
+  }
+
+  /**
+   * Returns an array of monitoring attributes to be returned in the request.
+   * @return an array of monitoring attributes to be returned in the request.
+   */
+  protected String[] getMonitoringAttributes()
+  {
+    return new String[] {
+        "approx-older-change-not-synchronized-millis", "missing-changes",
+        "base-dn", "server-id", "javaVersion", "currentConnections",
+        "ds-backend-id", "ds-backend-entry-count", "ds-base-dn-entry-count"
+    };
+  }
+
+  /**
+   * Takes the provided search result and updates the monitoring information
+   * accordingly.
+   * @param sr the search result.
+   * @param searchBaseDN the base search.
+   * @throws NamingException if there is an error retrieving the values of the
+   * search result.
+   */
+  protected void handleMonitoringSearchResult(SearchResult sr,
+      String searchBaseDN)
+  throws NamingException
+  {
+    if (javaVersion == null)
+    {
+      javaVersion = ConnectionUtils.getFirstValue(sr, "javaVersion");
+    }
+
+    if (numberConnections == -1)
+    {
+      String v = ConnectionUtils.getFirstValue(sr, "currentConnections");
+      if (v != null)
+      {
+        numberConnections = Integer.parseInt(v);
+      }
+    }
+
+    String dn = ConnectionUtils.getFirstValue(sr, "base-dn");
+    String replicaId = ConnectionUtils.getFirstValue(sr, "server-id");
+
+    if ((dn != null)  && (replicaId != null))
+    {
+      for (BackendDescriptor backend : backends)
+      {
+        for (BaseDNDescriptor baseDN : backend.getBaseDns())
+        {
+          if (Utilities.areDnsEqual(baseDN.getDn().toString(), dn) &&
+              String.valueOf(baseDN.getReplicaID()).equals(replicaId))
+          {
+            try
+            {
+              baseDN.setAgeOfOldestMissingChange(
+                  new Long(ConnectionUtils.getFirstValue(sr,
+              "approx-older-change-not-synchronized-millis")));
+            }
+            catch (Throwable t)
+            {
+            }
+            try
+            {
+              baseDN.setMissingChanges(new Integer(
+                  ConnectionUtils.getFirstValue(sr, "missing-changes")));
+            }
+            catch (Throwable t)
+            {
+            }
+          }
+        }
+      }
+    }
+    else
+    {
+      String backendID = ConnectionUtils.getFirstValue(sr,
+          "ds-backend-id");
+      String entryCount = ConnectionUtils.getFirstValue(sr,
+          "ds-backend-entry-count");
+      Set<String> baseDnEntries = ConnectionUtils.getValues(sr,
+          "ds-base-dn-entry-count");
+      if ((backendID != null) && ((entryCount != null) ||
+          (baseDnEntries != null)))
+      {
+        for (BackendDescriptor backend : backends)
+        {
+          if (backend.getBackendID().equalsIgnoreCase(backendID))
+          {
+            if (entryCount != null)
+            {
+              backend.setEntries(Integer.parseInt(entryCount));
+            }
+            if (baseDnEntries != null)
+            {
+              for (String s : baseDnEntries)
+              {
+                int index = s.indexOf(" ");
+                if (index != -1)
+                {
+                  for (BaseDNDescriptor baseDN : backend.getBaseDns())
+                  {
+                    dn = s.substring(index +1);
+
+                    if (Utilities.areDnsEqual(dn,
+                        baseDN.getDn().toString()))
+                    {
+                      try
+                      {
+                        baseDN.setEntries(
+                            Integer.parseInt(s.substring(0, index)));
+                      }
+                      catch (Throwable t)
+                      {
+                        /* Ignore */
+                      }
+                      break;
+                    }
+                  }
+                }
+              }
+            }
+          }
+        }
+      }
+    }
   }
 
   private void updateMonitorInformation(InitialLdapContext ctx,
-      Set<BackendDescriptor> bs,
       List<OpenDsException> ex)
   {
     // Read monitoring information: since it is computed, it is faster
@@ -364,14 +498,9 @@
     SearchControls ctls = new SearchControls();
     ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
     ctls.setReturningAttributes(
-        new String[] {
-            "approx-older-change-not-synchronized-millis", "missing-changes",
-            "base-dn", "server-id", "javaVersion", "currentConnections",
-            "ds-backend-id", "ds-backend-entry-count", "ds-base-dn-entry-count"
-        });
+        getMonitoringAttributes());
     String filter = "(objectclass=*)";
 
-
     try
     {
       LdapName jndiName = new LdapName("cn=monitor");
@@ -384,106 +513,7 @@
       while (monitorEntries.hasMore())
       {
         SearchResult sr = (SearchResult)monitorEntries.next();
-
-        if (javaVersion == null)
-        {
-          javaVersion = ConnectionUtils.getFirstValue(sr, "javaVersion");
-        }
-
-        if (numberConnections == -1)
-        {
-          String v = ConnectionUtils.getFirstValue(sr, "currentConnections");
-          if (v != null)
-          {
-            numberConnections = Integer.parseInt(v);
-          }
-        }
-
-        String dn = ConnectionUtils.getFirstValue(sr, "base-dn");
-        String replicaId = ConnectionUtils.getFirstValue(sr, "server-id");
-
-        if ((dn != null)  && (replicaId != null))
-        {
-          for (BackendDescriptor backend : bs)
-          {
-            for (BaseDNDescriptor baseDN : backend.getBaseDns())
-            {
-              if (Utilities.areDnsEqual(baseDN.getDn().toString(), dn) &&
-                  String.valueOf(baseDN.getReplicaID()).equals(replicaId))
-              {
-                try
-                {
-                  baseDN.setAgeOfOldestMissingChange(
-                      new Long(ConnectionUtils.getFirstValue(sr,
-                  "approx-older-change-not-synchronized-millis")));
-                }
-                catch (Throwable t)
-                {
-                }
-                try
-                {
-                  baseDN.setMissingChanges(new Integer(
-                      ConnectionUtils.getFirstValue(sr, "missing-changes")));
-                }
-                catch (Throwable t)
-                {
-                }
-              }
-            }
-          }
-        }
-        else
-        {
-          String backendID = ConnectionUtils.getFirstValue(sr,
-              "ds-backend-id");
-          String entryCount = ConnectionUtils.getFirstValue(sr,
-              "ds-backend-entry-count");
-          Set<String> baseDnEntries = ConnectionUtils.getValues(sr,
-              "ds-base-dn-entry-count");
-          if ((backendID != null) && ((entryCount != null) ||
-              (baseDnEntries != null)))
-          {
-            for (BackendDescriptor backend : bs)
-            {
-              if (backend.getBackendID().equalsIgnoreCase(backendID))
-              {
-                if (entryCount != null)
-                {
-                  backend.setEntries(Integer.parseInt(entryCount));
-                }
-                if (baseDnEntries != null)
-                {
-                  for (String s : baseDnEntries)
-                  {
-                    int index = s.indexOf(" ");
-                    if (index != -1)
-                    {
-                      for (BaseDNDescriptor baseDN : backend.getBaseDns())
-                      {
-                        dn = s.substring(index +1);
-
-                        if (Utilities.areDnsEqual(dn,
-                            baseDN.getDn().toString()))
-                        {
-                          try
-                          {
-                            baseDN.setEntries(
-                                Integer.parseInt(s.substring(0, index)));
-                          }
-                          catch (Throwable t)
-                          {
-                            /* Ignore */
-                          }
-                          break;
-                        }
-                      }
-                    }
-                  }
-                }
-              }
-            }
-          }
-        }
+        handleMonitoringSearchResult(sr, "cn=monitor");
       }
     }
     catch (NamingException ne)
@@ -498,7 +528,8 @@
       ConnectionHandlerCfgClient connHandler, String name)
   throws OpenDsException
   {
-    SortedSet<InetAddress> addresses = new TreeSet<InetAddress>();
+    SortedSet<InetAddress> addresses = new TreeSet<InetAddress>(
+        getInetAddressComparator());
     int port;
 
     ConnectionHandlerDescriptor.Protocol protocol;
@@ -524,7 +555,7 @@
         protocol = ConnectionHandlerDescriptor.Protocol.LDAP;
       }
       SortedSet<InetAddress> v = ldap.getListenAddress();
-      if (v == null)
+      if (v != null)
       {
         addresses.addAll(v);
       }
@@ -568,7 +599,8 @@
   private ConnectionHandlerDescriptor getConnectionHandler(
       AdministrationConnectorCfgClient adminConnector) throws OpenDsException
   {
-    SortedSet<InetAddress> addresses = new TreeSet<InetAddress>();
+    SortedSet<InetAddress> addresses = new TreeSet<InetAddress>(
+        getInetAddressComparator());
 
     ConnectionHandlerDescriptor.Protocol protocol =
       ConnectionHandlerDescriptor.Protocol.ADMINISTRATION_CONNECTOR;
@@ -578,7 +610,7 @@
 
 
     SortedSet<InetAddress> v = adminConnector.getListenAddress();
-    if (v == null)
+    if (v != null)
     {
       addresses.addAll(v);
     }
diff --git a/opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigFromFile.java b/opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigFromFile.java
index d97afaa..3096624 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigFromFile.java
+++ b/opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigFromFile.java
@@ -383,7 +383,8 @@
   private ConnectionHandlerDescriptor getConnectionHandler(
       ConnectionHandlerCfg connHandler, String name) throws OpenDsException
   {
-    SortedSet<InetAddress> addresses = new TreeSet<InetAddress>();
+    SortedSet<InetAddress> addresses = new TreeSet<InetAddress>(
+        getInetAddressComparator());
     int port;
 
     ConnectionHandlerDescriptor.Protocol protocol;
@@ -408,7 +409,7 @@
         protocol = ConnectionHandlerDescriptor.Protocol.LDAP;
       }
       SortedSet<InetAddress> v = ldap.getListenAddress();
-      if (v == null)
+      if (v != null)
       {
         addresses.addAll(v);
       }
@@ -450,7 +451,8 @@
   private ConnectionHandlerDescriptor getConnectionHandler(
       AdministrationConnectorCfg adminConnector) throws OpenDsException
   {
-    SortedSet<InetAddress> addresses = new TreeSet<InetAddress>();
+    SortedSet<InetAddress> addresses = new TreeSet<InetAddress>(
+        getInetAddressComparator());
 
     ConnectionHandlerDescriptor.Protocol protocol =
       ConnectionHandlerDescriptor.Protocol.ADMINISTRATION_CONNECTOR;
@@ -460,7 +462,7 @@
 
 
     SortedSet<InetAddress> v = adminConnector.getListenAddress();
-    if (v == null)
+    if (v != null)
     {
       addresses.addAll(v);
     }
diff --git a/opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigReader.java b/opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigReader.java
index f50a8ac..731d097 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigReader.java
+++ b/opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigReader.java
@@ -30,8 +30,10 @@
 import static org.opends.messages.AdminToolMessages.*;
 
 import java.io.File;
+import java.net.InetAddress;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 import java.util.Set;
 
@@ -39,6 +41,7 @@
 import org.opends.guitools.controlpanel.datamodel.ConnectionHandlerDescriptor;
 import org.opends.guitools.controlpanel.datamodel.VLVSortOrder;
 import org.opends.guitools.controlpanel.task.OfflineUpdateException;
+import org.opends.server.admin.std.meta.AdministrationConnectorCfgDefn;
 import org.opends.server.config.ConfigException;
 import org.opends.server.core.DirectoryServer;
 import org.opends.server.types.DN;
@@ -311,4 +314,14 @@
     }
     return sortOrder;
   }
+
+  /**
+   * Returns the comparator to be used to sort InetAddresses.
+   * @return the comparator to be used to sort InetAddresses.
+   */
+  protected Comparator<InetAddress> getInetAddressComparator()
+  {
+    return AdministrationConnectorCfgDefn.getInstance().
+    getListenAddressPropertyDefinition();
+  }
 }

--
Gitblit v1.10.0