Java Code Examples for org.springframework.cache.interceptor.CacheOperationInvoker#ThrowableWrapper

The following examples show how to use org.springframework.cache.interceptor.CacheOperationInvoker#ThrowableWrapper . 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: JCacheCustomInterceptorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
protected Object invokeOperation(CacheOperationInvoker invoker) {
	try {
		return super.invokeOperation(invoker);
	}
	catch (CacheOperationInvoker.ThrowableWrapper e) {
		Throwable original = e.getOriginal();
		if (original.getClass() == UnsupportedOperationException.class) {
			return 55L;
		}
		else {
			throw new CacheOperationInvoker.ThrowableWrapper(
					new RuntimeException("wrapping original", original));
		}
	}
}
 
Example 2
Source File: JCacheInterceptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
	Method method = invocation.getMethod();

	CacheOperationInvoker aopAllianceInvoker = new CacheOperationInvoker() {
		@Override
		public Object invoke() {
			try {
				return invocation.proceed();
			}
			catch (Throwable ex) {
				throw new ThrowableWrapper(ex);
			}
		}
	};

	try {
		return execute(aopAllianceInvoker, invocation.getThis(), method, invocation.getArguments());
	}
	catch (CacheOperationInvoker.ThrowableWrapper th) {
		throw th.getOriginal();
	}
}
 
Example 3
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 4
Source File: CacheResultInterceptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Rewrite the call stack of the specified {@code exception} so that it matches
 * the current call stack up to (included) the specified method invocation.
 * <p>Clone the specified exception. If the exception is not {@code serializable},
 * the original exception is returned. If no common ancestor can be found, returns
 * the original exception.
 * <p>Used to make sure that a cached exception has a valid invocation context.
 * @param exception the exception to merge with the current call stack
 * @param className the class name of the common ancestor
 * @param methodName the method name of the common ancestor
 * @return a clone exception with a rewritten call stack composed of the current call
 * stack up to (included) the common ancestor specified by the {@code className} and
 * {@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) {

	Throwable clone = cloneException(exception);
	if (clone == null) {
		return new CacheOperationInvoker.ThrowableWrapper(exception);
	}

	StackTraceElement[] callStack = new Exception().getStackTrace();
	StackTraceElement[] cachedCallStack = exception.getStackTrace();

	int index = findCommonAncestorIndex(callStack, className, methodName);
	int cachedIndex = findCommonAncestorIndex(cachedCallStack, className, methodName);
	if (index == -1 || cachedIndex == -1) {
		return new CacheOperationInvoker.ThrowableWrapper(exception); // Cannot find common ancestor
	}
	StackTraceElement[] result = new StackTraceElement[cachedIndex + callStack.length - index];
	System.arraycopy(cachedCallStack, 0, result, 0, cachedIndex);
	System.arraycopy(callStack, index, result, cachedIndex, callStack.length - index);

	clone.setStackTrace(result);
	return new CacheOperationInvoker.ThrowableWrapper(clone);
}
 
Example 5
Source File: JCacheCustomInterceptorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
protected Object invokeOperation(CacheOperationInvoker invoker) {
	try {
		return super.invokeOperation(invoker);
	}
	catch (CacheOperationInvoker.ThrowableWrapper e) {
		Throwable original = e.getOriginal();
		if (original.getClass() == UnsupportedOperationException.class) {
			return 55L;
		}
		else {
			throw new CacheOperationInvoker.ThrowableWrapper(
					new RuntimeException("wrapping original", original));
		}
	}
}
 
Example 6
Source File: CacheResultInterceptor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Rewrite the call stack of the specified {@code exception} so that it matches
 * the current call stack up-to (included) the specified method invocation.
 * <p>Clone the specified exception. If the exception is not {@code serializable},
 * the original exception is returned. If no common ancestor can be found, returns
 * the original exception.
 * <p>Used to make sure that a cached exception has a valid invocation context.
 * @param exception the exception to merge with the current call stack
 * @param className the class name of the common ancestor
 * @param methodName the method name of the common ancestor
 * @return a clone exception with a rewritten call stack composed of the current
 * call stack up to (included) the common ancestor specified by the {@code className} and
 * {@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) {

	Throwable clone = cloneException(exception);
	if (clone == null) {
		return new CacheOperationInvoker.ThrowableWrapper(exception);
	}

	StackTraceElement[] callStack = new Exception().getStackTrace();
	StackTraceElement[] cachedCallStack = exception.getStackTrace();

	int index = findCommonAncestorIndex(callStack, className, methodName);
	int cachedIndex = findCommonAncestorIndex(cachedCallStack, className, methodName);
	if (index == -1 || cachedIndex == -1) {
		return new CacheOperationInvoker.ThrowableWrapper(exception); // Cannot find common ancestor
	}
	StackTraceElement[] result = new StackTraceElement[cachedIndex + callStack.length - index];
	System.arraycopy(cachedCallStack, 0, result, 0, cachedIndex);
	System.arraycopy(callStack, index, result, cachedIndex, callStack.length - index);

	clone.setStackTrace(result);
	return new CacheOperationInvoker.ThrowableWrapper(clone);
}
 
Example 7
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 8
Source File: JCacheInterceptor.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@Nullable
public Object invoke(final MethodInvocation invocation) throws Throwable {
	Method method = invocation.getMethod();

	CacheOperationInvoker aopAllianceInvoker = () -> {
		try {
			return invocation.proceed();
		}
		catch (Throwable ex) {
			throw new CacheOperationInvoker.ThrowableWrapper(ex);
		}
	};

	try {
		return execute(aopAllianceInvoker, invocation.getThis(), method, invocation.getArguments());
	}
	catch (CacheOperationInvoker.ThrowableWrapper th) {
		throw th.getOriginal();
	}
}
 
Example 9
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 10
Source File: CustomInterceptorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected Object invokeOperation(CacheOperationInvoker invoker) {
	try {
		return super.invokeOperation(invoker);
	}
	catch (CacheOperationInvoker.ThrowableWrapper e) {
		Throwable original = e.getOriginal();
		if (original.getClass() == UnsupportedOperationException.class) {
			return 55L;
		}
		else {
			throw new CacheOperationInvoker.ThrowableWrapper(
					new RuntimeException("wrapping original", original));
		}
	}
}
 
Example 11
Source File: CustomInterceptorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
protected Object invokeOperation(CacheOperationInvoker invoker) {
	try {
		return super.invokeOperation(invoker);
	}
	catch (CacheOperationInvoker.ThrowableWrapper e) {
		Throwable original = e.getOriginal();
		if (original.getClass() == UnsupportedOperationException.class) {
			return 55L;
		}
		else {
			throw new CacheOperationInvoker.ThrowableWrapper(
					new RuntimeException("wrapping original", original));
		}
	}
}
 
Example 12
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 13
Source File: CacheResultInterceptor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Rewrite the call stack of the specified {@code exception} so that it matches
 * the current call stack up to (included) the specified method invocation.
 * <p>Clone the specified exception. If the exception is not {@code serializable},
 * the original exception is returned. If no common ancestor can be found, returns
 * the original exception.
 * <p>Used to make sure that a cached exception has a valid invocation context.
 * @param exception the exception to merge with the current call stack
 * @param className the class name of the common ancestor
 * @param methodName the method name of the common ancestor
 * @return a clone exception with a rewritten call stack composed of the current call
 * stack up to (included) the common ancestor specified by the {@code className} and
 * {@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) {

	Throwable clone = cloneException(exception);
	if (clone == null) {
		return new CacheOperationInvoker.ThrowableWrapper(exception);
	}

	StackTraceElement[] callStack = new Exception().getStackTrace();
	StackTraceElement[] cachedCallStack = exception.getStackTrace();

	int index = findCommonAncestorIndex(callStack, className, methodName);
	int cachedIndex = findCommonAncestorIndex(cachedCallStack, className, methodName);
	if (index == -1 || cachedIndex == -1) {
		return new CacheOperationInvoker.ThrowableWrapper(exception); // Cannot find common ancestor
	}
	StackTraceElement[] result = new StackTraceElement[cachedIndex + callStack.length - index];
	System.arraycopy(cachedCallStack, 0, result, 0, cachedIndex);
	System.arraycopy(callStack, index, result, cachedIndex, callStack.length - index);

	clone.setStackTrace(result);
	return new CacheOperationInvoker.ThrowableWrapper(clone);
}
 
Example 14
Source File: JCacheInterceptor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
	Method method = invocation.getMethod();

	CacheOperationInvoker aopAllianceInvoker = new CacheOperationInvoker() {
		@Override
		public Object invoke() {
			try {
				return invocation.proceed();
			}
			catch (Throwable ex) {
				throw new ThrowableWrapper(ex);
			}
		}
	};

	try {
		return execute(aopAllianceInvoker, invocation.getThis(), method, invocation.getArguments());
	}
	catch (CacheOperationInvoker.ThrowableWrapper th) {
		throw th.getOriginal();
	}
}
 
Example 15
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 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 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 18
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 19
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 20
Source File: CachePutInterceptor.java    From lams with GNU General Public License v2.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;
	}
}