From 03fe0954e42abf00746b8efa4c79ee74cf514427 Mon Sep 17 00:00:00 2001
From: ludovicp <ludovicp@localhost>
Date: Thu, 27 May 2010 15:10:50 +0000
Subject: [PATCH] Fix an issue with connection timeouts in CLI. The timeout is now configurable on CLI with the --connectTimeout option (expressed in milliseconds), the default is 30 000 milliseconds. This solves Issue 4196.

---
 opends/src/server/org/opends/server/tools/tasks/TaskTool.java |   41 +++++++++++++++++++++++++++++++++++------
 1 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/opends/src/server/org/opends/server/tools/tasks/TaskTool.java b/opends/src/server/org/opends/server/tools/tasks/TaskTool.java
index 86de53a..59b5679 100644
--- a/opends/src/server/org/opends/server/tools/tasks/TaskTool.java
+++ b/opends/src/server/org/opends/server/tools/tasks/TaskTool.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2007-2009 Sun Microsystems, Inc.
+ *      Copyright 2007-2010 Sun Microsystems, Inc.
  */
 
 package org.opends.server.tools.tasks;
@@ -62,7 +62,6 @@
 import java.util.EnumSet;
 import java.util.Collections;
 import java.io.IOException;
-import javax.net.ssl.SSLException;
 
 /**
  * Base class for tools that are capable of operating either by running
@@ -203,7 +202,8 @@
         null, null, INFO_DESCRIPTION_TASK_DEPENDENCY_ID.get());
       argParser.addArgument(dependencyArg, taskGroup);
 
-      Set fdaValSet = EnumSet.allOf(FailedDependencyAction.class);
+      Set<FailedDependencyAction> fdaValSet =
+        EnumSet.allOf(FailedDependencyAction.class);
       failedDependencyActionArg = new StringArgument(
         OPTION_LONG_FAILED_DEPENDENCY_ACTION,
         OPTION_SHORT_FAILED_DEPENDENCY_ACTION,
@@ -300,7 +300,8 @@
 
       String fda = failedDependencyActionArg.getValue();
       if (null == FailedDependencyAction.fromString(fda)) {
-        Set fdaValSet = EnumSet.allOf(FailedDependencyAction.class);
+        Set<FailedDependencyAction> fdaValSet =
+          EnumSet.allOf(FailedDependencyAction.class);
         throw new ArgumentException(ERR_TASKTOOL_INVALID_FDA.get(fda,
                         StaticUtils.collectionToString(fdaValSet, ",")));
       }
@@ -501,8 +502,9 @@
         ret = 0;
       } catch (LDAPConnectionException e) {
         Message message = null;
-        if ((e.getCause() != null) && (e.getCause().getCause() != null) &&
-          e.getCause().getCause() instanceof SSLException) {
+        if (isWrongPortException(e,
+            new Integer(argParser.getArguments().getPort())))
+        {
           message = ERR_TASK_LDAP_FAILED_TO_CONNECT_WRONG_PORT.get(
             argParser.getArguments().getHostName(),
             argParser.getArguments().getPort());
@@ -566,4 +568,31 @@
     }
     return returnValue;
   }
+
+  /**
+   * Returns {@code true} if the provided exception was caused by trying to
+   * connect to the wrong port and {@code false} otherwise.
+   * @param t the exception to be analyzed.
+   * @param port the port to which we tried to connect.
+   * @return {@code true} if the provided exception was caused by trying to
+   * connect to the wrong port and {@code false} otherwise.
+   */
+  private boolean isWrongPortException(Throwable t, int port)
+  {
+    boolean isWrongPortException = false;
+    boolean isDefaultClearPort = (port - 389) % 1000 == 0;
+    while (t != null && isDefaultClearPort)
+    {
+      isWrongPortException = t instanceof java.net.SocketTimeoutException;
+      if (!isWrongPortException)
+      {
+        t = t.getCause();
+      }
+      else
+      {
+        break;
+      }
+    }
+    return isWrongPortException;
+  }
 }

--
Gitblit v1.10.0