[ https://issues.apache.org/jira/browse/POOL-338?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16431213#comment-16431213 ]
Gary Gregory edited comment on POOL-338 at 4/9/18 10:24 PM: ------------------------------------------------------------ Can you try the following on top of out git master: {noformat} diff --git a/pom.xml b/pom.xml index f913e97..cc8f799 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,7 @@ </parent> <modelVersion>4.0.0</modelVersion> <artifactId>commons-pool2</artifactId> - <version>2.5.1-SNAPSHOT</version> + <version>2.6.0-SNAPSHOT</version> <name>Apache Commons Pool</name> <inceptionYear>2001</inceptionYear> 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 d9a4df7..af332ce 100644 --- a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java @@ -589,30 +589,46 @@ } /** - * 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 evictionPolicy + * the eviction policy for this pool. + * @since 2.6.0 + */ + public void setEvictionPolicy(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 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) { - final String EVICTION_POLICY_TYPE_NAME = EvictionPolicy.class.getName(); + public final void setEvictionPolicyClassName(final String evictionPolicyClassName, final ClassLoader classLoader) { + @SuppressWarnings("rawtypes") + final Class<EvictionPolicy> INTERFACE_TYPE = EvictionPolicy.class; + final String EVICTION_POLICY_TYPE_NAME = INTERFACE_TYPE.getName(); final String exMessage = "Unable to create " + EVICTION_POLICY_TYPE_NAME + " instance of type " + evictionPolicyClassName; try { Class<?> clazz; - final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); String epcnClassLoaderDesc; try { - epcnClassLoaderDesc = "Thread context class loader: " + classLoader; + epcnClassLoaderDesc = "Class loader: " + classLoader; clazz = Class.forName(evictionPolicyClassName, true, classLoader); } catch (final ClassNotFoundException e) { - epcnClassLoaderDesc = "Default class loader"; - clazz = Class.forName(evictionPolicyClassName); + epcnClassLoaderDesc = EVICTION_POLICY_TYPE_NAME + " class loader"; + clazz = Class.forName(evictionPolicyClassName, true, INTERFACE_TYPE.getClassLoader()); } final Object policy = clazz.getConstructor().newInstance(); if (policy instanceof EvictionPolicy<?>) { @@ -622,7 +638,7 @@ } else { throw new IllegalArgumentException("Class " + evictionPolicyClassName + " from class loader [" + epcnClassLoaderDesc + "] does not implement " + EVICTION_POLICY_TYPE_NAME - + " from class loader [" + EvictionPolicy.class.getClassLoader() + "]"); + + " from class loader [" + INTERFACE_TYPE.getClassLoader() + "]"); } } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { @@ -631,6 +647,22 @@ } /** + * 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 * the value for the Evictor. @@ -1317,4 +1349,5 @@ builder.append(swallowedExceptionListener); } + } diff --git a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java index 0abc55b..915e083 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java @@ -1073,6 +1073,9 @@ // expected } + genericObjectPool.setEvictionPolicy(new TestEvictionPolicy<String>()); + assertEquals(TestEvictionPolicy.class.getName(), genericObjectPool.getEvictionPolicyClassName()); + genericObjectPool.setEvictionPolicyClassName(TestEvictionPolicy.class.getName()); assertEquals(TestEvictionPolicy.class.getName(), genericObjectPool.getEvictionPolicyClassName()); {noformat} was (Author: garydgregory): Can you try the following on top of out git master: {noformat} diff --git a/pom.xml b/pom.xml index f913e97..cc8f799 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,7 @@ </parent> <modelVersion>4.0.0</modelVersion> <artifactId>commons-pool2</artifactId> - <version>2.5.1-SNAPSHOT</version> + <version>2.6.0-SNAPSHOT</version> <name>Apache Commons Pool</name> <inceptionYear>2001</inceptionYear> 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 d9a4df7..a19f41a 100644 --- a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java +++ b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java @@ -589,30 +589,44 @@ } /** - * 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 evictionPolicy + * the eviction policy for this pool. + * @since 2.6.0 + */ + public void setEvictionPolicy(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 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) { final String EVICTION_POLICY_TYPE_NAME = EvictionPolicy.class.getName(); final String exMessage = "Unable to create " + EVICTION_POLICY_TYPE_NAME + " instance of type " + evictionPolicyClassName; try { Class<?> clazz; - final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); String epcnClassLoaderDesc; try { - epcnClassLoaderDesc = "Thread context class loader: " + classLoader; + epcnClassLoaderDesc = "Class loader: " + classLoader; clazz = Class.forName(evictionPolicyClassName, true, classLoader); } catch (final ClassNotFoundException e) { - epcnClassLoaderDesc = "Default class loader"; - clazz = Class.forName(evictionPolicyClassName); + epcnClassLoaderDesc = EVICTION_POLICY_TYPE_NAME + " class loader"; + clazz = Class.forName(evictionPolicyClassName, true, EvictionPolicy.class.getClassLoader()); } final Object policy = clazz.getConstructor().newInstance(); if (policy instanceof EvictionPolicy<?>) { @@ -631,6 +645,22 @@ } /** + * 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 * the value for the Evictor. @@ -1317,4 +1347,5 @@ builder.append(swallowedExceptionListener); } + } diff --git a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java index 0abc55b..915e083 100644 --- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java +++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java @@ -1073,6 +1073,9 @@ // expected } + genericObjectPool.setEvictionPolicy(new TestEvictionPolicy<String>()); + assertEquals(TestEvictionPolicy.class.getName(), genericObjectPool.getEvictionPolicyClassName()); + genericObjectPool.setEvictionPolicyClassName(TestEvictionPolicy.class.getName()); assertEquals(TestEvictionPolicy.class.getName(), genericObjectPool.getEvictionPolicyClassName()); {noformat} > GenericObjectPool constructor throws an exception > ------------------------------------------------- > > Key: POOL-338 > URL: https://issues.apache.org/jira/browse/POOL-338 > Project: Commons Pool > Issue Type: Bug > Affects Versions: 2.4.2, 2.4.3, 2.5.0 > Environment: Java 8, Liferay DXP (an OSGi environment). > Reporter: Michael C > Priority: Major > > Version 2.4.3 GenericObjectPool constructor throws this exception: > {{java.lang.IllegalArgumentException: > [org.apache.commons.pool2.impl.DefaultEvictionPolicy] does not implement > EvictionPolicy}} > {{ at > org.apache.commons.pool2.impl.BaseGenericObjectPool.setEvictionPolicyClassName(BaseGenericObjectPool.java:618)}} > {{ at > org.apache.commons.pool2.impl.GenericObjectPool.setConfig(GenericObjectPool.java:318)}} > {{ at > org.apache.commons.pool2.impl.GenericObjectPool.<init>(GenericObjectPool.java:115)}} > {{ at > org.apache.commons.pool2.impl.GenericObjectPool.<init>(GenericObjectPool.java:88)}} > > Version 2.5.0 throws the same exception. Version 2.4.2 or older's > setEvictionPolicyClassName method fail silently for the same reason. This > line in BaseGenericObjectPool evaluates to false for all versions: > {{ if (policy instanceof EvictionPolicy<?>) {}} > -- This message was sent by Atlassian JIRA (v7.6.3#76005)