From 23de152d5ce528a0bec277441cab35d9cafea6df Mon Sep 17 00:00:00 2001
From: Ludovic Poitou <ludovic.poitou@forgerock.com>
Date: Thu, 03 Oct 2013 18:00:04 +0000
Subject: [PATCH] Fix OPENDJ-1161 - Allow configuration of RMI port in JMX connector. These changes are adding a new optional configuration parameter : rmi-port, to allow specifying a fixed port for the RMI connection underlying JMX. This is required when managing applications need to connect to OpenDJ through a firewall. CR-2429.

---
 opends/src/server/org/opends/server/protocols/jmx/JmxConnectionHandler.java |   97 ++++++++++++++++++++++++++++--------------------
 1 files changed, 56 insertions(+), 41 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 8b85df8..3f902ca 100644
--- a/opends/src/server/org/opends/server/protocols/jmx/JmxConnectionHandler.java
+++ b/opends/src/server/org/opends/server/protocols/jmx/JmxConnectionHandler.java
@@ -122,6 +122,10 @@
       portChanged = true;
     }
 
+    if (currentConfig.getRmiPort() != config.getRmiPort())
+    {
+      rmiConnectorRestart = true;
+    }
     if (currentConfig.isUseSSL() != config.isUseSSL()) {
       rmiConnectorRestart = true;
     }
@@ -205,9 +209,7 @@
   @Override
   public Map<String, String> getAlerts()
   {
-    Map<String, String> alerts = new LinkedHashMap<String, String>();
-
-    return alerts;
+    return new LinkedHashMap<String, String>();
   }
 
 
@@ -280,6 +282,14 @@
     return currentConfig.getListenPort();
   }
 
+  /**
+   * Get the JMX connection handler's rmi port.
+   *
+   * @return Returns the JMX connection handler's rmi port.
+   */
+  public int getRmiPort() {
+    return currentConfig.getRmiPort();
+  }
 
 
   /**
@@ -326,22 +336,11 @@
     // Configuration is ok.
     currentConfig = config;
 
-    // Attempt to bind to the listen port to verify whether the connection
-    // handler will be able to start.
-    try
+    List<Message> reasons = new LinkedList<Message>();
+    if (!isPortConfigurationAcceptable(String.valueOf(config.dn()),
+        config.getListenPort(), reasons))
     {
-      if (StaticUtils.isAddressInUse(
-        new InetSocketAddress(config.getListenPort()).getAddress(),
-        config.getListenPort(), true)) {
-        throw new IOException(
-          ERR_CONNHANDLER_ADDRESS_INUSE.get().toString());
-      }
-    }
-    catch (Exception e)
-    {
-      Message message =
-          ERR_CONNHANDLER_CANNOT_BIND.get("JMX", String.valueOf(config.dn()),
-              WILDCARD_ADDRESS, config.getListenPort(), getExceptionMessage(e));
+      Message message = reasons.get(0);
       logError(message);
       throw new InitializationException(message);
     }
@@ -399,7 +398,6 @@
   }
 
 
-
   /**
    * {@inheritDoc}
    */
@@ -409,32 +407,48 @@
   {
     JMXConnectionHandlerCfg config = (JMXConnectionHandlerCfg) configuration;
 
-    if ((currentConfig == null) ||
+    if (currentConfig == null ||
         (!currentConfig.isEnabled() && config.isEnabled()) ||
-        (currentConfig.getListenPort() != config.getListenPort())) {
-      // Attempt to bind to the listen port to verify whether the connection
-      // handler will be able to start.
-      try {
-        if (StaticUtils.isAddressInUse(
-          new InetSocketAddress(config.getListenPort()).getAddress(),
-          config.getListenPort(), true)) {
-          throw new IOException(
-            ERR_CONNHANDLER_ADDRESS_INUSE.get().toString());
-        }
-      } catch (Exception e) {
-        Message message =
-            ERR_CONNHANDLER_CANNOT_BIND.get("JMX", String.valueOf(config.dn()),
-                WILDCARD_ADDRESS, config.getListenPort(),
-                getExceptionMessage(e));
-        unacceptableReasons.add(message);
-        return false;
-      }
+        currentConfig.getListenPort() != config.getListenPort() &&
+        !isPortConfigurationAcceptable(String.valueOf(config.dn()),
+          config.getListenPort(), unacceptableReasons))
+    {
+      return false;
+    }
+
+    if (config.getRmiPort() != 0 &&
+        (currentConfig == null ||
+        (!currentConfig.isEnabled() && config.isEnabled()) ||
+        currentConfig.getRmiPort() != config.getRmiPort()) &&
+        !isPortConfigurationAcceptable(String.valueOf(config.dn()),
+          config.getRmiPort(), unacceptableReasons))
+    {
+      return false;
     }
 
     return isConfigurationChangeAcceptable(config, unacceptableReasons);
   }
 
-
+  /**
+   * Attempt to bind to the port to verify whether the connection
+   * handler will be able to start.
+   * @return true is the port is free to use, false otherwise.
+   */
+  private boolean isPortConfigurationAcceptable(String configDN,
+                      int newPort, List<Message> unacceptableReasons) {
+    try {
+      if (StaticUtils.isAddressInUse(
+          new InetSocketAddress(newPort).getAddress(), newPort, true)) {
+        throw new IOException(ERR_CONNHANDLER_ADDRESS_INUSE.get().toString());
+      }
+    } catch (Exception e) {
+      Message message = ERR_CONNHANDLER_CANNOT_BIND.get("JMX", configDN,
+              WILDCARD_ADDRESS, newPort, getExceptionMessage(e));
+      unacceptableReasons.add(message);
+      return false;
+    }
+    return true;
+  }
 
   /**
    * {@inheritDoc}
@@ -453,7 +467,7 @@
    * Determines whether or not clients are allowed to connect over JMX
    * using SSL.
    *
-   * @return Returns <code>true</code> if clients are allowed to
+   * @return Returns {@code true} if clients are allowed to
    *         connect over JMX using SSL.
    */
   public boolean isUseSSL() {
@@ -494,8 +508,9 @@
     {
       rmiConnector.initialize();
     }
-    catch (RuntimeException e)
+    catch (RuntimeException ignore)
     {
+      // Already caught and logged
     }
   }
 

--
Gitblit v1.10.0