From 66c1a80d263b71195a525d1c1fdd59e464a606ae Mon Sep 17 00:00:00 2001
From: ludovicp <ludovicp@localhost>
Date: Fri, 20 Aug 2010 13:39:26 +0000
Subject: [PATCH] Fix all command lines that are doing Searches with JNDI, to avoid them sending systematic Abandon requests.

---
 opends/src/ads/org/opends/admin/ads/ADSContext.java                               |   41 ++++++++-
 opends/src/guitools/org/opends/guitools/controlpanel/browser/NodeRefresher.java   |   63 +++++++++++++--
 opends/src/ads/org/opends/admin/ads/util/ConnectionUtils.java                     |   17 +++
 opends/src/server/org/opends/server/admin/client/ldap/JNDIDirContextAdaptor.java  |   11 +-
 opends/src/guitools/org/opends/guitools/controlpanel/util/RemoteSchemaLoader.java |    6 +
 opends/src/guitools/org/opends/guitools/controlpanel/util/LDAPEntryReader.java    |   16 ++-
 opends/src/ads/org/opends/admin/ads/ADSContextHelper.java                         |   10 +-
 opends/src/ads/org/opends/admin/ads/ServerDescriptor.java                         |    2 
 opends/src/guitools/org/opends/guitools/controlpanel/ui/LocalOrRemotePanel.java   |   16 +++
 opends/src/guitools/org/opends/guitools/controlpanel/util/Utilities.java          |   18 ++++
 opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java              |   12 ++-
 opends/src/server/org/opends/server/tools/dsreplication/ReplicationCliMain.java   |   12 ++-
 12 files changed, 176 insertions(+), 48 deletions(-)

diff --git a/opends/src/ads/org/opends/admin/ads/ADSContext.java b/opends/src/ads/org/opends/admin/ads/ADSContext.java
index a8b0fba..1f3634f 100644
--- a/opends/src/ads/org/opends/admin/ads/ADSContext.java
+++ b/opends/src/ads/org/opends/admin/ads/ADSContext.java
@@ -784,7 +784,7 @@
       ne = attrs.getAll();
       while (ne.hasMore())
       {
-        Attribute attr = (Attribute)ne.next();
+        Attribute attr = ne.next();
         String attrID = attr.getID();
 
         if (!attrID.toLowerCase().equals(
@@ -870,14 +870,16 @@
 
             ne2 = dirContext.search(getInstanceKeysContainerDN(),
                 "(ds-cfg-key-id="+keyId+")", sc);
-            if (ne2.hasMore())
+            boolean found = false;
+            while (ne2.hasMore())
             {
               SearchResult certEntry = ne2.next();
               Attribute certAttr = certEntry.getAttributes().get(attrIDs[0]);
               properties.put(ServerProperty.INSTANCE_PUBLIC_KEY_CERTIFICATE,
                   certAttr.get());
+              found = true;
             }
-            else
+            if (!found)
             {
               LOG.log(Level.WARNING, "Could not find public key for "+
                   properties);
@@ -1412,10 +1414,20 @@
         sc.setReturningAttributes(attList);
         NamingEnumeration<SearchResult> ne = dirContext.search(
             dnCentralAdmin, "(objectclass=*)", sc);
-        SearchResult sr = ne.next();
+        try
+        {
+          while (ne.hasMore())
+          {
+            SearchResult sr = ne.next();
 
-        currentPrivileges = sr.getAttributes().get("ds-privilege-name")
-        .getAll();
+            currentPrivileges = sr.getAttributes().get("ds-privilege-name")
+            .getAll();
+          }
+        }
+        finally
+        {
+          handleCloseNamingEnumeration(ne);
+        }
       }
 
 
@@ -2237,7 +2249,22 @@
       SearchControls sc = new SearchControls();
 
       sc.setSearchScope(SearchControls.OBJECT_SCOPE);
-      result = getDirContext().search(dn, "(objectclass=*)", sc).hasMore();
+      sc.setReturningAttributes(new String[] {"1.1"});
+      NamingEnumeration<SearchResult> sr =
+        getDirContext().search(dn, "(objectclass=*)", sc);
+      result = false;
+      try
+      {
+        while (sr.hasMore())
+        {
+          sr.next();
+          result = true;
+        }
+      }
+      finally
+      {
+        sr.close();
+      }
     }
     catch (NameNotFoundException x)
     {
diff --git a/opends/src/ads/org/opends/admin/ads/ADSContextHelper.java b/opends/src/ads/org/opends/admin/ads/ADSContextHelper.java
index fe9fcaa..370e174 100644
--- a/opends/src/ads/org/opends/admin/ads/ADSContextHelper.java
+++ b/opends/src/ads/org/opends/admin/ads/ADSContextHelper.java
@@ -235,15 +235,17 @@
     NamingEnumeration<SearchResult> results = null;
     try
     {
-     results = ctx.search(ADSContext.getInstanceKeysContainerDN(), keyAttrs,
+      results = ctx.search(ADSContext.getInstanceKeysContainerDN(), keyAttrs,
          attrIDs);
-      if (results.hasMore()) {
+      boolean found = false;
+      while (results.hasMore()) {
         final Attribute keyIdAttr =
           results.next().getAttributes().get(attrIDs[0]);
         if (null != keyIdAttr) {
           /* attribute ds-cfg-key-id is the entry is a MUST in the schema */
           keyID = (String)keyIdAttr.get();
         }
+        found = true;
       }
       /* TODO: It is possible (but unexpected) that the caller specifies a
    ds-cfg-key-id value for which there is a certificate entry in ADS, but
@@ -251,7 +253,7 @@
    above search would not return the entry, but the below attempt to add
    an new entry with the supplied ds-cfg-key-id will fail (throw a
    NameAlreadyBoundException) */
-      else {
+      if (!found) {
         /* create key ID, if it was not supplied in serverProperties */
         if (null == keyID) {
           keyID = CryptoManagerImpl.getInstanceKeyID(
@@ -334,7 +336,7 @@
     {
       results = ctx.search(
           ADSContext.getInstanceKeysContainerDN(), keyAttrs, attrIDs);
-      if (results.hasMore()) {
+      while (results.hasMore()) {
         SearchResult res = results.next();
         ctx.destroySubcontext(res.getNameInNamespace());
       }
diff --git a/opends/src/ads/org/opends/admin/ads/ServerDescriptor.java b/opends/src/ads/org/opends/admin/ads/ServerDescriptor.java
index 6ee86b3..f3078cb 100644
--- a/opends/src/ads/org/opends/admin/ads/ServerDescriptor.java
+++ b/opends/src/ads/org/opends/admin/ads/ServerDescriptor.java
@@ -875,7 +875,7 @@
       Integer adminConnectorPort = null;
 
       // we should have a single administration connector
-      if (listeners.hasMore()) {
+      while (listeners.hasMore()) {
         SearchResult sr = listeners.next();
         String port = getFirstValue(sr, "ds-cfg-listen-port");
         adminConnectorPort = new Integer(port);
diff --git a/opends/src/ads/org/opends/admin/ads/util/ConnectionUtils.java b/opends/src/ads/org/opends/admin/ads/util/ConnectionUtils.java
index 7dfe426..a0e6e4a 100644
--- a/opends/src/ads/org/opends/admin/ads/util/ConnectionUtils.java
+++ b/opends/src/ads/org/opends/admin/ads/util/ConnectionUtils.java
@@ -39,6 +39,7 @@
 
 import javax.naming.CommunicationException;
 import javax.naming.Context;
+import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 import javax.naming.directory.Attribute;
 import javax.naming.directory.Attributes;
@@ -626,13 +627,23 @@
        * Search for the config to check that it is the directory manager.
        */
       SearchControls searchControls = new SearchControls();
-      searchControls.setCountLimit(1);
       searchControls.setSearchScope(
           SearchControls. OBJECT_SCOPE);
       searchControls.setReturningAttributes(
           new String[] {"1.1"});
-      ctx.search("cn=config", "objectclass=*", searchControls);
-
+      NamingEnumeration<SearchResult> sr =
+       ctx.search("cn=config", "objectclass=*", searchControls);
+      try
+      {
+        while (sr.hasMore())
+        {
+          sr.next();
+        }
+      }
+      finally
+      {
+        sr.close();
+      }
       connectedAsAdministrativeUser = true;
     } catch (NamingException ne)
     {
diff --git a/opends/src/guitools/org/opends/guitools/controlpanel/browser/NodeRefresher.java b/opends/src/guitools/org/opends/guitools/controlpanel/browser/NodeRefresher.java
index 3d3fdda..75e683f 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/browser/NodeRefresher.java
+++ b/opends/src/guitools/org/opends/guitools/controlpanel/browser/NodeRefresher.java
@@ -267,6 +267,7 @@
   /**
    * The method that actually does the refresh.
    */
+  @Override
   public void run() {
     final BasicNode node = getNode();
 
@@ -351,11 +352,23 @@
               ctls);
     try
     {
-      if (!s.hasMoreElements())
+      if (!s.hasMore())
       {
         throw new NameNotFoundException("Entry "+node.getDN()+
             " does not verify filter "+controller.getFilter());
       }
+      while (s.hasMore())
+      {
+        s.next();
+      }
+    }
+    catch (SizeLimitExceededException slme)
+    {
+      // We are just searching for an entry, but if there is more than one
+      // this exception will be thrown.  We call sr.hasMore after the
+      // first entry has been retrieved to avoid sending a systematic
+      // abandon when closing the s NamingEnumeration.
+      // See CR 6976906.
     }
     finally
     {
@@ -381,11 +394,23 @@
               ctls);
     try
     {
-      if (!s.hasMoreElements())
+      if (!s.hasMore())
       {
         throw new NameNotFoundException("Entry "+dn+
             " does not verify filter "+controller.getFilter());
       }
+      while (s.hasMore())
+      {
+        s.next();
+      }
+    }
+    catch (SizeLimitExceededException slme)
+    {
+      // We are just searching for an entry, but if there is more than one
+      // this exception will be thrown.  We call sr.hasMore after the
+      // first entry has been retrieved to avoid sending a systematic
+      // abandon when closing the s NamingEnumeration.
+      // See CR 6976906.
     }
     finally
     {
@@ -418,7 +443,7 @@
                 ctls);
       try
       {
-        if (s.hasMore())
+        while (s.hasMore())
         {
           localEntry = s.next();
           localEntry.setName(node.getDN());
@@ -534,7 +559,8 @@
             ctls);
         try
         {
-          if (sr.hasMore())
+          boolean found = false;
+          while (sr.hasMore())
           {
             entry = sr.next();
             String name;
@@ -547,12 +573,21 @@
               name = unquoteRelativeName(entry.getName())+","+remoteDn;
             }
             entry.setName(name);
+            found = true;
           }
-          else
+          if (!found)
           {
             throw new NameNotFoundException();
           }
         }
+        catch (SizeLimitExceededException sle)
+        {
+          // We are just searching for an entry, but if there is more than one
+          // this exception will be thrown.  We call sr.hasMore after the
+          // first entry has been retrieved to avoid sending a systematic
+          // abandon when closing the sr NamingEnumeration.
+          // See CR 6976906.
+        }
         finally
         {
           sr.close();
@@ -673,13 +708,19 @@
           ctls);
 
       throwAbandonIfNeeded(null);
-
-      if (searchResults.hasMoreElements()) { // May be parentNode has children
+      isLeafNode = true;
+      // Check if parentNode has children
+      while (searchResults.hasMoreElements()) {
         isLeafNode = false;
       }
-      else { // parentNode has no children
-        isLeafNode = true;
-      }
+    }
+    catch (SizeLimitExceededException e)
+    {
+      // We are just searching for an entry, but if there is more than one
+      // this exception will be thrown.  We call sr.hasMore after the
+      // first entry has been retrieved to avoid sending a systematic
+      // abandon when closing the searchResults NamingEnumeration.
+      // See CR 6976906.
     }
     catch (NamingException x) {
       throwAbandonIfNeeded(x);
@@ -787,7 +828,7 @@
           boolean add = false;
           if (useCustomFilter())
           {
-            // Check that is an inmediate child: use a faster method by just
+            // Check that is an immediate child: use a faster method by just
             // comparing the number of components.
             DN dn = null;
             try
diff --git a/opends/src/guitools/org/opends/guitools/controlpanel/ui/LocalOrRemotePanel.java b/opends/src/guitools/org/opends/guitools/controlpanel/ui/LocalOrRemotePanel.java
index d6ed200..a13a4e9 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/ui/LocalOrRemotePanel.java
+++ b/opends/src/guitools/org/opends/guitools/controlpanel/ui/LocalOrRemotePanel.java
@@ -122,6 +122,7 @@
   /**
    * {@inheritDoc}
    */
+  @Override
   public Message getTitle()
   {
     return INFO_CTRL_PANEL_LOCAL_OR_REMOTE_PANEL_TITLE.get();
@@ -130,6 +131,7 @@
   /**
    * {@inheritDoc}
    */
+  @Override
   public GenericDialog.ButtonType getButtonType()
   {
     return GenericDialog.ButtonType.OK_CANCEL;
@@ -406,6 +408,7 @@
   /**
    * {@inheritDoc}
    */
+  @Override
   public Component getPreferredFocusComponent()
   {
     if (pwd.isVisible())
@@ -428,6 +431,7 @@
   /**
    * {@inheritDoc}
    */
+  @Override
   public void toBeDisplayed(boolean visible)
   {
     super.toBeDisplayed(visible);
@@ -436,6 +440,7 @@
       // Do it outside the event thread if the panel requires it.
       BackgroundTask<Void> worker = new BackgroundTask<Void>()
       {
+        @Override
         public Void processBackgroundTask() throws Throwable
         {
           try
@@ -451,6 +456,7 @@
         }
 
 
+        @Override
         public void backgroundTaskCompleted(Void returnValue,
             Throwable t)
         {
@@ -479,6 +485,7 @@
   /**
    * {@inheritDoc}
    */
+  @Override
   public void okClicked()
   {
     setPrimaryValid(portLabel);
@@ -553,6 +560,7 @@
         /**
          * {@inheritDoc}
          */
+        @Override
         public InitialLdapContext processBackgroundTask() throws Throwable
         {
           getInfo().stopPooling();
@@ -638,6 +646,7 @@
         /**
          * {@inheritDoc}
          */
+        @Override
         public void backgroundTaskCompleted(InitialLdapContext ctx,
             Throwable throwable)
         {
@@ -818,6 +827,7 @@
   /**
    * {@inheritDoc}
    */
+  @Override
   public void cancelClicked()
   {
     setPrimaryValid(dnLabel);
@@ -945,7 +955,6 @@
        * Search for the version on the remote server.
        */
       SearchControls searchControls = new SearchControls();
-      searchControls.setCountLimit(1);
       searchControls.setSearchScope(
       SearchControls.OBJECT_SCOPE);
       searchControls.setReturningAttributes(
@@ -961,7 +970,10 @@
       SearchResult sr = null;
       try
       {
-        sr = en.next();
+        while (en.hasMore())
+        {
+          sr = en.next();
+        }
       }
       finally
       {
diff --git a/opends/src/guitools/org/opends/guitools/controlpanel/util/LDAPEntryReader.java b/opends/src/guitools/org/opends/guitools/controlpanel/util/LDAPEntryReader.java
index 49837fe..eec19fa 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/util/LDAPEntryReader.java
+++ b/opends/src/guitools/org/opends/guitools/controlpanel/util/LDAPEntryReader.java
@@ -48,9 +48,10 @@
  */
 public class LDAPEntryReader extends BackgroundTask<CustomSearchResult>
 {
-  private String dn;
-  private InitialLdapContext ctx;
-  private Set<EntryReadListener> listeners = new HashSet<EntryReadListener>();
+  private final String dn;
+  private final InitialLdapContext ctx;
+  private final Set<EntryReadListener> listeners =
+    new HashSet<EntryReadListener>();
   private boolean isOver;
   private boolean notifyListeners;
 
@@ -69,6 +70,7 @@
   /**
    * {@inheritDoc}
    */
+  @Override
   public CustomSearchResult processBackgroundTask() throws Throwable
   {
     isOver = false;
@@ -76,7 +78,6 @@
     try
     {
       SearchControls controls = new SearchControls();
-      controls.setCountLimit(1);
 
       String[] attrs = {"*", "+"};
       controls.setReturningAttributes(attrs);
@@ -85,7 +86,11 @@
 
       en = ctx.search(Utilities.getJNDIName(dn), filter, controls);
 
-      SearchResult sr = en.next();
+      SearchResult sr = null;
+      while (en.hasMore())
+      {
+        sr = en.next();
+      }
 
       return new CustomSearchResult(sr, dn);
     }
@@ -105,6 +110,7 @@
   /**
    * {@inheritDoc}
    */
+  @Override
   public void backgroundTaskCompleted(CustomSearchResult sr,
       Throwable throwable)
   {
diff --git a/opends/src/guitools/org/opends/guitools/controlpanel/util/RemoteSchemaLoader.java b/opends/src/guitools/org/opends/guitools/controlpanel/util/RemoteSchemaLoader.java
index 682b8c3..4434493 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/util/RemoteSchemaLoader.java
+++ b/opends/src/guitools/org/opends/guitools/controlpanel/util/RemoteSchemaLoader.java
@@ -96,7 +96,10 @@
     SearchResult sr = null;
     try
     {
-      sr = srs.next();
+      while (srs.hasMore())
+      {
+        sr = srs.next();
+      }
     }
     finally
     {
@@ -187,6 +190,7 @@
    * Returns the schema that was read.
    * @return the schema that was read.
    */
+  @Override
   public Schema getSchema()
   {
     return schema;
diff --git a/opends/src/guitools/org/opends/guitools/controlpanel/util/Utilities.java b/opends/src/guitools/org/opends/guitools/controlpanel/util/Utilities.java
index 96fd760..d33ac2e 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/util/Utilities.java
+++ b/opends/src/guitools/org/opends/guitools/controlpanel/util/Utilities.java
@@ -55,8 +55,10 @@
 import javax.naming.CompositeName;
 import javax.naming.InvalidNameException;
 import javax.naming.Name;
+import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 import javax.naming.directory.SearchControls;
+import javax.naming.directory.SearchResult;
 import javax.naming.ldap.InitialLdapContext;
 import javax.naming.ldap.LdapName;
 import javax.swing.BorderFactory;
@@ -628,6 +630,7 @@
       col.setCellRenderer(renderer);
     }
     MouseAdapter listMouseListener = new MouseAdapter() {
+      @Override
       public void mouseClicked(MouseEvent e) {
         TableColumnModel columnModel = table.getColumnModel();
         int viewColumn = columnModel.getColumnIndexAtX(e.getX());
@@ -2448,12 +2451,23 @@
      * Search for the config to check that it is the directory manager.
      */
     SearchControls searchControls = new SearchControls();
-    searchControls.setCountLimit(1);
     searchControls.setSearchScope(
     SearchControls. OBJECT_SCOPE);
     searchControls.setReturningAttributes(
     new String[] {"1.1"});
-    ctx.search("cn=config", "objectclass=*", searchControls);
+    NamingEnumeration<SearchResult> sr =
+      ctx.search("cn=config", "objectclass=*", searchControls);
+    try
+    {
+      while (sr.hasMore())
+      {
+        sr.next();
+      }
+    }
+    finally
+    {
+      sr.close();
+    }
   }
 
   /**
diff --git a/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java b/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java
index 2f89801..62de2fb 100644
--- a/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java
+++ b/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java
@@ -4643,7 +4643,6 @@
     }
     // Wait until it is over
     SearchControls searchControls = new SearchControls();
-    searchControls.setCountLimit(1);
     searchControls.setSearchScope(
         SearchControls. OBJECT_SCOPE);
     String filter = "objectclass=*";
@@ -4689,7 +4688,10 @@
         SearchResult sr = null;
         try
         {
-          sr = res.next();
+          while (res.hasMore())
+          {
+            sr = res.next();
+          }
         }
         finally
         {
@@ -4965,7 +4967,6 @@
     }
     // Wait until it is over
     SearchControls searchControls = new SearchControls();
-    searchControls.setCountLimit(1);
     searchControls.setSearchScope(
         SearchControls. OBJECT_SCOPE);
     String filter = "objectclass=*";
@@ -4991,7 +4992,10 @@
         SearchResult sr = null;
         try
         {
-          sr = res.next();
+          while (res.hasMore())
+          {
+            sr = res.next();
+          }
         }
         finally
         {
diff --git a/opends/src/server/org/opends/server/admin/client/ldap/JNDIDirContextAdaptor.java b/opends/src/server/org/opends/server/admin/client/ldap/JNDIDirContextAdaptor.java
index 7df97bf..bbe90b8 100644
--- a/opends/src/server/org/opends/server/admin/client/ldap/JNDIDirContextAdaptor.java
+++ b/opends/src/server/org/opends/server/admin/client/ldap/JNDIDirContextAdaptor.java
@@ -228,17 +228,20 @@
    */
   @Override
   public boolean entryExists(LdapName dn) throws NamingException {
+    boolean entryExists = false;
     String filter = "(objectClass=*)";
     SearchControls controls = new SearchControls();
     controls.setSearchScope(SearchControls.OBJECT_SCOPE);
-
+    controls.setReturningAttributes(new String[]{"1.1"});
     try {
       NamingEnumeration<SearchResult> results = dirContext.search(dn, filter,
           controls);
       try
       {
-        if (results.hasMore()) {
-          return true;
+        while (results.hasMore()) {
+          // To avoid having a systematic abandon in the server.
+          results.next();
+          entryExists = true;
         }
       }
       finally
@@ -248,7 +251,7 @@
     } catch (NameNotFoundException e) {
       // Fall through - entry not found.
     }
-    return false;
+    return entryExists;
   }
 
 
diff --git a/opends/src/server/org/opends/server/tools/dsreplication/ReplicationCliMain.java b/opends/src/server/org/opends/server/tools/dsreplication/ReplicationCliMain.java
index 0122baf..30b07cf 100644
--- a/opends/src/server/org/opends/server/tools/dsreplication/ReplicationCliMain.java
+++ b/opends/src/server/org/opends/server/tools/dsreplication/ReplicationCliMain.java
@@ -7721,7 +7721,6 @@
     }
     // Wait until it is over
     SearchControls searchControls = new SearchControls();
-    searchControls.setCountLimit(1);
     searchControls.setSearchScope(
         SearchControls. OBJECT_SCOPE);
     String filter = "objectclass=*";
@@ -7747,7 +7746,10 @@
         SearchResult sr = null;
         try
         {
-          sr = res.next();
+          while (res.hasMore())
+          {
+            sr = res.next();
+          }
         }
         finally
         {
@@ -7876,7 +7878,6 @@
     }
     // Wait until it is over
     SearchControls searchControls = new SearchControls();
-    searchControls.setCountLimit(1);
     searchControls.setSearchScope(
         SearchControls. OBJECT_SCOPE);
     String filter = "objectclass=*";
@@ -7908,7 +7909,10 @@
         SearchResult sr = null;
         try
         {
-          sr = res.next();
+          while (res.hasMore())
+          {
+            sr = res.next();
+          }
         }
         finally
         {

--
Gitblit v1.10.0