You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ps...@apache.org on 2013/08/09 07:20:37 UTC

svn commit: r1512140 [1/2] - in /commons/proper/pool/trunk/src: main/java/org/apache/commons/pool2/ main/java/org/apache/commons/pool2/impl/ test/java/org/apache/commons/pool2/ test/java/org/apache/commons/pool2/impl/ test/java/org/apache/commons/pool2...

Author: psteitz
Date: Fri Aug  9 05:20:37 2013
New Revision: 1512140

URL: http://svn.apache.org/r1512140
Log:
Eliminated PoolableObjectFactory, KeyedPoolableObjectFactory and replaced them with PooledObjectFactory, KeyedPooledObjectFactory, the (formerly) subclasses that create and operate on PooledObjects.  Similarly replaced the base factories with PooledObject versions.  Modified SoftReferenceObjectPool to manage PooledObjects.

Added:
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/BaseKeyedPooledObjectFactory.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/BasePooledObjectFactory.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/PooledSoftReference.java
Removed:
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/BaseKeyedPoolableObjectFactory.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/BasePoolableObjectFactory.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/KeyedPoolableObjectFactory.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/PoolableObjectFactory.java
Modified:
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/KeyedObjectPool.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/KeyedPooledObjectFactory.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/PoolUtils.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/PooledObjectFactory.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/PoolImplUtils.java
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/SoftReferenceObjectPool.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/MethodCallPoolableObjectFactory.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestBaseKeyedPoolableObjectFactory.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestBaseObjectPool.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestBasePoolableObjectFactory.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestObjectPool.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestPoolUtils.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/VisitTrackerFactory.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/WaiterFactory.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestAbandonedObjectPool.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPoolClassLoaders.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestSoftRefOutOfMemory.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/impl/TestSoftReferenceObjectPool.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/performance/PerformanceTest.java
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/performance/SleepingObjectFactory.java

Added: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/BaseKeyedPooledObjectFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/BaseKeyedPooledObjectFactory.java?rev=1512140&view=auto
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/BaseKeyedPooledObjectFactory.java (added)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/BaseKeyedPooledObjectFactory.java Fri Aug  9 05:20:37 2013
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.pool2;
+
+import org.apache.commons.pool2.impl.DefaultPooledObject;
+
+/**
+ * A base implementation of <code>KeyedPooledObjectFactory</code>.
+ * <p>
+ * All operations defined here are essentially no-op's.
+ * </p>
+ * This class is immutable, and therefore thread-safe.
+ *
+ * @see KeyedPooledObjectFactory
+ *
+ * @param <K> The type of keys managed by this factory.
+ * @param <V> Type of element managed by this factory.
+ *
+ * @version $Revision: 1333925 $
+ *
+ * @since 2.0
+ */
+public abstract class BaseKeyedPooledObjectFactory<K,V>
+        implements KeyedPooledObjectFactory<K,V> {
+
+    /**
+     * Create an instance that can be served by the pool.
+     *
+     * @param key the key used when constructing the object
+     * @return an instance that can be served by the pool
+     */
+    public abstract V create(K key)
+        throws Exception;
+    
+    public PooledObject<V> makeObject(K key) throws Exception {
+        return new DefaultPooledObject<V>(create(key));
+    }
+
+    /**
+     * Destroy an instance no longer needed by the pool.
+     * <p>
+     * The default implementation is a no-op.
+     *
+     * @param key the key used when selecting the instance
+     * @param p a {@code PooledObject} wrapping the the instance to be destroyed
+     */
+    @Override
+    public void destroyObject(K key, PooledObject<V> p)
+        throws Exception {
+    }
+
+    /**
+     * Ensures that the instance is safe to be returned by the pool.
+     * <p>
+     * The default implementation always returns <tt>true</tt>.
+     *
+     * @param key the key used when selecting the object
+     * @param p a {@code PooledObject} wrapping the the instance to be validated
+     * @return always <code>true</code> in the default implementation
+     */
+    @Override
+    public boolean validateObject(K key, PooledObject<V> p) {
+        return true;
+    }
+
+    /**
+     * Reinitialize an instance to be returned by the pool.
+     * <p>
+     * The default implementation is a no-op.
+     *
+     * @param key the key used when selecting the object
+     * @param p a {@code PooledObject} wrapping the the instance to be activated
+     */
+    @Override
+    public void activateObject(K key, PooledObject<V> p)
+        throws Exception {
+    }
+
+    /**
+     * Uninitialize an instance to be returned to the idle object pool.
+     * <p>
+     * The default implementation is a no-op.
+     *
+     * @param key the key used when selecting the object
+     * @param p a {@code PooledObject} wrapping the the instance to be passivated
+     */
+    @Override
+    public void passivateObject(K key, PooledObject<V> p)
+        throws Exception {
+    }
+}

Added: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/BasePooledObjectFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/BasePooledObjectFactory.java?rev=1512140&view=auto
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/BasePooledObjectFactory.java (added)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/BasePooledObjectFactory.java Fri Aug  9 05:20:37 2013
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.pool2;
+
+import org.apache.commons.pool2.impl.DefaultPooledObject;
+
+/**
+ * A base implementation of <code>PoolableObjectFactory</code>.
+ * <p>
+ * All operations defined here are essentially no-op's.
+ * <p>
+ * This class is immutable, and therefore thread-safe
+ *
+ * @param <T> Type of element managed in this factory.
+ *
+ * @see PoolableObjectFactory
+ * @see BaseKeyedPoolableObjectFactory
+ *
+ * @version $Revision: 1333925 $
+ *
+ * @since 2.0
+ */
+public abstract class BasePooledObjectFactory<T> implements PooledObjectFactory<T> {
+    /**
+     * Creates an object instance, to be wrapped in a {@link PooledObject}.
+     * <p>This method <strong>must</strong> support concurrent, multi-threaded
+     * activation.</p>
+     * 
+     * @return an instance to be served by the pool
+     */
+    public abstract T create() throws Exception;
+    
+    @Override
+    public PooledObject<T> makeObject() throws Exception {
+        return new DefaultPooledObject<T>(create());
+    }
+
+    /**
+     *  No-op.
+     *
+     *  @param p ignored
+     */
+    @Override
+    public void destroyObject(PooledObject<T> p)
+        throws Exception  {
+    }
+
+    /**
+     * This implementation always returns {@code true}.
+     *
+     * @param p ignored
+     *
+     * @return {@code true}
+     */
+    @Override
+    public boolean validateObject(PooledObject<T> p) {
+        return true;
+    }
+
+    /**
+     *  No-op.
+     *
+     *  @param p ignored
+     */
+    @Override
+    public void activateObject(PooledObject<T> p) throws Exception {
+    }
+
+    /**
+     *  No-op.
+     *
+     * @param p ignored
+     */
+    @Override
+    public void passivateObject(PooledObject<T> p)
+        throws Exception {
+    }
+}

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/KeyedObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/KeyedObjectPool.java?rev=1512140&r1=1512139&r2=1512140&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/KeyedObjectPool.java (original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/KeyedObjectPool.java Fri Aug  9 05:20:37 2013
@@ -21,8 +21,7 @@ import java.util.NoSuchElementException;
 /**
  * A "keyed" pooling interface.
  * <p>
- * A keyed pool pools instances of multiple types. Each type may be accessed
- * using an arbitrary key.
+ * A keyed pool maintains a pool of instances for each key value.
  * <p>
  * Example of use:
  * <pre style="border:solid thin; padding: 1ex;"
@@ -71,7 +70,7 @@ public interface KeyedObjectPool<K,V> {
      * with {@link KeyedPoolableObjectFactory#makeObject makeObject} or will be
      * a previously idle object and have been activated with
      * {@link KeyedPoolableObjectFactory#activateObject activateObject} and then
-     * validated with
+     * (optionally) validated with
      * {@link KeyedPoolableObjectFactory#validateObject validateObject}.
      * <p>
      * By contract, clients <strong>must</strong> return the borrowed object

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/KeyedPooledObjectFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/KeyedPooledObjectFactory.java?rev=1512140&r1=1512139&r2=1512140&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/KeyedPooledObjectFactory.java (original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/KeyedPooledObjectFactory.java Fri Aug  9 05:20:37 2013
@@ -17,10 +17,134 @@
 package org.apache.commons.pool2;
 
 /**
- * This interface extends {@link KeyedPoolableObjectFactory} to work with
- * {@link PooledObject}s rather than {@link Object}s. This allows the factory to
- * make use of state information from {@link PooledObject}.
+ * An interface defining life-cycle methods for
+ * instances to be served by a {@link KeyedObjectPool}.
+ * <p>
+ * By contract, when an {@link KeyedObjectPool}
+ * delegates to a {@link KeyedPooledObjectFactory},
+ * <ol>
+ *  <li>
+ *   {@link #makeObject} is called whenever a new instance is needed.
+ *  </li>
+ *  <li>
+ *   {@link #activateObject} is invoked on every instance that has been
+ *   {@link #passivateObject passivated} before it is
+ *   {@link KeyedObjectPool#borrowObject borrowed} from the pool.
+ *  </li>
+ *  <li>
+ *   {@link #validateObject} may be invoked on {@link #activateObject activated}
+ *   instances to make sure they can be
+ *   {@link KeyedObjectPool#borrowObject borrowed} from the pool.
+ *   <code>validateObject</code> may also be used to test an
+ *   instance being {@link KeyedObjectPool#returnObject returned} to the pool
+ *   before it is {@link #passivateObject passivated}. It will only be invoked
+ *   on an activated instance.
+ *  </li>
+ *  <li>
+ *   {@link #passivateObject passivateObject}
+ *   is invoked on every instance when it is returned to the pool.
+ *  </li>
+ *  <li>
+ *   {@link #destroyObject destroyObject}
+ *   is invoked on every instance when it is being "dropped" from the
+ *   pool (whether due to the response from <code>validateObject</code>,
+ *   or for reasons specific to the pool implementation.) There is no
+ *   guarantee that the instance being destroyed will
+ *   be considered active, passive or in a generally consistent state.
+ *  </li>
+ * </ol>
+ * {@link KeyedPoolableObjectFactory} must be thread-safe. The only promise
+ * an {@link KeyedObjectPool} makes is that the same instance of an object will
+ * not be passed to more than one method of a
+ * <code>KeyedPoolableObjectFactory</code> at a time.
+ * <p>
+ * While clients of a {@link KeyedObjectPool} borrow and return instances of
+ * the underlying value type V, the factory methods act on instances of
+ * {@link PooledObject PooledObject<V>}.  These are the object wrappers that
+ * pools use to track and maintain state informations about the objects that
+ * they manage.
+ *
+ * @see KeyedObjectPool
+ * @see BaseKeyedPooledObjectFactory
+ *
+ * @param <K> The type of keys managed by this factory.
+ * @param <V> Type of element managed by this factory.
+ *
+ * @version $Revision: 1333925 $
+ *
+ * @since 2.0
  */
-public interface KeyedPooledObjectFactory<K,V> extends
-        KeyedPoolableObjectFactory<K, PooledObject<V>> {
+public interface KeyedPooledObjectFactory<K,V> {
+    /**
+     * Create an instance that can be served by the pool and
+     * wrap it in a {@link PooledObject} to be managed by the pool.
+     *
+     * @param key the key used when constructing the object
+     *
+     * @return a {@code PooledObject} wrapping an instance that can
+     * be served by the pool.
+     *
+     * @throws Exception if there is a problem creating a new instance,
+     *    this will be propagated to the code requesting an object.
+     */
+    PooledObject<V> makeObject(K key) throws Exception;
+
+    /**
+     * Destroy an instance no longer needed by the pool.
+     * <p>
+     * It is important for implementations of this method to be aware that there
+     * is no guarantee about what state <code>obj</code> will be in and the
+     * implementation should be prepared to handle unexpected errors.
+     * <p>
+     * Also, an implementation must take in to consideration that instances lost
+     * to the garbage collector may never be destroyed.
+     *
+     * @param key the key used when selecting the instance
+     * @param p a {@code PooledObject} wrapping the instance to be destroyed
+     *
+     * @throws Exception should be avoided as it may be swallowed by
+     *    the pool implementation.
+     *
+     * @see #validateObject
+     * @see KeyedObjectPool#invalidateObject
+     */
+    void destroyObject(K key, PooledObject<V> p) throws Exception;
+
+    /**
+     * Ensures that the instance is safe to be returned by the pool.
+     *
+     * @param key the key used when selecting the object
+     * @param p a {@code PooledObject} wrapping the instance to be validated
+     *
+     * @return <code>false</code> if <code>obj</code> is not valid and should
+     *         be dropped from the pool, <code>true</code> otherwise.
+     */
+    boolean validateObject(K key, PooledObject<V> p);
+
+    /**
+     * Reinitialize an instance to be returned by the pool.
+     *
+     * @param key the key used when selecting the object
+     * @param p a {@code PooledObject} wrapping the instance to be activated
+     *
+     * @throws Exception if there is a problem activating <code>obj</code>,
+     *    this exception may be swallowed by the pool.
+     *
+     * @see #destroyObject
+     */
+    void activateObject(K key, PooledObject<V> p) throws Exception;
+
+    /**
+     * Uninitialize an instance to be returned to the idle object pool.
+     *
+     * @param key the key used when selecting the object
+     * @param p a {@code PooledObject} wrapping the instance to be passivated
+     *
+     * @throws Exception if there is a problem passivating <code>obj</code>,
+     *    this exception may be swallowed by the pool.
+     *
+     * @see #destroyObject
+     */
+    void passivateObject(K key, PooledObject<V> p) throws Exception;
 }
+

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/PoolUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/PoolUtils.java?rev=1512140&r1=1512139&r2=1512140&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/PoolUtils.java (original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/PoolUtils.java Fri Aug  9 05:20:37 2013
@@ -363,34 +363,34 @@ public final class PoolUtils {
     }
 
     /**
-     * Returns a synchronized (thread-safe) PoolableObjectFactory backed by the
-     * specified PoolableObjectFactory.
+     * Returns a synchronized (thread-safe) PooledObjectFactory backed by the
+     * specified PooledObjectFactory.
      *
      * @param factory
-     *            the PoolableObjectFactory to be "wrapped" in a synchronized
-     *            PoolableObjectFactory.
+     *            the PooledObjectFactory to be "wrapped" in a synchronized
+     *            PooledObjectFactory.
      * @param <T> the type of objects in the pool
-     * @return a synchronized view of the specified PoolableObjectFactory.
+     * @return a synchronized view of the specified PooledObjectFactory.
      */
-    public static <T> PoolableObjectFactory<T> synchronizedPoolableFactory(
-            final PoolableObjectFactory<T> factory) {
-        return new SynchronizedPoolableObjectFactory<T>(factory);
+    public static <T> PooledObjectFactory<T> synchronizedPooledFactory(
+            final PooledObjectFactory<T> factory) {
+        return new SynchronizedPooledObjectFactory<T>(factory);
     }
 
     /**
-     * Returns a synchronized (thread-safe) KeyedPoolableObjectFactory backed by
+     * Returns a synchronized (thread-safe) KeyedPooledObjectFactory backed by
      * the specified KeyedPoolableObjectFactory.
      *
      * @param keyedFactory
-     *            the KeyedPoolableObjectFactory to be "wrapped" in a
-     *            synchronized KeyedPoolableObjectFactory.
+     *            the KeyedPooledObjectFactory to be "wrapped" in a
+     *            synchronized KeyedPooledObjectFactory.
      * @param <K> the type of the pool key
      * @param <V> the type of pool entries
-     * @return a synchronized view of the specified KeyedPoolableObjectFactory.
+     * @return a synchronized view of the specified KeyedPooledObjectFactory.
      */
-    public static <K, V> KeyedPoolableObjectFactory<K, V> synchronizedPoolableFactory(
-            final KeyedPoolableObjectFactory<K, V> keyedFactory) {
-        return new SynchronizedKeyedPoolableObjectFactory<K, V>(keyedFactory);
+    public static <K, V> KeyedPooledObjectFactory<K, V> synchronizedKeyedPooledFactory(
+            final KeyedPooledObjectFactory<K, V> keyedFactory) {
+        return new SynchronizedKeyedPooledObjectFactory<K, V>(keyedFactory);
     }
 
     /**
@@ -610,7 +610,6 @@ public final class PoolUtils {
 
             } catch (Exception e) {
                 cancel();
-
             } finally {
                 // detect other types of Throwable and cancel this Timer
                 if (!success) {
@@ -1101,8 +1100,8 @@ public final class PoolUtils {
     }
 
     /**
-     * A fully synchronized PoolableObjectFactory that wraps a
-     * PoolableObjectFactory and synchronizes access to the wrapped factory
+     * A fully synchronized PooledObjectFactory that wraps a
+     * PooledObjectFactory and synchronizes access to the wrapped factory
      * methods.
      * <p>
      * <b>Note:</b> This should not be used on pool implementations that already
@@ -1110,13 +1109,13 @@ public final class PoolUtils {
      * Pool library.
      * </p>
      */
-    private static class SynchronizedPoolableObjectFactory<T> implements
-            PoolableObjectFactory<T> {
+    private static class SynchronizedPooledObjectFactory<T> implements
+            PooledObjectFactory<T> {
         /** Synchronization lock */
         private final WriteLock writeLock = new ReentrantReadWriteLock().writeLock();
 
         /** Wrapped factory */
-        private final PoolableObjectFactory<T> factory;
+        private final PooledObjectFactory<T> factory;
 
         /**
          * Create a SynchronizedPoolableObjectFactory wrapping the given
@@ -1127,7 +1126,7 @@ public final class PoolUtils {
          * @throws IllegalArgumentException
          *             if the factory is null
          */
-        SynchronizedPoolableObjectFactory(final PoolableObjectFactory<T> factory)
+        SynchronizedPooledObjectFactory(final PooledObjectFactory<T> factory)
                 throws IllegalArgumentException {
             if (factory == null) {
                 throw new IllegalArgumentException("factory must not be null.");
@@ -1139,7 +1138,7 @@ public final class PoolUtils {
          * {@inheritDoc}
          */
         @Override
-        public T makeObject() throws Exception {
+        public PooledObject<T> makeObject() throws Exception {
             writeLock.lock();
             try {
                 return factory.makeObject();
@@ -1152,10 +1151,10 @@ public final class PoolUtils {
          * {@inheritDoc}
          */
         @Override
-        public void destroyObject(final T obj) throws Exception {
+        public void destroyObject(final PooledObject<T> p) throws Exception {
             writeLock.lock();
             try {
-                factory.destroyObject(obj);
+                factory.destroyObject(p);
             } finally {
                 writeLock.unlock();
             }
@@ -1165,10 +1164,10 @@ public final class PoolUtils {
          * {@inheritDoc}
          */
         @Override
-        public boolean validateObject(final T obj) {
+        public boolean validateObject(final PooledObject<T> p) {
             writeLock.lock();
             try {
-                return factory.validateObject(obj);
+                return factory.validateObject(p);
             } finally {
                 writeLock.unlock();
             }
@@ -1178,10 +1177,10 @@ public final class PoolUtils {
          * {@inheritDoc}
          */
         @Override
-        public void activateObject(final T obj) throws Exception {
+        public void activateObject(final PooledObject<T> p) throws Exception {
             writeLock.lock();
             try {
-                factory.activateObject(obj);
+                factory.activateObject(p);
             } finally {
                 writeLock.unlock();
             }
@@ -1191,10 +1190,10 @@ public final class PoolUtils {
          * {@inheritDoc}
          */
         @Override
-        public void passivateObject(final T obj) throws Exception {
+        public void passivateObject(final PooledObject<T> p) throws Exception {
             writeLock.lock();
             try {
-                factory.passivateObject(obj);
+                factory.passivateObject(p);
             } finally {
                 writeLock.unlock();
             }
@@ -1214,8 +1213,8 @@ public final class PoolUtils {
     }
 
     /**
-     * A fully synchronized KeyedPoolableObjectFactory that wraps a
-     * KeyedPoolableObjectFactory and synchronizes access to the wrapped factory
+     * A fully synchronized KeyedPooledObjectFactory that wraps a
+     * KeyedPooledObjectFactory and synchronizes access to the wrapped factory
      * methods.
      * <p>
      * <b>Note:</b> This should not be used on pool implementations that already
@@ -1223,13 +1222,13 @@ public final class PoolUtils {
      * Pool library.
      * </p>
      */
-    private static class SynchronizedKeyedPoolableObjectFactory<K, V>
-            implements KeyedPoolableObjectFactory<K, V> {
+    private static class SynchronizedKeyedPooledObjectFactory<K, V>
+            implements KeyedPooledObjectFactory<K, V> {
         /** Synchronization lock */
         private final WriteLock writeLock = new ReentrantReadWriteLock().writeLock();
 
         /** Wrapped factory */
-        private final KeyedPoolableObjectFactory<K, V> keyedFactory;
+        private final KeyedPooledObjectFactory<K, V> keyedFactory;
 
         /**
          * Create a SynchronizedKeyedPoolableObjectFactory wrapping the given
@@ -1240,8 +1239,8 @@ public final class PoolUtils {
          * @throws IllegalArgumentException
          *             if the factory is null
          */
-        SynchronizedKeyedPoolableObjectFactory(
-                final KeyedPoolableObjectFactory<K, V> keyedFactory)
+        SynchronizedKeyedPooledObjectFactory(
+                final KeyedPooledObjectFactory<K, V> keyedFactory)
                 throws IllegalArgumentException {
             if (keyedFactory == null) {
                 throw new IllegalArgumentException(
@@ -1254,7 +1253,7 @@ public final class PoolUtils {
          * {@inheritDoc}
          */
         @Override
-        public V makeObject(final K key) throws Exception {
+        public PooledObject<V> makeObject(final K key) throws Exception {
             writeLock.lock();
             try {
                 return keyedFactory.makeObject(key);
@@ -1267,10 +1266,10 @@ public final class PoolUtils {
          * {@inheritDoc}
          */
         @Override
-        public void destroyObject(final K key, final V obj) throws Exception {
+        public void destroyObject(final K key, final PooledObject<V> p) throws Exception {
             writeLock.lock();
             try {
-                keyedFactory.destroyObject(key, obj);
+                keyedFactory.destroyObject(key, p);
             } finally {
                 writeLock.unlock();
             }
@@ -1280,10 +1279,10 @@ public final class PoolUtils {
          * {@inheritDoc}
          */
         @Override
-        public boolean validateObject(final K key, final V obj) {
+        public boolean validateObject(final K key, final PooledObject<V> p) {
             writeLock.lock();
             try {
-                return keyedFactory.validateObject(key, obj);
+                return keyedFactory.validateObject(key, p);
             } finally {
                 writeLock.unlock();
             }
@@ -1293,10 +1292,10 @@ public final class PoolUtils {
          * {@inheritDoc}
          */
         @Override
-        public void activateObject(final K key, final V obj) throws Exception {
+        public void activateObject(final K key, final PooledObject<V> p) throws Exception {
             writeLock.lock();
             try {
-                keyedFactory.activateObject(key, obj);
+                keyedFactory.activateObject(key, p);
             } finally {
                 writeLock.unlock();
             }
@@ -1306,10 +1305,10 @@ public final class PoolUtils {
          * {@inheritDoc}
          */
         @Override
-        public void passivateObject(final K key, final V obj) throws Exception {
+        public void passivateObject(final K key, final PooledObject<V> p) throws Exception {
             writeLock.lock();
             try {
-                keyedFactory.passivateObject(key, obj);
+                keyedFactory.passivateObject(key, p);
             } finally {
                 writeLock.unlock();
             }

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/PooledObjectFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/PooledObjectFactory.java?rev=1512140&r1=1512139&r2=1512140&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/PooledObjectFactory.java (original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/PooledObjectFactory.java Fri Aug  9 05:20:37 2013
@@ -17,10 +17,123 @@
 package org.apache.commons.pool2;
 
 /**
- * This interface extends {@link PoolableObjectFactory} to work with
- * {@link PooledObject}s rather than {@link Object}s. This allows the factory to
- * make use of state information from {@link PooledObject}.
+ * An interface defining life-cycle methods for instances to be served by an
+ * {@link ObjectPool}.
+ * <p>
+ * By contract, when an {@link ObjectPool} delegates to a
+ * {@link PooledObjectFactory},
+ * <ol>
+ *  <li>
+ *   {@link #makeObject} is called whenever a new instance is needed.
+ *  </li>
+ *  <li>
+ *   {@link #activateObject} is invoked on every instance that has been
+ *   {@link #passivateObject passivated} before it is
+ *   {@link ObjectPool#borrowObject borrowed} from the pool.
+ *  </li>
+ *  <li>
+ *   {@link #validateObject} may be invoked on {@link #activateObject activated}
+ *   instances to make sure they can be {@link ObjectPool#borrowObject borrowed}
+ *   from the pool. {@link #validateObject} may also be used to
+ *   test an instance being {@link ObjectPool#returnObject returned} to the pool
+ *   before it is {@link #passivateObject passivated}. It will only be invoked
+ *   on an activated instance.
+ *  </li>
+ *  <li>
+ *   {@link #passivateObject} is invoked on every instance when it is returned
+ *   to the pool.
+ *  </li>
+ *  <li>
+ *   {@link #destroyObject} is invoked on every instance when it is being
+ *   "dropped" from the pool (whether due to the response from
+ *   {@link #validateObject}, or for reasons specific to the pool
+ *   implementation.) There is no guarantee that the instance being destroyed
+ *   will be considered active, passive or in a generally consistent state.
+ *  </li>
+ * </ol>
+ * {@link PoolableObjectFactory} must be thread-safe. The only promise
+ * an {@link ObjectPool} makes is that the same instance of an object will not
+ * be passed to more than one method of a <code>PoolableObjectFactory</code>
+ * at a time.
+ * <p>
+ * While clients of a {@link KeyedObjectPool} borrow and return instances of
+ * the underlying value type {@code V}, the factory methods act on instances of
+ * {@link PooledObject PooledObject<V>}.  These are the object wrappers that
+ * pools use to track and maintain state information about the objects that
+ * they manage.
+ *
+ * @param <T> Type of element managed in this factory.
+ *
+ * @see ObjectPool
+ *
+ * @version $Revision: 1333925 $
+ *
+ * @since 2.0
  */
-public interface PooledObjectFactory<T>
-        extends PoolableObjectFactory<PooledObject<T>> {
+public interface PooledObjectFactory<T> {
+  /**
+   * Create an instance that can be served by the pool and wrap it in a
+   * {@link PooledObject} to be managed by the pool.
+   *
+   * @return a {@code PooledObject} wrapping an instance that can be served by the pool
+   *
+   * @throws Exception if there is a problem creating a new instance,
+   *    this will be propagated to the code requesting an object.
+   */
+  PooledObject<T> makeObject() throws Exception;
+
+  /**
+   * Destroys an instance no longer needed by the pool.
+   * <p>
+   * It is important for implementations of this method to be aware that there
+   * is no guarantee about what state <code>obj</code> will be in and the
+   * implementation should be prepared to handle unexpected errors.
+   * <p>
+   * Also, an implementation must take in to consideration that instances lost
+   * to the garbage collector may never be destroyed.
+   * </p>
+   *
+   * @param p a {@code PooledObject} wrapping the instance to be destroyed
+   *
+   * @throws Exception should be avoided as it may be swallowed by
+   *    the pool implementation.
+   *
+   * @see #validateObject
+   * @see ObjectPool#invalidateObject
+   */
+  void destroyObject(PooledObject<T> p) throws Exception;
+
+  /**
+   * Ensures that the instance is safe to be returned by the pool.
+   *
+   * @param p a {@code PooledObject} wrapping the instance to be validated
+   *
+   * @return <code>false</code> if <code>obj</code> is not valid and should
+   *         be dropped from the pool, <code>true</code> otherwise.
+   */
+  boolean validateObject(PooledObject<T> p);
+
+  /**
+   * Reinitialize an instance to be returned by the pool.
+   *
+   * @param p a {@code PooledObject} wrapping the instance to be activated
+   *
+   * @throws Exception if there is a problem activating <code>obj</code>,
+   *    this exception may be swallowed by the pool.
+   *
+   * @see #destroyObject
+   */
+  void activateObject(PooledObject<T> p) throws Exception;
+
+  /**
+   * Uninitialize an instance to be returned to the idle object pool.
+   *
+   * @param p a {@code PooledObject} wrapping the instance to be passivated
+   *
+   * @throws Exception if there is a problem passivating <code>obj</code>,
+   *    this exception may be swallowed by the pool.
+   *
+   * @see #destroyObject
+   */
+  void passivateObject(PooledObject<T> p) throws Exception;
 }

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/PoolImplUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/PoolImplUtils.java?rev=1512140&r1=1512139&r2=1512140&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/PoolImplUtils.java (original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/PoolImplUtils.java Fri Aug  9 05:20:37 2013
@@ -20,9 +20,7 @@ import java.lang.reflect.ParameterizedTy
 import java.lang.reflect.Type;
 import java.lang.reflect.TypeVariable;
 
-import org.apache.commons.pool2.KeyedPoolableObjectFactory;
 import org.apache.commons.pool2.KeyedPooledObjectFactory;
-import org.apache.commons.pool2.PoolableObjectFactory;
 import org.apache.commons.pool2.PooledObject;
 import org.apache.commons.pool2.PooledObjectFactory;
 
@@ -32,26 +30,6 @@ import org.apache.commons.pool2.PooledOb
 public class PoolImplUtils {
 
     /**
-     * Wraps a {@link PoolableObjectFactory} to convert it into a
-     * {@link PooledObjectFactory}.
-     */
-    public static <T> PooledObjectFactory<T> poolableToPooledObjectFactory(
-            PoolableObjectFactory<T> innerFactory) {
-        return new PoolableObjectFactoryWrapper<T>(innerFactory);
-    }
-
-
-    /**
-     * Wraps a {@link KeyedPoolableObjectFactory} to convert it into a
-     * {@link KeyedPooledObjectFactory}.
-     */
-    public static <K,V> KeyedPooledObjectFactory<K,V> poolableToKeyedPooledObjectFactory(
-            KeyedPoolableObjectFactory<K,V> innerFactory) {
-        return new KeyedPoolableObjectFactoryWrapper<K,V>(innerFactory);
-    }
-
-
-    /**
      * Identifies the concrete type of object that an object factory creates.
      */
     @SuppressWarnings("rawtypes")
@@ -123,79 +101,4 @@ public class PoolImplUtils {
             return null;
         }
     }
-
-
-    private static class PoolableObjectFactoryWrapper<T>
-            implements PooledObjectFactory<T> {
-
-        private final PoolableObjectFactory<T> innerFactory;
-
-        public PoolableObjectFactoryWrapper(PoolableObjectFactory<T> innerFactory) {
-            this.innerFactory = innerFactory;
-        }
-
-        @Override
-        public PooledObject<T> makeObject() throws Exception {
-            return new DefaultPooledObject<T>(innerFactory.makeObject());
-        }
-
-        @Override
-        public void destroyObject(PooledObject<T> p) throws Exception {
-            innerFactory.destroyObject(p.getObject());
-        }
-
-        @Override
-        public boolean validateObject(PooledObject<T> p) {
-            return innerFactory.validateObject(p.getObject());
-        }
-
-        @Override
-        public void activateObject(PooledObject<T> p) throws Exception {
-            innerFactory.activateObject(p.getObject());
-        }
-
-        @Override
-        public void passivateObject(PooledObject<T> p) throws Exception {
-            innerFactory.passivateObject(p.getObject());
-        }
-    }
-
-
-    private static class KeyedPoolableObjectFactoryWrapper<K,V>
-            implements KeyedPooledObjectFactory<K,V> {
-
-        private final KeyedPoolableObjectFactory<K,V> innerFactory;
-
-        public KeyedPoolableObjectFactoryWrapper(
-                KeyedPoolableObjectFactory<K,V> innerFactory) {
-            this.innerFactory = innerFactory;
-        }
-
-        @Override
-        public PooledObject<V> makeObject(K key) throws Exception {
-            V obj = innerFactory.makeObject(key);
-            return new DefaultPooledObject<V>(obj);
-        }
-
-        @Override
-        public void destroyObject(K key, PooledObject<V> p) throws Exception {
-            innerFactory.destroyObject(key, p.getObject());
-        }
-
-        @Override
-        public boolean validateObject(K key, PooledObject<V> p) {
-            return innerFactory.validateObject(key, p.getObject());
-        }
-
-        @Override
-        public void activateObject(K key, PooledObject<V> p) throws Exception {
-            innerFactory.activateObject(key, p.getObject());
-        }
-
-        @Override
-        public void passivateObject(K key, PooledObject<V> p)
-                throws Exception {
-            innerFactory.passivateObject(key, p.getObject());
-        }
-    }
 }

Added: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/PooledSoftReference.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/PooledSoftReference.java?rev=1512140&view=auto
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/PooledSoftReference.java (added)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/PooledSoftReference.java Fri Aug  9 05:20:37 2013
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.pool2.impl;
+
+import java.lang.ref.SoftReference;
+
+/**
+ * Extension of {@link DefaultPooledObject} to wrap pooled soft references.
+ * 
+ * <p>This class is intended to be thread-safe.</p>
+ *
+ * @param <T> the type of the underlying object that the wrapped SoftReference
+ * refers to.
+ *
+ * @version $Revision: $
+ *
+ * @since 2.0
+ */
+public class PooledSoftReference<T> extends DefaultPooledObject<T> {
+
+    /** SoftReference wrapped by this object */
+    private SoftReference<T> reference;
+
+    /**
+     * Creates a new PooledSoftReference wrapping the provided reference.
+     * 
+     * @param reference SoftReference to be managed by the pool
+     */
+    public PooledSoftReference(SoftReference<T> reference) {
+        super(null);  // Null the hard reference in the parent
+        this.reference = reference;
+    }
+
+    /**
+     * Returns the object that the wrapped SoftReference refers to.
+     * <p>
+     * Note that if the reference has been cleared, this method will return
+     * null.
+     * 
+     * @return Object referred to by the SoftReference
+     */
+    @Override
+    public T getObject() {
+        return reference.get();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString() {
+        StringBuilder result = new StringBuilder();
+        result.append("Referenced Object: ");
+        result.append(getObject().toString());
+        result.append(", State: ");
+        synchronized (this) {
+            result.append(getState().toString());
+        }
+        return result.toString();
+        // TODO add other attributes
+        // TODO encapsulate state and other attribute display in parent
+    }
+    
+    /**
+     * Returns the SoftReference wrapped by this object.
+     * 
+     * @return underlying SoftReference
+     */
+    public synchronized SoftReference<T> getReference() {
+        return reference;
+    }
+    
+    /**
+     * Sets the wrapped reference.
+     * 
+     * <p>This method exists to allow a new, non-registered reference to be
+     * held by the pool to track objects that have been checked out of the pool.
+     * The actual parameter <strong>should</strong> be a reference to the same
+     * object that {@link #getObject()} returns before calling this method.</p>
+     * 
+     * @param reference new reference
+     */
+    public synchronized void setReference(SoftReference<T> reference) {
+        this.reference = reference;
+    }
+}

Modified: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/SoftReferenceObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/SoftReferenceObjectPool.java?rev=1512140&r1=1512139&r2=1512140&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/SoftReferenceObjectPool.java (original)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/impl/SoftReferenceObjectPool.java Fri Aug  9 05:20:37 2013
@@ -21,13 +21,12 @@ import java.lang.ref.ReferenceQueue;
 import java.lang.ref.SoftReference;
 import java.util.ArrayList;
 import java.util.Iterator;
-import java.util.List;
 import java.util.NoSuchElementException;
 
 import org.apache.commons.pool2.BaseObjectPool;
 import org.apache.commons.pool2.ObjectPool;
 import org.apache.commons.pool2.PoolUtils;
-import org.apache.commons.pool2.PoolableObjectFactory;
+import org.apache.commons.pool2.PooledObjectFactory;
 
 /**
  * A {@link java.lang.ref.SoftReference SoftReference} based {@link ObjectPool}.
@@ -42,28 +41,54 @@ import org.apache.commons.pool2.Poolable
  * @since 2.0
  */
 public class SoftReferenceObjectPool<T> extends BaseObjectPool<T> {
+
+    /** Factory to source pooled objects */
+    private final PooledObjectFactory<T> factory;
+
+    /**
+     * Queue of broken references that might be able to be removed from
+     * <code>_pool</code>. This is used to help {@link #getNumIdle()} be more
+     * accurate with minimal performance overhead.
+     */
+    private final ReferenceQueue<T> refQueue = new ReferenceQueue<T>();
+
+    /** Count of instances that have been checkout out to pool clients */
+    private int numActive = 0; // @GuardedBy("this")
+    
+    /** Total number of instances that have been destroyed */
+    private long destroyCount = 0;
+    
+    /** Total number of instances that have been created */
+    private long createCount = 0;
+    
+    /** Idle references - waiting to be borrowed */
+    private final LinkedBlockingDeque<PooledSoftReference<T>> idleReferences =
+        new LinkedBlockingDeque<PooledSoftReference<T>>();
+    
+    /** All references - checked out or waiting to be borrowed. */
+    private final ArrayList<PooledSoftReference<T>> allReferences =
+        new ArrayList<PooledSoftReference<T>>();
+    
     /**
      * Create a <code>SoftReferenceObjectPool</code> with the specified factory.
      *
-     * @param factory
-     *            object factory to use.
+     * @param factory object factory to use.
      */
-    public SoftReferenceObjectPool(PoolableObjectFactory<T> factory) {
-        _pool = new ArrayList<SoftReference<T>>();
-        _factory = factory;
+    public SoftReferenceObjectPool(PooledObjectFactory<T> factory) {
+        this.factory = factory;
     }
 
     /**
      * Borrow an object from the pool. If there are no idle instances available
      * in the pool, the configured factory's
-     * {@link PoolableObjectFactory#makeObject()} method is invoked to create a
+     * {@link PooledObjectFactory#makeObject()} method is invoked to create a
      * new instance.
      * <p>
      * All instances are {@link PoolableObjectFactory#activateObject(Object)
-     * activated} and {@link PoolableObjectFactory#validateObject(Object)
+     * activated} and {@link PooledObjectFactory#validateObject(Object)
      * validated} before being returned by this method. If validation fails or
      * an exception occurs activating or validating an idle instance, the
-     * failing instance is {@link PoolableObjectFactory#destroyObject(Object)
+     * failing instance is {@link PooledObjectFactory#destroyObject(Object)
      * destroyed} and another instance is retrieved from the pool, validated and
      * activated. This process continues until either the pool is empty or an
      * instance passes validation. If the pool is empty on activation or it does
@@ -89,30 +114,38 @@ public class SoftReferenceObjectPool<T> 
         assertOpen();
         T obj = null;
         boolean newlyCreated = false;
+        PooledSoftReference<T> ref = null;
         while (null == obj) {
-            if (_pool.isEmpty()) {
-                if (null == _factory) {
+            if (idleReferences.isEmpty()) {
+                if (null == factory) {
                     throw new NoSuchElementException();
                 } else {
                     newlyCreated = true;
-                    obj = _factory.makeObject();
+                    obj = factory.makeObject().getObject();
+                    createCount++;
+                    // Do not register with the queue 
+                    ref = new PooledSoftReference<T>(new SoftReference<T>(obj));
+                    allReferences.add(ref); 
                 }
             } else {
-                SoftReference<T> ref = _pool.remove(_pool.size() - 1);
-                obj = ref.get();
-                ref.clear(); // prevent this ref from being enqueued with
-                             // refQueue.
+                ref = idleReferences.pollFirst();
+                obj = ref.getObject();
+                // Clear the reference so it will not be queued, but replace with a
+                // a new, non-registered reference so we can still track this object
+                // in allReferences
+                ref.getReference().clear();
+                ref.setReference(new SoftReference<T>(obj)); 
             }
-            if (null != _factory && null != obj) {
+            if (null != factory && null != obj) {
                 try {
-                    _factory.activateObject(obj);
-                    if (!_factory.validateObject(obj)) {
+                    factory.activateObject(ref);
+                    if (!factory.validateObject(ref)) {
                         throw new Exception("ValidateObject failed");
                     }
                 } catch (Throwable t) {
                     PoolUtils.checkRethrow(t);
                     try {
-                        _factory.destroyObject(obj);
+                        destroy(ref);
                     } catch (Throwable t2) {
                         PoolUtils.checkRethrow(t2);
                         // Swallowed
@@ -127,7 +160,8 @@ public class SoftReferenceObjectPool<T> 
                 }
             }
         }
-        _numActive++;
+        numActive++;
+        ref.allocate();
         return obj;
     }
 
@@ -137,9 +171,9 @@ public class SoftReferenceObjectPool<T> 
      * are true:
      * <ul>
      * <li>the pool is closed</li>
-     * <li>{@link PoolableObjectFactory#validateObject(Object) validation} fails
+     * <li>{@link PooledObjectFactory#validateObject(Object) validation} fails
      * </li>
-     * <li>{@link PoolableObjectFactory#passivateObject(Object) passivation}
+     * <li>{@link PooledObjectFactory#passivateObject(Object) passivation}
      * throws an exception</li>
      * </ul>
      * Exceptions passivating or destroying instances are silently swallowed.
@@ -151,12 +185,17 @@ public class SoftReferenceObjectPool<T> 
     @Override
     public synchronized void returnObject(T obj) throws Exception {
         boolean success = !isClosed();
-        if (_factory != null) {
-            if (!_factory.validateObject(obj)) {
+        final PooledSoftReference<T> ref = findReference(obj);
+        if (ref == null) {
+            throw new IllegalStateException(
+                "Returned object not currently part of this pool");
+        }
+        if (factory != null) {
+            if (!factory.validateObject(ref)) {
                 success = false;
             } else {
                 try {
-                    _factory.passivateObject(obj);
+                    factory.passivateObject(ref);
                 } catch (Exception e) {
                     success = false;
                 }
@@ -164,15 +203,18 @@ public class SoftReferenceObjectPool<T> 
         }
 
         boolean shouldDestroy = !success;
-        _numActive--;
+        numActive--;
         if (success) {
-            _pool.add(new SoftReference<T>(obj, refQueue));
+            
+            // Deallocate and add to the idle instance pool
+            ref.deallocate();
+            idleReferences.add(ref);
         }
-        notifyAll(); // _numActive has changed
+        notifyAll(); // numActive has changed
 
-        if (shouldDestroy && _factory != null) {
+        if (shouldDestroy && factory != null) {
             try {
-                _factory.destroyObject(obj);
+                destroy(ref);
             } catch (Exception e) {
                 // ignored
             }
@@ -184,11 +226,16 @@ public class SoftReferenceObjectPool<T> 
      */
     @Override
     public synchronized void invalidateObject(T obj) throws Exception {
-        _numActive--;
-        if (_factory != null) {
-            _factory.destroyObject(obj);
+        final PooledSoftReference<T> ref = findReference(obj);
+        if (ref == null) {
+            throw new IllegalStateException(
+                "Object to invalidate is not currently part of this pool");   
+        }
+        if (factory != null) {
+            destroy(ref);
         }
-        notifyAll(); // _numActive has changed
+        numActive--;
+        notifyAll(); // numActive has changed
     }
 
     /**
@@ -196,10 +243,10 @@ public class SoftReferenceObjectPool<T> 
      * "pre-loading" a pool with idle objects.
      * <p>
      * Before being added to the pool, the newly created instance is
-     * {@link PoolableObjectFactory#validateObject(Object) validated} and
-     * {@link PoolableObjectFactory#passivateObject(Object) passivated}. If
+     * {@link PooledObjectFactory#validateObject(Object) validated} and
+     * {@link PooledObjectFactory#passivateObject(Object) passivated}. If
      * validation fails, the new instance is
-     * {@link PoolableObjectFactory#destroyObject(Object) destroyed}. Exceptions
+     * {@link PooledObjectFactory#destroyObject(Object) destroyed}. Exceptions
      * generated by the factory <code>makeObject</code> or
      * <code>passivate</code> are propagated to the caller. Exceptions
      * destroying instances are silently swallowed.
@@ -213,28 +260,33 @@ public class SoftReferenceObjectPool<T> 
     @Override
     public synchronized void addObject() throws Exception {
         assertOpen();
-        if (_factory == null) {
+        if (factory == null) {
             throw new IllegalStateException(
                     "Cannot add objects without a factory.");
         }
-        T obj = _factory.makeObject();
+        T obj = factory.makeObject().getObject();
+        createCount++;
+        // Create and register with the queue
+        PooledSoftReference<T> ref = new PooledSoftReference<T>(
+                new SoftReference<T>(obj, refQueue));
+        allReferences.add(ref); 
 
         boolean success = true;
-        if (!_factory.validateObject(obj)) {
+        if (!factory.validateObject(ref)) {
             success = false;
         } else {
-            _factory.passivateObject(obj);
+            factory.passivateObject(ref);
         }
 
         boolean shouldDestroy = !success;
         if (success) {
-            _pool.add(new SoftReference<T>(obj, refQueue));
-            notifyAll(); // _numActive has changed
+            idleReferences.add(ref);
+            notifyAll(); // numActive has changed
         }
 
         if (shouldDestroy) {
             try {
-                _factory.destroyObject(obj);
+                destroy(ref);
             } catch (Exception e) {
                 // ignored
             }
@@ -250,7 +302,7 @@ public class SoftReferenceObjectPool<T> 
     @Override
     public synchronized int getNumIdle() {
         pruneClearedReferences();
-        return _pool.size();
+        return idleReferences.size();
     }
 
     /**
@@ -260,7 +312,7 @@ public class SoftReferenceObjectPool<T> 
      */
     @Override
     public synchronized int getNumActive() {
-        return _numActive;
+        return numActive;
     }
 
     /**
@@ -268,20 +320,20 @@ public class SoftReferenceObjectPool<T> 
      */
     @Override
     public synchronized void clear() {
-        if (null != _factory) {
-            Iterator<SoftReference<T>> iter = _pool.iterator();
+        if (null != factory) {
+            Iterator<PooledSoftReference<T>> iter = idleReferences.iterator();
             while (iter.hasNext()) {
                 try {
-                    T obj = iter.next().get();
-                    if (null != obj) {
-                        _factory.destroyObject(obj);
+                    final PooledSoftReference<T> ref = iter.next();
+                    if (null != ref.getObject()) {
+                        factory.destroyObject(ref);
                     }
                 } catch (Exception e) {
                     // ignore error, keep destroying the rest
                 }
             }
         }
-        _pool.clear();
+        idleReferences.clear();
         pruneClearedReferences();
     }
 
@@ -300,40 +352,73 @@ public class SoftReferenceObjectPool<T> 
     }
 
     /**
-     * If any idle objects were garbage collected, remove their
-     * {@link Reference} wrappers from the idle object pool.
-     */
-    private void pruneClearedReferences() {
-        Reference<? extends T> ref;
-        while ((ref = refQueue.poll()) != null) {
-            try {
-                _pool.remove(ref);
-            } catch (UnsupportedOperationException uoe) {
-                // ignored
-            }
-        }
-    }
-
-    /**
-     * Returns the {@link PoolableObjectFactory} used by this pool to create and
+     * Returns the {@link PooledObjectFactory} used by this pool to create and
      * manage object instances.
      *
      * @return the factory
      */
-    public synchronized PoolableObjectFactory<T> getFactory() {
-        return _factory;
+    public synchronized PooledObjectFactory<T> getFactory() {
+        return factory;
     }
-
-    private final List<SoftReference<T>> _pool;
-
-    private final PoolableObjectFactory<T> _factory;
-
+    
     /**
-     * Queue of broken references that might be able to be removed from
-     * <code>_pool</code>. This is used to help {@link #getNumIdle()} be more
-     * accurate with minimal performance overhead.
+     * If any idle objects were garbage collected, remove their
+     * {@link Reference} wrappers from the idle object pool.
      */
-    private final ReferenceQueue<T> refQueue = new ReferenceQueue<T>();
-
-    private int _numActive = 0; // @GuardedBy("this")
+    private void pruneClearedReferences() {
+        // Remove wrappers for enqueued references from idle and allReferences lists
+        removeClearedReferences(idleReferences.iterator());
+        removeClearedReferences(allReferences.iterator());
+        while (refQueue.poll() != null) {}
+    }
+    
+    /**
+     * Find the PooledSoftReference in allReferences that points to obj.
+     * 
+     * @param obj returning object
+     * @return PooledSoftReference wrapping a soft reference to obj
+     */
+    private PooledSoftReference<T> findReference(T obj) {
+        Iterator<PooledSoftReference<T>> iterator = allReferences.iterator();
+        while (iterator.hasNext()) {
+            final PooledSoftReference<T> reference = iterator.next();
+            if (reference.getObject() != null && reference.getObject().equals(obj)) {
+                return reference;
+            } 
+        }
+        return null;
+    }
+    
+    /**
+     * Destroy a {@code PooledSoftReference} and remove it from the idle and all
+     * references pools.
+     * 
+     * @param toDestroy PooledSoftReference to destroy
+     * @throws Exception
+     */
+    private void destroy(PooledSoftReference<T> toDestroy) throws Exception {
+        toDestroy.invalidate();
+        idleReferences.remove(toDestroy);
+        allReferences.remove(toDestroy);
+        try {
+            factory.destroyObject(toDestroy);
+        } finally {
+            destroyCount++;
+            toDestroy.getReference().clear();
+        }
+    }
+    
+    /**
+     * Clears cleared references from iterator's collection 
+     * @param iterator iterator over idle/allReferences
+     */
+    private void removeClearedReferences(Iterator<PooledSoftReference<T>> iterator) {
+        PooledSoftReference<T> ref;
+        while (iterator.hasNext()) {
+            ref = iterator.next();
+            if (ref.getReference() == null || ref.getReference().isEnqueued()) {
+                iterator.remove();
+            }
+        }
+    }
 }

Modified: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/MethodCallPoolableObjectFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/MethodCallPoolableObjectFactory.java?rev=1512140&r1=1512139&r2=1512140&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/MethodCallPoolableObjectFactory.java (original)
+++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/MethodCallPoolableObjectFactory.java Fri Aug  9 05:20:37 2013
@@ -20,7 +20,8 @@ package org.apache.commons.pool2;
 import java.util.List;
 import java.util.ArrayList;
 
-import org.apache.commons.pool2.PoolableObjectFactory;
+import org.apache.commons.pool2.PooledObjectFactory;
+import org.apache.commons.pool2.impl.DefaultPooledObject;
 
 /**
  * A poolable object factory that tracks how {@link MethodCall methods are called}.
@@ -28,7 +29,7 @@ import org.apache.commons.pool2.Poolable
  * @version $Revision$
  * @see MethodCall
  */
-public class MethodCallPoolableObjectFactory implements PoolableObjectFactory<Object> {
+public class MethodCallPoolableObjectFactory implements PooledObjectFactory<Object> {
     private final List<MethodCall> methodCalls = new ArrayList<MethodCall>();
     private int count = 0;
     private boolean valid = true;
@@ -110,7 +111,7 @@ public class MethodCallPoolableObjectFac
     }
 
     @Override
-    public Object makeObject() throws Exception {
+    public PooledObject<Object> makeObject() throws Exception {
         final MethodCall call = new MethodCall("makeObject");
         methodCalls.add(call);
         int count = this.count++;
@@ -119,20 +120,20 @@ public class MethodCallPoolableObjectFac
         }
         final Integer obj = new Integer(count);
         call.setReturned(obj);
-        return obj;
+        return new DefaultPooledObject<Object>(obj);
     }
 
     @Override
-    public void activateObject(final Object obj) throws Exception {
-        methodCalls.add(new MethodCall("activateObject", obj));
+    public void activateObject(final PooledObject<Object> obj) throws Exception {
+        methodCalls.add(new MethodCall("activateObject", obj.getObject()));
         if (activateObjectFail) {
             throw new PrivateException("activateObject");
         }
     }
 
     @Override
-    public boolean validateObject(final Object obj) {
-        final MethodCall call = new MethodCall("validateObject", obj);
+    public boolean validateObject(final PooledObject<Object> obj) {
+        final MethodCall call = new MethodCall("validateObject", obj.getObject());
         methodCalls.add(call);
         if (validateObjectFail) {
             throw new PrivateException("validateObject");
@@ -143,16 +144,16 @@ public class MethodCallPoolableObjectFac
     }
 
     @Override
-    public void passivateObject(final Object obj) throws Exception {
-        methodCalls.add(new MethodCall("passivateObject", obj));
+    public void passivateObject(final PooledObject<Object> obj) throws Exception {
+        methodCalls.add(new MethodCall("passivateObject", obj.getObject()));
         if (passivateObjectFail) {
             throw new PrivateException("passivateObject");
         }
     }
 
     @Override
-    public void destroyObject(final Object obj) throws Exception {
-        methodCalls.add(new MethodCall("destroyObject", obj));
+    public void destroyObject(final PooledObject<Object> obj) throws Exception {
+        methodCalls.add(new MethodCall("destroyObject", obj.getObject()));
         if (destroyObjectFail) {
             throw new PrivateException("destroyObject");
         }

Modified: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestBaseKeyedPoolableObjectFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestBaseKeyedPoolableObjectFactory.java?rev=1512140&r1=1512139&r2=1512140&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestBaseKeyedPoolableObjectFactory.java (original)
+++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestBaseKeyedPoolableObjectFactory.java Fri Aug  9 05:20:37 2013
@@ -27,9 +27,9 @@ public class TestBaseKeyedPoolableObject
 
     @Test
     public void testDefaultMethods() throws Exception {
-        KeyedPoolableObjectFactory<Object,Object> factory = new BaseKeyedPoolableObjectFactory<Object,Object>() { 
+        KeyedPooledObjectFactory<Object,Object> factory = new BaseKeyedPooledObjectFactory<Object,Object>() { 
             @Override
-            public Object makeObject(Object key) throws Exception {
+            public Object create(Object key) throws Exception {
                 return null;
             }
         };   

Modified: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestBaseObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestBaseObjectPool.java?rev=1512140&r1=1512139&r2=1512140&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestBaseObjectPool.java (original)
+++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestBaseObjectPool.java Fri Aug  9 05:20:37 2013
@@ -40,7 +40,7 @@ public class TestBaseObjectPool extends 
     }
 
     @Override
-    protected ObjectPool<Object> makeEmptyPool(final PoolableObjectFactory<Object> factory) {
+    protected ObjectPool<Object> makeEmptyPool(final PooledObjectFactory<Object> factory) {
         if (this.getClass() != TestBaseObjectPool.class) {
             fail("Subclasses of TestBaseObjectPool must reimplement this method.");
         }

Modified: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestBasePoolableObjectFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestBasePoolableObjectFactory.java?rev=1512140&r1=1512139&r2=1512140&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestBasePoolableObjectFactory.java (original)
+++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestBasePoolableObjectFactory.java Fri Aug  9 05:20:37 2013
@@ -27,9 +27,9 @@ public class TestBasePoolableObjectFacto
 
     @Test
     public void testDefaultMethods() throws Exception {
-        PoolableObjectFactory<Object> factory = new BasePoolableObjectFactory<Object>() { 
+        PooledObjectFactory<Object> factory = new BasePooledObjectFactory<Object>() { 
             @Override
-            public Object makeObject() throws Exception {
+            public Object create() throws Exception {
                 return null;
             }
         };   

Modified: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java?rev=1512140&r1=1512139&r2=1512140&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java (original)
+++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestKeyedObjectPool.java Fri Aug  9 05:20:37 2013
@@ -25,6 +25,7 @@ import java.util.NoSuchElementException;
 
 import junit.framework.TestCase;
 
+import org.apache.commons.pool2.impl.DefaultPooledObject;
 import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
 import org.junit.After;
 import org.junit.Test;
@@ -41,7 +42,7 @@ public abstract class TestKeyedObjectPoo
      * behaviors described in {@link KeyedObjectPool}.
      * Generally speaking there should be no limits on the various object counts.
      */
-    protected abstract KeyedObjectPool<Object,Object> makeEmptyPool(KeyedPoolableObjectFactory<Object,Object> factory);
+    protected abstract KeyedObjectPool<Object,Object> makeEmptyPool(KeyedPooledObjectFactory<Object,Object> factory);
 
     protected static final String KEY = "key";
 
@@ -81,9 +82,9 @@ public abstract class TestKeyedObjectPoo
     public void testClosedPoolBehavior() throws Exception {
         final KeyedObjectPool<Object,Object> pool;
         try {
-            pool = makeEmptyPool(new BaseKeyedPoolableObjectFactory<Object,Object>() {
+            pool = makeEmptyPool(new BaseKeyedPooledObjectFactory<Object,Object>() {
                 @Override
-                public Object makeObject(final Object key) throws Exception {
+                public Object create(final Object key) throws Exception {
                     return new Object();
                 }
             });
@@ -129,7 +130,7 @@ public abstract class TestKeyedObjectPoo
 
     @Test
     public void testKPOFAddObjectUsage() throws Exception {
-        final FailingKeyedPoolableObjectFactory factory = new FailingKeyedPoolableObjectFactory();
+        final FailingKeyedPooledObjectFactory factory = new FailingKeyedPooledObjectFactory();
         final KeyedObjectPool<Object,Object> pool;
         try {
             pool = makeEmptyPool(factory);
@@ -177,7 +178,7 @@ public abstract class TestKeyedObjectPoo
 
     @Test
     public void testKPOFBorrowObjectUsages() throws Exception {
-        final FailingKeyedPoolableObjectFactory factory = new FailingKeyedPoolableObjectFactory();
+        final FailingKeyedPooledObjectFactory factory = new FailingKeyedPooledObjectFactory();
         final KeyedObjectPool<Object,Object> pool;
         try {
             pool = makeEmptyPool(factory);
@@ -265,7 +266,7 @@ public abstract class TestKeyedObjectPoo
 
     @Test
     public void testKPOFReturnObjectUsages() throws Exception {
-        final FailingKeyedPoolableObjectFactory factory = new FailingKeyedPoolableObjectFactory();
+        final FailingKeyedPooledObjectFactory factory = new FailingKeyedPooledObjectFactory();
         final KeyedObjectPool<Object,Object> pool;
         try {
             pool = makeEmptyPool(factory);
@@ -323,7 +324,7 @@ public abstract class TestKeyedObjectPoo
 
     @Test
     public void testKPOFInvalidateObjectUsages() throws Exception {
-        final FailingKeyedPoolableObjectFactory factory = new FailingKeyedPoolableObjectFactory();
+        final FailingKeyedPooledObjectFactory factory = new FailingKeyedPooledObjectFactory();
         final KeyedObjectPool<Object,Object> pool;
         try {
             pool = makeEmptyPool(factory);
@@ -362,7 +363,7 @@ public abstract class TestKeyedObjectPoo
 
     @Test
     public void testKPOFClearUsages() throws Exception {
-        final FailingKeyedPoolableObjectFactory factory = new FailingKeyedPoolableObjectFactory();
+        final FailingKeyedPooledObjectFactory factory = new FailingKeyedPooledObjectFactory();
         final KeyedObjectPool<Object,Object> pool;
         try {
             pool = makeEmptyPool(factory);
@@ -385,7 +386,7 @@ public abstract class TestKeyedObjectPoo
 
     @Test
     public void testKPOFCloseUsages() throws Exception {
-        final FailingKeyedPoolableObjectFactory factory = new FailingKeyedPoolableObjectFactory();
+        final FailingKeyedPooledObjectFactory factory = new FailingKeyedPooledObjectFactory();
         KeyedObjectPool<Object,Object> pool;
         try {
             pool = makeEmptyPool(factory);
@@ -409,8 +410,8 @@ public abstract class TestKeyedObjectPoo
 
     @Test
     public void testToString() throws Exception {
-        final FailingKeyedPoolableObjectFactory factory =
-                new FailingKeyedPoolableObjectFactory();
+        final FailingKeyedPooledObjectFactory factory =
+                new FailingKeyedPooledObjectFactory();
         KeyedObjectPool<Object,Object> pool = makeEmptyPool(factory);
         try {
             pool.toString();
@@ -652,18 +653,18 @@ public abstract class TestKeyedObjectPoo
     }
 
 
-    private void reset(final KeyedObjectPool<Object,Object> pool, final FailingKeyedPoolableObjectFactory factory, final List<MethodCall> expectedMethods) throws Exception {
+    private void reset(final KeyedObjectPool<Object,Object> pool, final FailingKeyedPooledObjectFactory factory, final List<MethodCall> expectedMethods) throws Exception {
         pool.clear();
         clear(factory, expectedMethods);
         factory.reset();
     }
 
-    private void clear(final FailingKeyedPoolableObjectFactory factory, final List<MethodCall> expectedMethods) {
+    private void clear(final FailingKeyedPooledObjectFactory factory, final List<MethodCall> expectedMethods) {
         factory.getMethodCalls().clear();
         expectedMethods.clear();
     }
 
-    protected static class FailingKeyedPoolableObjectFactory implements KeyedPoolableObjectFactory<Object,Object> {
+    protected static class FailingKeyedPooledObjectFactory implements KeyedPooledObjectFactory<Object,Object> {
         private final List<MethodCall> methodCalls = new ArrayList<MethodCall>();
         private int count = 0;
         private boolean makeObjectFail;
@@ -672,7 +673,7 @@ public abstract class TestKeyedObjectPoo
         private boolean passivateObjectFail;
         private boolean destroyObjectFail;
 
-        public FailingKeyedPoolableObjectFactory() {
+        public FailingKeyedPooledObjectFactory() {
         }
 
         public void reset() {
@@ -738,7 +739,7 @@ public abstract class TestKeyedObjectPoo
         }
 
         @Override
-        public Object makeObject(final Object key) throws Exception {
+        public PooledObject makeObject(final Object key) throws Exception {
             final MethodCall call = new MethodCall("makeObject", key);
             methodCalls.add(call);
             int count = this.count++;
@@ -747,20 +748,20 @@ public abstract class TestKeyedObjectPoo
             }
             final Integer obj = new Integer(count);
             call.setReturned(obj);
-            return obj;
+            return new DefaultPooledObject(obj);
         }
 
         @Override
-        public void activateObject(final Object key, final Object obj) throws Exception {
-            methodCalls.add(new MethodCall("activateObject", key, obj));
+        public void activateObject(final Object key, final PooledObject obj) throws Exception {
+            methodCalls.add(new MethodCall("activateObject", key, obj.getObject()));
             if (activateObjectFail) {
                 throw new PrivateException("activateObject");
             }
         }
 
         @Override
-        public boolean validateObject(final Object key, final Object obj) {
-            final MethodCall call = new MethodCall("validateObject", key, obj);
+        public boolean validateObject(final Object key, final PooledObject obj) {
+            final MethodCall call = new MethodCall("validateObject", key, obj.getObject());
             methodCalls.add(call);
             if (validateObjectFail) {
                 throw new PrivateException("validateObject");
@@ -771,16 +772,16 @@ public abstract class TestKeyedObjectPoo
         }
 
         @Override
-        public void passivateObject(final Object key, final Object obj) throws Exception {
-            methodCalls.add(new MethodCall("passivateObject", key, obj));
+        public void passivateObject(final Object key, final PooledObject obj) throws Exception {
+            methodCalls.add(new MethodCall("passivateObject", key, obj.getObject()));
             if (passivateObjectFail) {
                 throw new PrivateException("passivateObject");
             }
         }
 
         @Override
-        public void destroyObject(final Object key, final Object obj) throws Exception {
-            methodCalls.add(new MethodCall("destroyObject", key, obj));
+        public void destroyObject(final Object key, final PooledObject obj) throws Exception {
+            methodCalls.add(new MethodCall("destroyObject", key, obj.getObject()));
             if (destroyObjectFail) {
                 throw new PrivateException("destroyObject");
             }

Modified: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestObjectPool.java?rev=1512140&r1=1512139&r2=1512140&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestObjectPool.java (original)
+++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestObjectPool.java Fri Aug  9 05:20:37 2013
@@ -21,7 +21,7 @@ import static junit.framework.Assert.*;
 import junit.framework.TestCase;
 
 import org.apache.commons.pool2.ObjectPool;
-import org.apache.commons.pool2.PoolableObjectFactory;
+import org.apache.commons.pool2.PooledObjectFactory;
 import org.apache.commons.pool2.impl.GenericObjectPool;
 import org.apache.commons.pool2.impl.SoftReferenceObjectPool;
 import org.apache.commons.pool2.PoolUtils;
@@ -45,7 +45,7 @@ public abstract class TestObjectPool {
      * Generally speaking there should be no limits on the various object counts.
      * @throws UnsupportedOperationException if the pool being tested does not follow pool contracts.
      */
-    protected abstract ObjectPool<Object> makeEmptyPool(PoolableObjectFactory<Object> factory) throws UnsupportedOperationException;
+    protected abstract ObjectPool<Object> makeEmptyPool(PooledObjectFactory<Object> factory) throws UnsupportedOperationException;
 
     @Test
     public void testClosedPoolBehavior() throws Exception {

Modified: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestPoolUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestPoolUtils.java?rev=1512140&r1=1512139&r2=1512140&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestPoolUtils.java (original)
+++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/TestPoolUtils.java Fri Aug  9 05:20:37 2013
@@ -36,6 +36,7 @@ import java.util.TimerTask;
 import junit.framework.Assert;
 import junit.framework.AssertionFailedError;
 
+import org.apache.commons.pool2.impl.DefaultPooledObject;
 import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
 import org.apache.commons.pool2.impl.GenericObjectPool;
 import org.apache.commons.pool2.impl.PoolImplUtils;
@@ -83,11 +84,10 @@ public class TestPoolUtils {
 
         // Test that the minIdle check doesn't add too many idle objects
         @SuppressWarnings("unchecked")
-        final PoolableObjectFactory<Object> pof = createProxy(PoolableObjectFactory.class, calledMethods);
-        final ObjectPool<Object> op = new GenericObjectPool<Object>(
-                PoolImplUtils.poolableToPooledObjectFactory(pof));
+        final PooledObjectFactory<Object> pof = createProxy(PooledObjectFactory.class, calledMethods);
+        final ObjectPool<Object> op = new GenericObjectPool<Object>(pof);
         PoolUtils.checkMinIdle(op, 2, 100);
-        Thread.sleep(400);
+        Thread.sleep(1000);
         assertEquals(2, op.getNumIdle());
         op.close();
         int makeObjectCount = 0;
@@ -161,11 +161,10 @@ public class TestPoolUtils {
 
         // Test that the minIdle check doesn't add too many idle objects
         @SuppressWarnings("unchecked")
-        final KeyedPoolableObjectFactory<Object,Object> kpof =
-            createProxy(KeyedPoolableObjectFactory.class, calledMethods);
+        final KeyedPooledObjectFactory<Object,Object> kpof =
+            createProxy(KeyedPooledObjectFactory.class, calledMethods);
         final KeyedObjectPool<Object,Object> kop =
-                new GenericKeyedObjectPool<Object,Object>(
-                        PoolImplUtils.poolableToKeyedPooledObjectFactory(kpof));
+                new GenericKeyedObjectPool<Object,Object>(kpof);
         PoolUtils.checkMinIdle(kop, key, 2, 100);
         Thread.sleep(400);
         assertEquals(2, kop.getNumIdle(key));
@@ -223,7 +222,7 @@ public class TestPoolUtils {
             // expected
         }
 
-        // Because this isn't determinist and you can get false failures, try more than once.
+        // Because this isn't deterministic and you can get false failures, try more than once.
         AssertionFailedError afe = null;
         int triesLeft = 3;
         do {
@@ -389,7 +388,7 @@ public class TestPoolUtils {
     @Test
     public void testSynchronizedPoolableFactoryPoolableObjectFactory() throws Exception {
         try {
-            PoolUtils.synchronizedPoolableFactory((PoolableObjectFactory<Object>)null);
+            PoolUtils.synchronizedPooledFactory((PooledObjectFactory<Object>)null);
             fail("PoolUtils.synchronizedPoolableFactory(PoolableObjectFactory) must not allow a null factory.");
         } catch(IllegalArgumentException iae) {
             // expected
@@ -397,10 +396,10 @@ public class TestPoolUtils {
 
         final List<String> calledMethods = new ArrayList<String>();
         @SuppressWarnings("unchecked")
-        final PoolableObjectFactory<Object> pof =
-                createProxy(PoolableObjectFactory.class, calledMethods);
+        final PooledObjectFactory<Object> pof =
+                createProxy(PooledObjectFactory.class, calledMethods);
 
-        final PoolableObjectFactory<Object> spof = PoolUtils.synchronizedPoolableFactory(pof);
+        final PooledObjectFactory<Object> spof = PoolUtils.synchronizedPooledFactory(pof);
         final List<String> expectedMethods = invokeEveryMethod(spof);
         assertEquals(expectedMethods, calledMethods);
 
@@ -410,7 +409,7 @@ public class TestPoolUtils {
     @Test
     public void testSynchronizedPoolableFactoryKeyedPoolableObjectFactory() throws Exception {
         try {
-            PoolUtils.synchronizedPoolableFactory((KeyedPoolableObjectFactory<Object,Object>)null);
+            PoolUtils.synchronizedKeyedPooledFactory((KeyedPooledObjectFactory<Object,Object>)null);
             fail("PoolUtils.synchronizedPoolableFactory(KeyedPoolableObjectFactory) must not allow a null factory.");
         } catch(IllegalArgumentException iae) {
             // expected
@@ -418,10 +417,10 @@ public class TestPoolUtils {
 
         final List<String> calledMethods = new ArrayList<String>();
         @SuppressWarnings("unchecked")
-        final KeyedPoolableObjectFactory<Object,Object> kpof =
-                createProxy(KeyedPoolableObjectFactory.class, calledMethods);
+        final KeyedPooledObjectFactory<Object,Object> kpof =
+                createProxy(KeyedPooledObjectFactory.class, calledMethods);
 
-        final KeyedPoolableObjectFactory<Object,Object> skpof = PoolUtils.synchronizedPoolableFactory(kpof);
+        final KeyedPooledObjectFactory<Object,Object> skpof = PoolUtils.synchronizedKeyedPooledFactory(kpof);
         final List<String> expectedMethods = invokeEveryMethod(skpof);
         assertEquals(expectedMethods, calledMethods);
 
@@ -727,7 +726,7 @@ public class TestPoolUtils {
         return expectedMethods;
     }
 
-    private static <T> List<String> invokeEveryMethod(PoolableObjectFactory<T> pof) throws Exception {
+    private static <T> List<String> invokeEveryMethod(PooledObjectFactory<T> pof) throws Exception {
         pof.activateObject(null);
         pof.destroyObject(null);
         pof.makeObject();
@@ -742,7 +741,7 @@ public class TestPoolUtils {
         return expectedMethods;
     }
 
-    private static <K,V> List<String> invokeEveryMethod(KeyedPoolableObjectFactory<K,V> kpof) throws Exception {
+    private static <K,V> List<String> invokeEveryMethod(KeyedPooledObjectFactory<K,V> kpof) throws Exception {
         kpof.activateObject(null, null);
         kpof.destroyObject(null, null);
         kpof.makeObject(null);
@@ -785,6 +784,8 @@ public class TestPoolUtils {
                 return new Long(0);
             } else if (Object.class.equals(method.getReturnType())) {
                 return new Object();
+            } else if (PooledObject.class.equals(method.getReturnType())) {
+                return new DefaultPooledObject<Object>(new Object());
             } else {
                 return null;
             }

Modified: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/VisitTrackerFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/VisitTrackerFactory.java?rev=1512140&r1=1512139&r2=1512140&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/VisitTrackerFactory.java (original)
+++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/VisitTrackerFactory.java Fri Aug  9 05:20:37 2013
@@ -17,59 +17,60 @@
 
 package org.apache.commons.pool2;
 
-import org.apache.commons.pool2.KeyedPoolableObjectFactory;
-import org.apache.commons.pool2.PoolableObjectFactory;
+import org.apache.commons.pool2.KeyedPooledObjectFactory;
+import org.apache.commons.pool2.PooledObjectFactory;
+import org.apache.commons.pool2.impl.DefaultPooledObject;
 
 /**
  * Factory that creates VisitTracker instances. Used to
  * test Evictor runs.
  *
  */
-public class VisitTrackerFactory<K> implements PoolableObjectFactory<VisitTracker<K>>, 
-    KeyedPoolableObjectFactory<K,VisitTracker<K>> {
+public class VisitTrackerFactory<K> implements PooledObjectFactory<VisitTracker<K>>, 
+    KeyedPooledObjectFactory<K,VisitTracker<K>> {
     private int nextId = 0;
     public VisitTrackerFactory() {
         super();
     }
     @Override
-    public VisitTracker<K> makeObject() {
-        return new VisitTracker<K>(nextId++);
+    public PooledObject<VisitTracker<K>> makeObject() {
+        return new DefaultPooledObject<VisitTracker<K>>(new VisitTracker<K>(nextId++));
     }
     @Override
-    public VisitTracker<K> makeObject(K key) {
-        return new VisitTracker<K>(nextId++, key);
+    public PooledObject<VisitTracker<K>> makeObject(K key) {
+        return new DefaultPooledObject<VisitTracker<K>>(new VisitTracker<K>(nextId++, key));
     }
     @Override
-    public void destroyObject(VisitTracker<K> obj) {
-        obj.destroy();
+    public void destroyObject(PooledObject<VisitTracker<K>> ref) {
+        ref.getObject().destroy();
     }
     @Override
-    public void destroyObject(K key, VisitTracker<K> obj) {
-        obj.destroy();
+    public void destroyObject(K key, PooledObject<VisitTracker<K>> ref) {
+        ref.getObject().destroy();
     }
     @Override
-    public boolean validateObject(VisitTracker<K> obj) {
-        return obj.validate();
+    public boolean validateObject(PooledObject<VisitTracker<K>> ref) {
+        return ref.getObject().validate();
     }
     @Override
-    public boolean validateObject(K key, VisitTracker<K> obj) {
-        return obj.validate();
+    public boolean validateObject(K key, PooledObject<VisitTracker<K>> ref) {
+        return ref.getObject().validate();
     }
     @Override
-    public void activateObject(VisitTracker<K> obj) throws Exception {
-        obj.activate();
+    public void activateObject(PooledObject<VisitTracker<K>> ref) throws Exception {
+        ref.getObject().activate();
     }
     @Override
-    public void activateObject(K key, VisitTracker<K> obj) throws Exception {
-        obj.activate();
+    public void activateObject(K key, PooledObject<VisitTracker<K>> ref) throws Exception {
+        ref.getObject().activate();
     }
     @Override
-    public void passivateObject(VisitTracker<K> obj) throws Exception {
-        obj.passivate();
+    public void passivateObject(PooledObject<VisitTracker<K>> ref) throws Exception {
+        ref.getObject().passivate();
     }
     @Override
-    public void passivateObject(K key, VisitTracker<K> obj) throws Exception {
-        obj.passivate();
+    public void passivateObject(K key, PooledObject<VisitTracker<K>> ref) throws Exception {
+        ref.getObject().passivate();
     }
     public void resetId() {
         nextId = 0;



Re: svn commit: r1512140 [1/2] - in /commons/proper/pool/trunk/src: main/java/org/apache/commons/pool2/ main/java/org/apache/commons/pool2/impl/ test/java/org/apache/commons/pool2/ test/java/org/apache/commons/pool2/impl/ test/java/org/apache/commons/pool2...

Posted by Phil Steitz <ph...@gmail.com>.
On 8/8/13 10:20 PM, psteitz@apache.org wrote:
> Author: psteitz
> Date: Fri Aug  9 05:20:37 2013
> New Revision: 1512140
>
> URL: http://svn.apache.org/r1512140
> Log:
> Eliminated PoolableObjectFactory, KeyedPoolableObjectFactory and replaced them with PooledObjectFactory, KeyedPooledObjectFactory, the (formerly) subclasses that create and operate on PooledObjects.  Similarly replaced the base factories with PooledObject versions.  Modified SoftReferenceObjectPool to manage PooledObjects.
>
>
Thanks in advance for careful review - especially
SoftReferenceObjectPool changes.  I tried to preserve the original
semantics of the class, but I am honestly not sure that is needed /
best.  In particular, the original version always cleared references
to objects when they were checked out.  To be able to track objects
via soft references, I needed to create new (unregistered)
references to the objects being checked out and make the
PooledSoftReferences mutable.  I am not sure this is necessary -
i.e., we could just leave the queued references in allReferences. 
If a client is holding a hard reference to an object, a soft
reference to it should not get queued, so I don't see why we need to
explicitly prevent it.  I also did not fix the over-synchronization
and violation of our invariant that factory methods are not called
in sync scope in that class.  One thing I did change is I added a
check similar to what GOP has for an object belonging to the pool on
return.  This will hurl on objects whose references have been
cleared, but unless I am missing something, this is not really a
logical possibility.  I am no expert on GC, so it is quite possible
I have this wrong, so please enlighten me if I do.   Finally, I did
not add JMX instrumentation.  I would be OK holding on all of these
to 2.1, but will work on the JMX stuff next.  All DBCP tests pass
with no change to the code in trunk other than dropping the unused
imports.

Phil

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org