From f5385bc8b3b571c48783a0fb3d08f5892a0c0fcf Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Fri, 28 Feb 2014 08:54:30 +0000
Subject: [PATCH] Additional fix for OPENDJ-1197: API is lacking functionality to specify TCP connect timeout

---
 opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/CommonLDAPOptions.java  |   72 ++++++++++++++---------------------
 opendj-sdk/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyUtils.java |   43 ++++++++++++++-------
 2 files changed, 57 insertions(+), 58 deletions(-)

diff --git a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/CommonLDAPOptions.java b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/CommonLDAPOptions.java
index c3c3bc5..e1b68e7 100644
--- a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/CommonLDAPOptions.java
+++ b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/CommonLDAPOptions.java
@@ -91,13 +91,13 @@
     }
 
     /**
-     * Returns the value of the {@link java.net.StandardSocketOptions#SO_LINGER
+     * Returns the value of the {@link java.net.SocketOptions#SO_LINGER
      * SO_LINGER} socket option for new connections.
      * <p>
      * The default setting is {@code -1} (disabled) and may be configured using
      * the {@code org.forgerock.opendj.io.linger} property.
      *
-     * @return The value of the {@link java.net.StandardSocketOptions#SO_LINGER
+     * @return The value of the {@link java.net.SocketOptions#SO_LINGER
      *         SO_LINGER} socket option for new connections, or -1 if linger
      *         should be disabled.
      */
@@ -144,48 +144,42 @@
     }
 
     /**
-     * Returns the value of the
-     * {@link java.net.StandardSocketOptions#SO_KEEPALIVE SO_KEEPALIVE} socket
-     * option for new connections.
+     * Returns the value of the {@link java.net.SocketOptions#SO_KEEPALIVE
+     * SO_KEEPALIVE} socket option for new connections.
      * <p>
      * The default setting is {@code true} and may be configured using the
      * {@code org.forgerock.opendj.io.keepAlive} property.
      *
-     * @return The value of the
-     *         {@link java.net.StandardSocketOptions#SO_KEEPALIVE SO_KEEPALIVE}
-     *         socket option for new connections.
+     * @return The value of the {@link java.net.SocketOptions#SO_KEEPALIVE
+     *         SO_KEEPALIVE} socket option for new connections.
      */
     public boolean isKeepAlive() {
         return keepAlive;
     }
 
     /**
-     * Returns the value of the
-     * {@link java.net.StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR} socket
-     * option for new connections.
+     * Returns the value of the {@link java.net.SocketOptions#SO_REUSEADDR
+     * SO_REUSEADDR} socket option for new connections.
      * <p>
      * The default setting is {@code true} and may be configured using the
      * {@code org.forgerock.opendj.io.reuseAddress} property.
      *
-     * @return The value of the
-     *         {@link java.net.StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR}
-     *         socket option for new connections.
+     * @return The value of the {@link java.net.SocketOptions#SO_REUSEADDR
+     *         SO_REUSEADDR} socket option for new connections.
      */
     public boolean isReuseAddress() {
         return reuseAddress;
     }
 
     /**
-     * Returns the value of the
-     * {@link java.net.StandardSocketOptions#TCP_NODELAY TCP_NODELAY} socket
-     * option for new connections.
+     * Returns the value of the {@link java.net.SocketOptions#TCP_NODELAY
+     * TCP_NODELAY} socket option for new connections.
      * <p>
      * The default setting is {@code true} and may be configured using the
      * {@code org.forgerock.opendj.io.tcpNoDelay} property.
      *
-     * @return The value of the
-     *         {@link java.net.StandardSocketOptions#TCP_NODELAY TCP_NODELAY}
-     *         socket option for new connections.
+     * @return The value of the {@link java.net.SocketOptions#TCP_NODELAY
+     *         TCP_NODELAY} socket option for new connections.
      */
     public boolean isTCPNoDelay() {
         return tcpNoDelay;
@@ -209,16 +203,14 @@
     }
 
     /**
-     * Specifies the value of the
-     * {@link java.net.StandardSocketOptions#SO_KEEPALIVE SO_KEEPALIVE} socket
-     * option for new connections.
+     * Specifies the value of the {@link java.net.SocketOptions#SO_KEEPALIVE
+     * SO_KEEPALIVE} socket option for new connections.
      * <p>
      * The default setting is {@code true} and may be configured using the
      * {@code org.forgerock.opendj.io.keepAlive} property.
      *
      * @param keepAlive
-     *            The value of the
-     *            {@link java.net.StandardSocketOptions#SO_KEEPALIVE
+     *            The value of the {@link java.net.SocketOptions#SO_KEEPALIVE
      *            SO_KEEPALIVE} socket option for new connections.
      * @return A reference to this set of options.
      */
@@ -228,18 +220,16 @@
     }
 
     /**
-     * Specifies the value of the
-     * {@link java.net.StandardSocketOptions#SO_LINGER SO_LINGER} socket option
-     * for new connections.
+     * Specifies the value of the {@link java.net.SocketOptions#SO_LINGER
+     * SO_LINGER} socket option for new connections.
      * <p>
      * The default setting is {@code -1} (disabled) and may be configured using
      * the {@code org.forgerock.opendj.io.linger} property.
      *
      * @param linger
-     *            The value of the
-     *            {@link java.net.StandardSocketOptions#SO_LINGER SO_LINGER}
-     *            socket option for new connections, or -1 if linger should be
-     *            disabled.
+     *            The value of the {@link java.net.SocketOptions#SO_LINGER
+     *            SO_LINGER} socket option for new connections, or -1 if linger
+     *            should be disabled.
      * @return A reference to this set of options.
      */
     public T setLinger(final int linger) {
@@ -273,16 +263,14 @@
     }
 
     /**
-     * Specifies the value of the
-     * {@link java.net.StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR} socket
-     * option for new connections.
+     * Specifies the value of the {@link java.net.SocketOptions#SO_REUSEADDR
+     * SO_REUSEADDR} socket option for new connections.
      * <p>
      * The default setting is {@code true} and may be configured using the
      * {@code org.forgerock.opendj.io.reuseAddress} property.
      *
      * @param reuseAddress
-     *            The value of the
-     *            {@link java.net.StandardSocketOptions#SO_REUSEADDR
+     *            The value of the {@link java.net.SocketOptions#SO_REUSEADDR
      *            SO_REUSEADDR} socket option for new connections.
      * @return A reference to this set of options.
      */
@@ -292,17 +280,15 @@
     }
 
     /**
-     * Specifies the value of the
-     * {@link java.net.StandardSocketOptions#TCP_NODELAY TCP_NODELAY} socket
-     * option for new connections.
+     * Specifies the value of the {@link java.net.SocketOptions#TCP_NODELAY
+     * TCP_NODELAY} socket option for new connections.
      * <p>
      * The default setting is {@code true} and may be configured using the
      * {@code org.forgerock.opendj.io.tcpNoDelay} property.
      *
      * @param tcpNoDelay
-     *            The value of the
-     *            {@link java.net.StandardSocketOptions#TCP_NODELAY TCP_NODELAY}
-     *            socket option for new connections.
+     *            The value of the {@link java.net.SocketOptions#TCP_NODELAY
+     *            TCP_NODELAY} socket option for new connections.
      * @return A reference to this set of options.
      */
     public T setTCPNoDelay(final boolean tcpNoDelay) {
diff --git a/opendj-sdk/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyUtils.java b/opendj-sdk/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyUtils.java
index b05f2e9..28b2485 100644
--- a/opendj-sdk/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyUtils.java
+++ b/opendj-sdk/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyUtils.java
@@ -25,9 +25,8 @@
  */
 package org.forgerock.opendj.grizzly;
 
-import java.io.IOException;
-import java.net.SocketOption;
-import java.net.StandardSocketOptions;
+import java.net.Socket;
+import java.net.SocketException;
 import java.nio.channels.SocketChannel;
 
 import org.forgerock.i18n.slf4j.LocalizedLogger;
@@ -196,19 +195,33 @@
 
         // Configure socket options.
         final SocketChannel channel = (SocketChannel) ((TCPNIOConnection) connection).getChannel();
-        setSocketOption(channel, StandardSocketOptions.TCP_NODELAY, tcpNoDelay, logger);
-        setSocketOption(channel, StandardSocketOptions.SO_KEEPALIVE, keepAlive, logger);
-        setSocketOption(channel, StandardSocketOptions.SO_REUSEADDR, reuseAddress, logger);
-        setSocketOption(channel, StandardSocketOptions.SO_LINGER, linger, logger);
-    }
-
-    private static <T> void setSocketOption(final SocketChannel channel,
-            final SocketOption<T> option, final T value, final LocalizedLogger logger) {
+        final Socket socket = channel.socket();
         try {
-            channel.setOption(option, value);
-        } catch (final IOException e) {
-            logger.traceException(e, "Unable to set " + option.name()
-                    + " to %d on client connection", value);
+            socket.setTcpNoDelay(tcpNoDelay);
+        } catch (final SocketException e) {
+            logger.traceException(e, "Unable to set TCP_NODELAY to %d on client connection",
+                    tcpNoDelay);
+        }
+        try {
+            socket.setKeepAlive(keepAlive);
+        } catch (final SocketException e) {
+            logger.traceException(e, "Unable to set SO_KEEPALIVE to %d on client connection",
+                    keepAlive);
+        }
+        try {
+            socket.setReuseAddress(reuseAddress);
+        } catch (final SocketException e) {
+            logger.traceException(e, "Unable to set SO_REUSEADDR to %d on client connection",
+                    reuseAddress);
+        }
+        try {
+            if (linger < 0) {
+                socket.setSoLinger(false, 0);
+            } else {
+                socket.setSoLinger(true, linger);
+            }
+        } catch (final SocketException e) {
+            logger.traceException(e, "Unable to set SO_LINGER to %d on client connection", linger);
         }
     }
 

--
Gitblit v1.10.0