KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > web > taglibs > cache > FlushTag


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.appserv.web.taglibs.cache;
25
26 import java.util.ResourceBundle JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28 import java.util.logging.Level JavaDoc;
29
30 import javax.servlet.jsp.*;
31 import javax.servlet.jsp.tagext.*;
32
33 import com.sun.enterprise.web.logging.pwc.LogDomains;
34
35 import com.sun.appserv.util.cache.Cache;
36
37 /**
38  * FlushTag is a JSP tag that is used with the CacheTag. The FlushTag
39  * allows you to invalidate a complete cache or a particular cache element
40  * identified by the key.
41  *
42  * Usage Example:
43  * <%@ taglib prefix="ias" uri="Sun ONE Application Server Tags" %>
44  * <ias:flush key="<%= cacheKey %>" />
45  */

46 public class FlushTag extends TagSupport
47 {
48     /**
49      * The key for the cache entry that needs to be flushed.
50      */

51     private String JavaDoc _key;
52
53     /**
54      * This specifies the scope of the cache that needs to be flushed.
55      */

56     private int _scope = PageContext.APPLICATION_SCOPE;
57
58     /**
59      * The logger to use for logging ALL web container related messages.
60      */

61     private static Logger JavaDoc _logger = null;
62
63     /**
64      * This indicates whether debug logging is on or not
65      */

66     private static boolean _debugLog;
67
68     /**
69      * The resource bundle containing the localized message strings.
70      */

71     private static ResourceBundle JavaDoc _rb = null;
72
73     // ---------------------------------------------------------------------
74
// Constructor and initialization
75

76     /**
77      * Default constructor that simply gets a handle to the web container
78      * subsystem's logger.
79      */

80     public FlushTag() {
81         super();
82          if (_logger == null) {
83              _logger = LogDomains.getLogger(LogDomains.PWC_LOGGER);
84              _rb = _logger.getResourceBundle();
85              _debugLog = _logger.isLoggable(Level.FINE);
86          }
87     }
88
89     // ---------------------------------------------------------------------
90
// Tag logic
91

92     /**
93      * doStartTag is called when the flush tag is encountered. By
94      * the time this is called, the tag attributes are already set.
95      *
96      * @throws JspException the standard exception thrown
97      * @return SKIP_BODY since the tag should be empty
98      */

99     public int doStartTag()
100         throws JspException
101     {
102         // get the cache from the specified scope
103
Cache cache = CacheUtil.getCache(pageContext, _scope);
104
105         // generate the cache key using the user specified key.
106

107         if (_key != null) {
108             String JavaDoc key = CacheUtil.generateKey(_key, pageContext);
109
110             // remove the entry for the key
111
cache.remove(key);
112
113             if (_debugLog)
114                 _logger.fine("FlushTag: clear ["+ key +"]");
115         } else {
116             // clear the entire cache
117
cache.clear();
118
119             if (_debugLog)
120                 _logger.fine("FlushTag: clear cache");
121         }
122
123         return SKIP_BODY;
124     }
125
126     /**
127      * doEndTag just resets all the valiables in case the tag is reused
128      *
129      * @throws JspException the standard exception thrown
130      * @return always returns EVAL_PAGE since we want the entire jsp evaluated
131      */

132     public int doEndTag()
133         throws JspException
134     {
135         _key = null;
136         _scope = PageContext.APPLICATION_SCOPE;
137
138         return EVAL_PAGE;
139     }
140
141     // ---------------------------------------------------------------------
142
// Attribute setters
143

144     /**
145      * This is set a key for the cache element that needs to be cleared
146      */

147     public void setKey(String JavaDoc key) {
148         if (key != null && key.length() > 0)
149             _key = key;
150     }
151
152     /**
153      * Sets the scope of the cache.
154      *
155      * @param scope the scope of the cache
156      *
157      * @throws IllegalArgumentException if the specified scope is different
158      * from request, session, and application
159      */

160     public void setScope(String JavaDoc scope) {
161         _scope = CacheUtil.convertScope(scope);
162     }
163 }
164
Popular Tags