From baefb23b50cf916538e061de157f1e9e81fc5a3c Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Wed, 08 Jan 2014 16:29:45 +0000
Subject: [PATCH] Fix OPENDJ-1270: Avoid unnecessary DNS lookups when performing bind requests

---
 opendj-core/src/main/java/org/forgerock/opendj/ldap/spi/LDAPConnectionFactoryImpl.java       |    6 
 opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java                       |    8 
 opendj-core/src/test/java/org/forgerock/opendj/ldap/spi/BasicLDAPConnectionFactory.java      |   10 
 opendj-core/src/test/java/org/forgerock/opendj/ldap/spi/BasicTransportProvider.java          |    8 
 opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyLDAPListener.java           |   31 +++-
 opendj-core/src/test/java/org/forgerock/opendj/ldap/LDAPServer.java                          |   10 
 opendj-core/src/main/java/org/forgerock/opendj/ldap/Connections.java                         |   49 ++++++++
 opendj-core/src/main/java/org/forgerock/opendj/ldap/spi/TransportProvider.java               |   16 +-
 opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyLDAPConnectionFactory.java  |   21 +--
 opendj-core/src/main/java/org/forgerock/opendj/ldap/spi/LDAPListenerImpl.java                |    6 
 opendj-core/src/main/java/org/forgerock/opendj/ldap/LDAPConnectionFactory.java               |   59 +++------
 opendj-core/src/test/java/org/forgerock/opendj/ldap/spi/BasicLDAPListener.java               |   10 
 opendj-grizzly/src/main/java/com/forgerock/opendj/grizzly/GrizzlyTransportProvider.java      |   16 +-
 opendj-grizzly/src/test/java/org/forgerock/opendj/grizzly/GrizzlyLDAPConnectionTestCase.java |    6 
 opendj-core/src/main/java/org/forgerock/opendj/ldap/LDAPListener.java                        |   57 +++------
 opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyLDAPConnection.java         |   10 -
 16 files changed, 165 insertions(+), 158 deletions(-)

diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/Connections.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/Connections.java
index 6598284..f8a2fab 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/Connections.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldap/Connections.java
@@ -22,13 +22,15 @@
  *
  *
  *      Copyright 2009-2010 Sun Microsystems, Inc.
- *      Portions copyright 2011-2013 ForgeRock AS
+ *      Portions copyright 2011-2014 ForgeRock AS
  */
 
 package org.forgerock.opendj.ldap;
 
 import static org.forgerock.opendj.ldap.RequestHandlerFactoryAdapter.adaptRequestHandler;
 
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 
@@ -677,6 +679,51 @@
         };
     }
 
+    /**
+     * Returns the host name associated with the provided
+     * {@code InetSocketAddress}, without performing a DNS lookup. This method
+     * attempts to provide functionality which is compatible with
+     * {@code InetSocketAddress.getHostString()} in JDK7. It can be removed once
+     * we drop support for JDK6.
+     *
+     * @param socketAddress
+     *            The socket address which is expected to be an instance of
+     *            {@code InetSocketAddress}.
+     * @return The host name associated with the provided {@code SocketAddress},
+     *         or {@code null} if it is unknown.
+     */
+    public static String getHostString(final InetSocketAddress socketAddress) {
+        /*
+         * See OPENDJ-1270 for more information about slow DNS queries.
+         *
+         * We must avoid calling getHostName() in the case where it is likely to
+         * perform a blocking DNS query. Ideally we would call getHostString(),
+         * but the method was only added in JDK7.
+         */
+        if (socketAddress.isUnresolved()) {
+            /*
+             * Usually socket addresses are resolved on creation. If the address
+             * is unresolved then there must be a user provided hostname instead
+             * and getHostName will not perform a reverse lookup.
+             */
+            return socketAddress.getHostName();
+        } else {
+            /*
+             * Simulate getHostString() by parsing the toString()
+             * representation. This assumes that the toString() representation
+             * is stable, which I assume it is because it is documented.
+             */
+            final InetAddress address = socketAddress.getAddress();
+            final String hostSlashIp = address.toString();
+            final int slashPos = hostSlashIp.indexOf('/');
+            if (slashPos == 0) {
+                return hostSlashIp.substring(1);
+            } else {
+                return hostSlashIp.substring(0, slashPos);
+            }
+        }
+    }
+
     // Prevent instantiation.
     private Connections() {
         // Do nothing.
diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/LDAPConnectionFactory.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/LDAPConnectionFactory.java
index 76e86e5..f3e4312 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/LDAPConnectionFactory.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldap/LDAPConnectionFactory.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2009-2010 Sun Microsystems, Inc.
- *      Portions copyright 2011-2013 ForgeRock AS.
+ *      Portions copyright 2011-2014 ForgeRock AS.
  */
 
 package org.forgerock.opendj.ldap;
@@ -31,7 +31,6 @@
 
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
-import java.net.SocketAddress;
 
 import org.forgerock.opendj.ldap.spi.LDAPConnectionFactoryImpl;
 import org.forgerock.opendj.ldap.spi.TransportProvider;
@@ -64,7 +63,7 @@
      * @throws ProviderNotFoundException if no provider is available or if the
      *             provider requested using options is not found.
      */
-    public LDAPConnectionFactory(final SocketAddress address) {
+    public LDAPConnectionFactory(final InetSocketAddress address) {
         this(address, new LDAPOptions());
     }
 
@@ -81,7 +80,7 @@
      * @throws ProviderNotFoundException if no provider is available or if the
      *             provider requested using options is not found.
      */
-    public LDAPConnectionFactory(final SocketAddress address, final LDAPOptions options) {
+    public LDAPConnectionFactory(final InetSocketAddress address, final LDAPOptions options) {
         Reject.ifNull(address, options);
         this.provider = getProvider(TransportProvider.class, options.getTransportProvider(),
                 options.getProviderClassLoader());
@@ -124,26 +123,19 @@
      */
     public LDAPConnectionFactory(final String host, final int port, final LDAPOptions options) {
         Reject.ifNull(host, options);
-        final SocketAddress address = new InetSocketAddress(host, port);
+        final InetSocketAddress address = new InetSocketAddress(host, port);
         this.provider = getProvider(TransportProvider.class, options.getTransportProvider(),
                 options.getProviderClassLoader());
         this.impl = provider.getLDAPConnectionFactory(address, options);
     }
 
     /**
-     * Returns the {@code InetAddress} that this LDAP listener is listening on.
+     * Returns the {@code InetAddress} of the Directory Server.
      *
-     * @return The {@code InetAddress} that this LDAP listener is listening on,
-     *         or {@code null} if it is unknown.
+     * @return The {@code InetAddress} of the Directory Server.
      */
     public InetAddress getAddress() {
-        final SocketAddress socketAddress = getSocketAddress();
-        if (socketAddress instanceof InetSocketAddress) {
-            final InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
-            return inetSocketAddress.getAddress();
-        } else {
-            return null;
-        }
+        return getSocketAddress().getAddress();
     }
 
     @Override
@@ -163,43 +155,32 @@
     }
 
     /**
-     * Returns the host name that this LDAP listener is listening on.
+     * Returns the host name of the Directory Server. The returned host name is
+     * the same host name that was provided during construction and may be an IP
+     * address. More specifically, this method will not perform a reverse DNS
+     * lookup.
      *
-     * @return The host name that this LDAP listener is listening on, or
-     *         {@code null} if it is unknown.
+     * @return The host name of the Directory Server.
      */
-    public String getHostname() {
-        final SocketAddress socketAddress = getSocketAddress();
-        if (socketAddress instanceof InetSocketAddress) {
-            final InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
-            return inetSocketAddress.getHostName();
-        } else {
-            return null;
-        }
+    public String getHostName() {
+        return Connections.getHostString(getSocketAddress());
     }
 
     /**
-     * Returns the port that this LDAP listener is listening on.
+     * Returns the port of the Directory Server.
      *
-     * @return The port that this LDAP listener is listening on, or {@code -1}
-     *         if it is unknown.
+     * @return The port of the Directory Server.
      */
     public int getPort() {
-        final SocketAddress socketAddress = getSocketAddress();
-        if (socketAddress instanceof InetSocketAddress) {
-            final InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
-            return inetSocketAddress.getPort();
-        } else {
-            return -1;
-        }
+        return getSocketAddress().getPort();
     }
 
     /**
-     * Returns the address used by the connections created by this factory.
+     * Returns the address of the Directory Server.
      *
-     * @return The address used by the connections.
+     * @return The address of the Directory Server.
      */
-    public SocketAddress getSocketAddress() {
+    public InetSocketAddress getSocketAddress() {
         return impl.getSocketAddress();
     }
 
diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/LDAPListener.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/LDAPListener.java
index e369b5f..c410a01 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/LDAPListener.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldap/LDAPListener.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2009-2010 Sun Microsystems, Inc.
- *      Portions copyright 2012 ForgeRock AS.
+ *      Portions copyright 2012-2014 ForgeRock AS.
  */
 
 package org.forgerock.opendj.ldap;
@@ -33,7 +33,6 @@
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
-import java.net.SocketAddress;
 
 import org.forgerock.opendj.ldap.spi.LDAPListenerImpl;
 import org.forgerock.opendj.ldap.spi.TransportProvider;
@@ -145,7 +144,7 @@
             final ServerConnectionFactory<LDAPClientContext, Integer> factory,
             final LDAPListenerOptions options) throws IOException {
         Reject.ifNull(factory, options);
-        final SocketAddress address = new InetSocketAddress(port);
+        final InetSocketAddress address = new InetSocketAddress(port);
         this.provider = getProvider(TransportProvider.class, options.getTransportProvider(),
                 options.getProviderClassLoader());
         this.impl = provider.getLDAPListener(address, factory, options);
@@ -166,7 +165,7 @@
      * @throws NullPointerException
      *             If {@code address} or {code factory} was {@code null}.
      */
-    public LDAPListener(final SocketAddress address,
+    public LDAPListener(final InetSocketAddress address,
             final ServerConnectionFactory<LDAPClientContext, Integer> factory) throws IOException {
         this(address, factory, new LDAPListenerOptions());
     }
@@ -189,7 +188,7 @@
      *             If {@code address}, {code factory}, or {@code options} was
      *             {@code null}.
      */
-    public LDAPListener(final SocketAddress address,
+    public LDAPListener(final InetSocketAddress address,
             final ServerConnectionFactory<LDAPClientContext, Integer> factory,
             final LDAPListenerOptions options) throws IOException {
         Reject.ifNull(address, factory, options);
@@ -244,7 +243,7 @@
             final ServerConnectionFactory<LDAPClientContext, Integer> factory,
             final LDAPListenerOptions options) throws IOException {
         Reject.ifNull(host, factory, options);
-        final SocketAddress address = new InetSocketAddress(host, port);
+        final InetSocketAddress address = new InetSocketAddress(host, port);
         this.provider = getProvider(TransportProvider.class, options.getTransportProvider(),
                 options.getProviderClassLoader());
         this.impl = provider.getLDAPListener(address, factory, options);
@@ -261,49 +260,31 @@
     /**
      * Returns the {@code InetAddress} that this LDAP listener is listening on.
      *
-     * @return The {@code InetAddress} that this LDAP listener is listening on,
-     *         or {@code null} if it is unknown.
+     * @return The {@code InetAddress} that this LDAP listener is listening on.
      */
     public InetAddress getAddress() {
-        final SocketAddress socketAddress = getSocketAddress();
-        if (socketAddress instanceof InetSocketAddress) {
-            final InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
-            return inetSocketAddress.getAddress();
-        } else {
-            return null;
-        }
+        return getSocketAddress().getAddress();
     }
 
     /**
-     * Returns the host name that this LDAP listener is listening on.
+     * Returns the host name that this LDAP listener is listening on. The
+     * returned host name is the same host name that was provided during
+     * construction and may be an IP address. More specifically, this method
+     * will not perform a reverse DNS lookup.
      *
-     * @return The host name that this LDAP listener is listening on, or
-     *         {@code null} if it is unknown.
+     * @return The host name that this LDAP listener is listening on.
      */
-    public String getHostname() {
-        final SocketAddress socketAddress = getSocketAddress();
-        if (socketAddress instanceof InetSocketAddress) {
-            final InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
-            return inetSocketAddress.getHostName();
-        } else {
-            return null;
-        }
+    public String getHostName() {
+        return Connections.getHostString(getSocketAddress());
     }
 
     /**
      * Returns the port that this LDAP listener is listening on.
      *
-     * @return The port that this LDAP listener is listening on, or {@code -1}
-     *         if it is unknown.
+     * @return The port that this LDAP listener is listening on.
      */
     public int getPort() {
-        final SocketAddress socketAddress = getSocketAddress();
-        if (socketAddress instanceof InetSocketAddress) {
-            final InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
-            return inetSocketAddress.getPort();
-        } else {
-            return -1;
-        }
+        return getSocketAddress().getPort();
     }
 
     /**
@@ -311,7 +292,7 @@
      *
      * @return The address that this LDAP listener is listening on.
      */
-    public SocketAddress getSocketAddress() {
+    public InetSocketAddress getSocketAddress() {
         return impl.getSocketAddress();
     }
 
@@ -325,9 +306,7 @@
         return provider.getName();
     }
 
-    /**
-     * {@inheritDoc}
-     */
+    @Override
     public String toString() {
         return impl.toString();
     }
diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/spi/LDAPConnectionFactoryImpl.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/spi/LDAPConnectionFactoryImpl.java
index ed5d466..bbd1c36 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/spi/LDAPConnectionFactoryImpl.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldap/spi/LDAPConnectionFactoryImpl.java
@@ -21,11 +21,11 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2013 ForgeRock AS.
+ *      Copyright 2013-2014 ForgeRock AS.
  */
 package org.forgerock.opendj.ldap.spi;
 
-import java.net.SocketAddress;
+import java.net.InetSocketAddress;
 
 import org.forgerock.opendj.ldap.ConnectionFactory;
 
@@ -47,6 +47,6 @@
      *
      * @return The address used by the connections.
      */
-    public SocketAddress getSocketAddress();
+    public InetSocketAddress getSocketAddress();
 
 }
diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/spi/LDAPListenerImpl.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/spi/LDAPListenerImpl.java
index d8952f9..ce001da 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/spi/LDAPListenerImpl.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldap/spi/LDAPListenerImpl.java
@@ -21,12 +21,12 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2013 ForgeRock AS.
+ *      Copyright 2013-2014 ForgeRock AS.
  */
 package org.forgerock.opendj.ldap.spi;
 
 import java.io.Closeable;
-import java.net.SocketAddress;
+import java.net.InetSocketAddress;
 
 /**
  * Interface for all classes that actually implement {@code LDAPListener}.
@@ -45,7 +45,7 @@
      *
      * @return The address that this LDAP listener is listening on.
      */
-    public SocketAddress getSocketAddress();
+    public InetSocketAddress getSocketAddress();
 
     /**
      * Closes this stream and releases any system resources associated
diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/spi/TransportProvider.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/spi/TransportProvider.java
index 4b5bdc7..d3f0841 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/spi/TransportProvider.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldap/spi/TransportProvider.java
@@ -21,12 +21,12 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2013 ForgeRock AS.
+ *      Copyright 2013-2014 ForgeRock AS.
  */
 package org.forgerock.opendj.ldap.spi;
 
 import java.io.IOException;
-import java.net.SocketAddress;
+import java.net.InetSocketAddress;
 
 import org.forgerock.opendj.ldap.LDAPClientContext;
 import org.forgerock.opendj.ldap.LDAPListenerOptions;
@@ -49,12 +49,13 @@
      * Returns an implementation of {@code LDAPConnectionFactory}.
      *
      * @param address
-     *          The address of the Directory Server to connect to.
+     *            The address of the Directory Server to connect to.
      * @param options
      *            The LDAP options to use when creating connections.
      * @return an implementation of {@code LDAPConnectionFactory}
      */
-    LDAPConnectionFactoryImpl getLDAPConnectionFactory(SocketAddress address, LDAPOptions options);
+    LDAPConnectionFactoryImpl getLDAPConnectionFactory(InetSocketAddress address,
+            LDAPOptions options);
 
     /**
      * Returns an implementation of {@code LDAPListener}.
@@ -71,9 +72,8 @@
      *             If an error occurred while trying to listen on the provided
      *             address.
      */
-    LDAPListenerImpl getLDAPListener(
-            SocketAddress address,
-            ServerConnectionFactory<LDAPClientContext, Integer> factory,
-            LDAPListenerOptions options) throws IOException;
+    LDAPListenerImpl getLDAPListener(InetSocketAddress address,
+            ServerConnectionFactory<LDAPClientContext, Integer> factory, LDAPListenerOptions options)
+            throws IOException;
 
 }
diff --git a/opendj-core/src/test/java/org/forgerock/opendj/ldap/LDAPServer.java b/opendj-core/src/test/java/org/forgerock/opendj/ldap/LDAPServer.java
index c1eed21..2b2ecbe 100644
--- a/opendj-core/src/test/java/org/forgerock/opendj/ldap/LDAPServer.java
+++ b/opendj-core/src/test/java/org/forgerock/opendj/ldap/LDAPServer.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2010 Sun Microsystems, Inc.
- *      Portions copyright 2011-2013 ForgeRock AS
+ *      Portions copyright 2011-2014 ForgeRock AS
  */
 
 package org.forgerock.opendj.ldap;
@@ -30,7 +30,7 @@
 import static org.forgerock.opendj.ldap.TestCaseUtils.findFreeSocketAddress;
 
 import java.io.IOException;
-import java.net.SocketAddress;
+import java.net.InetSocketAddress;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -226,8 +226,8 @@
                         final Map<String, String> props = new HashMap<String, String>();
                         props.put(Sasl.QOP, "auth-conf,auth-int,auth");
                         saslServer =
-                                Sasl.createSaslServer(saslMech, "ldap", clientContext
-                                        .getLocalAddress().getHostName(), props,
+                                Sasl.createSaslServer(saslMech, "ldap",
+                                        listener.getHostName(), props,
                                         new CallbackHandler() {
                                             public void handle(Callback[] callbacks)
                                                     throws IOException,
@@ -564,7 +564,7 @@
      *
      * @return The socket address of the server.
      */
-    public synchronized SocketAddress getSocketAddress() {
+    public synchronized InetSocketAddress getSocketAddress() {
         if (!isRunning) {
             throw new IllegalStateException("Server is not running");
         }
diff --git a/opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java b/opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java
index 3292513..944ad9f 100644
--- a/opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java
+++ b/opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2009-2010 Sun Microsystems, Inc.
- *      Portions copyright 2012-2013 ForgeRock AS.
+ *      Portions copyright 2012-2014 ForgeRock AS.
  */
 
 package org.forgerock.opendj.ldap;
@@ -98,14 +98,14 @@
      *
      * @return The free port.
      */
-    public static SocketAddress findFreeSocketAddress() {
+    public static InetSocketAddress findFreeSocketAddress() {
         try {
             ServerSocket serverLdapSocket = new ServerSocket();
             serverLdapSocket.setReuseAddress(true);
             serverLdapSocket.bind(new InetSocketAddress("127.0.0.1", 0));
             final SocketAddress address = serverLdapSocket.getLocalSocketAddress();
             serverLdapSocket.close();
-            return address;
+            return (InetSocketAddress) address;
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
@@ -147,7 +147,7 @@
      *
      * @return The socket address of the server.
      */
-    public static SocketAddress getServerSocketAddress() {
+    public static InetSocketAddress getServerSocketAddress() {
         return LDAPServer.getInstance().getSocketAddress();
     }
 
diff --git a/opendj-core/src/test/java/org/forgerock/opendj/ldap/spi/BasicLDAPConnectionFactory.java b/opendj-core/src/test/java/org/forgerock/opendj/ldap/spi/BasicLDAPConnectionFactory.java
index 0eafdd2..95c54ed 100644
--- a/opendj-core/src/test/java/org/forgerock/opendj/ldap/spi/BasicLDAPConnectionFactory.java
+++ b/opendj-core/src/test/java/org/forgerock/opendj/ldap/spi/BasicLDAPConnectionFactory.java
@@ -21,7 +21,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2013 ForgeRock AS.
+ *      Copyright 2013-2014 ForgeRock AS.
  */
 
 package org.forgerock.opendj.ldap.spi;
@@ -29,7 +29,7 @@
 import static org.forgerock.opendj.ldap.ErrorResultException.newErrorResult;
 import static org.mockito.Mockito.mock;
 
-import java.net.SocketAddress;
+import java.net.InetSocketAddress;
 
 import org.forgerock.opendj.ldap.Connection;
 import org.forgerock.opendj.ldap.ErrorResultException;
@@ -46,7 +46,7 @@
 public final class BasicLDAPConnectionFactory implements LDAPConnectionFactoryImpl {
 
     private final LDAPOptions options;
-    private final SocketAddress socketAddress;
+    private final InetSocketAddress socketAddress;
 
     /**
      * Creates a new LDAP connection factory which does nothing.
@@ -56,7 +56,7 @@
      * @param options
      *            The LDAP connection options to use when creating connections.
      */
-    public BasicLDAPConnectionFactory(final SocketAddress address, final LDAPOptions options) {
+    public BasicLDAPConnectionFactory(final InetSocketAddress address, final LDAPOptions options) {
         this.socketAddress = address;
         this.options = new LDAPOptions(options);
     }
@@ -89,7 +89,7 @@
      *
      * @return The address of the Directory Server.
      */
-    public SocketAddress getSocketAddress() {
+    public InetSocketAddress getSocketAddress() {
         return socketAddress;
     }
 
diff --git a/opendj-core/src/test/java/org/forgerock/opendj/ldap/spi/BasicLDAPListener.java b/opendj-core/src/test/java/org/forgerock/opendj/ldap/spi/BasicLDAPListener.java
index 646c604..435cd8e 100644
--- a/opendj-core/src/test/java/org/forgerock/opendj/ldap/spi/BasicLDAPListener.java
+++ b/opendj-core/src/test/java/org/forgerock/opendj/ldap/spi/BasicLDAPListener.java
@@ -21,13 +21,13 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2013 ForgeRock AS.
+ *      Copyright 2013-2014 ForgeRock AS.
  */
 
 package org.forgerock.opendj.ldap.spi;
 
 import java.io.IOException;
-import java.net.SocketAddress;
+import java.net.InetSocketAddress;
 
 import org.forgerock.opendj.ldap.LDAPClientContext;
 import org.forgerock.opendj.ldap.LDAPListenerOptions;
@@ -38,7 +38,7 @@
  */
 public final class BasicLDAPListener implements LDAPListenerImpl {
     private final ServerConnectionFactory<LDAPClientContext, Integer> connectionFactory;
-    private final SocketAddress socketAddress;
+    private final InetSocketAddress socketAddress;
 
     /**
      * Creates a new LDAP listener implementation which does nothing.
@@ -53,7 +53,7 @@
      * @throws IOException
      *             is never thrown with this do-nothing implementation
      */
-    public BasicLDAPListener(final SocketAddress address,
+    public BasicLDAPListener(final InetSocketAddress address,
             final ServerConnectionFactory<LDAPClientContext, Integer> factory,
             final LDAPListenerOptions options) throws IOException {
         this.connectionFactory = factory;
@@ -67,7 +67,7 @@
 
     /** {@inheritDoc} */
     @Override
-    public SocketAddress getSocketAddress() {
+    public InetSocketAddress getSocketAddress() {
         return socketAddress;
     }
 
diff --git a/opendj-core/src/test/java/org/forgerock/opendj/ldap/spi/BasicTransportProvider.java b/opendj-core/src/test/java/org/forgerock/opendj/ldap/spi/BasicTransportProvider.java
index 9468b9b..b8b3f5b 100644
--- a/opendj-core/src/test/java/org/forgerock/opendj/ldap/spi/BasicTransportProvider.java
+++ b/opendj-core/src/test/java/org/forgerock/opendj/ldap/spi/BasicTransportProvider.java
@@ -21,12 +21,12 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2013 ForgeRock AS.
+ *      Copyright 2013-2014 ForgeRock AS.
  */
 package org.forgerock.opendj.ldap.spi;
 
 import java.io.IOException;
-import java.net.SocketAddress;
+import java.net.InetSocketAddress;
 
 import org.forgerock.opendj.ldap.LDAPClientContext;
 import org.forgerock.opendj.ldap.LDAPListenerOptions;
@@ -50,14 +50,14 @@
 
     /** {@inheritDoc} */
     @Override
-    public LDAPConnectionFactoryImpl getLDAPConnectionFactory(SocketAddress address, LDAPOptions options) {
+    public LDAPConnectionFactoryImpl getLDAPConnectionFactory(InetSocketAddress address, LDAPOptions options) {
         return new BasicLDAPConnectionFactory(address, options);
     }
 
     /** {@inheritDoc} */
     @Override
     public LDAPListenerImpl getLDAPListener(
-            SocketAddress address,
+            InetSocketAddress address,
             ServerConnectionFactory<LDAPClientContext, Integer> factory,
             LDAPListenerOptions options)
             throws IOException {
diff --git a/opendj-grizzly/src/main/java/com/forgerock/opendj/grizzly/GrizzlyTransportProvider.java b/opendj-grizzly/src/main/java/com/forgerock/opendj/grizzly/GrizzlyTransportProvider.java
index 3ba263a..6357a83 100644
--- a/opendj-grizzly/src/main/java/com/forgerock/opendj/grizzly/GrizzlyTransportProvider.java
+++ b/opendj-grizzly/src/main/java/com/forgerock/opendj/grizzly/GrizzlyTransportProvider.java
@@ -21,12 +21,12 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2013 ForgeRock AS.
+ *      Copyright 2013-2014 ForgeRock AS.
  */
 package com.forgerock.opendj.grizzly;
 
 import java.io.IOException;
-import java.net.SocketAddress;
+import java.net.InetSocketAddress;
 
 import org.forgerock.opendj.grizzly.GrizzlyLDAPConnectionFactory;
 import org.forgerock.opendj.grizzly.GrizzlyLDAPListener;
@@ -43,23 +43,19 @@
  */
 public class GrizzlyTransportProvider implements TransportProvider {
 
-    /** {@inheritDoc} */
     @Override
-    public LDAPConnectionFactoryImpl getLDAPConnectionFactory(SocketAddress address, LDAPOptions options) {
+    public LDAPConnectionFactoryImpl getLDAPConnectionFactory(InetSocketAddress address,
+            LDAPOptions options) {
         return new GrizzlyLDAPConnectionFactory(address, options);
     }
 
-    /** {@inheritDoc} */
     @Override
-    public LDAPListenerImpl getLDAPListener(
-            SocketAddress address,
-            ServerConnectionFactory<LDAPClientContext, Integer> factory,
-            LDAPListenerOptions options)
+    public LDAPListenerImpl getLDAPListener(InetSocketAddress address,
+            ServerConnectionFactory<LDAPClientContext, Integer> factory, LDAPListenerOptions options)
             throws IOException {
         return new GrizzlyLDAPListener(address, factory, options);
     }
 
-    /** {@inheritDoc} */
     @Override
     public String getName() {
         return "Grizzly";
diff --git a/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyLDAPConnection.java b/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyLDAPConnection.java
index 75b65cf..55de201 100644
--- a/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyLDAPConnection.java
+++ b/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyLDAPConnection.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2010 Sun Microsystems, Inc.
- *      Portions Copyright 2011-2013 ForgeRock AS
+ *      Portions Copyright 2011-2014 ForgeRock AS
  */
 
 package org.forgerock.opendj.grizzly;
@@ -34,7 +34,6 @@
 import static org.forgerock.opendj.ldap.ErrorResultException.newErrorResult;
 
 import java.io.IOException;
-import java.net.InetSocketAddress;
 import java.security.GeneralSecurityException;
 import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
@@ -49,6 +48,7 @@
 import org.forgerock.opendj.io.LDAPWriter;
 import org.forgerock.opendj.ldap.AbstractAsynchronousConnection;
 import org.forgerock.opendj.ldap.ConnectionEventListener;
+import org.forgerock.opendj.ldap.Connections;
 import org.forgerock.opendj.ldap.ErrorResultException;
 import org.forgerock.opendj.ldap.FutureResult;
 import org.forgerock.opendj.ldap.IntermediateResponseHandler;
@@ -90,6 +90,7 @@
 import org.glassfish.grizzly.ssl.SSLFilter;
 
 import com.forgerock.opendj.util.CompletedFutureResult;
+
 import org.forgerock.util.Reject;
 
 /**
@@ -270,10 +271,7 @@
         final BindClient context;
         try {
             context =
-                    request.createBindClient(
-                            connection.getPeerAddress() instanceof InetSocketAddress
-                            ? ((InetSocketAddress) connection.getPeerAddress()).getHostName()
-                            : connection.getPeerAddress().toString());
+                    request.createBindClient(Connections.getHostString(factory.getSocketAddress()));
         } catch (final Exception e) {
             // FIXME: I18N need to have a better error message.
             // FIXME: Is this the best result code?
diff --git a/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyLDAPConnectionFactory.java b/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyLDAPConnectionFactory.java
index 56e7526..465ec23 100644
--- a/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyLDAPConnectionFactory.java
+++ b/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyLDAPConnectionFactory.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2010 Sun Microsystems, Inc.
- *      Portions copyright 2011-2013 ForgeRock AS
+ *      Portions copyright 2011-2014 ForgeRock AS
  */
 
 package org.forgerock.opendj.grizzly;
@@ -32,7 +32,7 @@
 import static org.forgerock.opendj.ldap.TimeoutChecker.TIMEOUT_CHECKER;
 
 import java.io.IOException;
-import java.net.SocketAddress;
+import java.net.InetSocketAddress;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -207,7 +207,7 @@
     private final LDAPClientFilter clientFilter;
     private final FilterChain defaultFilterChain;
     private final LDAPOptions options;
-    private final SocketAddress socketAddress;
+    private final InetSocketAddress socketAddress;
 
     /**
      * Prevents the transport and timeoutChecker being released when there are
@@ -235,7 +235,7 @@
      * @param options
      *            The LDAP connection options to use when creating connections.
      */
-    public GrizzlyLDAPConnectionFactory(final SocketAddress address, final LDAPOptions options) {
+    public GrizzlyLDAPConnectionFactory(final InetSocketAddress address, final LDAPOptions options) {
         this(address, options, null);
     }
 
@@ -253,13 +253,14 @@
      *            Grizzly TCP Transport NIO implementation to use for
      *            connections. If {@code null}, default transport will be used.
      */
-    public GrizzlyLDAPConnectionFactory(final SocketAddress address, final LDAPOptions options,
+    public GrizzlyLDAPConnectionFactory(final InetSocketAddress address, final LDAPOptions options,
             TCPNIOTransport transport) {
         this.transport = DEFAULT_TRANSPORT.acquireIfNull(transport);
         this.socketAddress = address;
         this.options = new LDAPOptions(options);
         this.clientFilter = new LDAPClientFilter(this.options.getDecodeOptions(), 0);
-        this.defaultFilterChain = GrizzlyUtils.buildFilterChain(this.transport.get().getProcessor(), clientFilter);
+        this.defaultFilterChain =
+                GrizzlyUtils.buildFilterChain(this.transport.get().getProcessor(), clientFilter);
 
     }
 
@@ -293,12 +294,8 @@
         return future;
     }
 
-    /**
-     * Returns the address of the Directory Server.
-     *
-     * @return The address of the Directory Server.
-     */
-    public SocketAddress getSocketAddress() {
+    @Override
+    public InetSocketAddress getSocketAddress() {
         return socketAddress;
     }
 
diff --git a/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyLDAPListener.java b/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyLDAPListener.java
index c525102..55ca916 100644
--- a/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyLDAPListener.java
+++ b/opendj-grizzly/src/main/java/org/forgerock/opendj/grizzly/GrizzlyLDAPListener.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2010 Sun Microsystems, Inc.
- *      Portions copyright 2011-2013 ForgeRock AS
+ *      Portions copyright 2011-2014 ForgeRock AS
  */
 
 package org.forgerock.opendj.grizzly;
@@ -31,9 +31,10 @@
 import static org.forgerock.opendj.grizzly.DefaultTCPNIOTransport.DEFAULT_TRANSPORT;
 
 import java.io.IOException;
-import java.net.SocketAddress;
+import java.net.InetSocketAddress;
 import java.util.concurrent.atomic.AtomicBoolean;
 
+import org.forgerock.opendj.ldap.Connections;
 import org.forgerock.opendj.ldap.DecodeOptions;
 import org.forgerock.opendj.ldap.LDAPClientContext;
 import org.forgerock.opendj.ldap.LDAPListenerOptions;
@@ -54,6 +55,7 @@
     private final ServerConnectionFactory<LDAPClientContext, Integer> connectionFactory;
     private final TCPNIOServerConnection serverConnection;
     private final AtomicBoolean isClosed = new AtomicBoolean();
+    private final InetSocketAddress socketAddress;
 
     /**
      * Creates a new LDAP listener implementation which will listen for LDAP
@@ -70,7 +72,7 @@
      *             If an error occurred while trying to listen on the provided
      *             address.
      */
-    public GrizzlyLDAPListener(final SocketAddress address,
+    public GrizzlyLDAPListener(final InetSocketAddress address,
             final ServerConnectionFactory<LDAPClientContext, Integer> factory,
             final LDAPListenerOptions options) throws IOException {
         this(address, factory, options, null);
@@ -95,19 +97,27 @@
      *             If an error occurred while trying to listen on the provided
      *             address.
      */
-    public GrizzlyLDAPListener(final SocketAddress address,
+    public GrizzlyLDAPListener(final InetSocketAddress address,
             final ServerConnectionFactory<LDAPClientContext, Integer> factory,
             final LDAPListenerOptions options, TCPNIOTransport transport) throws IOException {
         this.transport = DEFAULT_TRANSPORT.acquireIfNull(transport);
         this.connectionFactory = factory;
-
         final DecodeOptions decodeOptions = new DecodeOptions(options.getDecodeOptions());
-        final LDAPServerFilter serverFilter = new LDAPServerFilter(this, decodeOptions, options
-                .getMaxRequestSize());
-        final FilterChain ldapChain = GrizzlyUtils.buildFilterChain(this.transport.get().getProcessor(), serverFilter);
+        final LDAPServerFilter serverFilter =
+                new LDAPServerFilter(this, decodeOptions, options.getMaxRequestSize());
+        final FilterChain ldapChain =
+                GrizzlyUtils.buildFilterChain(this.transport.get().getProcessor(), serverFilter);
         final TCPNIOBindingHandler bindingHandler =
                 TCPNIOBindingHandler.builder(this.transport.get()).processor(ldapChain).build();
         this.serverConnection = bindingHandler.bind(address, options.getBacklog());
+
+        /*
+         * Get the socket address now, ensuring that the host is the same as the
+         * one provided in the constructor. The port will have changed if 0 was
+         * passed in.
+         */
+        final int port = ((InetSocketAddress) serverConnection.getLocalAddress()).getPort();
+        socketAddress = new InetSocketAddress(Connections.getHostString(address), port);
     }
 
     @Override
@@ -126,10 +136,9 @@
         }
     }
 
-    /** {@inheritDoc} */
     @Override
-    public SocketAddress getSocketAddress() {
-        return serverConnection.getLocalAddress();
+    public InetSocketAddress getSocketAddress() {
+        return socketAddress;
     }
 
     @Override
diff --git a/opendj-grizzly/src/test/java/org/forgerock/opendj/grizzly/GrizzlyLDAPConnectionTestCase.java b/opendj-grizzly/src/test/java/org/forgerock/opendj/grizzly/GrizzlyLDAPConnectionTestCase.java
index 81899cd..6ca0d90 100644
--- a/opendj-grizzly/src/test/java/org/forgerock/opendj/grizzly/GrizzlyLDAPConnectionTestCase.java
+++ b/opendj-grizzly/src/test/java/org/forgerock/opendj/grizzly/GrizzlyLDAPConnectionTestCase.java
@@ -21,7 +21,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2013 ForgeRock AS.
+ *      Copyright 2013-2014 ForgeRock AS.
  */
 
 package org.forgerock.opendj.grizzly;
@@ -31,7 +31,7 @@
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyZeroInteractions;
 
-import java.net.SocketAddress;
+import java.net.InetSocketAddress;
 import java.util.concurrent.TimeUnit;
 
 import org.forgerock.opendj.ldap.Connections;
@@ -76,7 +76,7 @@
     }
 
     private void doTestRequestTimeout(boolean isPersistentSearch) throws Exception {
-        SocketAddress address = TestCaseUtils.findFreeSocketAddress();
+        InetSocketAddress address = TestCaseUtils.findFreeSocketAddress();
 
         /*
          * Use a mock server implementation which will ignore incoming requests

--
Gitblit v1.10.0