1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32
33
34
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
51
52 public void setCacheManager(CacheManager cacheManager) {
53 this.cacheManager = cacheManager;
54 }
55
56
57
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
91
92
93
94
95
96 protected abstract Collection<String> getCacheNames(CacheOperationInvocationContext<?> context);
97
98 }