From 396d76a4ef45d048c11ce5f60edb294a98f7e7b7 Mon Sep 17 00:00:00 2001
From: matthew_swift <matthew_swift@localhost>
Date: Tue, 08 Dec 2009 11:41:02 +0000
Subject: [PATCH] Add Connection.isClosed, improve AsynchronousConnection.isClosed Javadoc, add comments to ConnectionPool and move it to top-level package with other connection factory decorators.

---
 opendj-sdk/sdk/src/org/opends/sdk/Connection.java             |   30 +++++++-------
 opendj-sdk/sdk/src/org/opends/sdk/AsynchronousConnection.java |   14 +++++--
 opendj-sdk/sdk/src/org/opends/sdk/ConnectionPool.java         |   42 ++++++++++++++-------
 opendj-sdk/sdk/src/org/opends/sdk/SynchronousConnection.java  |   10 +++++
 4 files changed, 63 insertions(+), 33 deletions(-)

diff --git a/opendj-sdk/sdk/src/org/opends/sdk/AsynchronousConnection.java b/opendj-sdk/sdk/src/org/opends/sdk/AsynchronousConnection.java
index 89c3c06..b0d8041 100644
--- a/opendj-sdk/sdk/src/org/opends/sdk/AsynchronousConnection.java
+++ b/opendj-sdk/sdk/src/org/opends/sdk/AsynchronousConnection.java
@@ -37,6 +37,7 @@
 import org.opends.sdk.responses.Result;
 
 
+
 /**
  * An asynchronous connection with a Directory Server over which read
  * and update operations may be performed. See RFC 4511 for the LDAPv3
@@ -268,6 +269,8 @@
    */
   void close(UnbindRequest request, String reason);
 
+
+
   /**
    * Compares an entry in the Directory Server using the provided
    * compare request.
@@ -495,12 +498,15 @@
       throws NullPointerException;
 
 
+
   /**
-   * Returns <code>true</code> if the connection is closed for
-   * <code>false</code> otherwise.
+   * Indicates whether or not this connection has been explicitly closed
+   * by calling {@code close}. This method will not return {@code true}
+   * if a fatal error has occurred on the connection unless {@code
+   * close} has been called.
    *
-   * @return <code>true</code> if the connection is closed for
-   *         <code>false</code> otherwise.
+   * @return {@code true} if this connection has been explicitly closed
+   *         by calling {@code close}, or {@code false} otherwise.
    */
   boolean isClosed();
 }
diff --git a/opendj-sdk/sdk/src/org/opends/sdk/Connection.java b/opendj-sdk/sdk/src/org/opends/sdk/Connection.java
index 14796ea..9aa99ea 100644
--- a/opendj-sdk/sdk/src/org/opends/sdk/Connection.java
+++ b/opendj-sdk/sdk/src/org/opends/sdk/Connection.java
@@ -38,7 +38,6 @@
 
 
 
-
 /**
  * A synchronous connection with a Directory Server over which read and
  * update operations may be performed. See RFC 4511 for the LDAPv3
@@ -307,7 +306,8 @@
    * @throws NullPointerException
    *           If {@code request} was {@code null}.
    */
-  void close(UnbindRequest request, String reason) throws NullPointerException;
+  void close(UnbindRequest request, String reason)
+      throws NullPointerException;
 
 
 
@@ -513,19 +513,19 @@
 
 
 
-  // /**
-  // * Indicates whether or not this connection has been explicitly
-  // closed
-  // * by calling {@code close}. This method will not return {@code
-  // true}
-  // * if a fatal error has occurred on the connection unless {@code
-  // * close} has been called.
-  // *
-  // * @return {@code true} if this connection has been explicitly
-  // closed
-  // * by calling {@code close}, or {@code false} otherwise.
-  // */
-  // boolean isClosed();
+  /**
+   * Indicates whether or not this connection has been explicitly closed
+   * by calling {@code close}. This method will not return {@code true}
+   * if a fatal error has occurred on the connection unless {@code
+   * close} has been called.
+   *
+   * @return {@code true} if this connection has been explicitly closed
+   *         by calling {@code close}, or {@code false} otherwise.
+   */
+  boolean isClosed();
+
+
+
   //
   //
   //
diff --git a/opendj-sdk/sdk/src/org/opends/sdk/ldap/ConnectionPool.java b/opendj-sdk/sdk/src/org/opends/sdk/ConnectionPool.java
similarity index 93%
rename from opendj-sdk/sdk/src/org/opends/sdk/ldap/ConnectionPool.java
rename to opendj-sdk/sdk/src/org/opends/sdk/ConnectionPool.java
index cfadeeb..398869c 100644
--- a/opendj-sdk/sdk/src/org/opends/sdk/ldap/ConnectionPool.java
+++ b/opendj-sdk/sdk/src/org/opends/sdk/ConnectionPool.java
@@ -25,7 +25,7 @@
  *      Copyright 2009 Sun Microsystems, Inc.
  */
 
-package org.opends.sdk.ldap;
+package org.opends.sdk;
 
 import java.util.Stack;
 import java.util.concurrent.ConcurrentLinkedQueue;
@@ -44,19 +44,21 @@
 import com.sun.opends.sdk.util.StaticUtils;
 
 /**
- * Created by IntelliJ IDEA. User: digitalperk Date: Nov 25, 2009 Time: 11:12:29
- * AM To change this template use File | Settings | File Templates.
+ * A simple connection pool implementation.
  */
 public class ConnectionPool
     extends AbstractConnectionFactory<AsynchronousConnection> {
   private final ConnectionFactory<?> connectionFactory;
   private volatile int numConnections;
   private final int poolSize;
+
+  // FIXME: should use a better collection than this - CLQ?
   private final Stack<AsynchronousConnection> pool;
+
   private final ConcurrentLinkedQueue<PendingConnectionFuture<?>> pendingFutures;
   private final Object lock = new Object();
 
-  private class PooledConnectionWapper
+  private final class PooledConnectionWapper
       implements AsynchronousConnection, ConnectionEventListener {
     private AsynchronousConnection connection;
 
@@ -244,6 +246,11 @@
         pool.remove(this);
         numConnections--;
         connection.removeConnectionEventListener(this);
+
+        // FIXME: should still close the connection, but we need to be
+        // careful that users of the pooled connection get a sensible
+        // error if they continue to use it (i.e. not an NPE or ISE).
+
         if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINE))
         {
           StaticUtils.DEBUG_LOG.finest(String
@@ -255,11 +262,11 @@
     }
   }
 
-  public class CompletedConnectionFuture
+  private static final class CompletedConnectionFuture
       implements ConnectionFuture<AsynchronousConnection> {
     private final PooledConnectionWapper connection;
 
-    public CompletedConnectionFuture(PooledConnectionWapper connection) {
+    private CompletedConnectionFuture(PooledConnectionWapper connection) {
       this.connection = connection;
     }
 
@@ -286,7 +293,7 @@
     }
   }
 
-  public class PendingConnectionFuture<P>
+  private final class PendingConnectionFuture<P>
       implements ConnectionFuture<AsynchronousConnection> {
     private volatile boolean isCancelled;
     private volatile PooledConnectionWapper connection;
@@ -296,12 +303,7 @@
     private final P p;
     private final CountDownLatch latch = new CountDownLatch(1);
 
-    public PendingConnectionFuture() {
-      this.handler = null;
-      this.p = null;
-    }
-
-    public PendingConnectionFuture(
+    private PendingConnectionFuture(
         P p,
         ConnectionResultHandler<? super AsynchronousConnection, P> handler) {
       this.handler = handler;
@@ -355,6 +357,18 @@
     }
   }
 
+
+
+  /**
+   * Creates a new connection pool which will maintain {@code poolSize}
+   * connections created using the provided connection factory.
+   *
+   * @param connectionFactory
+   *          The connection factory to use for creating new
+   *          connections.
+   * @param poolSize
+   *          The maximum size of the connection pool.
+   */
   public ConnectionPool(ConnectionFactory<?> connectionFactory, int poolSize) {
     this.connectionFactory = connectionFactory;
     this.poolSize = poolSize;
@@ -362,7 +376,7 @@
     this.pendingFutures = new ConcurrentLinkedQueue<PendingConnectionFuture<?>>();
   }
 
-  private class WrapConnectionResultHandler
+  private final class WrapConnectionResultHandler
       implements ConnectionResultHandler<AsynchronousConnection, Void> {
     private final PendingConnectionFuture<?> future;
 
diff --git a/opendj-sdk/sdk/src/org/opends/sdk/SynchronousConnection.java b/opendj-sdk/sdk/src/org/opends/sdk/SynchronousConnection.java
index af00ba9..d1bd6ef 100644
--- a/opendj-sdk/sdk/src/org/opends/sdk/SynchronousConnection.java
+++ b/opendj-sdk/sdk/src/org/opends/sdk/SynchronousConnection.java
@@ -260,4 +260,14 @@
     }
   }
 
+
+
+  /**
+   * {@inheritDoc}
+   */
+  public boolean isClosed()
+  {
+    return connection.isClosed();
+  }
+
 }

--
Gitblit v1.10.0