ExceptionTypeFilter and InstanceFilter live in util package itself now
Issue: SPR-9616
This commit is contained in:
@@ -25,7 +25,7 @@ import org.springframework.cache.interceptor.CacheOperationInvoker;
|
||||
import org.springframework.cache.interceptor.CacheResolver;
|
||||
import org.springframework.cache.jcache.model.CacheResultOperation;
|
||||
import org.springframework.util.SerializationUtils;
|
||||
import org.springframework.util.filter.ExceptionTypeFilter;
|
||||
import org.springframework.util.ExceptionTypeFilter;
|
||||
|
||||
/**
|
||||
* Intercept methods annotated with {@link CacheResult}.
|
||||
@@ -65,10 +65,10 @@ public class CacheResultInterceptor extends AbstractKeyCacheInterceptor<CacheRes
|
||||
}
|
||||
return invocationResult;
|
||||
}
|
||||
catch (CacheOperationInvoker.ThrowableWrapper t) {
|
||||
Throwable original = t.getOriginal();
|
||||
catch (CacheOperationInvoker.ThrowableWrapper ex) {
|
||||
Throwable original = ex.getOriginal();
|
||||
cacheException(exceptionCache, operation.getExceptionTypeFilter(), cacheKey, original);
|
||||
throw t;
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,13 +86,12 @@ public class CacheResultInterceptor extends AbstractKeyCacheInterceptor<CacheRes
|
||||
}
|
||||
|
||||
|
||||
protected void cacheException(Cache exceptionCache, ExceptionTypeFilter filter,
|
||||
Object cacheKey, Throwable t) {
|
||||
protected void cacheException(Cache exceptionCache, ExceptionTypeFilter filter, Object cacheKey, Throwable ex) {
|
||||
if (exceptionCache == null) {
|
||||
return;
|
||||
}
|
||||
if (filter.match(t.getClass())) {
|
||||
exceptionCache.put(cacheKey, t);
|
||||
if (filter.match(ex.getClass())) {
|
||||
exceptionCache.put(cacheKey, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,8 +99,7 @@ public class CacheResultInterceptor extends AbstractKeyCacheInterceptor<CacheRes
|
||||
private Cache resolveExceptionCache(CacheOperationInvocationContext<CacheResultOperation> context) {
|
||||
CacheResolver exceptionCacheResolver = context.getOperation().getExceptionCacheResolver();
|
||||
if (exceptionCacheResolver != null) {
|
||||
return extractFrom(context.getOperation()
|
||||
.getExceptionCacheResolver().resolveCaches(context));
|
||||
return extractFrom(context.getOperation().getExceptionCacheResolver().resolveCaches(context));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -121,8 +119,9 @@ public class CacheResultInterceptor extends AbstractKeyCacheInterceptor<CacheRes
|
||||
* {@code methodName} arguments, followed by stack trace elements of the specified
|
||||
* {@code exception} after the common ancestor.
|
||||
*/
|
||||
private static CacheOperationInvoker.ThrowableWrapper rewriteCallStack(Throwable exception,
|
||||
String className, String methodName) {
|
||||
private static CacheOperationInvoker.ThrowableWrapper rewriteCallStack(
|
||||
Throwable exception, String className, String methodName) {
|
||||
|
||||
Throwable clone = cloneException(exception);
|
||||
if (clone == null) {
|
||||
return new CacheOperationInvoker.ThrowableWrapper(exception);
|
||||
@@ -149,8 +148,8 @@ public class CacheResultInterceptor extends AbstractKeyCacheInterceptor<CacheRes
|
||||
try {
|
||||
return (T) SerializationUtils.deserialize(SerializationUtils.serialize(exception));
|
||||
}
|
||||
catch (Exception e) {
|
||||
return null; // exception parameter cannot be cloned
|
||||
catch (Exception ex) {
|
||||
return null; // exception parameter cannot be cloned
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.cache.jcache.model;
|
||||
|
||||
import static java.util.Arrays.*;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
@@ -25,7 +23,6 @@ import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.cache.annotation.CacheInvocationParameter;
|
||||
import javax.cache.annotation.CacheKey;
|
||||
import javax.cache.annotation.CacheMethodDetails;
|
||||
@@ -33,7 +30,9 @@ import javax.cache.annotation.CacheValue;
|
||||
|
||||
import org.springframework.cache.interceptor.CacheResolver;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.filter.ExceptionTypeFilter;
|
||||
import org.springframework.util.ExceptionTypeFilter;
|
||||
|
||||
import static java.util.Arrays.*;
|
||||
|
||||
/**
|
||||
* A base {@link JCacheOperation} implementation.
|
||||
@@ -49,6 +48,7 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache
|
||||
|
||||
protected final List<CacheParameterDetail> allParameterDetails;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
* @param methodDetails the {@link CacheMethodDetails} related to the cached method
|
||||
@@ -62,30 +62,32 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache
|
||||
this.allParameterDetails = initializeAllParameterDetails(methodDetails.getMethod());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the {@link ExceptionTypeFilter} to use to filter exceptions thrown while
|
||||
* invoking the method.
|
||||
*/
|
||||
public abstract ExceptionTypeFilter getExceptionTypeFilter();
|
||||
|
||||
|
||||
@Override
|
||||
public Method getMethod() {
|
||||
return methodDetails.getMethod();
|
||||
return this.methodDetails.getMethod();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Annotation> getAnnotations() {
|
||||
return methodDetails.getAnnotations();
|
||||
return this.methodDetails.getAnnotations();
|
||||
}
|
||||
|
||||
@Override
|
||||
public A getCacheAnnotation() {
|
||||
return methodDetails.getCacheAnnotation();
|
||||
return this.methodDetails.getCacheAnnotation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheName() {
|
||||
return methodDetails.getCacheName();
|
||||
return this.methodDetails.getCacheName();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -95,37 +97,28 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache
|
||||
|
||||
@Override
|
||||
public CacheResolver getCacheResolver() {
|
||||
return cacheResolver;
|
||||
return this.cacheResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CacheInvocationParameter[] getAllParameters(Object... values) {
|
||||
if (allParameterDetails.size() != values.length) {
|
||||
throw new IllegalStateException("Values mismatch, operation has "
|
||||
+ allParameterDetails.size() + " parameter(s) but got " + values.length + " value(s)");
|
||||
if (this.allParameterDetails.size() != values.length) {
|
||||
throw new IllegalStateException("Values mismatch, operation has " +
|
||||
this.allParameterDetails.size() + " parameter(s) but got " + values.length + " value(s)");
|
||||
}
|
||||
List<CacheInvocationParameter> result = new ArrayList<CacheInvocationParameter>();
|
||||
for (int i = 0; i < allParameterDetails.size(); i++) {
|
||||
result.add(allParameterDetails.get(i).toCacheInvocationParameter(values[i]));
|
||||
for (int i = 0; i < this.allParameterDetails.size(); i++) {
|
||||
result.add(this.allParameterDetails.get(i).toCacheInvocationParameter(values[i]));
|
||||
}
|
||||
return result.toArray(new CacheInvocationParameter[result.size()]);
|
||||
}
|
||||
|
||||
protected ExceptionTypeFilter createExceptionTypeFiler(Class<? extends Throwable>[] includes,
|
||||
Class<? extends Throwable>[] excludes) {
|
||||
protected ExceptionTypeFilter createExceptionTypeFilter(
|
||||
Class<? extends Throwable>[] includes, Class<? extends Throwable>[] excludes) {
|
||||
|
||||
return new ExceptionTypeFilter(asList(includes), asList(excludes), true);
|
||||
}
|
||||
|
||||
|
||||
private static List<CacheParameterDetail> initializeAllParameterDetails(Method method) {
|
||||
List<CacheParameterDetail> result = new ArrayList<CacheParameterDetail>();
|
||||
for (int i = 0; i < method.getParameterTypes().length; i++) {
|
||||
CacheParameterDetail detail = new CacheParameterDetail(method, i);
|
||||
result.add(detail);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getOperationDescription().append("]").toString();
|
||||
@@ -144,6 +137,16 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache
|
||||
}
|
||||
|
||||
|
||||
private static List<CacheParameterDetail> initializeAllParameterDetails(Method method) {
|
||||
List<CacheParameterDetail> result = new ArrayList<CacheParameterDetail>();
|
||||
for (int i = 0; i < method.getParameterTypes().length; i++) {
|
||||
CacheParameterDetail detail = new CacheParameterDetail(method, i);
|
||||
result.add(detail);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
protected static class CacheParameterDetail {
|
||||
|
||||
private final Class<?> rawType;
|
||||
@@ -156,7 +159,7 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache
|
||||
|
||||
private final boolean isValue;
|
||||
|
||||
private CacheParameterDetail(Method m, int parameterPosition) {
|
||||
public CacheParameterDetail(Method m, int parameterPosition) {
|
||||
this.rawType = m.getParameterTypes()[parameterPosition];
|
||||
this.annotations = new LinkedHashSet<Annotation>();
|
||||
boolean foundKeyAnnotation = false;
|
||||
@@ -176,15 +179,15 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache
|
||||
}
|
||||
|
||||
public int getParameterPosition() {
|
||||
return parameterPosition;
|
||||
return this.parameterPosition;
|
||||
}
|
||||
|
||||
protected boolean isKey() {
|
||||
return isKey;
|
||||
return this.isKey;
|
||||
}
|
||||
|
||||
protected boolean isValue() {
|
||||
return isValue;
|
||||
return this.isValue;
|
||||
}
|
||||
|
||||
public CacheInvocationParameter toCacheInvocationParameter(Object value) {
|
||||
@@ -192,35 +195,36 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected static class CacheInvocationParameterImpl implements CacheInvocationParameter {
|
||||
|
||||
private final CacheParameterDetail detail;
|
||||
|
||||
private final Object value;
|
||||
|
||||
private CacheInvocationParameterImpl(CacheParameterDetail detail, Object value) {
|
||||
public CacheInvocationParameterImpl(CacheParameterDetail detail, Object value) {
|
||||
this.detail = detail;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getRawType() {
|
||||
return detail.rawType;
|
||||
return this.detail.rawType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValue() {
|
||||
return value;
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Annotation> getAnnotations() {
|
||||
return detail.annotations;
|
||||
return this.detail.annotations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getParameterPosition() {
|
||||
return detail.parameterPosition;
|
||||
return this.detail.parameterPosition;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ import javax.cache.annotation.CachePut;
|
||||
|
||||
import org.springframework.cache.interceptor.CacheResolver;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.util.filter.ExceptionTypeFilter;
|
||||
import org.springframework.util.ExceptionTypeFilter;
|
||||
|
||||
/**
|
||||
* The {@link JCacheOperation} implementation for a {@link CachePut} operation.
|
||||
@@ -40,11 +40,13 @@ public class CachePutOperation extends BaseKeyCacheOperation<CachePut> {
|
||||
|
||||
private final CacheParameterDetail valueParameterDetail;
|
||||
|
||||
public CachePutOperation(CacheMethodDetails<CachePut> methodDetails,
|
||||
CacheResolver cacheResolver, KeyGenerator keyGenerator) {
|
||||
|
||||
public CachePutOperation(
|
||||
CacheMethodDetails<CachePut> methodDetails, CacheResolver cacheResolver, KeyGenerator keyGenerator) {
|
||||
|
||||
super(methodDetails, cacheResolver, keyGenerator);
|
||||
CachePut ann = methodDetails.getCacheAnnotation();
|
||||
this.exceptionTypeFilter = createExceptionTypeFiler(ann.cacheFor(), ann.noCacheFor());
|
||||
this.exceptionTypeFilter = createExceptionTypeFilter(ann.cacheFor(), ann.noCacheFor());
|
||||
this.valueParameterDetail = initializeValueParameterDetail(methodDetails.getMethod(), allParameterDetails);
|
||||
if (valueParameterDetail == null) {
|
||||
throw new IllegalArgumentException("No parameter annotated with @CacheValue was found for " +
|
||||
@@ -52,9 +54,10 @@ public class CachePutOperation extends BaseKeyCacheOperation<CachePut> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ExceptionTypeFilter getExceptionTypeFilter() {
|
||||
return exceptionTypeFilter;
|
||||
return this.exceptionTypeFilter;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,17 +77,18 @@ public class CachePutOperation extends BaseKeyCacheOperation<CachePut> {
|
||||
* @return the {@link CacheInvocationParameter} instance for the value parameter
|
||||
*/
|
||||
public CacheInvocationParameter getValueParameter(Object... values) {
|
||||
int parameterPosition = valueParameterDetail.getParameterPosition();
|
||||
int parameterPosition = this.valueParameterDetail.getParameterPosition();
|
||||
if (parameterPosition >= values.length) {
|
||||
throw new IllegalStateException("Values mismatch, value parameter at position "
|
||||
+ parameterPosition + " cannot be matched against " + values.length + " value(s)");
|
||||
throw new IllegalStateException("Values mismatch, value parameter at position " +
|
||||
parameterPosition + " cannot be matched against " + values.length + " value(s)");
|
||||
}
|
||||
return valueParameterDetail.toCacheInvocationParameter(values[parameterPosition]);
|
||||
return this.valueParameterDetail.toCacheInvocationParameter(values[parameterPosition]);
|
||||
}
|
||||
|
||||
|
||||
private static CacheParameterDetail initializeValueParameterDetail(Method method,
|
||||
List<CacheParameterDetail> allParameters) {
|
||||
private static CacheParameterDetail initializeValueParameterDetail(
|
||||
Method method, List<CacheParameterDetail> allParameters) {
|
||||
|
||||
CacheParameterDetail result = null;
|
||||
for (CacheParameterDetail parameter : allParameters) {
|
||||
if (parameter.isValue()) {
|
||||
|
||||
@@ -20,7 +20,7 @@ import javax.cache.annotation.CacheMethodDetails;
|
||||
import javax.cache.annotation.CacheRemoveAll;
|
||||
|
||||
import org.springframework.cache.interceptor.CacheResolver;
|
||||
import org.springframework.util.filter.ExceptionTypeFilter;
|
||||
import org.springframework.util.ExceptionTypeFilter;
|
||||
|
||||
/**
|
||||
* The {@link JCacheOperation} implementation for a {@link CacheRemoveAll} operation.
|
||||
@@ -33,15 +33,17 @@ public class CacheRemoveAllOperation extends BaseCacheOperation<CacheRemoveAll>
|
||||
|
||||
private final ExceptionTypeFilter exceptionTypeFilter;
|
||||
|
||||
|
||||
public CacheRemoveAllOperation(CacheMethodDetails<CacheRemoveAll> methodDetails, CacheResolver cacheResolver) {
|
||||
super(methodDetails, cacheResolver);
|
||||
CacheRemoveAll ann = methodDetails.getCacheAnnotation();
|
||||
this.exceptionTypeFilter = createExceptionTypeFiler(ann.evictFor(), ann.noEvictFor());
|
||||
this.exceptionTypeFilter = createExceptionTypeFilter(ann.evictFor(), ann.noEvictFor());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ExceptionTypeFilter getExceptionTypeFilter() {
|
||||
return exceptionTypeFilter;
|
||||
return this.exceptionTypeFilter;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,7 +21,7 @@ import javax.cache.annotation.CacheRemove;
|
||||
|
||||
import org.springframework.cache.interceptor.CacheResolver;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.util.filter.ExceptionTypeFilter;
|
||||
import org.springframework.util.ExceptionTypeFilter;
|
||||
|
||||
/**
|
||||
* The {@link JCacheOperation} implementation for a {@link CacheRemove} operation.
|
||||
@@ -34,16 +34,19 @@ public class CacheRemoveOperation extends BaseKeyCacheOperation<CacheRemove> {
|
||||
|
||||
private final ExceptionTypeFilter exceptionTypeFilter;
|
||||
|
||||
public CacheRemoveOperation(CacheMethodDetails<CacheRemove> methodDetails,
|
||||
CacheResolver cacheResolver, KeyGenerator keyGenerator) {
|
||||
|
||||
public CacheRemoveOperation(
|
||||
CacheMethodDetails<CacheRemove> methodDetails, CacheResolver cacheResolver, KeyGenerator keyGenerator) {
|
||||
|
||||
super(methodDetails, cacheResolver, keyGenerator);
|
||||
CacheRemove ann = methodDetails.getCacheAnnotation();
|
||||
this.exceptionTypeFilter = createExceptionTypeFiler(ann.evictFor(), ann.noEvictFor());
|
||||
this.exceptionTypeFilter = createExceptionTypeFilter(ann.evictFor(), ann.noEvictFor());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ExceptionTypeFilter getExceptionTypeFilter() {
|
||||
return exceptionTypeFilter;
|
||||
return this.exceptionTypeFilter;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,7 +22,7 @@ import javax.cache.annotation.CacheResult;
|
||||
import org.springframework.cache.interceptor.CacheResolver;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.filter.ExceptionTypeFilter;
|
||||
import org.springframework.util.ExceptionTypeFilter;
|
||||
|
||||
/**
|
||||
* The {@link JCacheOperation} implementation for a {@link CacheResult} operation.
|
||||
@@ -39,20 +39,22 @@ public class CacheResultOperation extends BaseKeyCacheOperation<CacheResult> {
|
||||
|
||||
private final String exceptionCacheName;
|
||||
|
||||
public CacheResultOperation(CacheMethodDetails<CacheResult> methodDetails,
|
||||
CacheResolver cacheResolver, KeyGenerator keyGenerator,
|
||||
CacheResolver exceptionCacheResolver) {
|
||||
|
||||
public CacheResultOperation(CacheMethodDetails<CacheResult> methodDetails, CacheResolver cacheResolver,
|
||||
KeyGenerator keyGenerator, CacheResolver exceptionCacheResolver) {
|
||||
|
||||
super(methodDetails, cacheResolver, keyGenerator);
|
||||
CacheResult ann = methodDetails.getCacheAnnotation();
|
||||
this.exceptionTypeFilter = createExceptionTypeFiler(ann.cachedExceptions(), ann.nonCachedExceptions());
|
||||
this.exceptionTypeFilter = createExceptionTypeFilter(ann.cachedExceptions(), ann.nonCachedExceptions());
|
||||
this.exceptionCacheResolver = exceptionCacheResolver;
|
||||
String exceptionCacheNameCandidate = ann.exceptionCacheName();
|
||||
this.exceptionCacheName = StringUtils.hasText(exceptionCacheNameCandidate) ? exceptionCacheNameCandidate : null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ExceptionTypeFilter getExceptionTypeFilter() {
|
||||
return exceptionTypeFilter;
|
||||
return this.exceptionTypeFilter;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,7 +71,7 @@ public class CacheResultOperation extends BaseKeyCacheOperation<CacheResult> {
|
||||
* use for matching exceptions thrown by this operation.
|
||||
*/
|
||||
public CacheResolver getExceptionCacheResolver() {
|
||||
return exceptionCacheResolver;
|
||||
return this.exceptionCacheResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,7 +80,7 @@ public class CacheResultOperation extends BaseKeyCacheOperation<CacheResult> {
|
||||
* @see javax.cache.annotation.CacheResult#exceptionCacheName()
|
||||
*/
|
||||
public String getExceptionCacheName() {
|
||||
return exceptionCacheName;
|
||||
return this.exceptionCacheName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user