org.springframework.cache.interceptor.CacheOperationInvocationContext Java Examples

The following examples show how to use org.springframework.cache.interceptor.CacheOperationInvocationContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConfigurableCacheResolver.java    From haven-platform with Apache License 2.0 6 votes vote down vote up
private <T extends Annotation> T getAnnotation(CacheOperationInvocationContext<?> context, Class<T> clazz) {
    try {
        // due to some cache proxy behaviour we can get method of superinterface instead of annotated method from target class
        // but sometime annotation has been appear on interface therefore we need check both cases
        Method proxiedMethod = context.getMethod();
        Class<?> targetClazz = context.getTarget().getClass();
        T annotation = null;
        if(!targetClazz.equals(proxiedMethod.getDeclaringClass())) {
            Method origMethod = targetClazz.getMethod(proxiedMethod.getName(), proxiedMethod.getParameterTypes());
            annotation = origMethod.getAnnotation(clazz);
        }
        if(annotation == null) {
            annotation = proxiedMethod.getAnnotation(clazz);
        }
        return annotation;
    } catch (NoSuchMethodException e) {
        throw Throwables.asRuntime(e);
    }
}
 
Example #2
Source File: JCacheAspectSupport.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private Object execute(CacheOperationInvocationContext<?> context, CacheOperationInvoker invoker) {
	CacheOperationInvoker adapter = new CacheOperationInvokerAdapter(invoker);
	BasicOperation operation = context.getOperation();

	if (operation instanceof CacheResultOperation) {
		return this.cacheResultInterceptor.invoke(
				(CacheOperationInvocationContext<CacheResultOperation>) context, adapter);
	}
	else if (operation instanceof CachePutOperation) {
		return this.cachePutInterceptor.invoke(
				(CacheOperationInvocationContext<CachePutOperation>) context, adapter);
	}
	else if (operation instanceof CacheRemoveOperation) {
		return this.cacheRemoveEntryInterceptor.invoke(
				(CacheOperationInvocationContext<CacheRemoveOperation>) context, adapter);
	}
	else if (operation instanceof CacheRemoveAllOperation) {
		return this.cacheRemoveAllInterceptor.invoke(
				(CacheOperationInvocationContext<CacheRemoveAllOperation>) context, adapter);
	}
	else {
		throw new IllegalArgumentException("Cannot handle " + operation);
	}
}
 
Example #3
Source File: CacheRemoveAllInterceptor.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
protected Object invoke(
		CacheOperationInvocationContext<CacheRemoveAllOperation> context, CacheOperationInvoker invoker) {

	CacheRemoveAllOperation operation = context.getOperation();

	boolean earlyRemove = operation.isEarlyRemove();
	if (earlyRemove) {
		removeAll(context);
	}

	try {
		Object result = invoker.invoke();
		if (!earlyRemove) {
			removeAll(context);
		}
		return result;
	}
	catch (CacheOperationInvoker.ThrowableWrapper ex) {
		Throwable original = ex.getOriginal();
		if (!earlyRemove && operation.getExceptionTypeFilter().match(original.getClass())) {
			removeAll(context);
		}
		throw ex;
	}
}
 
Example #4
Source File: JCacheAspectSupport.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private Object execute(CacheOperationInvocationContext<?> context, CacheOperationInvoker invoker) {
	CacheOperationInvoker adapter = new CacheOperationInvokerAdapter(invoker);
	BasicOperation operation = context.getOperation();

	if (operation instanceof CacheResultOperation) {
		return cacheResultInterceptor.invoke(
				(CacheOperationInvocationContext<CacheResultOperation>) context, adapter);
	}
	else if (operation instanceof CachePutOperation) {
		return cachePutInterceptor.invoke(
				(CacheOperationInvocationContext<CachePutOperation>) context, adapter);
	}
	else if (operation instanceof CacheRemoveOperation) {
		return cacheRemoveEntryInterceptor.invoke(
				(CacheOperationInvocationContext<CacheRemoveOperation>) context, adapter);
	}
	else if (operation instanceof CacheRemoveAllOperation) {
		return cacheRemoveAllInterceptor.invoke(
				(CacheOperationInvocationContext<CacheRemoveAllOperation>) context, adapter);
	}
	else {
		throw new IllegalArgumentException("Could not handle " + operation);
	}
}
 
Example #5
Source File: CacheRemoveEntryInterceptor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected Object invoke(
		CacheOperationInvocationContext<CacheRemoveOperation> context, CacheOperationInvoker invoker) {

	CacheRemoveOperation operation = context.getOperation();

	boolean earlyRemove = operation.isEarlyRemove();
	if (earlyRemove) {
		removeValue(context);
	}

	try {
		Object result = invoker.invoke();
		if (!earlyRemove) {
			removeValue(context);
		}
		return result;
	}
	catch (CacheOperationInvoker.ThrowableWrapper wrapperException) {
		Throwable ex = wrapperException.getOriginal();
		if (!earlyRemove && operation.getExceptionTypeFilter().match(ex.getClass())) {
			removeValue(context);
		}
		throw wrapperException;
	}
}
 
Example #6
Source File: CacheRemoveEntryInterceptor.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
protected Object invoke(
		CacheOperationInvocationContext<CacheRemoveOperation> context, CacheOperationInvoker invoker) {

	CacheRemoveOperation operation = context.getOperation();

	boolean earlyRemove = operation.isEarlyRemove();
	if (earlyRemove) {
		removeValue(context);
	}

	try {
		Object result = invoker.invoke();
		if (!earlyRemove) {
			removeValue(context);
		}
		return result;
	}
	catch (CacheOperationInvoker.ThrowableWrapper wrapperException) {
		Throwable ex = wrapperException.getOriginal();
		if (!earlyRemove && operation.getExceptionTypeFilter().match(ex.getClass())) {
			removeValue(context);
		}
		throw wrapperException;
	}
}
 
Example #7
Source File: CacheRemoveEntryInterceptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected Object invoke(CacheOperationInvocationContext<CacheRemoveOperation> context,
		CacheOperationInvoker invoker) {
	CacheRemoveOperation operation = context.getOperation();

	final boolean earlyRemove = operation.isEarlyRemove();

	if (earlyRemove) {
		removeValue(context);
	}

	try {
		Object result = invoker.invoke();
		if (!earlyRemove) {
			removeValue(context);
		}
		return result;
	}
	catch (CacheOperationInvoker.ThrowableWrapper t) {
		Throwable ex = t.getOriginal();
		if (!earlyRemove && operation.getExceptionTypeFilter().match(ex.getClass())) {
			removeValue(context);
		}
		throw t;
	}
}
 
Example #8
Source File: CacheRemoveAllInterceptor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected Object invoke(
		CacheOperationInvocationContext<CacheRemoveAllOperation> context, CacheOperationInvoker invoker) {

	CacheRemoveAllOperation operation = context.getOperation();

	boolean earlyRemove = operation.isEarlyRemove();
	if (earlyRemove) {
		removeAll(context);
	}

	try {
		Object result = invoker.invoke();
		if (!earlyRemove) {
			removeAll(context);
		}
		return result;
	}
	catch (CacheOperationInvoker.ThrowableWrapper ex) {
		Throwable original = ex.getOriginal();
		if (!earlyRemove && operation.getExceptionTypeFilter().match(original.getClass())) {
			removeAll(context);
		}
		throw ex;
	}
}
 
Example #9
Source File: CacheRemoveEntryInterceptor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
protected Object invoke(CacheOperationInvocationContext<CacheRemoveOperation> context,
		CacheOperationInvoker invoker) {
	CacheRemoveOperation operation = context.getOperation();

	final boolean earlyRemove = operation.isEarlyRemove();

	if (earlyRemove) {
		removeValue(context);
	}

	try {
		Object result = invoker.invoke();
		if (!earlyRemove) {
			removeValue(context);
		}
		return result;
	}
	catch (CacheOperationInvoker.ThrowableWrapper t) {
		Throwable ex = t.getOriginal();
		if (!earlyRemove && operation.getExceptionTypeFilter().match(ex.getClass())) {
			removeValue(context);
		}
		throw t;
	}
}
 
Example #10
Source File: CacheRemoveAllInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void removeAll(CacheOperationInvocationContext<CacheRemoveAllOperation> context) {
	Cache cache = resolveCache(context);
	if (logger.isTraceEnabled()) {
		logger.trace("Invalidating entire cache '" + cache.getName() + "' for operation "
				+ context.getOperation());
	}
	doClear(cache);
}
 
Example #11
Source File: CacheResolverAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context) {
	if (!(context instanceof CacheInvocationContext<?>)) {
		throw new IllegalStateException("Unexpected context " + context);
	}
	CacheInvocationContext<?> cacheInvocationContext = (CacheInvocationContext<?>) context;
	javax.cache.Cache<Object, Object> cache = target.resolveCache(cacheInvocationContext);
	Assert.notNull(cache, "Cannot resolve cache for '" + context + "' using '" + target + "'");
	return Collections.singleton(new JCacheCache(cache));
}
 
Example #12
Source File: CacheResolverAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context) {
	if (!(context instanceof CacheInvocationContext<?>)) {
		throw new IllegalStateException("Unexpected context " + context);
	}
	CacheInvocationContext<?> cacheInvocationContext = (CacheInvocationContext<?>) context;
	javax.cache.Cache<Object, Object> cache = target.resolveCache(cacheInvocationContext);
	Assert.notNull(cache, "Cannot resolve cache for '" + context + "' using '" + target + "'");
	return Collections.singleton(new JCacheCache(cache));
}
 
Example #13
Source File: CacheRemoveAllInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected void removeAll(CacheOperationInvocationContext<CacheRemoveAllOperation> context) {
	Cache cache = resolveCache(context);
	if (logger.isTraceEnabled()) {
		logger.trace("Invalidating entire cache '" + cache.getName() + "' for operation "
				+ context.getOperation());
	}
	doClear(cache);
}
 
Example #14
Source File: AbstractKeyCacheInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Generate a key for the specified invocation.
 * @param context the context of the invocation
 * @return the key to use
 */
protected Object generateKey(CacheOperationInvocationContext<O> context) {
	KeyGenerator keyGenerator = context.getOperation().getKeyGenerator();
	Object key = keyGenerator.generate(context.getTarget(), context.getMethod(), context.getArgs());
	if (logger.isTraceEnabled()) {
		logger.trace("Computed cache key " + key + " for operation " + context.getOperation());
	}
	return key;
}
 
Example #15
Source File: DefaultJCacheOperationSource.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context) {
	if (this.cacheResolver == null) {
		this.cacheResolver = new SimpleExceptionCacheResolver(getDefaultCacheManager());
	}
	return this.cacheResolver.resolveCaches(context);
}
 
Example #16
Source File: CacheRemoveAllInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Object invoke(CacheOperationInvocationContext<CacheRemoveAllOperation> context,
		CacheOperationInvoker invoker) {

	CacheRemoveAllOperation operation = context.getOperation();

	boolean earlyRemove = operation.isEarlyRemove();

	if (earlyRemove) {
		removeAll(context);
	}

	try {
		Object result = invoker.invoke();
		if (!earlyRemove) {
			removeAll(context);
		}
		return result;
	}
	catch (CacheOperationInvoker.ThrowableWrapper ex) {
		Throwable original = ex.getOriginal();
		if (!earlyRemove && operation.getExceptionTypeFilter().match(original.getClass())) {
			removeAll(context);
		}
		throw ex;
	}
}
 
Example #17
Source File: CachePutInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Object invoke(CacheOperationInvocationContext<CachePutOperation> context,
		CacheOperationInvoker invoker) {

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

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

	if (earlyPut) {
		cacheValue(context, value);
	}

	try {
		Object result = invoker.invoke();
		if (!earlyPut) {
			cacheValue(context, value);
		}
		return result;
	}
	catch (CacheOperationInvoker.ThrowableWrapper ex) {
		Throwable original = ex.getOriginal();
		if (!earlyPut && operation.getExceptionTypeFilter().match(original.getClass())) {
			cacheValue(context, value);
		}
		throw ex;
	}
}
 
Example #18
Source File: MultipleCacheResolver.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context) {
    Collection<Cache> caches = new ArrayList<Cache>();
    if ("getOrderDetail".equals(context.getMethod()
        .getName())) {
        caches.add(caffeineCacheManager.getCache(ORDER_CACHE));
    } else {
        caches.add(simpleCacheManager.getCache(ORDER_PRICE_CACHE));
    }
    return caches;
}
 
Example #19
Source File: SimpleExceptionCacheResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) {
	BasicOperation operation = context.getOperation();
	if (!(operation instanceof CacheResultOperation)) {
		throw new IllegalStateException("Could not extract exception cache name from " + operation);
	}
	CacheResultOperation cacheResultOperation = (CacheResultOperation) operation;
	String exceptionCacheName = cacheResultOperation.getExceptionCacheName();
	if (exceptionCacheName != null) {
		return Collections.singleton(exceptionCacheName);
	}
	return null;
}
 
Example #20
Source File: AbstractCacheInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve the cache to use.
 * @param context the invocation context
 * @return the cache to use (never null)
 */
protected Cache resolveCache(CacheOperationInvocationContext<O> context) {
	Collection<? extends Cache> caches = context.getOperation().getCacheResolver().resolveCaches(context);
	Cache cache = extractFrom(caches);
	if (cache == null) {
		throw new IllegalStateException("Cache could not have been resolved for " + context.getOperation());
	}
	return cache;
}
 
Example #21
Source File: CacheRemoveAllInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Object invoke(CacheOperationInvocationContext<CacheRemoveAllOperation> context,
		CacheOperationInvoker invoker) {

	CacheRemoveAllOperation operation = context.getOperation();

	boolean earlyRemove = operation.isEarlyRemove();

	if (earlyRemove) {
		removeAll(context);
	}

	try {
		Object result = invoker.invoke();
		if (!earlyRemove) {
			removeAll(context);
		}
		return result;
	}
	catch (CacheOperationInvoker.ThrowableWrapper ex) {
		Throwable original = ex.getOriginal();
		if (!earlyRemove && operation.getExceptionTypeFilter().match(original.getClass())) {
			removeAll(context);
		}
		throw ex;
	}
}
 
Example #22
Source File: DefaultJCacheOperationSource.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context) {
	if (this.cacheResolver == null) {
		this.cacheResolver = new SimpleExceptionCacheResolver(getDefaultCacheManager());
	}
	return this.cacheResolver.resolveCaches(context);
}
 
Example #23
Source File: AbstractKeyCacheInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate a key for the specified invocation.
 * @param context the context of the invocation
 * @return the key to use
 */
protected Object generateKey(CacheOperationInvocationContext<O> context) {
	KeyGenerator keyGenerator = context.getOperation().getKeyGenerator();
	Object key = keyGenerator.generate(context.getTarget(), context.getMethod(), context.getArgs());
	if (logger.isTraceEnabled()) {
		logger.trace("Computed cache key " + key + " for operation " + context.getOperation());
	}
	return key;
}
 
Example #24
Source File: SimpleExceptionCacheResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) {
	BasicOperation operation = context.getOperation();
	if (!(operation instanceof CacheResultOperation)) {
		throw new IllegalStateException("Could not extract exception cache name from " + operation);
	}
	CacheResultOperation cacheResultOperation = (CacheResultOperation) operation;
	String exceptionCacheName = cacheResultOperation.getExceptionCacheName();
	if (exceptionCacheName != null) {
		return Collections.singleton(exceptionCacheName);
	}
	return null;
}
 
Example #25
Source File: CacheRemoveEntryInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void removeValue(CacheOperationInvocationContext<CacheRemoveOperation> context) {
	Object key = generateKey(context);
	Cache cache = resolveCache(context);
	if (logger.isTraceEnabled()) {
		logger.trace("Invalidating key [" + key + "] on cache '" + cache.getName()
				+ "' for operation " + context.getOperation());
	}
	doEvict(cache, key);
}
 
Example #26
Source File: CacheResultInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Cache resolveExceptionCache(CacheOperationInvocationContext<CacheResultOperation> context) {
	CacheResolver exceptionCacheResolver = context.getOperation().getExceptionCacheResolver();
	if (exceptionCacheResolver != null) {
		return extractFrom(context.getOperation().getExceptionCacheResolver().resolveCaches(context));
	}
	return null;
}
 
Example #27
Source File: CacheResultInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Object invoke(CacheOperationInvocationContext<CacheResultOperation> context,
		CacheOperationInvoker invoker) {

	CacheResultOperation operation = context.getOperation();
	Object cacheKey = generateKey(context);

	Cache cache = resolveCache(context);
	Cache exceptionCache = resolveExceptionCache(context);

	if (!operation.isAlwaysInvoked()) {
		Cache.ValueWrapper cachedValue = doGet(cache, cacheKey);
		if (cachedValue != null) {
			return cachedValue.get();
		}
		checkForCachedException(exceptionCache, cacheKey);
	}

	try {
		Object invocationResult = invoker.invoke();
		doPut(cache, cacheKey, invocationResult);
		return invocationResult;
	}
	catch (CacheOperationInvoker.ThrowableWrapper ex) {
		Throwable original = ex.getOriginal();
		cacheException(exceptionCache, operation.getExceptionTypeFilter(), cacheKey, original);
		throw ex;
	}
}
 
Example #28
Source File: JCacheAspectSupport.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private CacheOperationInvocationContext<?> createCacheOperationInvocationContext(
		Object target, Object[] args, JCacheOperation<?> operation) {

	return new DefaultCacheInvocationContext<Annotation>(
			(JCacheOperation<Annotation>) operation, target, args);
}
 
Example #29
Source File: JCacheAspectSupport.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected Object execute(CacheOperationInvoker invoker, Object target, Method method, Object[] args) {
	// Check whether aspect is enabled to cope with cases where the AJ is pulled in automatically
	if (this.initialized) {
		Class<?> targetClass = AopProxyUtils.ultimateTargetClass(target);
		JCacheOperation<?> operation = getCacheOperationSource().getCacheOperation(method, targetClass);
		if (operation != null) {
			CacheOperationInvocationContext<?> context =
					createCacheOperationInvocationContext(target, args, operation);
			return execute(context, invoker);
		}
	}

	return invoker.invoke();
}
 
Example #30
Source File: CacheReproTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) {
	String cacheName = (String) context.getArgs()[0];
	if (cacheName != null) {
		return Collections.singleton(cacheName);
	}
	return null;
}