ensure we dont share the same meta cache key for two concurrent classes in cdi helper
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/jcs/trunk@1807337 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelper.java b/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelper.java
index ed0855b..4ec5731 100644
--- a/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelper.java
+++ b/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelper.java
@@ -86,7 +86,8 @@
public MethodMeta findMeta(final InvocationContext ic)
{
final Method mtd = ic.getMethod();
- final MethodKey key = new MethodKey(mtd);
+ final Class<?> refType = findKeyType(ic.getTarget());
+ final MethodKey key = new MethodKey(refType, mtd);
MethodMeta methodMeta = methods.get(key);
if (methodMeta == null)
{
@@ -103,6 +104,15 @@
return methodMeta;
}
+ private Class<?> findKeyType(final Object target)
+ {
+ if (null == target)
+ {
+ return null;
+ }
+ return target.getClass();
+ }
+
// it is unlikely we have all annotations but for now we have a single meta model
private MethodMeta createMeta(final InvocationContext ic)
{
@@ -432,13 +442,15 @@
private static final class MethodKey
{
+ private final Class<?> base;
private final Method delegate;
private final int hash;
- private MethodKey(final Method delegate)
+ private MethodKey(final Class<?> base, final Method delegate)
{
+ this.base = base; // we need a class to ensure inheritance don't fall in the same key
this.delegate = delegate;
- this.hash = delegate.hashCode();
+ this.hash = 31 * delegate.hashCode() + (base == null ? 0 : base.hashCode());
}
@Override
@@ -453,7 +465,7 @@
return false;
}
final MethodKey classKey = MethodKey.class.cast(o);
- return delegate.equals(classKey.delegate);
+ return delegate.equals(classKey.delegate) && ((base == null && classKey.base == null) || (base != null && base.equals(classKey.base)));
}
@Override
diff --git a/commons-jcs-jcache/src/test/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelperTest.java b/commons-jcs-jcache/src/test/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelperTest.java
index 7b800a3..bf5a014 100644
--- a/commons-jcs-jcache/src/test/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelperTest.java
+++ b/commons-jcs-jcache/src/test/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelperTest.java
@@ -37,8 +37,10 @@
@Test
public void proxyCacheDefaults()
{
- final MyParent child = MyParent.class.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
- new Class<?>[]{MyChild.class}, new InvocationHandler()
+ final CDIJCacheHelper helper = new CDIJCacheHelper();
+
+ final MyParent child1 = MyParent.class.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
+ new Class<?>[]{MyChild1.class}, new InvocationHandler()
{
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable
@@ -46,12 +48,29 @@
return null;
}
}));
- final CDIJCacheHelper.MethodMeta meta = new CDIJCacheHelper().findMeta(new InvocationContext()
+ final CDIJCacheHelper.MethodMeta meta1 = helper.findMeta(newContext(child1));
+ assertEquals("child", meta1.getCacheResultCacheName());
+
+ final MyParent child2 = MyParent.class.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
+ new Class<?>[]{MyChild2.class}, new InvocationHandler()
+ {
+ @Override
+ public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable
+ {
+ return null;
+ }
+ }));
+ final CDIJCacheHelper.MethodMeta meta2 = helper.findMeta(newContext(child2));
+ assertEquals("child2", meta2.getCacheResultCacheName());
+ }
+
+ private InvocationContext newContext(final MyParent child1) {
+ return new InvocationContext()
{
@Override
public Object getTarget()
{
- return child;
+ return child1;
}
@Override
@@ -100,8 +119,7 @@
{
return null;
}
- });
- assertEquals("child", meta.getCacheResultCacheName());
+ };
}
public interface MyParent
@@ -111,7 +129,12 @@
}
@CacheDefaults(cacheName = "child")
- public interface MyChild extends MyParent
+ public interface MyChild1 extends MyParent
+ {
+ }
+
+ @CacheDefaults(cacheName = "child2")
+ public interface MyChild2 extends MyParent
{
}
}