Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=62458
Update the internal fork of Commons Pool to dfef97b (2018-06-18) to pick up some bug fixes and enhancements.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc8.0.x/trunk@1833812 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/java/org/apache/tomcat/dbcp/pool2/impl/AbandonedConfig.java b/java/org/apache/tomcat/dbcp/pool2/impl/AbandonedConfig.java
index b018962..8188978 100644
--- a/java/org/apache/tomcat/dbcp/pool2/impl/AbandonedConfig.java
+++ b/java/org/apache/tomcat/dbcp/pool2/impl/AbandonedConfig.java
@@ -200,7 +200,7 @@
      * @see #getRequireFullStackTrace()
      * @since 2.5
      */
-    public void setRequireFullStackTrace(boolean requireFullStackTrace) {
+    public void setRequireFullStackTrace(final boolean requireFullStackTrace) {
         this.requireFullStackTrace = requireFullStackTrace;
     }
 
diff --git a/java/org/apache/tomcat/dbcp/pool2/impl/BaseGenericObjectPool.java b/java/org/apache/tomcat/dbcp/pool2/impl/BaseGenericObjectPool.java
index 6e89e6a..b5e2975 100644
--- a/java/org/apache/tomcat/dbcp/pool2/impl/BaseGenericObjectPool.java
+++ b/java/org/apache/tomcat/dbcp/pool2/impl/BaseGenericObjectPool.java
@@ -21,10 +21,12 @@
 import java.io.Writer;
 import java.lang.management.ManagementFactory;
 import java.lang.ref.WeakReference;
+import java.lang.reflect.InvocationTargetException;
 import java.util.Arrays;
 import java.util.Deque;
 import java.util.Iterator;
 import java.util.TimerTask;
+import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
 
@@ -38,6 +40,7 @@
 
 import org.apache.tomcat.dbcp.pool2.BaseObject;
 import org.apache.tomcat.dbcp.pool2.PooledObject;
+import org.apache.tomcat.dbcp.pool2.PooledObjectState;
 import org.apache.tomcat.dbcp.pool2.SwallowedExceptionListener;
 
 /**
@@ -60,6 +63,8 @@
      */
     public static final int MEAN_TIMING_STATS_CACHE_SIZE = 100;
 
+    private static final String EVICTION_POLICY_TYPE_NAME = EvictionPolicy.class.getName();
+
     // Configuration attributes
     private volatile int maxTotal =
             GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL;
@@ -106,7 +111,7 @@
 
 
     // Monitoring (primarily JMX) attributes
-    private final ObjectName oname;
+    private final ObjectName objectName;
     private final String creationStackTrace;
     private final AtomicLong borrowedCount = new AtomicLong(0);
     private final AtomicLong returnedCount = new AtomicLong(0);
@@ -130,12 +135,12 @@
      *                      overridden by the config
      * @param jmxNamePrefix Prefix to be used for JMX name for the new pool
      */
-    public BaseGenericObjectPool(final BaseObjectPoolConfig config,
+    public BaseGenericObjectPool(final BaseObjectPoolConfig<T> config,
             final String jmxNameBase, final String jmxNamePrefix) {
         if (config.getJmxEnabled()) {
-            this.oname = jmxRegister(config, jmxNameBase, jmxNamePrefix);
+            this.objectName = jmxRegister(config, jmxNameBase, jmxNamePrefix);
         } else {
-            this.oname = null;
+            this.objectName = null;
         }
 
         // Populate the creation stack trace
@@ -446,9 +451,11 @@
     }
 
     /**
-     * Sets the number of milliseconds to sleep between runs of the idle
-     * object evictor thread. When non-positive, no idle object evictor thread
-     * will be run.
+     * Sets the number of milliseconds to sleep between runs of the idle object evictor thread.
+     * <ul>
+     * <li>When positive, the idle object evictor thread starts.</li>
+     * <li>When non-positive, no idle object evictor thread runs.</li>
+     * </ul>
      *
      * @param timeBetweenEvictionRunsMillis
      *            number of milliseconds to sleep between evictor runs
@@ -588,47 +595,75 @@
     }
 
     /**
-     * Sets the name of the {@link EvictionPolicy} implementation that is
-     * used by this pool. The Pool will attempt to load the class using the
-     * thread context class loader. If that fails, the Pool will attempt to load
-     * the class using the class loader that loaded this class.
+     * Sets the eviction policy for this pool.
      *
-     * @param evictionPolicyClassName   the fully qualified class name of the
-     *                                  new eviction policy
+     * @param evictionPolicy
+     *            the eviction policy for this pool.
+     * @since 2.6.0
+     */
+    public void setEvictionPolicy(final EvictionPolicy<T> evictionPolicy) {
+        this.evictionPolicy = evictionPolicy;
+    }
+
+    /**
+     * Sets the name of the {@link EvictionPolicy} implementation that is used by this pool. The Pool will attempt to
+     * load the class using the given class loader. If that fails, use the class loader for the {@link EvictionPolicy}
+     * interface.
+     *
+     * @param evictionPolicyClassName
+     *            the fully qualified class name of the new eviction policy
+     * @param classLoader
+     *            the class loader to load the given {@code evictionPolicyClassName}.
      *
      * @see #getEvictionPolicyClassName()
+     * @since 2.6.0 If loading the class using the given class loader fails, use the class loader for the
+     *        {@link EvictionPolicy} interface.
      */
-    public final void setEvictionPolicyClassName(
-            final String evictionPolicyClassName) {
+    public final void setEvictionPolicyClassName(final String evictionPolicyClassName, final ClassLoader classLoader) {
+        // Getting epClass here and now best matches the caller's environment
+        final Class<?> epClass = EvictionPolicy.class;
+        final ClassLoader epClassLoader = epClass.getClassLoader();
         try {
-            Class<?> clazz;
             try {
-                clazz = Class.forName(evictionPolicyClassName, true,
-                        Thread.currentThread().getContextClassLoader());
-            } catch (final ClassNotFoundException e) {
-                clazz = Class.forName(evictionPolicyClassName);
+                setEvictionPolicy(evictionPolicyClassName, classLoader);
+            } catch (final ClassCastException | ClassNotFoundException e) {
+                setEvictionPolicy(evictionPolicyClassName, epClassLoader);
             }
-            final Object policy = clazz.getConstructor().newInstance();
-            if (policy instanceof EvictionPolicy<?>) {
-                @SuppressWarnings("unchecked") // safe, because we just checked the class
-                final
-                EvictionPolicy<T> evicPolicy = (EvictionPolicy<T>) policy;
-                this.evictionPolicy = evicPolicy;
-            } else {
-                throw new IllegalArgumentException("[" + evictionPolicyClassName +
-                        "] does not implement EvictionPolicy");
-            }
-        } catch (final ClassNotFoundException e) {
-            throw new IllegalArgumentException(
-                    "Unable to create EvictionPolicy instance of type " +
-                    evictionPolicyClassName, e);
-        } catch (final ReflectiveOperationException e) {
-            throw new IllegalArgumentException(
-                    "Unable to create EvictionPolicy instance of type " +
-                    evictionPolicyClassName, e);
+        } catch (final ClassCastException e) {
+            throw new IllegalArgumentException("Class " + evictionPolicyClassName + " from class loaders ["
+                    + classLoader + ", " + epClassLoader + "] do not implement " + EVICTION_POLICY_TYPE_NAME);
+        } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException
+                | InvocationTargetException | NoSuchMethodException e) {
+            final String exMessage = "Unable to create " + EVICTION_POLICY_TYPE_NAME + " instance of type "
+                    + evictionPolicyClassName;
+            throw new IllegalArgumentException(exMessage, e);
         }
     }
 
+    @SuppressWarnings("unchecked")
+    private void setEvictionPolicy(final String className, final ClassLoader classLoader)
+            throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
+        final Class<?> clazz = Class.forName(className, true, classLoader);
+        final Object policy = clazz.getConstructor().newInstance();
+        this.evictionPolicy = (EvictionPolicy<T>) policy;
+    }
+
+    /**
+     * Sets the name of the {@link EvictionPolicy} implementation that is used by this pool. The Pool will attempt to
+     * load the class using the thread context class loader. If that fails, the use the class loader for the
+     * {@link EvictionPolicy} interface.
+     *
+     * @param evictionPolicyClassName
+     *            the fully qualified class name of the new eviction policy
+     *
+     * @see #getEvictionPolicyClassName()
+     * @since 2.6.0 If loading the class using the thread context class loader fails, use the class loader for the
+     *        {@link EvictionPolicy} interface.
+     */
+    public final void setEvictionPolicyClassName(final String evictionPolicyClassName) {
+        setEvictionPolicyClassName(evictionPolicyClassName, Thread.currentThread().getContextClassLoader());
+    }
+
     /**
      * Gets the timeout that will be used when waiting for the Evictor to
      * shutdown if this pool is closed and it is the only pool still using the
@@ -686,8 +721,9 @@
      *
      * @return the eviction policy
      * @since 2.4
+     * @since 2.6.0 Changed access from protected to public.
      */
-    protected EvictionPolicy<T> getEvictionPolicy() {
+    public EvictionPolicy<T> getEvictionPolicy() {
         return evictionPolicy;
     }
 
@@ -742,7 +778,7 @@
      * @return the JMX name
      */
     public final ObjectName getJmxName() {
-        return oname;
+        return objectName;
     }
 
     /**
@@ -883,9 +919,9 @@
      * Swallows an exception and notifies the configured listener for swallowed
      * exceptions queue.
      *
-     * @param e exception to be swallowed
+     * @param swallowException exception to be swallowed
      */
-    final void swallowException(final Exception e) {
+    final void swallowException(final Exception swallowException) {
         final SwallowedExceptionListener listener = getSwallowedExceptionListener();
 
         if (listener == null) {
@@ -893,11 +929,9 @@
         }
 
         try {
-            listener.onSwallowException(e);
-        } catch (final OutOfMemoryError oome) {
-            throw oome;
-        } catch (final VirtualMachineError vme) {
-            throw vme;
+            listener.onSwallowException(swallowException);
+        } catch (final VirtualMachineError e) {
+            throw e;
         } catch (final Throwable t) {
             // Ignore. Enjoy the irony.
         }
@@ -934,16 +968,29 @@
     }
 
     /**
+     * Marks the object as returning to the pool.
+     * @param pooledObject instance to return to the keyed pool
+     */
+    protected void markReturningState(PooledObject<T> pooledObject) {
+        synchronized(pooledObject) {
+            final PooledObjectState state = pooledObject.getState();
+            if (state != PooledObjectState.ALLOCATED) {
+                throw new IllegalStateException(
+                        "Object has already been returned to this pool or is invalid");
+            }
+            pooledObject.markReturning(); // Keep from being marked abandoned
+        }
+    }
+
+    /**
      * Unregisters this pool's MBean.
      */
     final void jmxUnregister() {
-        if (oname != null) {
+        if (objectName != null) {
             try {
                 ManagementFactory.getPlatformMBeanServer().unregisterMBean(
-                        oname);
-            } catch (final MBeanRegistrationException e) {
-                swallowException(e);
-            } catch (final InstanceNotFoundException e) {
+                        objectName);
+            } catch (final MBeanRegistrationException | InstanceNotFoundException e) {
                 swallowException(e);
             }
         }
@@ -962,7 +1009,7 @@
      * @param jmxNamePrefix name prefix
      * @return registered ObjectName, null if registration fails
      */
-    private ObjectName jmxRegister(final BaseObjectPoolConfig config,
+    private ObjectName jmxRegister(final BaseObjectPoolConfig<T> config,
             final String jmxNameBase, String jmxNamePrefix) {
         ObjectName objectName = null;
         final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
@@ -999,10 +1046,7 @@
             } catch (final InstanceAlreadyExistsException e) {
                 // Increment the index and try again
                 i++;
-            } catch (final MBeanRegistrationException e) {
-                // Shouldn't happen. Skip registration if it does.
-                registered = true;
-            } catch (final NotCompliantMBeanException e) {
+            } catch (final MBeanRegistrationException | NotCompliantMBeanException e) {
                 // Shouldn't happen. Skip registration if it does.
                 registered = true;
             }
@@ -1032,7 +1076,10 @@
      *
      * @see GenericKeyedObjectPool#setTimeBetweenEvictionRunsMillis
      */
-    class Evictor extends TimerTask {
+    class Evictor implements Runnable {
+
+        private ScheduledFuture<?> scheduledFuture;
+
         /**
          * Run pool maintenance.  Evict objects qualifying for eviction and then
          * ensure that the minimum number of idle instances are available.
@@ -1080,6 +1127,16 @@
                 Thread.currentThread().setContextClassLoader(savedClassLoader);
             }
         }
+
+
+        void setScheduledFuture(final ScheduledFuture<?> scheduledFuture) {
+            this.scheduledFuture = scheduledFuture;
+        }
+
+
+        void cancel() {
+            scheduledFuture.cancel(false);
+        }
     }
 
     /**
@@ -1296,7 +1353,7 @@
         builder.append(", factoryClassLoader=");
         builder.append(factoryClassLoader);
         builder.append(", oname=");
-        builder.append(oname);
+        builder.append(objectName);
         builder.append(", creationStackTrace=");
         builder.append(creationStackTrace);
         builder.append(", borrowedCount=");
@@ -1323,4 +1380,5 @@
         builder.append(swallowedExceptionListener);
     }
 
+
 }
diff --git a/java/org/apache/tomcat/dbcp/pool2/impl/BaseObjectPoolConfig.java b/java/org/apache/tomcat/dbcp/pool2/impl/BaseObjectPoolConfig.java
index 5528716..7814c6d 100644
--- a/java/org/apache/tomcat/dbcp/pool2/impl/BaseObjectPoolConfig.java
+++ b/java/org/apache/tomcat/dbcp/pool2/impl/BaseObjectPoolConfig.java
@@ -24,10 +24,12 @@
  * defined by the public constants.
  * <p>
  * This class is not thread-safe.
+ * </p>
  *
+ * @param <T> Type of element pooled.
  * @since 2.0
  */
-public abstract class BaseObjectPoolConfig extends BaseObject implements Cloneable {
+public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Cloneable {
 
     /**
      * The default value for the {@code lifo} configuration attribute.
@@ -159,9 +161,7 @@
      * @see GenericObjectPool#getEvictionPolicyClassName()
      * @see GenericKeyedObjectPool#getEvictionPolicyClassName()
      */
-    public static final String DEFAULT_EVICTION_POLICY_CLASS_NAME =
-            "org.apache.tomcat.dbcp.pool2.impl.DefaultEvictionPolicy";
-
+    public static final String DEFAULT_EVICTION_POLICY_CLASS_NAME = DefaultEvictionPolicy.class.getName();
 
     private boolean lifo = DEFAULT_LIFO;
 
@@ -181,6 +181,8 @@
     private int numTestsPerEvictionRun =
             DEFAULT_NUM_TESTS_PER_EVICTION_RUN;
 
+    private EvictionPolicy<T> evictionPolicy = null; // Only 2.6.0 applications set this
+
     private String evictionPolicyClassName = DEFAULT_EVICTION_POLICY_CLASS_NAME;
 
     private boolean testOnCreate = DEFAULT_TEST_ON_CREATE;
@@ -553,6 +555,21 @@
     }
 
     /**
+     * Get the value for the {@code evictionPolicyClass} configuration
+     * attribute for pools created with this configuration instance.
+     *
+     * @return  The current setting of {@code evictionPolicyClass} for this
+     *          configuration instance
+     *
+     * @see GenericObjectPool#getEvictionPolicy()
+     * @see GenericKeyedObjectPool#getEvictionPolicy()
+     * @since 2.6.0
+     */
+    public EvictionPolicy<T> getEvictionPolicy() {
+        return evictionPolicy;
+    }
+
+    /**
      * Get the value for the {@code evictionPolicyClassName} configuration
      * attribute for pools created with this configuration instance.
      *
@@ -567,6 +584,21 @@
     }
 
     /**
+     * Set the value for the {@code evictionPolicyClass} configuration
+     * attribute for pools created with this configuration instance.
+     *
+     * @param evictionPolicy The new setting of
+     *        {@code evictionPolicyClass} for this configuration instance
+     *
+     * @see GenericObjectPool#getEvictionPolicy()
+     * @see GenericKeyedObjectPool#getEvictionPolicy()
+     * @since 2.6.0
+     */
+    public void setEvictionPolicy(final EvictionPolicy<T> evictionPolicy) {
+        this.evictionPolicy = evictionPolicy;
+    }
+
+    /**
      * Set the value for the {@code evictionPolicyClassName} configuration
      * attribute for pools created with this configuration instance.
      *
diff --git a/java/org/apache/tomcat/dbcp/pool2/impl/EvictionPolicy.java b/java/org/apache/tomcat/dbcp/pool2/impl/EvictionPolicy.java
index 3c91933..e97ffa5 100644
--- a/java/org/apache/tomcat/dbcp/pool2/impl/EvictionPolicy.java
+++ b/java/org/apache/tomcat/dbcp/pool2/impl/EvictionPolicy.java
@@ -40,6 +40,5 @@
      * @return <code>true</code> if the object should be evicted, otherwise
      *             <code>false</code>
      */
-    boolean evict(EvictionConfig config, PooledObject<T> underTest,
-            int idleCount);
+    boolean evict(EvictionConfig config, PooledObject<T> underTest, int idleCount);
 }
diff --git a/java/org/apache/tomcat/dbcp/pool2/impl/EvictionTimer.java b/java/org/apache/tomcat/dbcp/pool2/impl/EvictionTimer.java
index 06e9f73..d7cad98 100644
--- a/java/org/apache/tomcat/dbcp/pool2/impl/EvictionTimer.java
+++ b/java/org/apache/tomcat/dbcp/pool2/impl/EvictionTimer.java
@@ -18,7 +18,7 @@
 
 import java.security.AccessController;
 import java.security.PrivilegedAction;
-import java.util.TimerTask;
+import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
@@ -44,9 +44,6 @@
     /** Executor instance */
     private static ScheduledThreadPoolExecutor executor; //@GuardedBy("EvictionTimer.class")
 
-    /** Static usage count tracker */
-    private static int usageCount; //@GuardedBy("EvictionTimer.class")
-
     /** Prevent instantiation */
     private EvictionTimer() {
         // Hide the default constructor
@@ -66,19 +63,22 @@
 
     /**
      * Add the specified eviction task to the timer. Tasks that are added with a
-     * call to this method *must* call {@link #cancel(TimerTask)} to cancel the
-     * task to prevent memory and/or thread leaks in application server
-     * environments.
+     * call to this method *must* call {@link #cancel(BaseGenericObjectPool.Evictor,long,TimeUnit)}
+     * to cancel the task to prevent memory and/or thread leaks in application
+     * server environments.
      * @param task      Task to be scheduled
      * @param delay     Delay in milliseconds before task is executed
      * @param period    Time in milliseconds between executions
      */
-    static synchronized void schedule(final Runnable task, final long delay, final long period) {
+    static synchronized void schedule(
+            final BaseGenericObjectPool<?>.Evictor task, final long delay, final long period) {
         if (null == executor) {
             executor = new ScheduledThreadPoolExecutor(1, new EvictorThreadFactory());
+            executor.setRemoveOnCancelPolicy(true);
         }
-        usageCount++;
-        executor.scheduleWithFixedDelay(task, delay, period, TimeUnit.MILLISECONDS);
+        final ScheduledFuture<?> scheduledFuture =
+                executor.scheduleWithFixedDelay(task, delay, period, TimeUnit.MILLISECONDS);
+        task.setScheduledFuture(scheduledFuture);
     }
 
     /**
@@ -90,10 +90,10 @@
      *                  terminate?
      * @param unit      The units for the specified timeout
      */
-    static synchronized void cancel(final TimerTask task, final long timeout, final TimeUnit unit) {
+    static synchronized void cancel(
+            final BaseGenericObjectPool<?>.Evictor task, final long timeout, final TimeUnit unit) {
         task.cancel();
-        usageCount--;
-        if (usageCount == 0) {
+        if (executor.getQueue().size() == 0) {
             executor.shutdown();
             try {
                 executor.awaitTermination(timeout, unit);
diff --git a/java/org/apache/tomcat/dbcp/pool2/impl/GenericKeyedObjectPool.java b/java/org/apache/tomcat/dbcp/pool2/impl/GenericKeyedObjectPool.java
index eb4b3f6..3844294 100644
--- a/java/org/apache/tomcat/dbcp/pool2/impl/GenericKeyedObjectPool.java
+++ b/java/org/apache/tomcat/dbcp/pool2/impl/GenericKeyedObjectPool.java
@@ -85,7 +85,7 @@
      * @param factory the factory to be used to create entries
      */
     public GenericKeyedObjectPool(final KeyedPooledObjectFactory<K,T> factory) {
-        this(factory, new GenericKeyedObjectPoolConfig());
+        this(factory, new GenericKeyedObjectPoolConfig<T>());
     }
 
     /**
@@ -98,8 +98,8 @@
      *                  the configuration object will not be reflected in the
      *                  pool.
      */
-    public GenericKeyedObjectPool(final KeyedPooledObjectFactory<K,T> factory,
-            final GenericKeyedObjectPoolConfig config) {
+    public GenericKeyedObjectPool(final KeyedPooledObjectFactory<K, T> factory,
+            final GenericKeyedObjectPoolConfig<T> config) {
 
         super(config, ONAME_BASE, config.getJmxNamePrefix());
 
@@ -111,8 +111,6 @@
         this.fairness = config.getFairness();
 
         setConfig(config);
-
-        startEvictor(getTimeBetweenEvictionRunsMillis());
     }
 
     /**
@@ -237,7 +235,7 @@
      *
      * @see GenericKeyedObjectPoolConfig
      */
-    public void setConfig(final GenericKeyedObjectPoolConfig conf) {
+    public void setConfig(final GenericKeyedObjectPoolConfig<T> conf) {
         setLifo(conf.getLifo());
         setMaxIdlePerKey(conf.getMaxIdlePerKey());
         setMaxTotalPerKey(conf.getMaxTotalPerKey());
@@ -251,11 +249,16 @@
         setTestWhileIdle(conf.getTestWhileIdle());
         setNumTestsPerEvictionRun(conf.getNumTestsPerEvictionRun());
         setMinEvictableIdleTimeMillis(conf.getMinEvictableIdleTimeMillis());
-        setSoftMinEvictableIdleTimeMillis(
-                conf.getSoftMinEvictableIdleTimeMillis());
-        setTimeBetweenEvictionRunsMillis(
-                conf.getTimeBetweenEvictionRunsMillis());
-        setEvictionPolicyClassName(conf.getEvictionPolicyClassName());
+        setSoftMinEvictableIdleTimeMillis(conf.getSoftMinEvictableIdleTimeMillis());
+        setTimeBetweenEvictionRunsMillis(conf.getTimeBetweenEvictionRunsMillis());
+        final EvictionPolicy<T> policy = conf.getEvictionPolicy();
+        if (policy == null) {
+            // Use the class name (pre-2.6.0 compatible)
+            setEvictionPolicyClassName(conf.getEvictionPolicyClassName());
+        } else {
+            // Otherwise, use the class (2.6.0 feature)
+            setEvictionPolicy(policy);
+        }
         setEvictorShutdownTimeoutMillis(conf.getEvictorShutdownTimeoutMillis());
     }
 
@@ -470,14 +473,7 @@
                     "Returned object not currently part of this pool");
         }
 
-        synchronized(p) {
-            final PooledObjectState state = p.getState();
-            if (state != PooledObjectState.ALLOCATED) {
-                throw new IllegalStateException(
-                        "Object has already been returned to this pool or is invalid");
-            }
-            p.markReturning(); // Keep from being marked abandoned (once GKOP does this)
-        }
+        markReturningState(p);
 
         final long activeTime = p.getActiveTimeMillis();
 
@@ -489,13 +485,7 @@
                     } catch (final Exception e) {
                         swallowException(e);
                     }
-                    if (objectDeque.idleObjects.hasTakeWaiters()) {
-                        try {
-                            addObject(key);
-                        } catch (final Exception e) {
-                            swallowException(e);
-                        }
-                    }
+                    whenWaitersAddObject(key, objectDeque.idleObjects);
                     return;
                 }
             }
@@ -509,13 +499,7 @@
                 } catch (final Exception e) {
                     swallowException(e);
                 }
-                if (objectDeque.idleObjects.hasTakeWaiters()) {
-                    try {
-                        addObject(key);
-                    } catch (final Exception e) {
-                        swallowException(e);
-                    }
-                }
+                whenWaitersAddObject(key, objectDeque.idleObjects);
                 return;
             }
 
@@ -555,6 +539,20 @@
         }
     }
 
+    /**
+     * Whether there is at least one thread waiting on this deque, add an pool object.
+     * @param key
+     * @param idleObjects
+     */
+    private void whenWaitersAddObject(final K key, LinkedBlockingDeque<PooledObject<T>> idleObjects) {
+        if (idleObjects.hasTakeWaiters()) {
+            try {
+                addObject(key);
+            } catch (final Exception e) {
+                swallowException(e);
+            }
+        }
+    }
 
     /**
      * {@inheritDoc}
diff --git a/java/org/apache/tomcat/dbcp/pool2/impl/GenericKeyedObjectPoolConfig.java b/java/org/apache/tomcat/dbcp/pool2/impl/GenericKeyedObjectPoolConfig.java
index dc40035..4310674 100644
--- a/java/org/apache/tomcat/dbcp/pool2/impl/GenericKeyedObjectPoolConfig.java
+++ b/java/org/apache/tomcat/dbcp/pool2/impl/GenericKeyedObjectPoolConfig.java
@@ -23,10 +23,12 @@
  * <p>
  * This class is not thread-safe; it is only intended to be used to provide
  * attributes used when creating a pool.
+ * </p>
  *
+ * @param <T> Type of element pooled.
  * @since 2.0
  */
-public class GenericKeyedObjectPoolConfig extends BaseObjectPoolConfig {
+public class GenericKeyedObjectPoolConfig<T> extends BaseObjectPoolConfig<T> {
 
     /**
      * The default value for the {@code maxTotalPerKey} configuration attribute.
@@ -171,10 +173,11 @@
         this.maxIdlePerKey = maxIdlePerKey;
     }
 
+    @SuppressWarnings("unchecked")
     @Override
-    public GenericKeyedObjectPoolConfig clone() {
+    public GenericKeyedObjectPoolConfig<T> clone() {
         try {
-            return (GenericKeyedObjectPoolConfig) super.clone();
+            return (GenericKeyedObjectPoolConfig<T>) super.clone();
         } catch (final CloneNotSupportedException e) {
             throw new AssertionError(); // Can't happen
         }
diff --git a/java/org/apache/tomcat/dbcp/pool2/impl/GenericKeyedObjectPoolMXBean.java b/java/org/apache/tomcat/dbcp/pool2/impl/GenericKeyedObjectPoolMXBean.java
index 2d29dfe..cf330f8 100644
--- a/java/org/apache/tomcat/dbcp/pool2/impl/GenericKeyedObjectPoolMXBean.java
+++ b/java/org/apache/tomcat/dbcp/pool2/impl/GenericKeyedObjectPoolMXBean.java
@@ -34,169 +34,204 @@
  * @since 2.0
  */
 public interface GenericKeyedObjectPoolMXBean<K> {
+
     // Expose getters for configuration settings
+
     /**
      * See {@link GenericKeyedObjectPool#getBlockWhenExhausted()}
      * @return See {@link GenericKeyedObjectPool#getBlockWhenExhausted()}
      */
     boolean getBlockWhenExhausted();
+
     /**
      * See {@link GenericKeyedObjectPool#getFairness()}
      * @return See {@link GenericKeyedObjectPool#getFairness()}
      */
     boolean getFairness();
+
     /**
      * See {@link GenericKeyedObjectPool#getLifo()}
      * @return See {@link GenericKeyedObjectPool#getLifo()}
      */
     boolean getLifo();
+
     /**
      * See {@link GenericKeyedObjectPool#getMaxIdlePerKey()}
      * @return See {@link GenericKeyedObjectPool#getMaxIdlePerKey()}
      */
     int getMaxIdlePerKey();
+
     /**
      * See {@link GenericKeyedObjectPool#getMaxTotal()}
      * @return See {@link GenericKeyedObjectPool#getMaxTotal()}
      */
     int getMaxTotal();
+
     /**
      * See {@link GenericKeyedObjectPool#getMaxTotalPerKey()}
      * @return See {@link GenericKeyedObjectPool#getMaxTotalPerKey()}
      */
     int getMaxTotalPerKey();
+
     /**
      * See {@link GenericKeyedObjectPool#getMaxWaitMillis()}
      * @return See {@link GenericKeyedObjectPool#getMaxWaitMillis()}
      */
     long getMaxWaitMillis();
+
     /**
      * See {@link GenericKeyedObjectPool#getMinEvictableIdleTimeMillis()}
      * @return See {@link GenericKeyedObjectPool#getMinEvictableIdleTimeMillis()}
      */
     long getMinEvictableIdleTimeMillis();
+
     /**
      * See {@link GenericKeyedObjectPool#getMinIdlePerKey()}
      * @return See {@link GenericKeyedObjectPool#getMinIdlePerKey()}
      */
     int getMinIdlePerKey();
+
     /**
      * See {@link GenericKeyedObjectPool#getNumActive()}
      * @return See {@link GenericKeyedObjectPool#getNumActive()}
      */
     int getNumActive();
+
     /**
      * See {@link GenericKeyedObjectPool#getNumIdle()}
      * @return See {@link GenericKeyedObjectPool#getNumIdle()}
      */
     int getNumIdle();
+
     /**
      * See {@link GenericKeyedObjectPool#getNumTestsPerEvictionRun()}
      * @return See {@link GenericKeyedObjectPool#getNumTestsPerEvictionRun()}
      */
     int getNumTestsPerEvictionRun();
+
     /**
      * See {@link GenericKeyedObjectPool#getTestOnCreate()}
      * @return See {@link GenericKeyedObjectPool#getTestOnCreate()}
      * @since 2.2
      */
     boolean getTestOnCreate();
+
     /**
      * See {@link GenericKeyedObjectPool#getTestOnBorrow()}
      * @return See {@link GenericKeyedObjectPool#getTestOnBorrow()}
      */
     boolean getTestOnBorrow();
+
     /**
      * See {@link GenericKeyedObjectPool#getTestOnReturn()}
      * @return See {@link GenericKeyedObjectPool#getTestOnReturn()}
      */
     boolean getTestOnReturn();
+
     /**
      * See {@link GenericKeyedObjectPool#getTestWhileIdle()}
      * @return See {@link GenericKeyedObjectPool#getTestWhileIdle()}
      */
     boolean getTestWhileIdle();
+
     /**
      * See {@link GenericKeyedObjectPool#getTimeBetweenEvictionRunsMillis()}
      * @return See {@link GenericKeyedObjectPool#getTimeBetweenEvictionRunsMillis()}
      */
     long getTimeBetweenEvictionRunsMillis();
+
     /**
      * See {@link GenericKeyedObjectPool#isClosed()}
      * @return See {@link GenericKeyedObjectPool#isClosed()}
      */
     boolean isClosed();
+
     // Expose getters for monitoring attributes
+
     /**
      * See {@link GenericKeyedObjectPool#getNumActivePerKey()}
      * @return See {@link GenericKeyedObjectPool#getNumActivePerKey()}
      */
     Map<String,Integer> getNumActivePerKey();
+
     /**
      * See {@link GenericKeyedObjectPool#getBorrowedCount()}
      * @return See {@link GenericKeyedObjectPool#getBorrowedCount()}
      */
     long getBorrowedCount();
+
     /**
      * See {@link GenericKeyedObjectPool#getReturnedCount()}
      * @return See {@link GenericKeyedObjectPool#getReturnedCount()}
      */
     long getReturnedCount();
+
     /**
      * See {@link GenericKeyedObjectPool#getCreatedCount()}
      * @return See {@link GenericKeyedObjectPool#getCreatedCount()}
      */
     long getCreatedCount();
+
     /**
      * See {@link GenericKeyedObjectPool#getDestroyedCount()}
      * @return See {@link GenericKeyedObjectPool#getDestroyedCount()}
      */
     long getDestroyedCount();
+
     /**
      * See {@link GenericKeyedObjectPool#getDestroyedByEvictorCount()}
      * @return See {@link GenericKeyedObjectPool#getDestroyedByEvictorCount()}
      */
     long getDestroyedByEvictorCount();
+
     /**
      * See {@link GenericKeyedObjectPool#getDestroyedByBorrowValidationCount()}
      * @return See {@link GenericKeyedObjectPool#getDestroyedByBorrowValidationCount()}
      */
     long getDestroyedByBorrowValidationCount();
+
     /**
      * See {@link GenericKeyedObjectPool#getMeanActiveTimeMillis()}
      * @return See {@link GenericKeyedObjectPool#getMeanActiveTimeMillis()}
      */
     long getMeanActiveTimeMillis();
+
     /**
      * See {@link GenericKeyedObjectPool#getMeanIdleTimeMillis()}
      * @return See {@link GenericKeyedObjectPool#getMeanIdleTimeMillis()}
      */
     long getMeanIdleTimeMillis();
+
     /**
      * See {@link GenericKeyedObjectPool#getMaxBorrowWaitTimeMillis()}
      * @return See {@link GenericKeyedObjectPool#getMaxBorrowWaitTimeMillis()}
      */
     long getMeanBorrowWaitTimeMillis();
+
     /**
      * See {@link GenericKeyedObjectPool#getMaxBorrowWaitTimeMillis()}
      * @return See {@link GenericKeyedObjectPool#getMaxBorrowWaitTimeMillis()}
      */
     long getMaxBorrowWaitTimeMillis();
+
     /**
      * See {@link GenericKeyedObjectPool#getCreationStackTrace()}
      * @return See {@link GenericKeyedObjectPool#getCreationStackTrace()}
      */
     String getCreationStackTrace();
+
     /**
      * See {@link GenericKeyedObjectPool#getNumWaiters()}
      * @return See {@link GenericKeyedObjectPool#getNumWaiters()}
      */
     int getNumWaiters();
+
     /**
      * See {@link GenericKeyedObjectPool#getNumWaitersByKey()}
      * @return See {@link GenericKeyedObjectPool#getNumWaitersByKey()}
      */
     Map<String,Integer> getNumWaitersByKey();
+
     /**
      * See {@link GenericKeyedObjectPool#listAllObjects()}
      * @return See {@link GenericKeyedObjectPool#listAllObjects()}
diff --git a/java/org/apache/tomcat/dbcp/pool2/impl/GenericObjectPool.java b/java/org/apache/tomcat/dbcp/pool2/impl/GenericObjectPool.java
index 965f91a..a92db06 100644
--- a/java/org/apache/tomcat/dbcp/pool2/impl/GenericObjectPool.java
+++ b/java/org/apache/tomcat/dbcp/pool2/impl/GenericObjectPool.java
@@ -77,18 +77,18 @@
         implements ObjectPool<T>, GenericObjectPoolMXBean, UsageTracking<T> {
 
     /**
-     * Create a new <code>GenericObjectPool</code> using defaults from
+     * Creates a new <code>GenericObjectPool</code> using defaults from
      * {@link GenericObjectPoolConfig}.
      *
      * @param factory The object factory to be used to create object instances
      *                used by this pool
      */
     public GenericObjectPool(final PooledObjectFactory<T> factory) {
-        this(factory, new GenericObjectPoolConfig());
+        this(factory, new GenericObjectPoolConfig<T>());
     }
 
     /**
-     * Create a new <code>GenericObjectPool</code> using a specific
+     * Creates a new <code>GenericObjectPool</code> using a specific
      * configuration.
      *
      * @param factory   The object factory to be used to create object instances
@@ -99,7 +99,7 @@
      *                  pool.
      */
     public GenericObjectPool(final PooledObjectFactory<T> factory,
-            final GenericObjectPoolConfig config) {
+            final GenericObjectPoolConfig<T> config) {
 
         super(config, ONAME_BASE, config.getJmxNamePrefix());
 
@@ -112,12 +112,10 @@
         idleObjects = new LinkedBlockingDeque<>(config.getFairness());
 
         setConfig(config);
-
-        startEvictor(getTimeBetweenEvictionRunsMillis());
     }
 
     /**
-     * Create a new <code>GenericObjectPool</code> that tracks and destroys
+     * Creates a new <code>GenericObjectPool</code> that tracks and destroys
      * objects that are checked out, but never returned to the pool.
      *
      * @param factory   The object factory to be used to create object instances
@@ -130,7 +128,7 @@
      *                         and removal.  The configuration is used by value.
      */
     public GenericObjectPool(final PooledObjectFactory<T> factory,
-            final GenericObjectPoolConfig config, final AbandonedConfig abandonedConfig) {
+            final GenericObjectPoolConfig<T> config, final AbandonedConfig abandonedConfig) {
         this(factory, config);
         setAbandonedConfig(abandonedConfig);
     }
@@ -221,7 +219,7 @@
     }
 
     /**
-     * Whether or not abandoned object removal is configured for this pool.
+     * Gets whether or not abandoned object removal is configured for this pool.
      *
      * @return true if this pool is configured to detect and remove
      * abandoned objects
@@ -232,7 +230,7 @@
     }
 
     /**
-     * Will this pool identify and log any abandoned objects?
+     * Gets whether this pool identifies and logs any abandoned objects.
      *
      * @return {@code true} if abandoned object removal is configured for this
      *         pool and removal events are to be logged otherwise {@code false}
@@ -246,8 +244,8 @@
     }
 
     /**
-     * Will a check be made for abandoned objects when an object is borrowed
-     * from this pool?
+     * Gets whether a check is made for abandoned objects when an object is borrowed
+     * from this pool.
      *
      * @return {@code true} if abandoned object removal is configured to be
      *         activated by borrowObject otherwise {@code false}
@@ -261,7 +259,7 @@
     }
 
     /**
-     * Will a check be made for abandoned objects when the evictor runs?
+     * Gets whether a check is made for abandoned objects when the evictor runs.
      *
      * @return {@code true} if abandoned object removal is configured to be
      *         activated when the evictor runs otherwise {@code false}
@@ -275,7 +273,7 @@
     }
 
     /**
-     * Obtain the timeout before which an object will be considered to be
+     * Obtains the timeout before which an object will be considered to be
      * abandoned by this pool.
      *
      * @return The abandoned object timeout in seconds if abandoned object
@@ -297,7 +295,7 @@
      *
      * @see GenericObjectPoolConfig
      */
-    public void setConfig(final GenericObjectPoolConfig conf) {
+    public void setConfig(final GenericObjectPoolConfig<T> conf) {
         setLifo(conf.getLifo());
         setMaxIdle(conf.getMaxIdle());
         setMinIdle(conf.getMinIdle());
@@ -310,11 +308,16 @@
         setTestWhileIdle(conf.getTestWhileIdle());
         setNumTestsPerEvictionRun(conf.getNumTestsPerEvictionRun());
         setMinEvictableIdleTimeMillis(conf.getMinEvictableIdleTimeMillis());
-        setTimeBetweenEvictionRunsMillis(
-                conf.getTimeBetweenEvictionRunsMillis());
-        setSoftMinEvictableIdleTimeMillis(
-                conf.getSoftMinEvictableIdleTimeMillis());
-        setEvictionPolicyClassName(conf.getEvictionPolicyClassName());
+        setTimeBetweenEvictionRunsMillis(conf.getTimeBetweenEvictionRunsMillis());
+        setSoftMinEvictableIdleTimeMillis(conf.getSoftMinEvictableIdleTimeMillis());
+        final EvictionPolicy<T> policy = conf.getEvictionPolicy();
+        if (policy == null) {
+            // Use the class name (pre-2.6.0 compatible)
+            setEvictionPolicyClassName(conf.getEvictionPolicyClassName());
+        } else {
+            // Otherwise, use the class (2.6.0 feature)
+            setEvictionPolicy(policy);
+        }
         setEvictorShutdownTimeoutMillis(conf.getEvictorShutdownTimeoutMillis());
     }
 
@@ -341,7 +344,7 @@
     }
 
     /**
-     * Obtain a reference to the factory used to create, destroy and validate
+     * Obtains a reference to the factory used to create, destroy and validate
      * the objects used by this pool.
      *
      * @return the factory
@@ -362,7 +365,7 @@
     }
 
     /**
-     * Borrow an object from the pool using the specific waiting time which only
+     * Borrows an object from the pool using the specific waiting time which only
      * applies if {@link #getBlockWhenExhausted()} is true.
      * <p>
      * If there is one or more idle instance available in the pool, then an
@@ -533,14 +536,7 @@
             return; // Object was abandoned and removed
         }
 
-        synchronized(p) {
-            final PooledObjectState state = p.getState();
-            if (state != PooledObjectState.ALLOCATED) {
-                throw new IllegalStateException(
-                        "Object has already been returned to this pool or is invalid");
-            }
-            p.markReturning(); // Keep from being marked abandoned
-        }
+        markReturningState(p);
 
         final long activeTime = p.getActiveTimeMillis();
 
@@ -888,7 +884,7 @@
         final PooledObject<T> p;
         try {
             p = factory.makeObject();
-        } catch (final Exception e) {
+        } catch (final Throwable e) {
             createCount.decrementAndGet();
             throw e;
         } finally {
@@ -976,7 +972,7 @@
     }
 
     /**
-     * Create an object, and place it into the pool. addObject() is useful for
+     * Creates an object, and place it into the pool. addObject() is useful for
      * "pre-loading" a pool with idle objects.
      * <p>
      * If there is no capacity available to add to the pool, this is a no-op
@@ -994,7 +990,7 @@
     }
 
     /**
-     * Add the provided wrapped pooled object to the set of idle objects for
+     * Adds the provided wrapped pooled object to the set of idle objects for
      * this pool. The object must already be part of the pool.  If {@code p}
      * is null, this is a no-op (no exception, but no impact on the pool).
      *
@@ -1014,7 +1010,7 @@
     }
 
     /**
-     * Calculate the number of objects to test in a run of the idle object
+     * Calculates the number of objects to test in a run of the idle object
      * evictor.
      *
      * @return The number of objects to test for validity
@@ -1029,7 +1025,7 @@
     }
 
     /**
-     * Recover abandoned objects which have been checked out but
+     * Recovers abandoned objects which have been checked out but
      * not used since longer than the removeAbandonedTimeout.
      *
      * @param ac The configuration to use to identify abandoned objects
@@ -1085,7 +1081,7 @@
     private volatile String factoryType = null;
 
     /**
-     * Return an estimate of the number of threads currently blocked waiting for
+     * Returns an estimate of the number of threads currently blocked waiting for
      * an object from the pool. This is intended for monitoring only, not for
      * synchronization control.
      *
@@ -1101,7 +1097,7 @@
     }
 
     /**
-     * Return the type - including the specific type rather than the generic -
+     * Returns the type - including the specific type rather than the generic -
      * of the factory.
      *
      * @return A string representation of the factory type
diff --git a/java/org/apache/tomcat/dbcp/pool2/impl/GenericObjectPoolConfig.java b/java/org/apache/tomcat/dbcp/pool2/impl/GenericObjectPoolConfig.java
index 7685c7c..32cdfe0 100644
--- a/java/org/apache/tomcat/dbcp/pool2/impl/GenericObjectPoolConfig.java
+++ b/java/org/apache/tomcat/dbcp/pool2/impl/GenericObjectPoolConfig.java
@@ -23,10 +23,12 @@
  * <p>
  * This class is not thread-safe; it is only intended to be used to provide
  * attributes used when creating a pool.
+ * </p>
  *
+ * @param <T> Type of element pooled.
  * @since 2.0
  */
-public class GenericObjectPoolConfig extends BaseObjectPoolConfig {
+public class GenericObjectPoolConfig<T> extends BaseObjectPoolConfig<T> {
 
     /**
      * The default value for the {@code maxTotal} configuration attribute.
@@ -133,10 +135,11 @@
         this.minIdle = minIdle;
     }
 
+    @SuppressWarnings("unchecked")
     @Override
-    public GenericObjectPoolConfig clone() {
+    public GenericObjectPoolConfig<T> clone() {
         try {
-            return (GenericObjectPoolConfig) super.clone();
+            return (GenericObjectPoolConfig<T>) super.clone();
         } catch (final CloneNotSupportedException e) {
             throw new AssertionError(); // Can't happen
         }
diff --git a/java/org/apache/tomcat/dbcp/pool2/impl/LinkedBlockingDeque.java b/java/org/apache/tomcat/dbcp/pool2/impl/LinkedBlockingDeque.java
index 3c89f5a..be92f91 100644
--- a/java/org/apache/tomcat/dbcp/pool2/impl/LinkedBlockingDeque.java
+++ b/java/org/apache/tomcat/dbcp/pool2/impl/LinkedBlockingDeque.java
@@ -901,7 +901,7 @@
     }
 
     /**
-     * Empty the queue to the specified collection.
+     * Drains the queue to the specified collection.
      *
      * @param c The collection to add the elements to
      *
@@ -920,7 +920,7 @@
     }
 
     /**
-     * Empty no more than the specified number of elements from the queue to the
+     * Drains no more than the specified number of elements from the queue to the
      * specified collection.
      *
      * @param c           collection to add the elements to
@@ -1330,7 +1330,7 @@
     }
 
     /**
-     * Save the state of this deque to a stream (that is, serialize it).
+     * Saves the state of this deque to a stream (that is, serialize it).
      *
      * @serialData The capacity (int), followed by elements (each an
      * {@code Object}) in the proper order, followed by a null
@@ -1354,7 +1354,7 @@
     }
 
     /**
-     * Reconstitute this deque from a stream (that is,
+     * Reconstitutes this deque from a stream (that is,
      * deserialize it).
      * @param s the stream
      */
@@ -1379,8 +1379,7 @@
     // Monitoring methods
 
     /**
-     * Returns true if there are threads waiting to take instances from this deque.
-     * See disclaimer on accuracy in
+     * Returns true if there are threads waiting to take instances from this deque. See disclaimer on accuracy in
      * {@link java.util.concurrent.locks.ReentrantLock#hasWaiters(Condition)}.
      *
      * @return true if there is at least one thread waiting on this deque's notEmpty condition.
@@ -1395,9 +1394,8 @@
     }
 
     /**
-     * Returns the length of the queue of threads waiting to take instances from this deque.
-     * See disclaimer on accuracy in
-     * {@link java.util.concurrent.locks.ReentrantLock#getWaitQueueLength(Condition)}.
+     * Returns the length of the queue of threads waiting to take instances from this deque. See disclaimer on accuracy
+     * in {@link java.util.concurrent.locks.ReentrantLock#getWaitQueueLength(Condition)}.
      *
      * @return number of threads waiting on this deque's notEmpty condition.
      */
@@ -1411,8 +1409,7 @@
     }
 
     /**
-     * Interrupts the threads currently waiting to take an object from the pool.
-     * See disclaimer on accuracy in
+     * Interrupts the threads currently waiting to take an object from the pool. See disclaimer on accuracy in
      * {@link java.util.concurrent.locks.ReentrantLock#getWaitingThreads(Condition)}.
      */
     public void interuptTakeWaiters() {
diff --git a/java/org/apache/tomcat/dbcp/pool2/impl/NoOpCallStack.java b/java/org/apache/tomcat/dbcp/pool2/impl/NoOpCallStack.java
index ffb91b5..80a0825 100644
--- a/java/org/apache/tomcat/dbcp/pool2/impl/NoOpCallStack.java
+++ b/java/org/apache/tomcat/dbcp/pool2/impl/NoOpCallStack.java
@@ -26,13 +26,16 @@
  */
 public class NoOpCallStack implements CallStack {
 
+    /**
+     * Singleton instance.
+     */
     public static final CallStack INSTANCE = new NoOpCallStack();
 
     private NoOpCallStack() {
     }
 
     @Override
-    public boolean printStackTrace(PrintWriter writer) {
+    public boolean printStackTrace(final PrintWriter writer) {
         return false;
     }
 
diff --git a/java/org/apache/tomcat/dbcp/pool2/impl/PoolImplUtils.java b/java/org/apache/tomcat/dbcp/pool2/impl/PoolImplUtils.java
index 2a818e6..cb0661c 100644
--- a/java/org/apache/tomcat/dbcp/pool2/impl/PoolImplUtils.java
+++ b/java/org/apache/tomcat/dbcp/pool2/impl/PoolImplUtils.java
@@ -32,84 +32,115 @@
     /**
      * Identifies the concrete type of object that an object factory creates.
      *
-     * @param factory The factory to examine
+     * @param factoryClass
+     *            The factory to examine
      *
      * @return the type of object the factory creates
      */
     @SuppressWarnings("rawtypes")
-    static Class<?> getFactoryType(final Class<? extends PooledObjectFactory> factory) {
-        return (Class<?>) getGenericType(PooledObjectFactory.class, factory);
-    }
-
-
-    /**
-     * Obtain the concrete type used by an implementation of an interface that
-     * uses a generic type.
-     *
-     * @param type  The interface that defines a generic type
-     * @param clazz The class that implements the interface with a concrete type
-     * @param <T>   The interface type
-     *
-     * @return concrete type used by the implementation
-     */
-    private static <T> Object getGenericType(final Class<T> type,
-            final Class<? extends T> clazz) {
-
-        // Look to see if this class implements the generic interface
-
-        // Get all the interfaces
-        final Type[] interfaces = clazz.getGenericInterfaces();
-        for (final Type iface : interfaces) {
-            // Only need to check interfaces that use generics
-            if (iface instanceof ParameterizedType) {
-                final ParameterizedType pi = (ParameterizedType) iface;
-                // Look for the generic interface
-                if (pi.getRawType() instanceof Class) {
-                    if (type.isAssignableFrom((Class<?>) pi.getRawType())) {
-                        return getTypeParameter(
-                                clazz, pi.getActualTypeArguments()[0]);
+    static Class<?> getFactoryType(final Class<? extends PooledObjectFactory> factoryClass) {
+        final Class<PooledObjectFactory> type = PooledObjectFactory.class;
+        final Object genericType = getGenericType(type, factoryClass);
+        if (genericType instanceof Integer) {
+            // POOL-324 org.apache.commons.pool2.impl.GenericObjectPool.getFactoryType() throws
+            // java.lang.ClassCastException
+            //
+            // A bit hackish, but we must handle cases when getGenericType() does not return a concrete types.
+            final ParameterizedType pi = getParameterizedType(type, factoryClass);
+            if (pi != null) {
+                final Type[] bounds = ((TypeVariable) pi.getActualTypeArguments()[((Integer) genericType).intValue()]).getBounds();
+                if (bounds != null && bounds.length > 0) {
+                    final Type bound0 = bounds[0];
+                    if (bound0 instanceof Class) {
+                        return (Class<?>) bound0;
                     }
                 }
             }
+            // last resort: Always return a Class
+            return Object.class;
+        }
+        return (Class<?>) genericType;
+    }
+
+    /**
+     * Obtains the concrete type used by an implementation of an interface that uses a generic type.
+     *
+     * @param type
+     *            The interface that defines a generic type
+     * @param clazz
+     *            The class that implements the interface with a concrete type
+     * @param <T>
+     *            The interface type
+     *
+     * @return concrete type used by the implementation
+     */
+    private static <T> Object getGenericType(final Class<T> type, final Class<? extends T> clazz) {
+        if (type == null || clazz == null) {
+            // Error will be logged further up the call stack
+            return null;
+        }
+
+        // Look to see if this class implements the generic interface
+        final ParameterizedType pi = getParameterizedType(type, clazz);
+        if (pi != null) {
+            return getTypeParameter(clazz, pi.getActualTypeArguments()[0]);
         }
 
         // Interface not found on this class. Look at the superclass.
         @SuppressWarnings("unchecked")
-        final
-        Class<? extends T> superClazz =
-                (Class<? extends T>) clazz.getSuperclass();
+        final Class<? extends T> superClass = (Class<? extends T>) clazz.getSuperclass();
 
-        final Object result = getGenericType(type, superClazz);
+        final Object result = getGenericType(type, superClass);
         if (result instanceof Class<?>) {
-            // Superclass implements interface and defines explicit type for
-            // generic
+            // Superclass implements interface and defines explicit type for generic
             return result;
         } else if (result instanceof Integer) {
-            // Superclass implements interface and defines unknown type for
-            // generic
+            // Superclass implements interface and defines unknown type for generic
             // Map that unknown type to the generic types defined in this class
-            final ParameterizedType superClassType =
-                    (ParameterizedType) clazz.getGenericSuperclass();
-            return getTypeParameter(clazz,
-                    superClassType.getActualTypeArguments()[
-                            ((Integer) result).intValue()]);
+            final ParameterizedType superClassType = (ParameterizedType) clazz.getGenericSuperclass();
+            return getTypeParameter(clazz, superClassType.getActualTypeArguments()[((Integer) result).intValue()]);
         } else {
             // Error will be logged further up the call stack
             return null;
         }
     }
 
+    /**
+     * Gets the matching parameterized type or null.
+     * @param type
+     *            The interface that defines a generic type
+     * @param clazz
+     *            The class that implements the interface with a concrete type
+     * @param <T>
+     *            The interface type
+     */
+    private static <T> ParameterizedType getParameterizedType(final Class<T> type, final Class<? extends T> clazz) {
+        for (final Type iface : clazz.getGenericInterfaces()) {
+            // Only need to check interfaces that use generics
+            if (iface instanceof ParameterizedType) {
+                final ParameterizedType pi = (ParameterizedType) iface;
+                // Look for the generic interface
+                if (pi.getRawType() instanceof Class) {
+                    if (type.isAssignableFrom((Class<?>) pi.getRawType())) {
+                        return pi;
+                    }
+                }
+            }
+        }
+        return null;
+    }
 
     /**
-     * For a generic parameter, return either the Class used or if the type
-     * is unknown, the index for the type in definition of the class
+     * For a generic parameter, return either the Class used or if the type is unknown, the index for the type in
+     * definition of the class
      *
-     * @param clazz defining class
-     * @param argType the type argument of interest
+     * @param clazz
+     *            defining class
+     * @param argType
+     *            the type argument of interest
      *
-     * @return An instance of {@link Class} representing the type used by the
-     *         type parameter or an instance of {@link Integer} representing
-     *         the index for the type in the definition of the defining class
+     * @return An instance of {@link Class} representing the type used by the type parameter or an instance of
+     *         {@link Integer} representing the index for the type in the definition of the defining class
      */
     private static Object getTypeParameter(final Class<?> clazz, final Type argType) {
         if (argType instanceof Class<?>) {
diff --git a/java/org/apache/tomcat/dbcp/pool2/impl/SoftReferenceObjectPool.java b/java/org/apache/tomcat/dbcp/pool2/impl/SoftReferenceObjectPool.java
index 3708c2a..66154a5 100644
--- a/java/org/apache/tomcat/dbcp/pool2/impl/SoftReferenceObjectPool.java
+++ b/java/org/apache/tomcat/dbcp/pool2/impl/SoftReferenceObjectPool.java
@@ -78,7 +78,7 @@
     }
 
     /**
-     * Borrow an object from the pool. If there are no idle instances available
+     * Borrows an object from the pool. If there are no idle instances available
      * in the pool, the configured factory's
      * {@link PooledObjectFactory#makeObject()} method is invoked to create a
      * new instance.
@@ -243,7 +243,7 @@
     }
 
     /**
-     * Create an object, and place it into the pool. addObject() is useful for
+     * Creates an object, and places it into the pool. addObject() is useful for
      * "pre-loading" a pool with idle objects.
      * <p>
      * Before being added to the pool, the newly created instance is
@@ -313,7 +313,7 @@
     }
 
     /**
-     * Return the number of instances currently borrowed from this pool.
+     * Returns the number of instances currently borrowed from this pool.
      *
      * @return the number of instances currently borrowed from this pool
      */
@@ -345,7 +345,7 @@
     }
 
     /**
-     * Close this pool, and free any resources associated with it. Invokes
+     * Closes this pool, and frees any resources associated with it. Invokes
      * {@link #clear()} to destroy and remove instances in the pool.
      * <p>
      * Calling {@link #addObject} or {@link #borrowObject} after invoking this
@@ -382,7 +382,7 @@
     }
 
     /**
-     * Find the PooledSoftReference in allReferences that points to obj.
+     * Finds the PooledSoftReference in allReferences that points to obj.
      *
      * @param obj returning object
      * @return PooledSoftReference wrapping a soft reference to obj
@@ -399,7 +399,7 @@
     }
 
     /**
-     * Destroy a {@code PooledSoftReference} and remove it from the idle and all
+     * Destroys a {@code PooledSoftReference} and removes it from the idle and all
      * references pools.
      *
      * @param toDestroy PooledSoftReference to destroy