001/*
002 * Copyright 2002-2014 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.util.Collection;
020import java.util.Collections;
021
022import org.springframework.cache.CacheManager;
023import org.springframework.cache.interceptor.AbstractCacheResolver;
024import org.springframework.cache.interceptor.BasicOperation;
025import org.springframework.cache.interceptor.CacheOperationInvocationContext;
026import org.springframework.cache.interceptor.CacheResolver;
027
028/**
029 * A simple {@link CacheResolver} that resolves the exception cache
030 * based on a configurable {@link CacheManager} and the name of the
031 * cache: {@link CacheResultOperation#getExceptionCacheName()}
032 *
033 * @author Stephane Nicoll
034 * @since 4.1
035 * @see CacheResultOperation#getExceptionCacheName()
036 */
037public class SimpleExceptionCacheResolver extends AbstractCacheResolver {
038
039        public SimpleExceptionCacheResolver(CacheManager cacheManager) {
040                super(cacheManager);
041        }
042
043        @Override
044        protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) {
045                BasicOperation operation = context.getOperation();
046                if (!(operation instanceof CacheResultOperation)) {
047                        throw new IllegalStateException("Could not extract exception cache name from " + operation);
048                }
049                CacheResultOperation cacheResultOperation = (CacheResultOperation) operation;
050                String exceptionCacheName = cacheResultOperation.getExceptionCacheName();
051                if (exceptionCacheName != null) {
052                        return Collections.singleton(exceptionCacheName);
053                }
054                return null;
055        }
056
057}