1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.cache;
35
36 import info.magnolia.module.cache.executor.Bypass;
37 import info.magnolia.module.cache.listeners.AbstractCacheListener;
38 import info.magnolia.objectfactory.Components;
39 import info.magnolia.repository.RepositoryManager;
40
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Map.Entry;
46
47 import javax.inject.Inject;
48
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52
53
54
55
56
57
58
59
60 public class ContentCachingConfiguration {
61
62 private static final Logger log = LoggerFactory.getLogger(ContentCachingConfiguration.class);
63
64 private CacheModule cacheModule;
65 private RepositoryManager repositoryManager;
66
67 private String name;
68 private String cacheName;
69 private CachePolicy cachePolicy;
70 private FlushPolicy flushPolicy;
71 private BrowserCachePolicy browserCachePolicy;
72
73 private Map<String, CachePolicyExecutor> executors = new HashMap<String, CachePolicyExecutor>();
74 private List<AbstractCacheListener> listeners = new ArrayList<AbstractCacheListener>();
75
76
77
78
79 @Deprecated
80 public ContentCachingConfiguration() {
81 this(Components.getComponent(CacheModule.class), Components.getComponent(RepositoryManager.class));
82 }
83
84 @Inject
85 public ContentCachingConfiguration(CacheModule cacheModule, RepositoryManager repositoryManager) {
86 this.cacheModule = cacheModule;
87 this.repositoryManager = repositoryManager;
88 flushPolicy = new FlushAllListeningPolicy(cacheModule, repositoryManager);
89 }
90
91
92
93
94 ContentCachingConfiguration(String name, CachePolicy cachePolicy, FlushPolicy flushPolicy) {
95 this.name = name;
96 this.cachePolicy = cachePolicy;
97 this.flushPolicy = flushPolicy;
98 }
99
100 public String getName() {
101 return name;
102 }
103
104 public void setName(String name) {
105 this.name = name;
106 }
107
108 public String getCacheName() {
109 return cacheName != null ? cacheName : name;
110 }
111
112 public void setCacheName(String cacheName) {
113 this.cacheName = cacheName;
114 }
115
116 public CachePolicy getCachePolicy() {
117 if (cachePolicy != null) {
118 return cachePolicy;
119 }
120
121 log.error("Check your cache configuration! CachePolicy is probably not set! Falling back Never cache policy.");
122 return new info.magnolia.module.cache.cachepolicy.Never();
123 }
124
125 public void setCachePolicy(CachePolicy cachePolicy) {
126 this.cachePolicy = cachePolicy;
127 }
128
129 public FlushPolicy getFlushPolicy() {
130 return flushPolicy;
131 }
132
133 public void setFlushPolicy(FlushPolicy flushPolicy) {
134 this.flushPolicy = flushPolicy;
135 }
136
137 public CachePolicyExecutor getExecutor(CachePolicyResult.CachePolicyBehaviour behaviour) {
138 if (executors.containsKey(behaviour.getName())) {
139 return executors.get(behaviour.getName());
140 }
141
142 log.error("Check your cache configuration! Executor " + behaviour.getName() + " is probably not set! Bypassing cache.");
143 return new Bypass();
144 }
145
146 public BrowserCachePolicy getBrowserCachePolicy() {
147 if (browserCachePolicy != null) {
148 return browserCachePolicy;
149 }
150
151 log.error("Check your cache configuration! BrowserCachePolicy is probably not set! Falling back to Never cache policy.");
152 return new info.magnolia.module.cache.browsercachepolicy.Never();
153 }
154
155 public void setBrowserCachePolicy(BrowserCachePolicy browserCachePolicy) {
156 this.browserCachePolicy = browserCachePolicy;
157 }
158
159 public Map<String, CachePolicyExecutor> getExecutors() {
160 return executors;
161 }
162
163 public void setExecutors(Map<String, CachePolicyExecutor> executors) {
164 for (Entry<String, CachePolicyExecutor> executor : executors.entrySet()) {
165 this.addExecutor(executor.getKey(), executor.getValue());
166 }
167 }
168
169 public void addExecutor(String name, CachePolicyExecutor executor) {
170 executor.setContentCachingConfiguration(this);
171 this.executors.put(name, executor);
172 }
173
174 public List<AbstractCacheListener> getListeners() {
175 return listeners;
176 }
177
178 public void setListeners(List<AbstractCacheListener> listeners) {
179 this.listeners = listeners;
180 }
181
182 public AbstractCacheListener getListener(String name) {
183 for (AbstractCacheListener listener : getListeners()) {
184 if (name.equals(listener.getName())) {
185 return listener;
186 }
187 }
188 return null;
189 }
190
191 }