View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.cache.interceptor;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  
23  import org.springframework.beans.factory.InitializingBean;
24  import org.springframework.cache.Cache;
25  import org.springframework.cache.CacheManager;
26  import org.springframework.util.Assert;
27  
28  /**
29   * A base {@link CacheResolver} implementation that requires the concrete
30   * implementation to provide the collection of cache name(s) based on the
31   * invocation context.
32   *
33   * @author Stephane Nicoll
34   * @since 4.1
35   */
36  public abstract class AbstractCacheResolver implements CacheResolver, InitializingBean {
37  
38  	private CacheManager cacheManager;
39  
40  
41  	protected AbstractCacheResolver() {
42  	}
43  
44  	protected AbstractCacheResolver(CacheManager cacheManager) {
45  		this.cacheManager = cacheManager;
46  	}
47  
48  
49  	/**
50  	 * Set the {@link CacheManager} that this instance should use.
51  	 */
52  	public void setCacheManager(CacheManager cacheManager) {
53  		this.cacheManager = cacheManager;
54  	}
55  
56  	/**
57  	 * Return the {@link CacheManager} that this instance use.
58  	 */
59  	public CacheManager getCacheManager() {
60  		return this.cacheManager;
61  	}
62  
63  	@Override
64  	public void afterPropertiesSet()  {
65  		Assert.notNull(this.cacheManager, "CacheManager must not be null");
66  	}
67  
68  
69  	@Override
70  	public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context) {
71  		Collection<String> cacheNames = getCacheNames(context);
72  		if (cacheNames == null) {
73  			return Collections.emptyList();
74  		}
75  		else {
76  			Collection<Cache> result = new ArrayList<Cache>();
77  			for (String cacheName : cacheNames) {
78  				Cache cache = this.cacheManager.getCache(cacheName);
79  				if (cache == null) {
80  					throw new IllegalArgumentException("Cannot find cache named '" +
81  							cacheName + "' for " + context.getOperation());
82  				}
83  				result.add(cache);
84  			}
85  			return result;
86  		}
87  	}
88  
89  	/**
90  	 * Provide the name of the cache(s) to resolve against the current cache manager.
91  	 * <p>It is acceptable to return {@code null} to indicate that no cache could
92  	 * be resolved for this invocation.
93  	 * @param context the context of the particular invocation
94  	 * @return the cache name(s) to resolve or {@code null} if no cache should be resolved
95  	 */
96  	protected abstract Collection<String> getCacheNames(CacheOperationInvocationContext<?> context);
97  
98  }