KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > cache > InvalidateTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.cache;
18
19 import javax.servlet.jsp.*;
20 import javax.servlet.jsp.tagext.*;
21 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
22
23 /**
24  * <p>Invalidates a particular cache entry, or an entire cache.</p>
25  *
26  * @author Shawn Bayern
27  */

28
29 public class InvalidateTag extends TagSupport {
30
31     //*********************************************************************
32
// Private state
33

34     String JavaDoc nameExpr; // tag attribute
35
String JavaDoc keyExpr; // tag attribute
36

37     int scope; // parsed tag attribute
38
String JavaDoc name, key; // parsed tag attribute
39

40     //*********************************************************************
41
// Tag logic
42

43     public int doStartTag() throws JspException {
44       evaluateExpressions();
45       if (keyExpr == null || key == null) {
46         // invalidate whole cache
47
CacheUtil.invalidateCache(scope, name, pageContext);
48       } else {
49         // invalidate cache item
50
CacheUtil.invalidateCachedItem(scope, name, key, pageContext);
51       }
52       return SKIP_BODY;
53     }
54
55     //*********************************************************************
56
// Attribute accessors
57

58     public void setScope(String JavaDoc scope) {
59       if (scope.equalsIgnoreCase("page"))
60     this.scope = PageContext.PAGE_SCOPE;
61       else if (scope.equalsIgnoreCase("request"))
62         this.scope = PageContext.REQUEST_SCOPE;
63       else if (scope.equalsIgnoreCase("session"))
64         this.scope = PageContext.SESSION_SCOPE;
65       else if (scope.equalsIgnoreCase("application"))
66         this.scope = PageContext.APPLICATION_SCOPE;
67       else
68         throw new IllegalArgumentException JavaDoc("invalid scope");
69     }
70
71     public void setName(String JavaDoc nameExpr) {
72       this.nameExpr = nameExpr;
73     }
74
75     public void setKey(String JavaDoc keyExpr) {
76       this.keyExpr = keyExpr;
77     }
78
79
80     //*********************************************************************
81
// Constructor and initialization
82

83     public InvalidateTag() {
84       super();
85       init();
86     }
87
88     private void init() {
89       scope = PageContext.APPLICATION_SCOPE;
90       name = nameExpr = null;
91       key = keyExpr = null;
92     }
93     
94
95     //*********************************************************************
96
// Private utility methods
97

98     private void evaluateExpressions() throws JspException {
99       if (nameExpr != null)
100         name = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
101           "name",
102           nameExpr,
103           String JavaDoc.class,
104           this,
105           pageContext);
106       if (keyExpr != null)
107         key = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
108           "key",
109           keyExpr,
110           String JavaDoc.class,
111           this,
112           pageContext);
113     }
114
115 }
116
Popular Tags