From f2090c0d863b07e3bad8d16a3efddfad6ff77960 Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Tue, 08 Oct 2013 14:16:54 +0000
Subject: [PATCH] Fix OPENDJ-346 Consider using java.util.ServiceLoader for loading extensions and requesting transport implementations This a a part of OPENDJ-175 - Decouple OpenDJ LDAP SDK from Grizzly  CR-2440

---
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/LDAPOptions.java |  133 +++++++++++++++++++++++++++++--------------
 1 files changed, 89 insertions(+), 44 deletions(-)

diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/LDAPOptions.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/LDAPOptions.java
index 4a8e8fc..9bcee09 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/LDAPOptions.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/LDAPOptions.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2010 Sun Microsystems, Inc.
- *      Portions copyright 2012 ForgeRock AS.
+ *      Portions copyright 2012-2013 ForgeRock AS.
  */
 
 package org.forgerock.opendj.ldap;
@@ -33,8 +33,6 @@
 
 import javax.net.ssl.SSLContext;
 
-import org.glassfish.grizzly.nio.transport.TCPNIOTransport;
-
 import com.forgerock.opendj.util.Validator;
 
 /**
@@ -63,18 +61,15 @@
     private DecodeOptions decodeOptions;
     private List<String> enabledCipherSuites = new LinkedList<String>();
     private List<String> enabledProtocols = new LinkedList<String>();
-    private TCPNIOTransport transport;
+    private ClassLoader providerClassLoader;
+    private String transportProvider;
 
     /**
      * Creates a new set of connection options with default settings. SSL will
      * not be enabled, and a default set of decode options will be used.
      */
     public LDAPOptions() {
-        this.sslContext = null;
-        this.timeoutInMillis = 0;
-        this.useStartTLS = false;
         this.decodeOptions = new DecodeOptions();
-        this.transport = null;
     }
 
     /**
@@ -91,7 +86,8 @@
         this.decodeOptions = new DecodeOptions(options.decodeOptions);
         this.enabledCipherSuites.addAll(options.getEnabledCipherSuites());
         this.enabledProtocols.addAll(options.getEnabledProtocols());
-        this.transport = options.transport;
+        this.providerClassLoader = options.providerClassLoader;
+        this.transportProvider = options.transportProvider;
     }
 
     /**
@@ -123,22 +119,6 @@
     }
 
     /**
-     * Returns the Grizzly TCP transport which will be used when initiating
-     * connections with the Directory Server.
-     * <p>
-     * By default this method will return {@code null} indicating that the
-     * default transport factory should be used to obtain a TCP transport.
-     *
-     * @return The Grizzly TCP transport which will be used when initiating
-     *         connections with the Directory Server, or {@code null} if the
-     *         default transport factory should be used to obtain a TCP
-     *         transport.
-     */
-    public final TCPNIOTransport getTCPNIOTransport() {
-        return transport;
-    }
-
-    /**
      * Returns the operation timeout in the specified unit.
      *
      * @param unit
@@ -187,25 +167,6 @@
     }
 
     /**
-     * Sets the Grizzly TCP transport which will be used when initiating
-     * connections with the Directory Server.
-     * <p>
-     * By default this method will return {@code null} indicating that the
-     * default transport factory will be used to obtain a TCP transport.
-     *
-     * @param transport
-     *            The Grizzly TCP transport which will be used when initiating
-     *            connections with the Directory Server, or {@code null} if the
-     *            default transport factory should be used to obtain a TCP
-     *            transport.
-     * @return A reference to this LDAP connection options.
-     */
-    public final LDAPOptions setTCPNIOTransport(final TCPNIOTransport transport) {
-        this.transport = transport;
-        return this;
-    }
-
-    /**
      * Sets the operation timeout. If the response is not received from the
      * Directory Server in the timeout period, the operation will be abandoned
      * and an error result returned. A timeout setting of 0 disables timeout
@@ -315,4 +276,88 @@
         return enabledCipherSuites;
     }
 
+    /**
+     * Gets the class loader which will be used to load the
+     * {@code TransportProvider}.
+     * <p>
+     * By default this method will return {@code null} indicating that the
+     * default class loader will be used.
+     * <p>
+     * The transport provider is loaded using {@code java.util.ServiceLoader},
+     * the JDK service-provider loading facility. The provider must be
+     * accessible from the same class loader that was initially queried to
+     * locate the configuration file; note that this is not necessarily the
+     * class loader from which the file was actually loaded. This method allows
+     * to provide a class loader to be used for loading the provider.
+     *
+     * @return The class loader which will be used when loading the transport
+     *         provider, or {@code null} if the default class loader should be
+     *         used.
+     */
+    public final ClassLoader getProviderClassLoader() {
+        return providerClassLoader;
+    }
+
+    /**
+     * Sets the class loader which will be used to load the
+     * {@code TransportProvider}.
+     * <p>
+     * The default class loader will be used if no class loader is set using
+     * this method.
+     * <p>
+     * The transport provider is loaded using {@code java.util.ServiceLoader},
+     * the JDK service-provider loading facility. The provider must be
+     * accessible from the same class loader that was initially queried to
+     * locate the configuration file; note that this is not necessarily the
+     * class loader from which the file was actually loaded. This method allows
+     * to provide a class loader to be used for loading the provider.
+     *
+     * @param classLoader
+     *            The class loader which will be used when loading the transport
+     *            provider, or {@code null} if the default class loader should
+     *            be used.
+     * @return A reference to this LDAP connection options.
+     */
+    public final LDAPOptions setProviderClassLoader(ClassLoader classLoader) {
+        this.providerClassLoader = classLoader;
+        return this;
+    }
+
+    /**
+     * Returns the name of the provider used for transport.
+     * <p>
+     * Transport providers implement {@code TransportProvider}
+     * interface.
+     * <p>
+     * The name should correspond to the name of an existing provider, as
+     * returned by {@code TransportProvider#getName()} method.
+     *
+     * @return The name of transport provider. The name is {@code null} if no
+     *         specific provider has been selected. In that case, the first
+     *         provider found will be used.
+     */
+    public String getTransportProvider() {
+        return transportProvider;
+    }
+
+    /**
+     * Sets the name of the provider to use for transport.
+     * <p>
+     * Transport providers implement {@code TransportProvider}
+     * interface.
+     * <p>
+     * The name should correspond to the name of an existing provider, as
+     * returned by {@code TransportProvider#getName()} method.
+     *
+     * @param providerName
+     *            The name of transport provider, or {@code null} if no specific
+     *            provider is preferred. In that case, the first provider found
+     *            will be used.
+     * @return A reference to this LDAP connection options.
+     */
+    public LDAPOptions setTransportProvider(String providerName) {
+        this.transportProvider = providerName;
+        return this;
+    }
+
 }

--
Gitblit v1.10.0