|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| package com.opensymphony.oscache.base; |
|
6 |
| |
|
7 |
| import com.opensymphony.oscache.base.algorithm.AbstractConcurrentReadCache; |
|
8 |
| import com.opensymphony.oscache.base.events.*; |
|
9 |
| import com.opensymphony.oscache.base.persistence.PersistenceListener; |
|
10 |
| import com.opensymphony.oscache.util.StringUtil; |
|
11 |
| |
|
12 |
| import org.apache.commons.logging.Log; |
|
13 |
| import org.apache.commons.logging.LogFactory; |
|
14 |
| |
|
15 |
| import java.util.*; |
|
16 |
| |
|
17 |
| import javax.swing.event.EventListenerList; |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| public abstract class AbstractCacheAdministrator implements java.io.Serializable { |
|
34 |
| private static transient final Log log = LogFactory.getLog(AbstractCacheAdministrator.class); |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| public final static String CACHE_MEMORY_KEY = "cache.memory"; |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| public final static String CACHE_CAPACITY_KEY = "cache.capacity"; |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| public final static String CACHE_ALGORITHM_KEY = "cache.algorithm"; |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| public final static String CACHE_DISK_UNLIMITED_KEY = "cache.unlimited.disk"; |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| public final static String CACHE_BLOCKING_KEY = "cache.blocking"; |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| public static final String PERSISTENCE_CLASS_KEY = "cache.persistence.class"; |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| public static final String CACHE_PERSISTENCE_OVERFLOW_KEY = "cache.persistence.overflow.only"; |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| public static final String CACHE_ENTRY_EVENT_LISTENERS_KEY = "cache.event.listeners"; |
|
96 |
| protected Config config = null; |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| protected EventListenerList listenerList = new EventListenerList(); |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| protected String algorithmClass = null; |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| protected int cacheCapacity = -1; |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| private boolean blocking = false; |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| private boolean memoryCaching = true; |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| private boolean overflowPersistence; |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| private boolean unlimitedDiskCache; |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
0
| protected AbstractCacheAdministrator() {
|
|
147 |
0
| this(null);
|
|
148 |
| } |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
195
| protected AbstractCacheAdministrator(Properties p) {
|
|
156 |
195
| loadProps(p);
|
|
157 |
195
| initCacheParameters();
|
|
158 |
| |
|
159 |
195
| if (log.isDebugEnabled()) {
|
|
160 |
0
| log.debug("Constructed AbstractCacheAdministrator()");
|
|
161 |
| } |
|
162 |
| } |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
0
| public void setAlgorithmClass(String newAlgorithmClass) {
|
|
174 |
0
| algorithmClass = newAlgorithmClass;
|
|
175 |
| } |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
225
| public boolean isBlocking() {
|
|
184 |
225
| return blocking;
|
|
185 |
| } |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
20
| protected void setCacheCapacity(int newCacheCapacity) {
|
|
195 |
20
| cacheCapacity = newCacheCapacity;
|
|
196 |
| } |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
235
| public boolean isMemoryCaching() {
|
|
206 |
235
| return memoryCaching;
|
|
207 |
| } |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
795
| public String getProperty(String key) {
|
|
216 |
795
| return config.getProperty(key);
|
|
217 |
| } |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
235
| public boolean isUnlimitedDiskCache() {
|
|
223 |
235
| return unlimitedDiskCache;
|
|
224 |
| } |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
235
| public boolean isOverflowPersistence() {
|
|
232 |
235
| return this.overflowPersistence;
|
|
233 |
| } |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
0
| public void setOverflowPersistence(boolean overflowPersistence) {
|
|
241 |
0
| this.overflowPersistence = overflowPersistence;
|
|
242 |
| } |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
29
| protected CacheEventListener[] getCacheEventListeners() {
|
|
249 |
29
| List classes = StringUtil.split(config.getProperty(CACHE_ENTRY_EVENT_LISTENERS_KEY), ',');
|
|
250 |
29
| CacheEventListener[] listeners = new CacheEventListener[classes.size()];
|
|
251 |
| |
|
252 |
29
| for (int i = 0; i < classes.size(); i++) {
|
|
253 |
29
| String className = (String) classes.get(i);
|
|
254 |
| |
|
255 |
29
| try {
|
|
256 |
29
| Class clazz = Class.forName(className);
|
|
257 |
| |
|
258 |
29
| if (!CacheEventListener.class.isAssignableFrom(clazz)) {
|
|
259 |
0
| log.error("Specified listener class '" + className + "' does not implement CacheEventListener. Ignoring this listener.");
|
|
260 |
| } else { |
|
261 |
29
| listeners[i] = (CacheEventListener) clazz.newInstance();
|
|
262 |
| } |
|
263 |
| } catch (ClassNotFoundException e) { |
|
264 |
0
| log.error("CacheEventListener class '" + className + "' not found. Ignoring this listener.", e);
|
|
265 |
| } catch (InstantiationException e) { |
|
266 |
0
| log.error("CacheEventListener class '" + className + "' could not be instantiated because it is not a concrete class. Ignoring this listener.", e);
|
|
267 |
| } catch (IllegalAccessException e) { |
|
268 |
0
| log.error("CacheEventListener class '" + className + "' could not be instantiated because it is not public. Ignoring this listener.", e);
|
|
269 |
| } |
|
270 |
| } |
|
271 |
| |
|
272 |
29
| return listeners;
|
|
273 |
| } |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
116
| protected Cache setPersistenceListener(Cache cache) {
|
|
289 |
116
| String persistenceClassname = config.getProperty(PERSISTENCE_CLASS_KEY);
|
|
290 |
| |
|
291 |
116
| try {
|
|
292 |
116
| Class clazz = Class.forName(persistenceClassname);
|
|
293 |
116
| PersistenceListener persistenceListener = (PersistenceListener) clazz.newInstance();
|
|
294 |
| |
|
295 |
116
| cache.setPersistenceListener(persistenceListener.configure(config));
|
|
296 |
| } catch (ClassNotFoundException e) { |
|
297 |
0
| log.error("PersistenceListener class '" + persistenceClassname + "' not found. Check your configuration.", e);
|
|
298 |
| } catch (Exception e) { |
|
299 |
0
| log.error("Error instantiating class '" + persistenceClassname + "'", e);
|
|
300 |
| } |
|
301 |
| |
|
302 |
116
| return cache;
|
|
303 |
| } |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
195
| protected Cache configureStandardListeners(Cache cache) {
|
|
314 |
195
| if (config.getProperty(PERSISTENCE_CLASS_KEY) != null) {
|
|
315 |
116
| cache = setPersistenceListener(cache);
|
|
316 |
| } |
|
317 |
| |
|
318 |
195
| if (config.getProperty(CACHE_ENTRY_EVENT_LISTENERS_KEY) != null) {
|
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
29
| CacheEventListener[] listeners = getCacheEventListeners();
|
|
323 |
| |
|
324 |
29
| for (int i = 0; i < listeners.length; i++) {
|
|
325 |
| |
|
326 |
29
| if (listeners[i] instanceof LifecycleAware) {
|
|
327 |
0
| try {
|
|
328 |
0
| ((LifecycleAware) listeners[i]).initialize(cache, config);
|
|
329 |
| } catch (InitializationException e) { |
|
330 |
0
| log.error("Could not initialize listener '" + listeners[i].getClass().getName() + "'. Listener ignored.", e);
|
|
331 |
| |
|
332 |
0
| continue;
|
|
333 |
| } |
|
334 |
| } |
|
335 |
| |
|
336 |
29
| if (listeners[i] instanceof CacheEntryEventListener) {
|
|
337 |
29
| cache.addCacheEventListener(listeners[i]);
|
|
338 |
0
| } else if (listeners[i] instanceof CacheMapAccessEventListener) {
|
|
339 |
0
| cache.addCacheEventListener(listeners[i]);
|
|
340 |
| } |
|
341 |
| } |
|
342 |
| } |
|
343 |
| |
|
344 |
195
| return cache;
|
|
345 |
| } |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
| |
|
352 |
10
| protected void finalizeListeners(Cache cache) {
|
|
353 |
| |
|
354 |
10
| if (cache == null) {
|
|
355 |
0
| return;
|
|
356 |
| } |
|
357 |
| |
|
358 |
10
| Object[] listeners = cache.listenerList.getListenerList();
|
|
359 |
| |
|
360 |
10
| for (int i = listeners.length - 2; i >= 0; i -= 2) {
|
|
361 |
1
| if (listeners[i + 1] instanceof LifecycleAware) {
|
|
362 |
0
| try {
|
|
363 |
0
| ((LifecycleAware) listeners[i + 1]).finialize();
|
|
364 |
| } catch (FinalizationException e) { |
|
365 |
0
| log.error("Listener could not be finalized", e);
|
|
366 |
| } |
|
367 |
| } |
|
368 |
| } |
|
369 |
| } |
|
370 |
| |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
| |
|
378 |
| |
|
379 |
| |
|
380 |
| |
|
381 |
| |
|
382 |
195
| private void initCacheParameters() {
|
|
383 |
195
| algorithmClass = getProperty(CACHE_ALGORITHM_KEY);
|
|
384 |
| |
|
385 |
195
| blocking = "true".equalsIgnoreCase(getProperty(CACHE_BLOCKING_KEY));
|
|
386 |
| |
|
387 |
195
| String cacheMemoryStr = getProperty(CACHE_MEMORY_KEY);
|
|
388 |
| |
|
389 |
195
| if ((cacheMemoryStr != null) && cacheMemoryStr.equalsIgnoreCase("false")) {
|
|
390 |
58
| memoryCaching = false;
|
|
391 |
| } |
|
392 |
| |
|
393 |
195
| unlimitedDiskCache = Boolean.valueOf(config.getProperty(CACHE_DISK_UNLIMITED_KEY)).booleanValue();
|
|
394 |
195
| overflowPersistence = Boolean.valueOf(config.getProperty(CACHE_PERSISTENCE_OVERFLOW_KEY)).booleanValue();
|
|
395 |
| |
|
396 |
195
| String cacheSize = getProperty(CACHE_CAPACITY_KEY);
|
|
397 |
| |
|
398 |
195
| try {
|
|
399 |
195
| if ((cacheSize != null) && (cacheSize.length() > 0)) {
|
|
400 |
88
| cacheCapacity = Integer.parseInt(cacheSize);
|
|
401 |
| } |
|
402 |
| } catch (NumberFormatException e) { |
|
403 |
0
| log.error("The value supplied for the cache capacity, '" + cacheSize + "', is not a valid number. The cache capacity setting is being ignored.");
|
|
404 |
| } |
|
405 |
| } |
|
406 |
| |
|
407 |
| |
|
408 |
| |
|
409 |
| |
|
410 |
195
| private void loadProps(Properties p) {
|
|
411 |
195
| config = new Config(p);
|
|
412 |
| } |
|
413 |
| } |