You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2018/05/27 16:17:16 UTC

commons-pool git commit: [POOL-338] GenericObjectPool constructor may throw an exception under OSGi.

Repository: commons-pool
Updated Branches:
  refs/heads/master ed9fd0b0d -> be3bcaff4


[POOL-338] GenericObjectPool constructor may throw an exception under
OSGi.

Project: http://git-wip-us.apache.org/repos/asf/commons-pool/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-pool/commit/be3bcaff
Tree: http://git-wip-us.apache.org/repos/asf/commons-pool/tree/be3bcaff
Diff: http://git-wip-us.apache.org/repos/asf/commons-pool/diff/be3bcaff

Branch: refs/heads/master
Commit: be3bcaff4f12f610d9eb74ef1da39cc004d21992
Parents: ed9fd0b
Author: Michael C <mi...@hotmail.com>
Authored: Sun May 27 10:17:14 2018 -0600
Committer: Gary Gregory <ga...@gmail.com>
Committed: Sun May 27 10:17:14 2018 -0600

----------------------------------------------------------------------
 .../pool2/impl/BaseGenericObjectPool.java       | 36 +++++++++-----------
 1 file changed, 17 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-pool/blob/be3bcaff/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
index cb6d5a8..c4837c0 100644
--- a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
+++ b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
@@ -617,28 +617,18 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject {
      *        {@link EvictionPolicy} interface.
      */
     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;
-            String epcnClassLoaderDesc;
-            // Getting epClass here and now best matches the caller's environment
-            final Class<?> epClass = EvictionPolicy.class;
-            final ClassLoader epClassLoader = epClass.getClassLoader();
             try {
-                epcnClassLoaderDesc = classLoader.toString();
-                clazz = Class.forName(evictionPolicyClassName, true, classLoader);
-            } catch (final ClassNotFoundException e) {
-                epcnClassLoaderDesc = epClassLoader.toString();
-                clazz = Class.forName(evictionPolicyClassName, true, epClassLoader);
-            }
-            final Object policy = clazz.getConstructor().newInstance();
-            if (epClass.isInstance(policy)) {
-                @SuppressWarnings("unchecked") // safe, because we just checked the class
-                final EvictionPolicy<T> evicPolicy = (EvictionPolicy<T>) policy;
-                this.evictionPolicy = evicPolicy;
-            } else {
-                throw new IllegalArgumentException("Class " + evictionPolicyClassName + " from class loader ["
-                        + epcnClassLoaderDesc + "] does not implement " + EVICTION_POLICY_TYPE_NAME);
+                setEvictionPolicy(evictionPolicyClassName, classLoader);
+            } catch (final ClassCastException | ClassNotFoundException e) {
+                setEvictionPolicy(evictionPolicyClassName, epClassLoader);
             }
+        } 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 "
@@ -647,6 +637,14 @@ public abstract class BaseGenericObjectPool<T> extends BaseObject {
         }
     }
 
+    @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