001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.cache.interceptor;
018
019import java.io.Serializable;
020import java.lang.reflect.Method;
021
022import org.aopalliance.intercept.MethodInterceptor;
023import org.aopalliance.intercept.MethodInvocation;
024
025/**
026 * AOP Alliance MethodInterceptor for declarative cache
027 * management using the common Spring caching infrastructure
028 * ({@link org.springframework.cache.Cache}).
029 *
030 * <p>Derives from the {@link CacheAspectSupport} class which
031 * contains the integration with Spring's underlying caching API.
032 * CacheInterceptor simply calls the relevant superclass methods
033 * in the correct order.
034 *
035 * <p>CacheInterceptors are thread-safe.
036 *
037 * @author Costin Leau
038 * @author Juergen Hoeller
039 * @since 3.1
040 */
041@SuppressWarnings("serial")
042public class CacheInterceptor extends CacheAspectSupport implements MethodInterceptor, Serializable {
043
044        @Override
045        public Object invoke(final MethodInvocation invocation) throws Throwable {
046                Method method = invocation.getMethod();
047
048                CacheOperationInvoker aopAllianceInvoker = new CacheOperationInvoker() {
049                        @Override
050                        public Object invoke() {
051                                try {
052                                        return invocation.proceed();
053                                }
054                                catch (Throwable ex) {
055                                        throw new ThrowableWrapper(ex);
056                                }
057                        }
058                };
059
060                try {
061                        return execute(aopAllianceInvoker, invocation.getThis(), method, invocation.getArguments());
062                }
063                catch (CacheOperationInvoker.ThrowableWrapper th) {
064                        throw th.getOriginal();
065                }
066        }
067
068}