mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

neil_a_wilson
08.05.2007 aa9e57a11efc74e6abba5a47da2305ce1f70c65e
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 Issue Numbers: 1231, 1234
3 files modified
118 ■■■■■ changed files
opends/src/server/org/opends/server/messages/ProtocolMessages.java 28 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/protocols/jmx/JmxConnectionHandler.java 37 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/protocols/ldap/LDAPConnectionHandler.java 53 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/messages/ProtocolMessages.java
@@ -4659,6 +4659,28 @@
  /**
   * The message ID for the message that will be used if the LDAP connection
   * handler cannot bind to a configured address:port.  This takes four
   * arguments, which are the configuration entry DN, IP address, port  number,
   * and a message explaining the problem that occurred.
   */
  public static final int MSGID_LDAP_CONNHANDLER_CANNOT_BIND =
       CATEGORY_MASK_PROTOCOL | SEVERITY_MASK_SEVERE_ERROR | 432;
  /**
   * The message ID for the message that will be used if the JMX connection
   * handler cannot bind to a configured port.  This takes three arguments,
   * which are the configuration entry DN, port  number, and a message
   * explaining the problem that occurred.
   */
  public static final int MSGID_JMX_CONNHANDLER_CANNOT_BIND =
       CATEGORY_MASK_PROTOCOL | SEVERITY_MASK_SEVERE_ERROR | 433;
  /**
   * Associates a set of generic messages with the message IDs defined in this
   * class.
   */
@@ -5914,6 +5936,9 @@
                    " of configuration entry %s has an invalid value %s " +
                    "which does not reference an enabled trust manager " +
                    "provider");
    registerMessage(MSGID_LDAP_CONNHANDLER_CANNOT_BIND,
                    "The LDAP connection handler defined in configuration " +
                    "entry %s was unable to bind to %s:%d:  %s");
    registerMessage(MSGID_LDAP_CONNHANDLER_CANNOT_DETERMINE_TRUSTMANAGER_DN,
                    "An error occurred while processing the " +
                    ATTR_TRUSTMANAGER_DN + " attribute in configuration " +
@@ -6448,6 +6473,9 @@
            ATTR_USE_SSL + " attribute in configuration entry %s, " +
            "which is used to indicate whether to use SSL when " +
            "accepting client connections:  %s");
    registerMessage(MSGID_JMX_CONNHANDLER_CANNOT_BIND,
            "The JMX connection handler defined in configuration entry %s " +
            "was unable to bind to port %d:  %s");
    registerMessage(MSGID_JMX_CONNHANDLER_DESCRIPTION_SSL_CERT_NICKNAME,
            "Specifies the nickname of the certificate that the " +
            "connection handler should use when accepting SSL-based " +
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 {
opends/src/server/org/opends/server/protocols/ldap/LDAPConnectionHandler.java
@@ -37,8 +37,10 @@
import static org.opends.server.util.ServerConstants.*;
import static org.opends.server.util.StaticUtils.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
@@ -571,9 +573,9 @@
  /**
   * {@inheritDoc}
   */
  public void initializeConnectionHandler(
      LDAPConnectionHandlerCfg config)
      throws ConfigException, InitializationException {
  public void initializeConnectionHandler(LDAPConnectionHandlerCfg config)
         throws ConfigException, InitializationException
  {
    // SSL and StartTLS are mutually exclusive.
    if (config.isAllowStartTLS() && config.isUseSSL()) {
      int msgID = MSGID_LDAP_CONNHANDLER_CANNOT_HAVE_SSL_AND_STARTTLS;
@@ -723,6 +725,50 @@
    // Perform any additional initialization that might be required.
    statTracker = new LDAPStatistics(handlerName + " Statistics");
    // Attempt to bind to the listen port on all configured addresses to
    // verify whether the connection handler will be able to start.
    LinkedList<ServerSocket> testListenSockets = new LinkedList<ServerSocket>();
    try
    {
      for (InetAddress a : listenAddresses)
      {
        try
        {
          ServerSocket s = new ServerSocket();
          s.setReuseAddress(true);
          s.bind(new InetSocketAddress(a, listenPort));
          testListenSockets.add(s);
        }
        catch (IOException e)
        {
          if (debugEnabled())
          {
            TRACER.debugCaught(DebugLogLevel.ERROR, e);
          }
          int    msgID   = MSGID_LDAP_CONNHANDLER_CANNOT_BIND;
          String message = getMessage(msgID, String.valueOf(config.dn()),
                                      a.getHostAddress(), listenPort,
                                      getExceptionMessage(e));
          logError(ErrorLogCategory.CONNECTION_HANDLING,
                   ErrorLogSeverity.SEVERE_ERROR, message, msgID);
          throw new InitializationException(msgID, message);
        }
      }
    }
    finally
    {
      for (ServerSocket s : testListenSockets)
      {
        try
        {
          s.close();
        } catch (Exception e) {}
      }
    }
    // Create and start the request handlers.
    requestHandlers = new LDAPRequestHandler[numRequestHandlers];
    for (int i = 0; i < numRequestHandlers; i++) {
@@ -733,6 +779,7 @@
      requestHandlers[i].start();
    }
    // Register this as a change listener.
    config.addLDAPChangeListener(this);
  }