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

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

diff --git a/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/ldap/GrizzlyUtils.java b/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/ldap/GrizzlyUtils.java
index b9002d0..4ded303 100644
--- a/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/ldap/GrizzlyUtils.java
+++ b/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/ldap/GrizzlyUtils.java
@@ -27,9 +27,8 @@
 
 import static com.forgerock.opendj.util.StaticUtils.DEBUG_LOG;
 
-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 java.util.logging.Level;
 
@@ -50,18 +49,33 @@
 
         // Configure socket options.
         final SocketChannel channel = (SocketChannel) ((TCPNIOConnection) connection).getChannel();
-        setSocketOption(channel, StandardSocketOptions.TCP_NODELAY, tcpNoDelay);
-        setSocketOption(channel, StandardSocketOptions.SO_KEEPALIVE, keepAlive);
-        setSocketOption(channel, StandardSocketOptions.SO_REUSEADDR, reuseAddress);
-        setSocketOption(channel, StandardSocketOptions.SO_LINGER, linger);
-    }
-
-    private static <T> void setSocketOption(final SocketChannel channel,
-            final SocketOption<T> option, final T value) {
+        final Socket socket = channel.socket();
         try {
-            channel.setOption(option, value);
-        } catch (final IOException e) {
-            DEBUG_LOG.log(Level.FINE, "Unable to set " + option.name() + " to " + value
+            socket.setTcpNoDelay(tcpNoDelay);
+        } catch (final SocketException e) {
+            DEBUG_LOG.log(Level.FINE, "Unable to set TCP_NODELAY to " + tcpNoDelay
+                    + " on client connection", e);
+        }
+        try {
+            socket.setKeepAlive(keepAlive);
+        } catch (final SocketException e) {
+            DEBUG_LOG.log(Level.FINE, "Unable to set SO_KEEPALIVE to " + keepAlive
+                    + " on client connection", e);
+        }
+        try {
+            socket.setReuseAddress(reuseAddress);
+        } catch (final SocketException e) {
+            DEBUG_LOG.log(Level.FINE, "Unable to set SO_REUSEADDR to " + reuseAddress
+                    + " on client connection", e);
+        }
+        try {
+            if (linger < 0) {
+                socket.setSoLinger(false, 0);
+            } else {
+                socket.setSoLinger(true, linger);
+            }
+        } catch (final SocketException e) {
+            DEBUG_LOG.log(Level.FINE, "Unable to set SO_LINGER to " + linger
                     + " on client connection", e);
         }
     }
diff --git a/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/CommonLDAPOptions.java b/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/CommonLDAPOptions.java
index 7dac273..05b0cce 100644
--- a/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/CommonLDAPOptions.java
+++ b/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/CommonLDAPOptions.java
@@ -90,13 +90,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.
      */
@@ -105,48 +105,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;
@@ -170,16 +164,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.
      */
@@ -189,18 +181,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) {
@@ -209,16 +199,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.
      */
@@ -228,17 +216,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) {

--
Gitblit v1.10.0