Author: rmannibucau
Date: Tue Sep  5 10:52:42 2017
New Revision: 1807337

URL: http://svn.apache.org/viewvc?rev=1807337&view=rev
Log:
ensure we dont share the same meta cache key for two concurrent classes in cdi 
helper

Modified:
    
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelper.java
    
commons/proper/jcs/trunk/commons-jcs-jcache/src/test/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelperTest.java

Modified: 
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelper.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelper.java?rev=1807337&r1=1807336&r2=1807337&view=diff
==============================================================================
--- 
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelper.java
 (original)
+++ 
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelper.java
 Tue Sep  5 10:52:42 2017
@@ -86,7 +86,8 @@ public class CDIJCacheHelper
     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 @@ public class CDIJCacheHelper
         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 @@ public class CDIJCacheHelper
 
     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 @@ public class CDIJCacheHelper
                 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

Modified: 
commons/proper/jcs/trunk/commons-jcs-jcache/src/test/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelperTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/test/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelperTest.java?rev=1807337&r1=1807336&r2=1807337&view=diff
==============================================================================
--- 
commons/proper/jcs/trunk/commons-jcs-jcache/src/test/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelperTest.java
 (original)
+++ 
commons/proper/jcs/trunk/commons-jcs-jcache/src/test/java/org/apache/commons/jcs/jcache/cdi/CDIJCacheHelperTest.java
 Tue Sep  5 10:52:42 2017
@@ -37,8 +37,22 @@ public class CDIJCacheHelperTest
     @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
+                    {
+                        return null;
+                    }
+                }));
+        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
@@ -46,12 +60,17 @@ public class CDIJCacheHelperTest
                         return null;
                     }
                 }));
-        final CDIJCacheHelper.MethodMeta meta = new 
CDIJCacheHelper().findMeta(new InvocationContext()
+        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 @@ public class CDIJCacheHelperTest
             {
                 return null;
             }
-        });
-        assertEquals("child", meta.getCacheResultCacheName());
+        };
     }
 
     public interface MyParent
@@ -111,7 +129,12 @@ public class CDIJCacheHelperTest
     }
 
     @CacheDefaults(cacheName = "child")
-    public interface MyChild extends MyParent
+    public interface MyChild1 extends MyParent
+    {
+    }
+
+    @CacheDefaults(cacheName = "child2")
+    public interface MyChild2 extends MyParent
     {
     }
 }


Reply via email to