001/* 002 * Copyright 2002-2018 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.jcache.interceptor; 018 019import java.io.Serializable; 020import java.lang.reflect.Method; 021import java.util.function.Supplier; 022 023import org.aopalliance.intercept.MethodInterceptor; 024import org.aopalliance.intercept.MethodInvocation; 025 026import org.springframework.cache.interceptor.CacheErrorHandler; 027import org.springframework.cache.interceptor.CacheOperationInvoker; 028import org.springframework.cache.interceptor.SimpleCacheErrorHandler; 029import org.springframework.lang.Nullable; 030import org.springframework.util.function.SingletonSupplier; 031 032/** 033 * AOP Alliance MethodInterceptor for declarative cache 034 * management using JSR-107 caching annotations. 035 * 036 * <p>Derives from the {@link JCacheAspectSupport} class which 037 * contains the integration with Spring's underlying caching API. 038 * JCacheInterceptor simply calls the relevant superclass method. 039 * 040 * <p>JCacheInterceptors are thread-safe. 041 * 042 * @author Stephane Nicoll 043 * @author Juergen Hoeller 044 * @since 4.1 045 * @see org.springframework.cache.interceptor.CacheInterceptor 046 */ 047@SuppressWarnings("serial") 048public class JCacheInterceptor extends JCacheAspectSupport implements MethodInterceptor, Serializable { 049 050 /** 051 * Construct a new {@code JCacheInterceptor} with the default error handler. 052 */ 053 public JCacheInterceptor() { 054 } 055 056 /** 057 * Construct a new {@code JCacheInterceptor} with the given error handler. 058 * @param errorHandler a supplier for the error handler to use, 059 * applying the default error handler if the supplier is not resolvable 060 * @since 5.1 061 */ 062 public JCacheInterceptor(@Nullable Supplier<CacheErrorHandler> errorHandler) { 063 this.errorHandler = new SingletonSupplier<>(errorHandler, SimpleCacheErrorHandler::new); 064 } 065 066 067 @Override 068 @Nullable 069 public Object invoke(final MethodInvocation invocation) throws Throwable { 070 Method method = invocation.getMethod(); 071 072 CacheOperationInvoker aopAllianceInvoker = () -> { 073 try { 074 return invocation.proceed(); 075 } 076 catch (Throwable ex) { 077 throw new CacheOperationInvoker.ThrowableWrapper(ex); 078 } 079 }; 080 081 try { 082 return execute(aopAllianceInvoker, invocation.getThis(), method, invocation.getArguments()); 083 } 084 catch (CacheOperationInvoker.ThrowableWrapper th) { 085 throw th.getOriginal(); 086 } 087 } 088 089}