|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| package com.opensymphony.oscache.web; |
|
6 |
| |
|
7 |
| import com.opensymphony.oscache.base.*; |
|
8 |
| import com.opensymphony.oscache.base.events.CacheEventListener; |
|
9 |
| import com.opensymphony.oscache.base.events.ScopeEvent; |
|
10 |
| import com.opensymphony.oscache.base.events.ScopeEventListener; |
|
11 |
| import com.opensymphony.oscache.base.events.ScopeEventType; |
|
12 |
| |
|
13 |
| import org.apache.commons.logging.Log; |
|
14 |
| import org.apache.commons.logging.LogFactory; |
|
15 |
| |
|
16 |
| import java.io.Serializable; |
|
17 |
| |
|
18 |
| import java.util.*; |
|
19 |
| |
|
20 |
| import javax.servlet.ServletContext; |
|
21 |
| import javax.servlet.http.HttpServletRequest; |
|
22 |
| import javax.servlet.http.HttpSession; |
|
23 |
| import javax.servlet.jsp.PageContext; |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| public class ServletCacheAdministrator extends AbstractCacheAdministrator implements Serializable { |
|
42 |
| private static final transient Log log = LogFactory.getLog(ServletCacheAdministrator.class); |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| private final static String CACHE_USE_HOST_DOMAIN_KEY = "cache.use.host.domain.in.key"; |
|
48 |
| private final static String CACHE_KEY_KEY = "cache.key"; |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| private final static String DEFAULT_CACHE_KEY = "__oscache_cache"; |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| public final static String SESSION_SCOPE_NAME = "session"; |
|
59 |
| public final static String APPLICATION_SCOPE_NAME = "application"; |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| private final static String CACHE_ADMINISTRATOR_KEY = "__oscache_admin"; |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| public final static String HASH_KEY_SCOPE = "scope"; |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| public final static String HASH_KEY_SESSION_ID = "sessionId"; |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| public final static String HASH_KEY_CONTEXT_TMPDIR = "context.tempdir"; |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| private final static String FILE_SEPARATOR = "/"; |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| private final static char FILE_SEPARATOR_CHAR = FILE_SEPARATOR.charAt(0); |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| private final static short AVERAGE_KEY_LENGTH = 30; |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| private static final String m_strBase64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| private Map flushTimes; |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| private transient ServletContext context; |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| private String cacheKey; |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| private boolean useHostDomainInKey = false; |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
0
| private ServletCacheAdministrator(ServletContext context, Properties p) {
|
|
134 |
0
| super(p);
|
|
135 |
0
| config.set(HASH_KEY_CONTEXT_TMPDIR, context.getAttribute("javax.servlet.context.tempdir"));
|
|
136 |
| |
|
137 |
0
| flushTimes = new HashMap();
|
|
138 |
0
| initHostDomainInKey();
|
|
139 |
0
| this.context = context;
|
|
140 |
| } |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
0
| public static ServletCacheAdministrator getInstance(ServletContext context) {
|
|
149 |
0
| return getInstance(context, null);
|
|
150 |
| } |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
0
| public synchronized static ServletCacheAdministrator getInstance(ServletContext context, Properties p) {
|
|
163 |
| |
|
164 |
0
| ServletCacheAdministrator admin = (ServletCacheAdministrator) context.getAttribute(CACHE_ADMINISTRATOR_KEY);
|
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
0
| if (admin == null) {
|
|
169 |
0
| admin = new ServletCacheAdministrator(context, p);
|
|
170 |
0
| context.setAttribute(CACHE_ADMINISTRATOR_KEY, admin);
|
|
171 |
| |
|
172 |
0
| if (log.isInfoEnabled()) {
|
|
173 |
0
| log.info("Created new instance of ServletCacheAdministrator");
|
|
174 |
| } |
|
175 |
| |
|
176 |
0
| admin.getAppScopeCache(context);
|
|
177 |
| } |
|
178 |
| |
|
179 |
0
| if (admin.context == null) {
|
|
180 |
0
| admin.context = context;
|
|
181 |
| } |
|
182 |
| |
|
183 |
0
| return admin;
|
|
184 |
| } |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
0
| public static void destroyInstance(ServletContext context) {
|
|
191 |
0
| ServletCacheAdministrator admin;
|
|
192 |
0
| admin = (ServletCacheAdministrator) context.getAttribute(CACHE_ADMINISTRATOR_KEY);
|
|
193 |
| |
|
194 |
0
| if (admin != null) {
|
|
195 |
| |
|
196 |
0
| Cache cache = (Cache) context.getAttribute(admin.getCacheKey());
|
|
197 |
| |
|
198 |
0
| if (cache != null) {
|
|
199 |
0
| admin.finalizeListeners(cache);
|
|
200 |
0
| context.removeAttribute(admin.getCacheKey());
|
|
201 |
0
| context.removeAttribute(CACHE_ADMINISTRATOR_KEY);
|
|
202 |
0
| cache = null;
|
|
203 |
| |
|
204 |
0
| if (log.isInfoEnabled()) {
|
|
205 |
0
| log.info("Shut down the ServletCacheAdministrator");
|
|
206 |
| } |
|
207 |
| } |
|
208 |
| |
|
209 |
0
| admin = null;
|
|
210 |
| } |
|
211 |
| } |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
0
| public Cache getCache(HttpServletRequest request, int scope) {
|
|
222 |
0
| if (scope == PageContext.APPLICATION_SCOPE) {
|
|
223 |
0
| return getAppScopeCache(context);
|
|
224 |
| } |
|
225 |
| |
|
226 |
0
| if (scope == PageContext.SESSION_SCOPE) {
|
|
227 |
0
| return getSessionScopeCache(request.getSession(true));
|
|
228 |
| } |
|
229 |
| |
|
230 |
0
| throw new RuntimeException("The supplied scope value of " + scope + " is invalid. Acceptable values are PageContext.APPLICATION_SCOPE and PageContext.SESSION_SCOPE");
|
|
231 |
| } |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
0
| public Cache getAppScopeCache(ServletContext context) {
|
|
241 |
0
| Cache cache;
|
|
242 |
0
| Object obj = context.getAttribute(getCacheKey());
|
|
243 |
| |
|
244 |
0
| if ((obj == null) || !(obj instanceof Cache)) {
|
|
245 |
0
| if (log.isInfoEnabled()) {
|
|
246 |
0
| log.info("Created new application-scoped cache at key: " + getCacheKey());
|
|
247 |
| } |
|
248 |
| |
|
249 |
0
| cache = createCache(PageContext.APPLICATION_SCOPE, null);
|
|
250 |
0
| context.setAttribute(getCacheKey(), cache);
|
|
251 |
| } else { |
|
252 |
0
| cache = (Cache) obj;
|
|
253 |
| } |
|
254 |
| |
|
255 |
0
| return cache;
|
|
256 |
| } |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
0
| public Cache getSessionScopeCache(HttpSession session) {
|
|
266 |
0
| Cache cache;
|
|
267 |
0
| Object obj = session.getAttribute(getCacheKey());
|
|
268 |
| |
|
269 |
0
| if ((obj == null) || !(obj instanceof Cache)) {
|
|
270 |
0
| if (log.isInfoEnabled()) {
|
|
271 |
0
| log.info("Created new session-scoped cache in session " + session.getId() + " at key: " + getCacheKey());
|
|
272 |
| } |
|
273 |
| |
|
274 |
0
| cache = createCache(PageContext.SESSION_SCOPE, session.getId());
|
|
275 |
0
| session.setAttribute(getCacheKey(), cache);
|
|
276 |
| } else { |
|
277 |
0
| cache = (Cache) obj;
|
|
278 |
| } |
|
279 |
| |
|
280 |
0
| return cache;
|
|
281 |
| } |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
0
| public String getCacheKey() {
|
|
290 |
0
| if (cacheKey == null) {
|
|
291 |
0
| cacheKey = getProperty(CACHE_KEY_KEY);
|
|
292 |
| |
|
293 |
0
| if (cacheKey == null) {
|
|
294 |
0
| cacheKey = DEFAULT_CACHE_KEY;
|
|
295 |
| } |
|
296 |
| } |
|
297 |
| |
|
298 |
0
| return cacheKey;
|
|
299 |
| } |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
0
| public void setFlushTime(Date date, int scope) {
|
|
308 |
0
| if (log.isInfoEnabled()) {
|
|
309 |
0
| log.info("Flushing scope " + scope + " at " + date);
|
|
310 |
| } |
|
311 |
| |
|
312 |
0
| synchronized (flushTimes) {
|
|
313 |
0
| if (date != null) {
|
|
314 |
| |
|
315 |
0
| dispatchScopeEvent(ScopeEventType.SCOPE_FLUSHED, scope, date, null);
|
|
316 |
0
| flushTimes.put(new Integer(scope), date);
|
|
317 |
| } else { |
|
318 |
0
| logError("setFlushTime called with a null date.");
|
|
319 |
0
| throw new IllegalArgumentException("setFlushTime called with a null date.");
|
|
320 |
| } |
|
321 |
| } |
|
322 |
| } |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
0
| public void setFlushTime(int scope) {
|
|
330 |
0
| setFlushTime(new Date(), scope);
|
|
331 |
| } |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
0
| public Date getFlushTime(int scope) {
|
|
341 |
0
| synchronized (flushTimes) {
|
|
342 |
0
| return (Date) flushTimes.get(new Integer(scope));
|
|
343 |
| } |
|
344 |
| } |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
| |
|
352 |
| |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
0
| public Object getFromCache(int scope, HttpServletRequest request, String key, int refreshPeriod) throws NeedsRefreshException {
|
|
357 |
0
| Cache cache = getCache(request, scope);
|
|
358 |
0
| key = this.generateEntryKey(key, request, scope);
|
|
359 |
0
| return cache.getFromCache(key, refreshPeriod);
|
|
360 |
| } |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
| |
|
368 |
| |
|
369 |
| |
|
370 |
| |
|
371 |
0
| public boolean isScopeFlushed(CacheEntry cacheEntry, int scope) {
|
|
372 |
0
| Date flushDateTime = getFlushTime(scope);
|
|
373 |
| |
|
374 |
0
| if (flushDateTime != null) {
|
|
375 |
0
| long lastUpdate = cacheEntry.getLastUpdate();
|
|
376 |
0
| return (flushDateTime.getTime() >= lastUpdate);
|
|
377 |
| } else { |
|
378 |
0
| return false;
|
|
379 |
| } |
|
380 |
| } |
|
381 |
| |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
| |
|
387 |
0
| public void addScopeEventListener(ScopeEventListener listener) {
|
|
388 |
0
| listenerList.add(ScopeEventListener.class, listener);
|
|
389 |
| } |
|
390 |
| |
|
391 |
| |
|
392 |
| |
|
393 |
| |
|
394 |
| |
|
395 |
| |
|
396 |
| |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
0
| public void cancelUpdate(int scope, HttpServletRequest request, String key) {
|
|
401 |
0
| Cache cache = getCache(request, scope);
|
|
402 |
0
| key = this.generateEntryKey(key, request, scope);
|
|
403 |
0
| cache.cancelUpdate(key);
|
|
404 |
| } |
|
405 |
| |
|
406 |
| |
|
407 |
| |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
0
| public void flushAll(Date date) {
|
|
412 |
0
| synchronized (flushTimes) {
|
|
413 |
0
| setFlushTime(date, PageContext.APPLICATION_SCOPE);
|
|
414 |
0
| setFlushTime(date, PageContext.SESSION_SCOPE);
|
|
415 |
0
| setFlushTime(date, PageContext.REQUEST_SCOPE);
|
|
416 |
0
| setFlushTime(date, PageContext.PAGE_SCOPE);
|
|
417 |
| } |
|
418 |
| |
|
419 |
| |
|
420 |
0
| dispatchScopeEvent(ScopeEventType.ALL_SCOPES_FLUSHED, -1, date, null);
|
|
421 |
| } |
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
0
| public void flushAll() {
|
|
427 |
0
| flushAll(new Date());
|
|
428 |
| } |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
| |
|
433 |
| |
|
434 |
| |
|
435 |
| |
|
436 |
| |
|
437 |
| |
|
438 |
| |
|
439 |
| |
|
440 |
| |
|
441 |
| |
|
442 |
| |
|
443 |
| |
|
444 |
| |
|
445 |
| |
|
446 |
| |
|
447 |
0
| public String generateEntryKey(String key, HttpServletRequest request, int scope) {
|
|
448 |
0
| return generateEntryKey(key, request, scope, null, null);
|
|
449 |
| } |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
| |
|
455 |
| |
|
456 |
| |
|
457 |
| |
|
458 |
| |
|
459 |
| |
|
460 |
| |
|
461 |
| |
|
462 |
| |
|
463 |
| |
|
464 |
| |
|
465 |
| |
|
466 |
| |
|
467 |
| |
|
468 |
| |
|
469 |
0
| public String generateEntryKey(String key, HttpServletRequest request, int scope, String language) {
|
|
470 |
0
| return generateEntryKey(key, request, scope, language, null);
|
|
471 |
| } |
|
472 |
| |
|
473 |
| |
|
474 |
| |
|
475 |
| |
|
476 |
| |
|