From aa9e57a11efc74e6abba5a47da2305ce1f70c65e Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Sun, 08 Jul 2007 00:05:17 +0000
Subject: [PATCH] Update the LDAP and JMX connection handlers so that they attempt to bind a server socket to the configured port for all appropriate addresses during the initialization phase.  This should provide a reliable mechanism for determining whether the connection handler will be allowed to start, and it will be more accurate and faster than the earlier attempt to achieve the same result using SetupUtils.canUseAsPort().

---
 opends/src/server/org/opends/server/protocols/jmx/JmxConnectionHandler.java |   37 ++++++++++++++++++++++++++++++++++---
 1 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/opends/src/server/org/opends/server/protocols/jmx/JmxConnectionHandler.java b/opends/src/server/org/opends/server/protocols/jmx/JmxConnectionHandler.java
index b27bb2c..ae86468 100644
--- a/opends/src/server/org/opends/server/protocols/jmx/JmxConnectionHandler.java
+++ b/opends/src/server/org/opends/server/protocols/jmx/JmxConnectionHandler.java
@@ -31,7 +31,10 @@
 import static org.opends.server.loggers.ErrorLogger.logError;
 import static org.opends.server.messages.MessageHandler.getMessage;
 import static org.opends.server.messages.ProtocolMessages.*;
+import static org.opends.server.util.StaticUtils.*;
 
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.LinkedHashMap;
@@ -308,9 +311,9 @@
   /**
    * {@inheritDoc}
    */
-  public void initializeConnectionHandler(
-      JMXConnectionHandlerCfg config)
-      throws ConfigException, InitializationException {
+  public void initializeConnectionHandler(JMXConnectionHandlerCfg config)
+         throws ConfigException, InitializationException
+  {
     // Validate the key manager provider DN.
     DN keyManagerProviderDN = config.getKeyManagerProviderDN();
     if (keyManagerProviderDN != null) {
@@ -340,6 +343,34 @@
     // Configuration is ok.
     currentConfig = config;
 
+
+    // Attempt to bind to the listen port to verify whether the connection
+    // handler will be able to start.
+    ServerSocket s = null;
+    try
+    {
+      s = new ServerSocket();
+      s.setReuseAddress(true);
+      s.bind(new InetSocketAddress(config.getListenPort()));
+    }
+    catch (Exception e)
+    {
+      int    msgID   = MSGID_JMX_CONNHANDLER_CANNOT_BIND;
+      String message = getMessage(msgID, String.valueOf(config.dn()),
+                                  config.getListenPort(),
+                                  getExceptionMessage(e));
+      logError(ErrorLogCategory.CONNECTION_HANDLING,
+               ErrorLogSeverity.SEVERE_ERROR, message, msgID);
+      throw new InitializationException(msgID, message);
+    }
+    finally
+    {
+      try
+      {
+        s.close();
+      } catch (Exception e) {}
+    }
+
     if (config.isUseSSL()) {
       protocol = "JMX+SSL";
     } else {

--
Gitblit v1.10.0