Clover coverage report - Coverage timestamp: do jan 22 2004 21:12:32 CET FRAMES NO FRAMES |
file stats: | LOC: | 172 |  | Methods: | 6
| NCLOC: | 76 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
FlushTag.java | 0% | 0% | 0% | 0% |

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;
11 |  |
     import javax.servlet.jsp.JspTagException;
12 |  |
     import javax.servlet.jsp.PageContext;
13 |  |
     import javax.servlet.jsp.tagext.TagSupport;
14 |  |
15 |  |
     /**
16 |  |
      * FlushTag flushes caches created with &lt;cache&gt;.
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.2 $
43 |  |
      */
44 |  |
     public class FlushTag extends TagSupport {
45 |  |
         ServletCacheAdministrator admin = null;
46 |  |
47 |  |
         /**
48 |  |
          * A cache group.
49 |  |
          * If specified, all content in that group will be flushed
50 |  |
          */
51 |  |
         String group = null;
52 |  |
53 |  |
         /**
54 |  |
          * Tag key.
55 |  |
          */
56 |  |
         String key = null;
57 |  |
58 |  |
         /**
59 |  |
          * if pattern value is specified, all keys that contain the pattern are flushed.
60 |  |
          */
61 |  |
         String pattern = null;
62 |  |
         String 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 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 | 0 |
         public void setGroup(String group) {
77 | 0 |
             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 | 0 |
         public void setKey(String value) {
87 | 0 |
             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 | 0 |
         public void setLanguage(String value) {
96 | 0 |
             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 | 0 |
         public void setPattern(String value) {
105 | 0 |
             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 | 0 |
         public void setScope(String value) {
114 | 0 |
             if (value != null) {
115 | 0 |
                 if (value.equalsIgnoreCase(ServletCacheAdministrator.SESSION_SCOPE_NAME)) {
116 | 0 |
                     cacheScope = PageContext.SESSION_SCOPE;
117 | 0 |
                 } else if (value.equalsIgnoreCase(ServletCacheAdministrator.APPLICATION_SCOPE_NAME)) {
118 | 0 |
                     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 | 0 |
         public int doStartTag() throws JspTagException {
130 | 0 |
             if (admin == null) {
131 | 0 |
                 admin = ServletCacheAdministrator.getInstance(pageContext.getServletContext());
132 |  |
             }
133 |  |
134 | 0 |
             if (group != null) // We're flushing a group
135 |  |
              {
136 | 0 |
                 if (cacheScope >= 0) {
137 | 0 |
                     Cache cache = admin.getCache((HttpServletRequest) pageContext.getRequest(), cacheScope);
138 | 0 |
                     cache.flushGroup(group);
139 |  |
                 } else {
140 | 0 |
                     throw new JspTagException("A cache group was specified for flushing, but the scope wasn't supplied or was invalid");
141 |  |
                 }
142 | 0 |
             } else if (pattern != null) // We're flushing keys which contain the pattern
143 |  |
              {
144 | 0 |
                 if (cacheScope >= 0) {
145 | 0 |
                     Cache cache = admin.getCache((HttpServletRequest) pageContext.getRequest(), cacheScope);
146 | 0 |
                     cache.flushPattern(pattern);
147 |  |
                 } else {
148 | 0 |
                     throw new JspTagException("A pattern was specified for flushing, but the scope wasn't supplied or was invalid");
149 |  |
                 }
150 | 0 |
             } else if (key == null) // we're flushing a whole scope
151 |  |
              {
152 | 0 |
                 if (cacheScope >= 0) {
153 | 0 |
                     admin.setFlushTime(cacheScope);
154 |  |
                 } else {
155 | 0 |
                     admin.flushAll();
156 |  |
                 }
157 |  |
             } else // we're flushing just one key
158 |  |
              {
159 | 0 |
                 if (cacheScope >= 0) {
160 | 0 |
                     String actualKey = admin.generateEntryKey(key, (HttpServletRequest) pageContext.getRequest(), cacheScope, language);
161 |  |
162 | 0 |
                     Cache cache = admin.getCache((HttpServletRequest) pageContext.getRequest(), cacheScope);
163 | 0 |
                     cache.flushEntry(actualKey);
164 |  |
                 } else {
165 | 0 |
                     throw new JspTagException("A cache key was specified for flushing, but the scope wasn't supplied or was invalid");
166 |  |
                 }
167 |  |
             }
168 |  |
169 | 0 |
             return SKIP_BODY;
170 |  |
         }
171 |  |
     }
172 |  |