From 2d5ba62ec69e7ffa4b98149a9f6fef539e38251f Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Tue, 16 Jun 2009 10:48:51 +0000
Subject: [PATCH] Fix for issue 3912 (Default automatic Backup should be offered by the control panel)

---
 opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigFromDirContext.java |  122 +++++++++++++++++++++++++++++++++++++++-
 1 files changed, 118 insertions(+), 4 deletions(-)

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 7f6e720..9782bf1 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigFromDirContext.java
+++ b/opends/src/guitools/org/opends/guitools/controlpanel/util/ConfigFromDirContext.java
@@ -33,6 +33,7 @@
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -67,7 +68,9 @@
 import org.opends.server.admin.client.ldap.LDAPManagementContext;
 import org.opends.server.admin.std.client.*;
 import org.opends.server.admin.std.meta.LocalDBIndexCfgDefn.IndexType;
+import org.opends.server.config.ConfigConstants;
 import org.opends.server.core.DirectoryServer;
+import org.opends.server.tools.tasks.TaskEntry;
 import org.opends.server.types.DN;
 import org.opends.server.types.OpenDsException;
 import org.opends.server.util.ServerConstants;
@@ -238,6 +241,7 @@
       new HashSet<ConnectionHandlerDescriptor>();
     Set<BackendDescriptor> bs = new HashSet<BackendDescriptor>();
     Set<DN> as = new HashSet<DN>();
+    Set<TaskEntry> ts = new HashSet<TaskEntry>();
 
     rootMonitor = null;
     jvmMemoryUsage = null;
@@ -528,11 +532,23 @@
     }
     catch (Throwable t)
     {
-      LOG.log(Level.WARNING, "Error reading configuration: "+t, t);
+      LOG.log(Level.WARNING, "Error reading monitoring: "+t, t);
       OnlineUpdateException oupe = new OnlineUpdateException(
           ERR_READING_CONFIG_LDAP.get(t.toString()), t);
       ex.add(oupe);
     }
+    try
+    {
+      updateTaskInformation(ctx, ex, ts);
+    }
+    catch (Throwable t)
+    {
+      LOG.log(Level.WARNING, "Error reading task information: "+t, t);
+      OnlineUpdateException oupe = new OnlineUpdateException(
+          ERR_READING_CONFIG_LDAP.get(t.toString()), t);
+      ex.add(oupe);
+    }
+    taskEntries = Collections.unmodifiableSet(ts);
     for (ConnectionHandlerDescriptor ch : getConnectionHandlers())
     {
       ch.setMonitoringEntries(getMonitoringEntries(ch));
@@ -762,6 +778,33 @@
     }
   }
 
+  /**
+   * Takes the provided search result and updates the task information
+   * accordingly.
+   * @param sr the search result.
+   * @param searchBaseDN the base search.
+   * @param taskEntries the collection of TaskEntries to be updated.
+   * @throws NamingException if there is an error retrieving the values of the
+   * search result.
+   */
+  protected void handleTaskSearchResult(SearchResult sr, String searchBaseDN,
+      Collection<TaskEntry> taskEntries)
+  throws NamingException
+  {
+    CustomSearchResult csr = new CustomSearchResult(sr, searchBaseDN);
+    try
+    {
+      if (isTaskEntry(csr))
+      {
+        taskEntries.add(new TaskEntry(csr.getEntry()));
+      }
+    }
+    catch (OpenDsException ode)
+    {
+      exceptions.add(ode);
+    }
+  }
+
   private void updateMonitorInformation(InitialLdapContext ctx,
       List<OpenDsException> ex)
   {
@@ -796,6 +839,37 @@
     }
   }
 
+  private void updateTaskInformation(InitialLdapContext ctx,
+      List<OpenDsException> ex, Collection<TaskEntry> ts)
+  {
+    // Read monitoring information: since it is computed, it is faster
+    // to get everything in just one request.
+    SearchControls ctls = new SearchControls();
+    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
+    ctls.setReturningAttributes(
+        getMonitoringAttributes());
+    String filter = "(objectclass=ds-task)";
+
+    try
+    {
+      LdapName jndiName = new LdapName(ConfigConstants.DN_TASK_ROOT);
+
+      NamingEnumeration taskEntries = ctx.search(jndiName, filter, ctls);
+
+      while (taskEntries.hasMore())
+      {
+        SearchResult sr = (SearchResult)taskEntries.next();
+        handleTaskSearchResult(sr, ConfigConstants.DN_TASK_ROOT, ts);
+      }
+    }
+    catch (NamingException ne)
+    {
+      OnlineUpdateException oue = new OnlineUpdateException(
+          ERR_READING_CONFIG_LDAP.get(ne.getMessage().toString()), ne);
+      ex.add(oue);
+    }
+  }
+
   private ConnectionHandlerDescriptor getConnectionHandler(
       ConnectionHandlerCfgClient connHandler, String name)
   throws OpenDsException
@@ -962,6 +1036,24 @@
     return isConnectionHandler;
   }
 
+  private boolean isTaskEntry(CustomSearchResult csr) throws OpenDsException
+  {
+    boolean isTaskEntry = false;
+    Set<Object> vs = csr.getAttributeValues("objectclass");
+    if ((vs != null) && !vs.isEmpty())
+    {
+      for (Object oc : vs)
+      {
+        if (oc.toString().equalsIgnoreCase("ds-task"))
+        {
+          isTaskEntry = true;
+          break;
+        }
+      }
+    }
+    return isTaskEntry;
+  }
+
   /**
    * Commodity method to get the string representation to be used in the
    * hash maps as key.
@@ -977,11 +1069,33 @@
       ConnectionHandlerDescriptor ch)
   {
     Set<CustomSearchResult> monitorEntries = new HashSet<CustomSearchResult>();
-    for (String key : hmConnectionHandlersMonitor.keySet())
+    if (ch.getState() == ConnectionHandlerDescriptor.State.ENABLED)
     {
-      if (key.indexOf(getKey(ch.getName())) != -1)
+      for (String key : hmConnectionHandlersMonitor.keySet())
       {
-        monitorEntries.add(hmConnectionHandlersMonitor.get(key));
+        // The name of the connection handler does not appear necessarily in the
+        // key (which is based on the DN of the monitoring entry).  In general
+        // the DN contains the String specified in
+        // LDAPConnectionHandler.DEFAULT_FRIENDLY_NAME, so we have to check that
+        // this connection handler is the right one.
+        // See org.opends.server.protocols.ldap.LDAPConnectionHandler to see
+        // how the DN of the monitoring entry is generated.
+        if (key.indexOf(getKey("port "+ch.getPort())) != -1)
+        {
+          boolean hasAllAddresses = true;
+          for (InetAddress a : ch.getAddresses())
+          {
+            if (key.indexOf(getKey(a.getHostAddress())) == -1)
+            {
+              hasAllAddresses = false;
+              break;
+            }
+          }
+          if (hasAllAddresses)
+          {
+            monitorEntries.add(hmConnectionHandlersMonitor.get(key));
+          }
+        }
       }
     }
 

--
Gitblit v1.10.0