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.interceptor; 018 019import java.util.ArrayList; 020import java.util.Collection; 021import java.util.Collections; 022 023import org.springframework.beans.factory.InitializingBean; 024import org.springframework.cache.Cache; 025import org.springframework.cache.CacheManager; 026import org.springframework.lang.Nullable; 027import org.springframework.util.Assert; 028 029/** 030 * A base {@link CacheResolver} implementation that requires the concrete 031 * implementation to provide the collection of cache name(s) based on the 032 * invocation context. 033 * 034 * @author Stephane Nicoll 035 * @author Juergen Hoeller 036 * @since 4.1 037 */ 038public abstract class AbstractCacheResolver implements CacheResolver, InitializingBean { 039 040 @Nullable 041 private CacheManager cacheManager; 042 043 044 /** 045 * Construct a new {@code AbstractCacheResolver}. 046 * @see #setCacheManager 047 */ 048 protected AbstractCacheResolver() { 049 } 050 051 /** 052 * Construct a new {@code AbstractCacheResolver} for the given {@link CacheManager}. 053 * @param cacheManager the CacheManager to use 054 */ 055 protected AbstractCacheResolver(CacheManager cacheManager) { 056 this.cacheManager = cacheManager; 057 } 058 059 060 /** 061 * Set the {@link CacheManager} that this instance should use. 062 */ 063 public void setCacheManager(CacheManager cacheManager) { 064 this.cacheManager = cacheManager; 065 } 066 067 /** 068 * Return the {@link CacheManager} that this instance uses. 069 */ 070 public CacheManager getCacheManager() { 071 Assert.state(this.cacheManager != null, "No CacheManager set"); 072 return this.cacheManager; 073 } 074 075 @Override 076 public void afterPropertiesSet() { 077 Assert.notNull(this.cacheManager, "CacheManager is required"); 078 } 079 080 081 @Override 082 public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context) { 083 Collection<String> cacheNames = getCacheNames(context); 084 if (cacheNames == null) { 085 return Collections.emptyList(); 086 } 087 Collection<Cache> result = new ArrayList<>(cacheNames.size()); 088 for (String cacheName : cacheNames) { 089 Cache cache = getCacheManager().getCache(cacheName); 090 if (cache == null) { 091 throw new IllegalArgumentException("Cannot find cache named '" + 092 cacheName + "' for " + context.getOperation()); 093 } 094 result.add(cache); 095 } 096 return result; 097 } 098 099 /** 100 * Provide the name of the cache(s) to resolve against the current cache manager. 101 * <p>It is acceptable to return {@code null} to indicate that no cache could 102 * be resolved for this invocation. 103 * @param context the context of the particular invocation 104 * @return the cache name(s) to resolve, or {@code null} if no cache should be resolved 105 */ 106 @Nullable 107 protected abstract Collection<String> getCacheNames(CacheOperationInvocationContext<?> context); 108 109}