KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > web > tag > FlushTag


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

5 package com.opensymphony.oscache.web.tag;
6
7 import com.opensymphony.oscache.base.Cache;
8 import com.opensymphony.oscache.web.ServletCacheAdministrator;
9
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.jsp.JspTagException JavaDoc;
12 import javax.servlet.jsp.PageContext JavaDoc;
13 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
14
15 /**
16  * FlushTag flushes caches created with <cache>.
17  *
18  * This tag provides programmatic control over when caches are flushed,
19  * and can flush all caches at once.<p>
20  *
21  * Usage Examples:
22  * <pre><code>
23  * &lt;%@ taglib uri="oscache" prefix="cache" %&gt;
24  * &lt;cache:flush scope="application" /&gt;
25  * &lt;cache:flush scope="session" key="foobar" /&gt;
26  * </code></pre>
27  *
28  * Note: If no scope is provided (or scope is null), it will flush
29  * all caches globally - use with care!<p>
30  * <p>
31  * Flushing is done by setting an appropriate application level time,
32  * which &lt;cache&gt; always looks at before retrieving the cache.
33  * If this 'flush time' is &gt; that cache's last update, it will refresh
34  * the cache.
35  * <p>
36  * As such caches are not all 'flushed', they are all marked
37  * to be refreshed at their next access. That is the only way that
38  * the content can still be available if the refresh fails.
39  *
40  * @author <a HREF="mailto:mike@atlassian.com">Mike Cannon-Brookes</a>
41  * @author <a HREF="&#109;a&#105;&#108;&#116;&#111;:chris&#64;swebtec.&#99;&#111;&#109;">Chris Miller</a>
42  * @version $Revision: 1.1 $
43  */

44 public class FlushTag extends TagSupport JavaDoc {
45     ServletCacheAdministrator admin = null;
46
47     /**
48      * A cache group.
49      * If specified, all content in that group will be flushed
50      */

51     String JavaDoc group = null;
52
53     /**
54      * Tag key.
55      */

56     String JavaDoc key = null;
57
58     /**
59      * if pattern value is specified, all keys that contain the pattern are flushed.
60      */

61     String JavaDoc pattern = null;
62     String JavaDoc scope = null;
63     int cacheScope = -1;
64
65     /**
66      * The ISO-639 language code to distinguish different pages in application scope.
67      */

68     private String JavaDoc language = null;
69
70     /**
71      * The group to be flushed.
72      * If specified, all cached content in the group will be flushed.
73      *
74      * @param group The name of the group to flush.
75      */

76     public void setGroup(String JavaDoc group) {
77         this.group = group;
78     }
79
80     /**
81      * The key to be flushed.
82      * If specified, only one cache entry will be flushed.
83      *
84      * @param value The key of the specific entry to flush.
85      */

86     public void setKey(String JavaDoc value) {
87         this.key = value;
88     }
89
90     /**
91      * Set the ISO-639 language code to distinguish different pages in application scope.
92      *
93      * @param value The language code for this cache entry.
94      */

95     public void setLanguage(String JavaDoc value) {
96         this.language = value;
97     }
98
99     /**
100      * The key pattern to be flushed.
101      * If specified, all entries that contain the pattern will be flushed.
102      * @param value The key of the specific entry to flush.
103      */

104     public void setPattern(String JavaDoc value) {
105         this.pattern = value;
106     }
107
108     /**
109      * Set the scope of this flush.
110      *
111      * @param value The scope - either "application" (default) or "session".
112      */

113     public void setScope(String JavaDoc value) {
114         if (value != null) {
115             if (value.equalsIgnoreCase(ServletCacheAdministrator.SESSION_SCOPE_NAME)) {
116                 cacheScope = PageContext.SESSION_SCOPE;
117             } else if (value.equalsIgnoreCase(ServletCacheAdministrator.APPLICATION_SCOPE_NAME)) {
118                 cacheScope = PageContext.APPLICATION_SCOPE;
119             }
120         }
121     }
122
123     /**
124      * Process the start of the tag.
125      *
126      * @throws JspTagException The standard tag exception thrown.
127      * @return The standard Tag return.
128      */

129     public int doStartTag() throws JspTagException JavaDoc {
130         if (admin == null) {
131             admin = ServletCacheAdministrator.getInstance(pageContext.getServletContext());
132         }
133
134         if (group != null) // We're flushing a group
135
{
136             if (cacheScope >= 0) {
137                 Cache cache = admin.getCache((HttpServletRequest JavaDoc) pageContext.getRequest(), cacheScope);
138                 cache.flushGroup(group);
139             } else {
140                 throw new JspTagException JavaDoc("A cache group was specified for flushing, but the scope wasn't supplied or was invalid");
141             }
142         } else if (pattern != null) // We're flushing keys which contain the pattern
143
{
144             if (cacheScope >= 0) {
145                 Cache cache = admin.getCache((HttpServletRequest JavaDoc) pageContext.getRequest(), cacheScope);
146                 cache.flushPattern(pattern);
147             } else {
148                 throw new JspTagException JavaDoc("A pattern was specified for flushing, but the scope wasn't supplied or was invalid");
149             }
150         } else if (key == null) // we're flushing a whole scope
151
{
152             if (cacheScope >= 0) {
153                 admin.setFlushTime(cacheScope);
154             } else {
155                 admin.flushAll();
156             }
157         } else // we're flushing just one key
158
{
159             if (cacheScope >= 0) {
160                 String JavaDoc actualKey = admin.generateEntryKey(key, (HttpServletRequest JavaDoc) pageContext.getRequest(), cacheScope, language);
161
162                 Cache cache = admin.getCache((HttpServletRequest JavaDoc) pageContext.getRequest(), cacheScope);
163                 cache.flushEntry(actualKey);
164             } else {
165                 throw new JspTagException JavaDoc("A cache key was specified for flushing, but the scope wasn't supplied or was invalid");
166             }
167         }
168
169         return SKIP_BODY;
170     }
171 }
172
Popular Tags