From fa59221ed4c2777097b0fd1a38c6839d60680499 Mon Sep 17 00:00:00 2001
From: matthew_swift <matthew_swift@localhost>
Date: Fri, 04 Dec 2009 11:12:28 +0000
Subject: [PATCH] Merge Bo's most recent changes.

---
 opendj-sdk/sdk/src/org/opends/sdk/ldap/LDAPConnection.java |  193 ++++++++++++++++++++++++++----------------------
 1 files changed, 104 insertions(+), 89 deletions(-)

diff --git a/opendj-sdk/sdk/src/org/opends/sdk/ldap/LDAPConnection.java b/opendj-sdk/sdk/src/org/opends/sdk/ldap/LDAPConnection.java
index b13e923..b2c71d6 100644
--- a/opendj-sdk/sdk/src/org/opends/sdk/ldap/LDAPConnection.java
+++ b/opendj-sdk/sdk/src/org/opends/sdk/ldap/LDAPConnection.java
@@ -38,6 +38,7 @@
 import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.net.ssl.SSLContext;
@@ -130,11 +131,15 @@
                 saslContext.evaluateCredentials(result
                     .getServerSASLCredentials());
               }
-              catch (SaslException se)
+              catch (SaslException e)
               {
                 pendingBindOrStartTLS = -1;
 
-                Result errorResult = adaptException(se);
+                // FIXME: I18N need to have a better error message.
+                // FIXME: Is this the best result code?
+                Result errorResult = Responses.newResult(
+                    ResultCode.CLIENT_SIDE_LOCAL_ERROR).setDiagnosticMessage(
+                    "An error occurred during SASL authentication").setCause(e);
                 future.handleErrorResult(errorResult);
                 return;
               }
@@ -163,7 +168,10 @@
                   {
                     pendingRequests.remove(messageID);
 
-                    Result errorResult = adaptException(e);
+                    // FIXME: what other sort of IOExceptions can be thrown?
+                    // FIXME: Is this the best result code?
+                    Result errorResult = Responses.newResult(
+                        ResultCode.CLIENT_SIDE_ENCODING_ERROR).setCause(e);
                     connectionErrorOccurred(errorResult);
                     future.handleErrorResult(errorResult);
                   }
@@ -251,7 +259,20 @@
      */
     public void handleException(Throwable throwable)
     {
-      Result errorResult = adaptException(throwable);
+      Result errorResult;
+      if(throwable instanceof EOFException)
+      {
+        // FIXME: Is this the best result code?
+        errorResult = Responses.newResult(
+            ResultCode.CLIENT_SIDE_SERVER_DOWN).setCause(throwable);
+      }
+      else
+      {
+        // FIXME: what other sort of IOExceptions can be thrown?
+        // FIXME: Is this the best result code?
+        errorResult = Responses.newResult(
+            ResultCode.CLIENT_SIDE_DECODING_ERROR).setCause(throwable);
+      }
       connectionErrorOccurred(errorResult);
     }
 
@@ -625,7 +646,8 @@
 
   private boolean isClosed = false;
 
-  private final List<ConnectionEventListener> listeners = new LinkedList<ConnectionEventListener>();
+  private final List<ConnectionEventListener> listeners =
+      new CopyOnWriteArrayList<ConnectionEventListener>();
 
   private final AtomicInteger nextMsgID = new AtomicInteger(1);
 
@@ -702,7 +724,10 @@
           }
           catch (IOException e)
           {
-            Result errorResult = adaptException(e);
+            // FIXME: what other sort of IOExceptions can be thrown?
+            // FIXME: Is this the best result code?
+            Result errorResult = Responses.newResult(
+                ResultCode.CLIENT_SIDE_ENCODING_ERROR).setCause(e);
             connectionErrorOccurred(errorResult);
           }
         }
@@ -754,7 +779,10 @@
         {
           pendingRequests.remove(messageID);
 
-          Result errorResult = adaptException(e);
+          // FIXME: what other sort of IOExceptions can be thrown?
+          // FIXME: Is this the best result code?
+          Result errorResult = Responses.newResult(
+              ResultCode.CLIENT_SIDE_ENCODING_ERROR).setCause(e);
           connectionErrorOccurred(errorResult);
           future.handleErrorResult(errorResult);
         }
@@ -847,7 +875,11 @@
             }
             catch (SaslException e)
             {
-              Result errorResult = adaptException(e);
+              // FIXME: I18N need to have a better error message.
+              // FIXME: Is this the best result code?
+              Result errorResult = Responses.newResult(
+                  ResultCode.CLIENT_SIDE_LOCAL_ERROR).setDiagnosticMessage(
+                  "An error occurred during SASL authentication").setCause(e);
               future.handleErrorResult(errorResult);
               return future;
             }
@@ -860,7 +892,8 @@
           else
           {
             pendingRequests.remove(messageID);
-            future.handleResult(Responses.newBindResult(ResultCode.PROTOCOL_ERROR)
+            future.handleResult(Responses.newBindResult(
+                ResultCode.CLIENT_SIDE_AUTH_UNKNOWN)
                 .setDiagnosticMessage("Auth type not supported"));
           }
           asn1Writer.flush();
@@ -869,7 +902,10 @@
         {
           pendingRequests.remove(messageID);
 
-          Result errorResult = adaptException(e);
+          // FIXME: what other sort of IOExceptions can be thrown?
+          // FIXME: Is this the best result code?
+          Result errorResult = Responses.newResult(
+              ResultCode.CLIENT_SIDE_ENCODING_ERROR).setCause(e);
           connectionErrorOccurred(errorResult);
           future.handleErrorResult(errorResult);
         }
@@ -890,25 +926,25 @@
    */
   public void close()
   {
-    close(Requests.newUnbindRequest());
+    close(Requests.newUnbindRequest(), null);
   }
 
 
-
   /**
    * {@inheritDoc}
    */
-  public void close(UnbindRequest request) throws NullPointerException
-  {
+  public void close(UnbindRequest request, String reason)
+      throws NullPointerException {
     // FIXME: I18N need to internationalize this message.
     Validator.ensureNotNull(request);
 
-    close(request, false, Responses.newResult(ResultCode.CLIENT_SIDE_USER_CANCELLED)
-        .setDiagnosticMessage("Connection closed by client"));
+    close(request, false,
+        Responses.newResult(ResultCode.CLIENT_SIDE_USER_CANCELLED)
+            .setDiagnosticMessage("Connection closed by client" +
+            (reason != null ? ": " + reason : "")));
   }
 
 
-
   /**
    * {@inheritDoc}
    */
@@ -950,7 +986,10 @@
         {
           pendingRequests.remove(messageID);
 
-          Result errorResult = adaptException(e);
+          // FIXME: what other sort of IOExceptions can be thrown?
+          // FIXME: Is this the best result code?
+          Result errorResult = Responses.newResult(
+              ResultCode.CLIENT_SIDE_ENCODING_ERROR).setCause(e);
           connectionErrorOccurred(errorResult);
           future.handleErrorResult(errorResult);
         }
@@ -1005,7 +1044,10 @@
         {
           pendingRequests.remove(messageID);
 
-          Result errorResult = adaptException(e);
+          // FIXME: what other sort of IOExceptions can be thrown?
+          // FIXME: Is this the best result code?
+          Result errorResult = Responses.newResult(
+              ResultCode.CLIENT_SIDE_ENCODING_ERROR).setCause(e);
           connectionErrorOccurred(errorResult);
           future.handleErrorResult(errorResult);
         }
@@ -1081,7 +1123,10 @@
         {
           pendingRequests.remove(messageID);
 
-          Result errorResult = adaptException(e);
+          // FIXME: what other sort of IOExceptions can be thrown?
+          // FIXME: Is this the best result code?
+          Result errorResult = Responses.newResult(
+              ResultCode.CLIENT_SIDE_ENCODING_ERROR).setCause(e);
           connectionErrorOccurred(errorResult);
           future.handleErrorResult(errorResult);
         }
@@ -1136,7 +1181,10 @@
         {
           pendingRequests.remove(messageID);
 
-          Result errorResult = adaptException(e);
+          // FIXME: what other sort of IOExceptions can be thrown?
+          // FIXME: Is this the best result code?
+          Result errorResult = Responses.newResult(
+              ResultCode.CLIENT_SIDE_ENCODING_ERROR).setCause(e);
           connectionErrorOccurred(errorResult);
           future.handleErrorResult(errorResult);
         }
@@ -1191,7 +1239,10 @@
         {
           pendingRequests.remove(messageID);
 
-          Result errorResult = adaptException(e);
+          // FIXME: what other sort of IOExceptions can be thrown?
+          // FIXME: Is this the best result code?
+          Result errorResult = Responses.newResult(
+              ResultCode.CLIENT_SIDE_ENCODING_ERROR).setCause(e);
           connectionErrorOccurred(errorResult);
           future.handleErrorResult(errorResult);
         }
@@ -1264,7 +1315,10 @@
         {
           pendingRequests.remove(messageID);
 
-          Result errorResult = adaptException(e);
+          // FIXME: what other sort of IOExceptions can be thrown?
+          // FIXME: Is this the best result code?
+          Result errorResult = Responses.newResult(
+              ResultCode.CLIENT_SIDE_ENCODING_ERROR).setCause(e);
           connectionErrorOccurred(errorResult);
           future.handleErrorResult(errorResult);
         }
@@ -1307,57 +1361,6 @@
 
 
 
-  private Result adaptException(Throwable t)
-  {
-    if (t instanceof ExecutionException)
-    {
-      ExecutionException e = (ExecutionException) t;
-      t = e.getCause();
-    }
-
-    Result errorResult;
-
-    try
-    {
-      throw t;
-    }
-    catch (SaslException e)
-    {
-      // FIXME: I18N need to have a better error message.
-      // FIXME: Is this the best result code?
-      errorResult = Responses.newResult(ResultCode.CLIENT_SIDE_LOCAL_ERROR).setDiagnosticMessage(
-          "An error occurred during SASL authentication").setCause(e);
-    }
-    catch (EOFException e)
-    {
-      // FIXME: I18N need to have a better error message.
-      // FIXME: what sort of IOExceptions can be thrown?
-      // FIXME: Is this the best result code?
-      errorResult = Responses.newResult(ResultCode.CLIENT_SIDE_SERVER_DOWN).setDiagnosticMessage(
-          "Connection unexpectedly terminated by server").setCause(e);
-    }
-    catch (IOException e)
-    {
-      // FIXME: I18N need to have a better error message.
-      // FIXME: what sort of IOExceptions can be thrown?
-      // FIXME: Is this the best result code?
-      errorResult = Responses.newResult(ResultCode.CLIENT_SIDE_LOCAL_ERROR).setDiagnosticMessage(
-          "An error occurred whilst attempting to send a request: "
-              + e.toString()).setCause(e);
-    }
-    catch (Throwable e)
-    {
-      // FIXME: I18N need to have a better error message.
-      // FIXME: Is this the best result code?
-      errorResult = Responses.newResult(ResultCode.CLIENT_SIDE_LOCAL_ERROR).setDiagnosticMessage(
-          "An unknown error occurred: " + e.toString()).setCause(e);
-    }
-
-    return errorResult;
-  }
-
-
-
   private void close(UnbindRequest unbindRequest,
       boolean isDisconnectNotification, Result reason)
   {
@@ -1489,8 +1492,8 @@
       {
         for (ConnectionEventListener listener : listeners)
         {
-          listener.connectionErrorOccurred(false, ErrorResultException
-              .wrap(reason));
+          listener.connectionErrorOccurred(isDisconnectNotification,
+              ErrorResultException.wrap(reason));
         }
       }
     }
@@ -1506,16 +1509,16 @@
 
 
   // TODO uncomment if we decide these methods are useful.
-  // /**
-  // * {@inheritDoc}
-  // */
-  // public boolean isClosed()
-  // {
-  // synchronized (writeLock)
-  // {
-  // return isClosed;
-  // }
-  // }
+  /**
+   * {@inheritDoc}
+   */
+  public boolean isClosed()
+  {
+    synchronized (writeLock)
+    {
+      return isClosed;
+    }
+  }
   //
   //
   //
@@ -1645,11 +1648,23 @@
       sslHandshaker.handshake(reader, writer, sslEngineConfigurator)
           .get();
     }
+    catch (ExecutionException ee)
+    {
+      // FIXME: what other sort of IOExceptions can be thrown?
+      // FIXME: Is this the best result code?
+      Result errorResult = Responses.newResult(
+          ResultCode.CLIENT_SIDE_CONNECT_ERROR).setCause(ee.getCause());
+      connectionErrorOccurred(errorResult);
+      throw ErrorResultException.wrap(errorResult);
+    }
     catch (Exception e)
     {
-      Result result = adaptException(e);
-      connectionErrorOccurred(result);
-      throw ErrorResultException.wrap(result);
+      // FIXME: what other sort of IOExceptions can be thrown?
+      // FIXME: Is this the best result code?
+      Result errorResult = Responses.newResult(
+          ResultCode.CLIENT_SIDE_CONNECT_ERROR).setCause(e);
+      connectionErrorOccurred(errorResult);
+      throw ErrorResultException.wrap(errorResult);
     }
   }
 

--
Gitblit v1.10.0