You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by rm...@apache.org on 2015/02/26 10:20:15 UTC
svn commit: r1662397 -
/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/
Author: rmannibucau
Date: Thu Feb 26 09:20:14 2015
New Revision: 1662397
URL: http://svn.apache.org/r1662397
Log:
ignoring CacheValue for key parameters
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/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationContextImpl.java
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyInvocationContextImpl.java
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CachePutInterceptor.java
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveAllInterceptor.java
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveInterceptor.java
commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResultInterceptor.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=1662397&r1=1662396&r2=1662397&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 Thu Feb 26 09:20:14 2015
@@ -20,14 +20,20 @@ package org.apache.commons.jcs.jcache.cd
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Logger;
import javax.annotation.PreDestroy;
import javax.cache.annotation.CacheDefaults;
+import javax.cache.annotation.CacheInvocationParameter;
+import javax.cache.annotation.CacheKey;
import javax.cache.annotation.CacheKeyGenerator;
import javax.cache.annotation.CacheResolverFactory;
+import javax.cache.annotation.CacheValue;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
@@ -45,6 +51,7 @@ public class CDIJCacheHelper
private volatile CacheResolverFactoryImpl defaultCacheResolverFactory = null; // lazy to not create any cache if not needed
private final CacheKeyGeneratorImpl defaultCacheKeyGenerator = new CacheKeyGeneratorImpl();
private final ConcurrentMap<Method, String> generatedNames = new ConcurrentHashMap<Method, String>();
+ private final ConcurrentMap<Method, Integer[]> parameterIndexes = new ConcurrentHashMap<Method, Integer[]>();
@Inject
private BeanManager beanManager;
@@ -215,4 +222,52 @@ public class CDIJCacheHelper
}
return defaultCacheResolverFactory;
}
+
+ public Integer[] keyParameterIndexes(final Method method)
+ {
+ Integer[] val = parameterIndexes.get(method);
+ if (val == null)
+ {
+ final List<Integer> keys = new LinkedList<Integer>();
+ final Annotation[][] parameterAnnotations = method.getParameterAnnotations();
+
+ // first check if keys are specified explicitely
+ for (int i = 0; i < method.getParameterTypes().length; i++)
+ {
+ final Annotation[] annotations = parameterAnnotations[i];
+ for (final Annotation a : annotations)
+ {
+ if (a.annotationType().equals(CacheKey.class))
+ {
+ keys.add(i);
+ break;
+ }
+ }
+ }
+
+ // if not then use all parameters but value ones
+ if (keys.isEmpty())
+ {
+ for (int i = 0; i < method.getParameterTypes().length; i++)
+ {
+ final Annotation[] annotations = parameterAnnotations[i];
+ boolean value = false;
+ for (final Annotation a : annotations)
+ {
+ if (a.annotationType().equals(CacheValue.class))
+ {
+ value = true;
+ break;
+ }
+ }
+ if (!value) {
+ keys.add(i);
+ }
+ }
+ }
+ val = keys.toArray(new Integer[keys.size()]);
+ parameterIndexes.putIfAbsent(method, val);
+ }
+ return val;
+ }
}
Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationContextImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationContextImpl.java?rev=1662397&r1=1662396&r2=1662397&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationContextImpl.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheInvocationContextImpl.java Thu Feb 26 09:20:14 2015
@@ -18,11 +18,11 @@
*/
package org.apache.commons.jcs.jcache.cdi;
+import java.lang.annotation.Annotation;
+import java.util.HashSet;
import javax.cache.annotation.CacheInvocationContext;
import javax.cache.annotation.CacheInvocationParameter;
import javax.interceptor.InvocationContext;
-import java.lang.annotation.Annotation;
-import java.util.HashSet;
import static java.util.Arrays.asList;
@@ -46,14 +46,7 @@ public class CacheInvocationContextImpl<
{
if (parameters == null)
{
- final Object[] args = delegate.getParameters();
- final Class<?>[] parameterTypes = getMethod().getParameterTypes();
- final Annotation[][] parameterAnnotations = getMethod().getParameterAnnotations();
- parameters = new CacheInvocationParameter[args.length];
- for (int i = 0; i < args.length; i++)
- {
- parameters[i] = new CacheInvocationParameterImpl(parameterTypes[i], args[i], new HashSet<Annotation>(asList(parameterAnnotations[i])), i);
- }
+ parameters = doGetAllParameters(null);
}
return parameters;
}
@@ -67,4 +60,34 @@ public class CacheInvocationContextImpl<
}
throw new IllegalArgumentException(cls.getName());
}
+
+ protected CacheInvocationParameter[] doGetAllParameters(final Integer[] indexes)
+ {
+ final Object[] args = delegate.getParameters();
+ final Class<?>[] parameterTypes = getMethod().getParameterTypes();
+ final Annotation[][] parameterAnnotations = getMethod().getParameterAnnotations();
+
+ final CacheInvocationParameter[] parametersAsArray = new CacheInvocationParameter[indexes == null ? args.length : indexes.length];
+ if (indexes == null)
+ {
+ for (int i = 0; i < args.length; i++)
+ {
+ parametersAsArray[i] = newCacheInvocationParameterImpl(parameterTypes[i], args[i], parameterAnnotations[i], i);
+ }
+ }
+ else
+ {
+ for (int idx = 0; idx < indexes.length; idx++)
+ {
+ final int i = indexes[idx];
+ parametersAsArray[i] = newCacheInvocationParameterImpl(parameterTypes[i], args[i], parameterAnnotations[i], i);
+ }
+ }
+ return parametersAsArray;
+ }
+
+ private CacheInvocationParameterImpl newCacheInvocationParameterImpl(final Class<?> type, final Object arg,
+ final Annotation[] annotations, final int i) {
+ return new CacheInvocationParameterImpl(type, arg, new HashSet<Annotation>(asList(annotations)), i);
+ }
}
Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyInvocationContextImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyInvocationContextImpl.java?rev=1662397&r1=1662396&r2=1662397&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyInvocationContextImpl.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheKeyInvocationContextImpl.java Thu Feb 26 09:20:14 2015
@@ -29,12 +29,15 @@ import java.util.LinkedList;
public class CacheKeyInvocationContextImpl<A extends Annotation> extends CacheInvocationContextImpl<A> implements CacheKeyInvocationContext<A>
{
+ private final Integer[] keyIndexes;
private CacheInvocationParameter[] keyParams = null;
private CacheInvocationParameter valueParam = null;
- public CacheKeyInvocationContextImpl(final InvocationContext delegate, final A annotation, final String name)
+ public CacheKeyInvocationContextImpl(final InvocationContext delegate, final A annotation, final String name,
+ final Integer[] keyIndexes)
{
super(delegate, annotation, name);
+ this.keyIndexes = keyIndexes;
}
@Override
@@ -55,7 +58,7 @@ public class CacheKeyInvocationContextIm
}
if (keys.isEmpty())
{
- keyParams = getAllParameters();
+ keyParams = doGetAllParameters(keyIndexes);
}
else
{
Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CachePutInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CachePutInterceptor.java?rev=1662397&r1=1662396&r2=1662397&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CachePutInterceptor.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CachePutInterceptor.java Thu Feb 26 09:20:14 2015
@@ -19,6 +19,7 @@
package org.apache.commons.jcs.jcache.cdi;
import java.io.Serializable;
+import java.lang.reflect.Method;
import javax.annotation.Priority;
import javax.cache.Cache;
import javax.cache.annotation.CacheDefaults;
@@ -43,11 +44,13 @@ public class CachePutInterceptor impleme
{
final CacheDefaults defaults = helper.findDefaults(ic);
- final CachePut cachePut = ic.getMethod().getAnnotation(CachePut.class);
- final String cacheName = helper.defaultName(ic.getMethod(), defaults, cachePut.cacheName());
+ final Method method = ic.getMethod();
+ final CachePut cachePut = method.getAnnotation(CachePut.class);
+ final String cacheName = helper.defaultName(method, defaults, cachePut.cacheName());
final boolean afterInvocation = cachePut.afterInvocation();
- final CacheKeyInvocationContext<CachePut> context = new CacheKeyInvocationContextImpl<CachePut>(ic, cachePut, cacheName);
+ final CacheKeyInvocationContext<CachePut> context = new CacheKeyInvocationContextImpl<CachePut>(
+ ic, cachePut, cacheName, helper.keyParameterIndexes(method));
if (!afterInvocation)
{
doCache(context, defaults, cachePut);
Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveAllInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveAllInterceptor.java?rev=1662397&r1=1662396&r2=1662397&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveAllInterceptor.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveAllInterceptor.java Thu Feb 26 09:20:14 2015
@@ -19,6 +19,7 @@
package org.apache.commons.jcs.jcache.cdi;
import java.io.Serializable;
+import java.lang.reflect.Method;
import javax.annotation.Priority;
import javax.cache.Cache;
import javax.cache.annotation.CacheDefaults;
@@ -42,11 +43,13 @@ public class CacheRemoveAllInterceptor i
{
final CacheDefaults defaults = helper.findDefaults(ic);
- final CacheRemoveAll cacheRemoveAll = ic.getMethod().getAnnotation(CacheRemoveAll.class);
- final String cacheName = helper.defaultName(ic.getMethod(), defaults, cacheRemoveAll.cacheName());
+ final Method method = ic.getMethod();
+ final CacheRemoveAll cacheRemoveAll = method.getAnnotation(CacheRemoveAll.class);
+ final String cacheName = helper.defaultName(method, defaults, cacheRemoveAll.cacheName());
final boolean afterInvocation = cacheRemoveAll.afterInvocation();
- final CacheKeyInvocationContext<CacheRemoveAll> context = new CacheKeyInvocationContextImpl<CacheRemoveAll>(ic, cacheRemoveAll, cacheName);
+ final CacheKeyInvocationContext<CacheRemoveAll> context = new CacheKeyInvocationContextImpl<CacheRemoveAll>(
+ ic, cacheRemoveAll, cacheName, helper.keyParameterIndexes(method));
if (!afterInvocation)
{
removeAll(context, defaults, cacheRemoveAll);
Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveInterceptor.java?rev=1662397&r1=1662396&r2=1662397&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveInterceptor.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheRemoveInterceptor.java Thu Feb 26 09:20:14 2015
@@ -19,6 +19,7 @@
package org.apache.commons.jcs.jcache.cdi;
import java.io.Serializable;
+import java.lang.reflect.Method;
import javax.annotation.Priority;
import javax.cache.Cache;
import javax.cache.annotation.CacheDefaults;
@@ -43,11 +44,13 @@ public class CacheRemoveInterceptor impl
{
final CacheDefaults defaults = helper.findDefaults(ic);
- final CacheRemove cacheRemove = ic.getMethod().getAnnotation(CacheRemove.class);
- final String cacheName = helper.defaultName(ic.getMethod(), defaults, cacheRemove.cacheName());
+ final Method method = ic.getMethod();
+ final CacheRemove cacheRemove = method.getAnnotation(CacheRemove.class);
+ final String cacheName = helper.defaultName(method, defaults, cacheRemove.cacheName());
final boolean afterInvocation = cacheRemove.afterInvocation();
- final CacheKeyInvocationContext<CacheRemove> context = new CacheKeyInvocationContextImpl<CacheRemove>(ic, cacheRemove, cacheName);
+ final CacheKeyInvocationContext<CacheRemove> context =
+ new CacheKeyInvocationContextImpl<CacheRemove>(ic, cacheRemove, cacheName, helper.keyParameterIndexes(method));
if (!afterInvocation)
{
Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResultInterceptor.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResultInterceptor.java?rev=1662397&r1=1662396&r2=1662397&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResultInterceptor.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/cdi/CacheResultInterceptor.java Thu Feb 26 09:20:14 2015
@@ -19,6 +19,7 @@
package org.apache.commons.jcs.jcache.cdi;
import java.io.Serializable;
+import java.lang.reflect.Method;
import javax.annotation.Priority;
import javax.cache.Cache;
import javax.cache.annotation.CacheDefaults;
@@ -45,10 +46,12 @@ public class CacheResultInterceptor impl
{
final CacheDefaults defaults = helper.findDefaults(ic);
- final CacheResult cacheResult = ic.getMethod().getAnnotation(CacheResult.class);
- final String cacheName = helper.defaultName(ic.getMethod(), defaults, cacheResult.cacheName());
+ final Method method = ic.getMethod();
+ final CacheResult cacheResult = method.getAnnotation(CacheResult.class);
+ final String cacheName = helper.defaultName(method, defaults, cacheResult.cacheName());
- final CacheKeyInvocationContext<CacheResult> context = new CacheKeyInvocationContextImpl<CacheResult>(ic, cacheResult, cacheName);
+ final CacheKeyInvocationContext<CacheResult> context = new CacheKeyInvocationContextImpl<CacheResult>(
+ ic, cacheResult, cacheName, helper.keyParameterIndexes(method));
final CacheResolverFactory cacheResolverFactory = helper.cacheResolverFactoryFor(defaults, cacheResult.cacheResolverFactory());
final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context);