KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > web > ServletCache


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.oscache.web;
6
7 import com.opensymphony.oscache.base.Cache;
8 import com.opensymphony.oscache.base.CacheEntry;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 import java.io.Serializable JavaDoc;
14
15 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
16 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
17
18 /**
19  * A simple extension of Cache that implements a session binding listener,
20  * and deletes it's entries when unbound
21  *
22  * @author <a HREF="mailto:mike@atlassian.com">Mike Cannon-Brookes</a>
23  * @author <a HREF="mailto:tgochenour@peregrine.com">Todd Gochenour</a>
24  * @author <a HREF="mailto:fbeauregard@pyxis-tech.com">Francois Beauregard</a>
25  * @version $Revision: 1.3 $
26  */

27 public final class ServletCache extends Cache implements HttpSessionBindingListener JavaDoc, Serializable JavaDoc {
28     private static transient final Log log = LogFactory.getLog(ServletCache.class);
29
30     /**
31      * The admin for this cache
32      */

33     private ServletCacheAdministrator admin;
34
35     /**
36      * The scope of that cache.
37      */

38     private int scope;
39
40     /**
41      * Create a new ServletCache
42      *
43      * @param admin The ServletCacheAdministrator to administer this ServletCache.
44      * @param scope The scope of all entries in this hashmap
45      */

46     public ServletCache(ServletCacheAdministrator admin, int scope) {
47         super(admin.isMemoryCaching(), admin.isUnlimitedDiskCache(), admin.isOverflowPersistence());
48         setScope(scope);
49         this.admin = admin;
50     }
51
52     /**
53      * Create a new Cache
54      *
55      * @param admin The CacheAdministrator to administer this Cache.
56      * @param algorithmClass The class that implement an algorithm
57      * @param limit The maximum cache size in number of entries
58      * @param scope The cache scope
59      */

60     public ServletCache(ServletCacheAdministrator admin, String JavaDoc algorithmClass, int limit, int scope) {
61         super(admin.isMemoryCaching(), admin.isUnlimitedDiskCache(), admin.isOverflowPersistence(), admin.isBlocking(), algorithmClass, limit);
62         setScope(scope);
63         this.admin = admin;
64     }
65
66     /**
67      * Get the cache scope
68      *
69      * @return The cache scope
70      */

71     public int getScope() {
72         return scope;
73     }
74
75     private void setScope(int scope) {
76         this.scope = scope;
77     }
78
79     /**
80      * When this Cache is bound to the session, do nothing.
81      *
82      * @param event The SessionBindingEvent.
83      */

84     public void valueBound(HttpSessionBindingEvent JavaDoc event) {
85     }
86
87     /**
88      * When the users's session ends, all listeners are finalized and the
89      * session cache directory is deleted from disk.
90      *
91      * @param event The event that triggered this unbinding.
92      */

93     public void valueUnbound(HttpSessionBindingEvent JavaDoc event) {
94         if (log.isInfoEnabled()) {
95             // CACHE-229: don't access the session's id, because this can throw an IllegalStateException
96
log.info("[Cache] Unbound from session " + event.getSession() + " using name " + event.getName());
97         }
98
99         admin.finalizeListeners(this);
100         clear();
101     }
102
103     /**
104      * Indicates whether or not the cache entry is stale. This overrides the
105      * {@link Cache#isStale(CacheEntry, int, String)} method to take into account any
106      * flushing that may have been applied to the scope that this cache belongs to.
107      *
108      * @param cacheEntry The cache entry to test the freshness of.
109      * @param refreshPeriod The maximum allowable age of the entry, in seconds.
110      * @param cronExpiry A cron expression that defines fixed expiry dates and/or
111      * times for this cache entry.
112      *
113      * @return <code>true</code> if the entry is stale, <code>false</code> otherwise.
114      */

115     protected boolean isStale(CacheEntry cacheEntry, int refreshPeriod, String JavaDoc cronExpiry) {
116         return super.isStale(cacheEntry, refreshPeriod, cronExpiry) || admin.isScopeFlushed(cacheEntry, scope);
117     }
118 }
119
Popular Tags