Skip to content

Commit

Permalink
polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Jul 23, 2014
1 parent 5be1ff2 commit d9e0b29
Show file tree
Hide file tree
Showing 53 changed files with 433 additions and 293 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import javax.cache.annotation.CacheValue;

import org.springframework.cache.Cache;
import org.springframework.cache.interceptor.SimpleKeyGenerator;
import org.springframework.cache.jcache.config.JCacheableService;
import org.springframework.cache.jcache.interceptor.SimpleGeneratedCacheKey;
import org.springframework.cache.jcache.support.TestableCacheKeyGenerator;
import org.springframework.cache.jcache.support.TestableCacheResolverFactory;

Expand Down Expand Up @@ -111,7 +111,7 @@ public void putWithException(@CacheKey String id, @CacheValue Object value, bool
@Override
@CachePut(afterInvocation = false)
public void earlyPut(String id, @CacheValue Object value) {
SimpleGeneratedCacheKey key = new SimpleGeneratedCacheKey(id);
Object key = SimpleKeyGenerator.generateKey(id);
Cache.ValueWrapper valueWrapper = defaultCache.get(key);
if (valueWrapper == null) {
throw new AssertionError("Excepted value to be put in cache with key " + key);
Expand Down Expand Up @@ -143,7 +143,7 @@ public void removeWithException(@CacheKey String id, boolean matchFilter) {
@Override
@CacheRemove(afterInvocation = false)
public void earlyRemove(String id) {
SimpleGeneratedCacheKey key = new SimpleGeneratedCacheKey(id);
Object key = SimpleKeyGenerator.generateKey(id);
Cache.ValueWrapper valueWrapper = defaultCache.get(key);
if (valueWrapper != null) {
throw new AssertionError("Value with key " + key + " expected to be already remove from cache");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,17 @@ protected void useCachingConfigurer(JCacheConfigurer config) {
public JCacheOperationSource cacheOperationSource() {
DefaultJCacheOperationSource source = new DefaultJCacheOperationSource();
if (this.cacheManager != null) {
source.setCacheManager(cacheManager);
source.setCacheManager(this.cacheManager);
}
if (keyGenerator != null) {
source.setKeyGenerator(keyGenerator);
if (this.keyGenerator != null) {
source.setKeyGenerator(this.keyGenerator);
}
if (this.cacheResolver != null) {
source.setCacheResolver(cacheResolver);
source.setCacheResolver(this.cacheResolver);
}
if (this.exceptionCacheResolver != null) {
source.setExceptionCacheResolver(exceptionCacheResolver);
source.setExceptionCacheResolver(this.exceptionCacheResolver);
}

return source;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
import org.springframework.cache.interceptor.CacheOperationInvoker;
import org.springframework.cache.jcache.model.BaseCacheOperation;

/**
* A base interceptor for JSR-107 cache annotations.
Expand All @@ -37,7 +36,7 @@
* @since 4.1
*/
@SuppressWarnings("serial")
public abstract class AbstractCacheInterceptor<O extends BaseCacheOperation<A>, A extends Annotation>
abstract class AbstractCacheInterceptor<O extends AbstractJCacheOperation<A>, A extends Annotation>
extends AbstractCacheInvoker implements Serializable {

protected final Log logger = LogFactory.getLog(getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.commons.logging.LogFactory;

import org.springframework.cache.interceptor.MethodCacheKey;
import org.springframework.cache.jcache.model.JCacheOperation;
import org.springframework.core.BridgeMethodResolver;
import org.springframework.util.ClassUtils;

Expand Down Expand Up @@ -58,7 +57,7 @@ public JCacheOperation<?> getCacheOperation(Method method, Class<?> targetClass)
return cached;
}
else {
JCacheOperation<?> operation = computeCacheOperations(method, targetClass);
JCacheOperation<?> operation = computeCacheOperation(method, targetClass);
if (operation != null) {
if (logger.isDebugEnabled()) {
logger.debug("Adding cacheable method '" + method.getName()
Expand All @@ -70,7 +69,7 @@ public JCacheOperation<?> getCacheOperation(Method method, Class<?> targetClass)
}
}

private JCacheOperation<?> computeCacheOperations(Method method, Class<?> targetClass) {
private JCacheOperation<?> computeCacheOperation(Method method, Class<?> targetClass) {
// Don't allow no-public methods as required.
if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.cache.jcache.model;
package org.springframework.cache.jcache.interceptor;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
Expand All @@ -32,30 +32,33 @@
* @author Stephane Nicoll
* @since 4.1
*/
public abstract class BaseKeyCacheOperation<A extends Annotation> extends BaseCacheOperation<A> {
abstract class AbstractJCacheKeyOperation<A extends Annotation> extends AbstractJCacheOperation<A> {

private final KeyGenerator keyGenerator;

private final List<CacheParameterDetail> keyParameterDetails;


/**
* Create a new instance.
* @param methodDetails the {@link CacheMethodDetails} related to the cached method
* @param cacheResolver the cache resolver to resolve regular caches
* @param keyGenerator the key generator to compute cache keys
*/
protected BaseKeyCacheOperation(CacheMethodDetails<A> methodDetails,
protected AbstractJCacheKeyOperation(CacheMethodDetails<A> methodDetails,
CacheResolver cacheResolver, KeyGenerator keyGenerator) {

super(methodDetails, cacheResolver);
this.keyGenerator = keyGenerator;
this.keyParameterDetails = initializeKeyParameterDetails(allParameterDetails);
this.keyParameterDetails = initializeKeyParameterDetails(this.allParameterDetails);
}


/**
* Return the {@link KeyGenerator} to use to compute cache keys.
*/
public KeyGenerator getKeyGenerator() {
return keyGenerator;
return this.keyGenerator;
}

/**
Expand All @@ -72,7 +75,7 @@ public KeyGenerator getKeyGenerator() {
*/
public CacheInvocationParameter[] getKeyParameters(Object... values) {
List<CacheInvocationParameter> result = new ArrayList<CacheInvocationParameter>();
for (CacheParameterDetail keyParameterDetail : keyParameterDetails) {
for (CacheParameterDetail keyParameterDetail : this.keyParameterDetails) {
int parameterPosition = keyParameterDetail.getParameterPosition();
if (parameterPosition >= values.length) {
throw new IllegalStateException("Values mismatch, key parameter at position "
Expand All @@ -95,7 +98,7 @@ private static List<CacheParameterDetail> initializeKeyParameterDetails(List<Cac
annotated.add(allParameter);
}
}
return annotated.size() == 0 ? all : annotated;
return (annotated.isEmpty() ? all : annotated);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.cache.jcache.model;
package org.springframework.cache.jcache.interceptor;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
Expand All @@ -40,7 +40,7 @@
* @author Stephane Nicoll
* @since 4.1
*/
public abstract class BaseCacheOperation<A extends Annotation> implements JCacheOperation<A> {
abstract class AbstractJCacheOperation<A extends Annotation> implements JCacheOperation<A> {

private final CacheMethodDetails<A> methodDetails;

Expand All @@ -54,7 +54,7 @@ public abstract class BaseCacheOperation<A extends Annotation> implements JCache
* @param methodDetails the {@link CacheMethodDetails} related to the cached method
* @param cacheResolver the cache resolver to resolve regular caches
*/
protected BaseCacheOperation(CacheMethodDetails<A> methodDetails, CacheResolver cacheResolver) {
protected AbstractJCacheOperation(CacheMethodDetails<A> methodDetails, CacheResolver cacheResolver) {
Assert.notNull(methodDetails, "method details must not be null.");
Assert.notNull(cacheResolver, "cache resolver must not be null.");
this.methodDetails = methodDetails;
Expand Down Expand Up @@ -159,13 +159,13 @@ protected static class CacheParameterDetail {

private final boolean isValue;

public CacheParameterDetail(Method m, int parameterPosition) {
this.rawType = m.getParameterTypes()[parameterPosition];
public CacheParameterDetail(Method method, int parameterPosition) {
this.rawType = method.getParameterTypes()[parameterPosition];
this.annotations = new LinkedHashSet<Annotation>();
boolean foundKeyAnnotation = false;
boolean foundValueAnnotation = false;
for (Annotation annotation : m.getParameterAnnotations()[parameterPosition]) {
annotations.add(annotation);
for (Annotation annotation : method.getParameterAnnotations()[parameterPosition]) {
this.annotations.add(annotation);
if (CacheKey.class.isAssignableFrom(annotation.annotationType())) {
foundKeyAnnotation = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.jcache.model.BaseKeyCacheOperation;

/**
* A base interceptor for JSR-107 key-based cache annotations.
Expand All @@ -32,7 +31,7 @@
* @since 4.1
*/
@SuppressWarnings("serial")
public abstract class AbstractKeyCacheInterceptor<O extends BaseKeyCacheOperation<A>, A extends Annotation>
abstract class AbstractKeyCacheInterceptor<O extends AbstractJCacheKeyOperation<A>, A extends Annotation>
extends AbstractCacheInterceptor<O, A> {

protected AbstractKeyCacheInterceptor(CacheErrorHandler errorHandler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@

import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.jcache.model.CachePutOperation;
import org.springframework.cache.jcache.model.CacheRemoveAllOperation;
import org.springframework.cache.jcache.model.CacheRemoveOperation;
import org.springframework.cache.jcache.model.CacheResultOperation;
import org.springframework.cache.jcache.model.DefaultCacheMethodDetails;
import org.springframework.cache.jcache.model.JCacheOperation;
import org.springframework.util.StringUtils;

/**
Expand All @@ -48,8 +42,7 @@
* @author Stephane Nicoll
* @since 4.1
*/
public abstract class AnnotationCacheOperationSource
extends AbstractFallbackJCacheOperationSource {
public abstract class AnnotationJCacheOperationSource extends AbstractFallbackJCacheOperationSource {

/**
* Locate or create an instance of the specified {@code type}.
Expand Down Expand Up @@ -236,7 +229,6 @@ protected String determineCacheName(Method method, CacheDefaults defaults, Strin

/**
* Generate a default cache name for the specified {@link Method}.
*
* @param method the annotated method
* @return the default cache name, according to JSR-107
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
import org.springframework.cache.interceptor.CacheOperationInvoker;
import org.springframework.cache.jcache.model.CachePutOperation;

/**
* Intercept methods annotated with {@link CachePut}.
Expand All @@ -32,7 +31,7 @@
* @since 4.1
*/
@SuppressWarnings("serial")
public class CachePutInterceptor extends AbstractKeyCacheInterceptor<CachePutOperation, CachePut> {
class CachePutInterceptor extends AbstractKeyCacheInterceptor<CachePutOperation, CachePut> {

public CachePutInterceptor(CacheErrorHandler errorHandler) {
super(errorHandler);
Expand All @@ -41,11 +40,12 @@ public CachePutInterceptor(CacheErrorHandler errorHandler) {
@Override
protected Object invoke(CacheOperationInvocationContext<CachePutOperation> context,
CacheOperationInvoker invoker) {

CacheKeyInvocationContext<CachePut> invocationContext = createCacheKeyInvocationContext(context);
CachePutOperation operation = context.getOperation();

final boolean earlyPut = operation.isEarlyPut();
final Object value = invocationContext.getValueParameter().getValue();
boolean earlyPut = operation.isEarlyPut();
Object value = invocationContext.getValueParameter().getValue();

if (earlyPut) {
cacheValue(context, value);
Expand All @@ -58,12 +58,12 @@ protected Object invoke(CacheOperationInvocationContext<CachePutOperation> conte
}
return result;
}
catch (CacheOperationInvoker.ThrowableWrapper t) {
Throwable ex = t.getOriginal();
if (!earlyPut && operation.getExceptionTypeFilter().match(ex.getClass())) {
catch (CacheOperationInvoker.ThrowableWrapper ex) {
Throwable original = ex.getOriginal();
if (!earlyPut && operation.getExceptionTypeFilter().match(original.getClass())) {
cacheValue(context, value);
}
throw t;
throw ex;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.cache.jcache.model;
package org.springframework.cache.jcache.interceptor;

import java.lang.reflect.Method;
import java.util.List;
Expand All @@ -34,7 +34,7 @@
* @since 4.1
* @see CachePut
*/
public class CachePutOperation extends BaseKeyCacheOperation<CachePut> {
class CachePutOperation extends AbstractJCacheKeyOperation<CachePut> {

private final ExceptionTypeFilter exceptionTypeFilter;

Expand All @@ -47,8 +47,8 @@ public CachePutOperation(
super(methodDetails, cacheResolver, keyGenerator);
CachePut ann = methodDetails.getCacheAnnotation();
this.exceptionTypeFilter = createExceptionTypeFilter(ann.cacheFor(), ann.noCacheFor());
this.valueParameterDetail = initializeValueParameterDetail(methodDetails.getMethod(), allParameterDetails);
if (valueParameterDetail == null) {
this.valueParameterDetail = initializeValueParameterDetail(methodDetails.getMethod(), this.allParameterDetails);
if (this.valueParameterDetail == null) {
throw new IllegalArgumentException("No parameter annotated with @CacheValue was found for " +
"" + methodDetails.getMethod());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
import org.springframework.cache.interceptor.CacheOperationInvoker;
import org.springframework.cache.jcache.model.CacheRemoveAllOperation;

/**
* Intercept methods annotated with {@link CacheRemoveAll}.
Expand All @@ -31,7 +30,7 @@
* @since 4.1
*/
@SuppressWarnings("serial")
public class CacheRemoveAllInterceptor
class CacheRemoveAllInterceptor
extends AbstractCacheInterceptor<CacheRemoveAllOperation, CacheRemoveAll> {

protected CacheRemoveAllInterceptor(CacheErrorHandler errorHandler) {
Expand All @@ -41,9 +40,10 @@ protected CacheRemoveAllInterceptor(CacheErrorHandler errorHandler) {
@Override
protected Object invoke(CacheOperationInvocationContext<CacheRemoveAllOperation> context,
CacheOperationInvoker invoker) {

CacheRemoveAllOperation operation = context.getOperation();

final boolean earlyRemove = operation.isEarlyRemove();
boolean earlyRemove = operation.isEarlyRemove();

if (earlyRemove) {
removeAll(context);
Expand All @@ -56,12 +56,12 @@ protected Object invoke(CacheOperationInvocationContext<CacheRemoveAllOperation>
}
return result;
}
catch (CacheOperationInvoker.ThrowableWrapper t) {
Throwable ex = t.getOriginal();
if (!earlyRemove && operation.getExceptionTypeFilter().match(ex.getClass())) {
catch (CacheOperationInvoker.ThrowableWrapper ex) {
Throwable original = ex.getOriginal();
if (!earlyRemove && operation.getExceptionTypeFilter().match(original.getClass())) {
removeAll(context);
}
throw t;
throw ex;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.cache.jcache.model;
package org.springframework.cache.jcache.interceptor;

import javax.cache.annotation.CacheMethodDetails;
import javax.cache.annotation.CacheRemoveAll;
Expand All @@ -29,7 +29,7 @@
* @since 4.1
* @see CacheRemoveAll
*/
public class CacheRemoveAllOperation extends BaseCacheOperation<CacheRemoveAll> {
class CacheRemoveAllOperation extends AbstractJCacheOperation<CacheRemoveAll> {

private final ExceptionTypeFilter exceptionTypeFilter;

Expand Down
Loading

0 comments on commit d9e0b29

Please sign in to comment.