Java Code Examples for org.springframework.cache.interceptor.CacheOperationInvocationContext#getOperation()

The following examples show how to use org.springframework.cache.interceptor.CacheOperationInvocationContext#getOperation() . 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: 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 2
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 3
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 4
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 5
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 6
Source File: CacheResultInterceptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
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 7
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 8
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 9
Source File: CachePutInterceptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected Object invoke(
		CacheOperationInvocationContext<CachePutOperation> context, CacheOperationInvoker invoker) {

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

	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 10
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 11
Source File: CacheResultInterceptor.java    From spring4-understanding with Apache License 2.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();
		cache.put(cacheKey, invocationResult);
		return invocationResult;
	}
	catch (CacheOperationInvoker.ThrowableWrapper ex) {
		Throwable original = ex.getOriginal();
		cacheException(exceptionCache, operation.getExceptionTypeFilter(), cacheKey, original);
		throw ex;
	}
}
 
Example 12
Source File: AbstractCacheInterceptor.java    From spring-analysis-note with MIT License 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 13
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 14
Source File: AbstractCacheInterceptor.java    From java-technology-stack with MIT License 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 15
Source File: CachePutInterceptor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected Object invoke(
		CacheOperationInvocationContext<CachePutOperation> context, CacheOperationInvoker invoker) {

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

	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 16
Source File: SimpleExceptionCacheResolver.java    From spring-analysis-note with MIT License 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 17
Source File: JCacheAspectSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
private Object execute(CacheOperationInvocationContext<?> context, CacheOperationInvoker invoker) {
	CacheOperationInvoker adapter = new CacheOperationInvokerAdapter(invoker);
	BasicOperation operation = context.getOperation();

	if (operation instanceof CacheResultOperation) {
		Assert.state(this.cacheResultInterceptor != null, "No CacheResultInterceptor");
		return this.cacheResultInterceptor.invoke(
				(CacheOperationInvocationContext<CacheResultOperation>) context, adapter);
	}
	else if (operation instanceof CachePutOperation) {
		Assert.state(this.cachePutInterceptor != null, "No CachePutInterceptor");
		return this.cachePutInterceptor.invoke(
				(CacheOperationInvocationContext<CachePutOperation>) context, adapter);
	}
	else if (operation instanceof CacheRemoveOperation) {
		Assert.state(this.cacheRemoveEntryInterceptor != null, "No CacheRemoveEntryInterceptor");
		return this.cacheRemoveEntryInterceptor.invoke(
				(CacheOperationInvocationContext<CacheRemoveOperation>) context, adapter);
	}
	else if (operation instanceof CacheRemoveAllOperation) {
		Assert.state(this.cacheRemoveAllInterceptor != null, "No CacheRemoveAllInterceptor");
		return this.cacheRemoveAllInterceptor.invoke(
				(CacheOperationInvocationContext<CacheRemoveAllOperation>) context, adapter);
	}
	else {
		throw new IllegalArgumentException("Cannot handle " + operation);
	}
}
 
Example 18
Source File: AbstractKeyCacheInterceptor.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Create a {@link CacheKeyInvocationContext} based on the specified invocation.
 * @param context the context of the invocation.
 * @return the related {@code CacheKeyInvocationContext}
 */
protected CacheKeyInvocationContext<A> createCacheKeyInvocationContext(CacheOperationInvocationContext<O> context) {
	return new DefaultCacheKeyInvocationContext<>(context.getOperation(), context.getTarget(), context.getArgs());
}
 
Example 19
Source File: AbstractKeyCacheInterceptor.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a {@link CacheKeyInvocationContext} based on the specified invocation.
 * @param context the context of the invocation.
 * @return the related {@code CacheKeyInvocationContext}
 */
protected CacheKeyInvocationContext<A> createCacheKeyInvocationContext(
		CacheOperationInvocationContext<O> context) {
	return new DefaultCacheKeyInvocationContext<A>(context.getOperation(), context.getTarget(), context.getArgs());
}
 
Example 20
Source File: AbstractKeyCacheInterceptor.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Create a {@link CacheKeyInvocationContext} based on the specified invocation.
 * @param context the context of the invocation.
 * @return the related {@code CacheKeyInvocationContext}
 */
protected CacheKeyInvocationContext<A> createCacheKeyInvocationContext(
		CacheOperationInvocationContext<O> context) {
	return new DefaultCacheKeyInvocationContext<A>(context.getOperation(), context.getTarget(), context.getArgs());
}