From 32034d853f3a284424ccfa87b6de210f1ca814e1 Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Tue, 29 Nov 2011 00:31:21 +0000
Subject: [PATCH] Fix OPENDJ-43 (Synchronous Connection decorator implementations should not use AsynchronousConnections) and OPENDJ-328 (Make it easier to implement connection decorators).

---
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AbstractLoadBalancingAlgorithm.java |   58 +++++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 41 insertions(+), 17 deletions(-)

diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AbstractLoadBalancingAlgorithm.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AbstractLoadBalancingAlgorithm.java
index 9de0205..4d41dba 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AbstractLoadBalancingAlgorithm.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AbstractLoadBalancingAlgorithm.java
@@ -23,6 +23,7 @@
  *
  *
  *      Copyright 2010 Sun Microsystems, Inc.
+ *      Portions copyright 2011 ForgeRock AS.
  */
 
 package org.forgerock.opendj.ldap;
@@ -40,7 +41,6 @@
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.logging.Level;
 
-import com.forgerock.opendj.util.AbstractConnectionFactory;
 import com.forgerock.opendj.util.AsynchronousFutureResult;
 import com.forgerock.opendj.util.StaticUtils;
 import com.forgerock.opendj.util.Validator;
@@ -57,9 +57,8 @@
  */
 abstract class AbstractLoadBalancingAlgorithm implements LoadBalancingAlgorithm
 {
-  private final class MonitoredConnectionFactory extends
-      AbstractConnectionFactory implements
-      ResultHandler<AsynchronousConnection>
+  private final class MonitoredConnectionFactory implements ConnectionFactory,
+      ResultHandler<Connection>
   {
 
     private final ConnectionFactory factory;
@@ -84,28 +83,53 @@
     /**
      * {@inheritDoc}
      */
-    @Override
-    public FutureResult<AsynchronousConnection> getAsynchronousConnection(
-        final ResultHandler<? super AsynchronousConnection> resultHandler)
+    public Connection getConnection() throws ErrorResultException,
+        InterruptedException
     {
-      final AsynchronousFutureResult<AsynchronousConnection> future =
-        new AsynchronousFutureResult<AsynchronousConnection>(resultHandler);
+      final Connection connection;
+      try
+      {
+        connection = factory.getConnection();
+      }
+      catch (ErrorResultException e)
+      {
+        // Attempt failed - try next factory.
+        notifyOffline(e);
+        final int nextIndex = (index + 1) % monitoredFactories.size();
+        final MonitoredConnectionFactory nextFactory =
+            getMonitoredConnectionFactory(nextIndex);
+        return nextFactory.getConnection();
+      }
+      notifyOnline();
+      return connection;
+    }
 
-      final ResultHandler<AsynchronousConnection> failoverHandler =
-        new ResultHandler<AsynchronousConnection>()
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public FutureResult<Connection> getConnectionAsync(
+        final ResultHandler<? super Connection> resultHandler)
+    {
+      final AsynchronousFutureResult<Connection> future =
+        new AsynchronousFutureResult<Connection>(resultHandler);
+
+      final ResultHandler<Connection> failoverHandler =
+        new ResultHandler<Connection>()
       {
         @Override
         public void handleErrorResult(final ErrorResultException error)
         {
           // Attempt failed - try next factory.
           notifyOffline(error);
-
           final int nextIndex = (index + 1) % monitoredFactories.size();
           try
           {
             final MonitoredConnectionFactory nextFactory =
               getMonitoredConnectionFactory(nextIndex);
-            nextFactory.getAsynchronousConnection(future);
+            nextFactory.getConnectionAsync(future);
           }
           catch (final ErrorResultException e)
           {
@@ -116,14 +140,14 @@
 
 
         @Override
-        public void handleResult(final AsynchronousConnection result)
+        public void handleResult(final Connection result)
         {
           notifyOnline();
           future.handleResult(result);
         }
       };
 
-      factory.getAsynchronousConnection(failoverHandler);
+      factory.getConnectionAsync(failoverHandler);
       return future;
     }
 
@@ -144,7 +168,7 @@
      * Handle monitoring connection request success.
      */
     @Override
-    public void handleResult(final AsynchronousConnection connection)
+    public void handleResult(final Connection connection)
     {
       notifyOnline();
 
@@ -179,7 +203,7 @@
           StaticUtils.DEBUG_LOG.fine(String
               .format("Attempting reconnect to offline factory " + this));
         }
-        pendingConnectFuture = factory.getAsynchronousConnection(this);
+        pendingConnectFuture = factory.getConnectionAsync(this);
       }
     }
 

--
Gitblit v1.10.0