From e60baaef11af6bcd6e7aee73b9e304732bf9a16e Mon Sep 17 00:00:00 2001
From: Chris Ridd <chris.ridd@forgerock.com>
Date: Fri, 23 May 2014 15:13:23 +0000
Subject: [PATCH] Backport fix for OPENDJ-1466: Improve initialization of InetSocketAddress in SDK to prevent cached DNS data.

---
 opendj-ldap-sdk/src/main/java/com/forgerock/opendj/ldap/LDAPConnectionFactoryImpl.java |   51 ++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/ldap/LDAPConnectionFactoryImpl.java b/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/ldap/LDAPConnectionFactoryImpl.java
index 2f397d2..56a5a99 100644
--- a/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/ldap/LDAPConnectionFactoryImpl.java
+++ b/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/ldap/LDAPConnectionFactoryImpl.java
@@ -211,8 +211,8 @@
                 return timeoutEndTime - currentTime;
             } else {
                 future.handleErrorResult(newErrorResult(ResultCode.CLIENT_SIDE_CONNECT_ERROR,
-                        LDAP_CONNECTION_CONNECT_TIMEOUT.get(socketAddress.toString(), getTimeout())
-                                .toString()));
+                        LDAP_CONNECTION_CONNECT_TIMEOUT.get(getSocketAddress().toString(),
+                                getTimeout()).toString()));
                 return 0;
             }
         }
@@ -227,6 +227,8 @@
     private final FilterChain defaultFilterChain;
     private final LDAPOptions options;
     private final InetSocketAddress socketAddress;
+    private final String host;
+    private final int port;
 
     /**
      * Prevents the transport and timeoutChecker being released when there are
@@ -246,8 +248,8 @@
 
     /**
      * Creates a new LDAP connection factory implementation which can be used to
-     * create connections to the Directory Server at the provided host and port
-     * address using provided connection options.
+     * create connections to the Directory Server at the provided address using
+     * provided connection options.
      *
      * @param address
      *            The address of the Directory Server to connect to.
@@ -257,6 +259,8 @@
     public LDAPConnectionFactoryImpl(final InetSocketAddress address, final LDAPOptions options) {
         this.transport = DEFAULT_TRANSPORT.acquireIfNull(options.getTCPNIOTransport());
         this.socketAddress = address;
+        this.host = null;
+        this.port = -1;
         this.options = new LDAPOptions(options);
         this.clientFilter =
                 new LDAPClientFilter(new LDAPReader(this.options.getDecodeOptions()), 0);
@@ -264,6 +268,30 @@
                 FilterChainBuilder.stateless().add(new TransportFilter()).add(clientFilter).build();
     }
 
+    /**
+     * Creates a new LDAP connection factory implementation which can be used to
+     * create connections to the Directory Server at the provided host and port
+     * number using provided connection options.
+     *
+     * @param host
+     *            The host name of the Directory Server to connect to.
+     * @param port
+     *            The port number of the Directory Server to connect to.
+     * @param options
+     *            The LDAP connection options to use when creating connections.
+     */
+    public LDAPConnectionFactoryImpl(final String host, final int port, final LDAPOptions options) {
+        this.transport = DEFAULT_TRANSPORT.acquireIfNull(options.getTCPNIOTransport());
+        this.socketAddress = null;
+        this.host = host;
+        this.port = port;
+        this.options = new LDAPOptions(options);
+        this.clientFilter =
+              new LDAPClientFilter(new LDAPReader(this.options.getDecodeOptions()), 0);
+        this.defaultFilterChain =
+              FilterChainBuilder.stateless().add(new TransportFilter()).add(clientFilter).build();
+    }
+
     @Override
     public void close() {
         if (isClosed.compareAndSet(false, true)) {
@@ -289,7 +317,7 @@
                         .build();
         final AsynchronousFutureResult<Connection, ResultHandler<? super Connection>> future =
                 new AsynchronousFutureResult<Connection, ResultHandler<? super Connection>>(handler);
-        connectorHandler.connect(socketAddress, new CompletionHandlerAdapter(future));
+        connectorHandler.connect(getSocketAddress(), new CompletionHandlerAdapter(future));
         return future;
     }
 
@@ -299,14 +327,23 @@
      * @return The address of the Directory Server.
      */
     public InetSocketAddress getSocketAddress() {
-        return socketAddress;
+        if (socketAddress != null) {
+            return socketAddress;
+        }
+        return new InetSocketAddress(host, port);
     }
 
     @Override
     public String toString() {
         final StringBuilder builder = new StringBuilder();
         builder.append("LDAPConnectionFactory(");
-        builder.append(getSocketAddress().toString());
+        if (socketAddress != null) {
+            builder.append(socketAddress.toString());
+        } else {
+            builder.append(host);
+            builder.append(':');
+            builder.append(port);
+        }
         builder.append(')');
         return builder.toString();
     }

--
Gitblit v1.10.0