From 06b0a511fd8d009392b688d65c8a474cde500e65 Mon Sep 17 00:00:00 2001
From: ludovicp <ludovicp@localhost>
Date: Fri, 30 Jul 2010 12:57:02 +0000
Subject: [PATCH] Improves error message printed when the client tools time out during  the establishment of the connection.

---
 opendj-sdk/opends/src/server/org/opends/server/tools/LDAPToolUtils.java |   42 ++++++++++++++++++++
 opendj-sdk/opends/src/messages/messages/tools.properties                |    4 +
 opendj-sdk/opends/src/server/org/opends/server/tools/LDAPModify.java    |    9 ++--
 opendj-sdk/opends/src/server/org/opends/server/tools/LDAPCompare.java   |   10 ++--
 opendj-sdk/opends/src/server/org/opends/server/tools/LDAPDelete.java    |    3 +
 5 files changed, 56 insertions(+), 12 deletions(-)

diff --git a/opendj-sdk/opends/src/messages/messages/tools.properties b/opendj-sdk/opends/src/messages/messages/tools.properties
index 6bb3a24..a338ed8 100644
--- a/opendj-sdk/opends/src/messages/messages/tools.properties
+++ b/opendj-sdk/opends/src/messages/messages/tools.properties
@@ -2567,5 +2567,7 @@
 MILD_ERR_MAKELDIF_CANNOT_WRITE_ENTRY_WITHOUT_DN_1713=An error occurred while \
 attempting to write entry to LDIF:  Could not calculate the DN for the \
 entry (no value found for the RDN attribute %s)
-INFO_LABEL_DBTEST_INDEX_UNDEFINED_RECORD_COUNT_1714=Undefined
+SEVERE_ERR_CLIENT_SIDE_TIMEOUT_1714=A client side timeout occurred.\
+ %nAdditional Information:  %s 
+INFO_LABEL_DBTEST_INDEX_UNDEFINED_RECORD_COUNT_1715=Undefined
 
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPCompare.java b/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPCompare.java
index 7fc1d41..690323f 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPCompare.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPCompare.java
@@ -87,13 +87,13 @@
 
 
   // The message ID counter to use for requests.
-  private AtomicInteger nextMessageID;
+  private final AtomicInteger nextMessageID;
 
   // The print stream to use for standard error.
-  private PrintStream err;
+  private final PrintStream err;
 
   // The print stream to use for standard output.
-  private PrintStream out;
+  private final PrintStream out;
 
   // Tells whether the command-line is being executed in script friendly mode
   // or not.
@@ -225,7 +225,8 @@
         }
         if (!compareOptions.continueOnError())
         {
-          throw new IOException(ae.getMessage());
+          String message = LDAPToolUtils.getMessageForConnectionException(ae);
+          throw new IOException(message, ae);
         }
         else
         {
@@ -991,7 +992,6 @@
       connection.connectToHost(bindDNValue, bindPasswordValue, nextMessageID,
           timeout);
 
-
       ldapCompare = new LDAPCompare(nextMessageID, out, err);
       ldapCompare.isScriptFriendly = scriptFriendlyArgument.isPresent();
       if(fileNameValue == null && dnStrings.isEmpty())
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPDelete.java b/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPDelete.java
index 66545f2..1c5c127 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPDelete.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPDelete.java
@@ -201,7 +201,8 @@
         }
         if (!deleteOptions.continueOnError())
         {
-          throw new IOException(ae.getMessage());
+          String msg = LDAPToolUtils.getMessageForConnectionException(ae);
+          throw new IOException(msg, ae);
         }
         else
         {
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPModify.java b/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPModify.java
index d566cb6..4614352 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPModify.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPModify.java
@@ -98,13 +98,13 @@
   private static final String CLASS_NAME = "org.opends.server.tools.LDAPModify";
 
   // The message ID counter to use for requests.
-  private AtomicInteger nextMessageID;
+  private final AtomicInteger nextMessageID;
 
   // The print stream to use for standard error.
-  private PrintStream err;
+  private final PrintStream err;
 
   // The print stream to use for standard output.
-  private PrintStream out;
+  private final PrintStream out;
 
   // The LDIF file name.
   private String fileName = null;
@@ -352,7 +352,8 @@
           err.println(wrapText(ae.getMessage(), MAX_LINE_WIDTH));
           if (!modifyOptions.continueOnError())
           {
-            throw new IOException(ae.getMessage());
+            String msg = LDAPToolUtils.getMessageForConnectionException(ae);
+            throw new IOException(msg, ae);
           }
           return;
         }
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPToolUtils.java b/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPToolUtils.java
index 5cdcf93..5310fe2 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPToolUtils.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPToolUtils.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2006-2009 Sun Microsystems, Inc.
+ *      Copyright 2006-2010 Sun Microsystems, Inc.
  */
 package org.opends.server.tools;
 import org.opends.messages.Message;
@@ -31,7 +31,9 @@
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.PrintStream;
+import java.net.SocketTimeoutException;
 
+import org.opends.server.protocols.asn1.ASN1Exception;
 import org.opends.server.protocols.ldap.LDAPControl;
 import org.opends.server.protocols.ldap.LDAPResultCode;
 import org.opends.server.types.DN;
@@ -277,5 +279,43 @@
       err.println(ERR_TOOL_MATCHED_DN.get(matchedDN.toString()));
     }
   }
+
+  /**
+   * Returns the message to be displayed to the user when an exception occurs.
+   * <br>
+   * The code simply checks that the exception corresponds to a client side
+   * time out.
+   * @param ae the asn1exception that occurred connecting to the server or
+   * handling the response from the server.
+   * @return the message to be displayed to the user when an exception occurs.
+   */
+  public static String getMessageForConnectionException(ASN1Exception ae)
+  {
+    String msg;
+    Throwable cause = ae.getCause();
+    if (cause != null)
+    {
+      boolean isTimeout = false;
+      while (cause != null && !isTimeout)
+      {
+        isTimeout = cause instanceof SocketTimeoutException;
+        cause = cause.getCause();
+      }
+      if (isTimeout)
+      {
+        msg = ERR_CLIENT_SIDE_TIMEOUT.get(
+            ae.getMessageObject().toString()).toString();
+      }
+      else
+      {
+        msg = ae.getMessageObject().toString();
+      }
+    }
+    else
+    {
+      msg = ae.getMessageObject().toString();
+    }
+    return msg;
+  }
 }
 

--
Gitblit v1.10.0