KickJava   Java API By Example, From Geeks To Geeks.

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


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>Caches a fragment of a JSP page.</p>
25  *
26  * @author Shawn Bayern
27  */

28
29 public class CacheTag extends BodyTagSupport {
30
31     //*********************************************************************
32
// Private state
33

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

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

40     private LRUCache cache; // cache
41
private String JavaDoc cached; // value from cache
42

43     //*********************************************************************
44
// Tag logic
45

46     public int doStartTag() throws JspException {
47       evaluateExpressions();
48       cache = CacheUtil.getCache(scope, name, pageContext);
49       cached = cache.get(key);
50       if (cached != null)
51         return SKIP_BODY;
52       else
53         return EVAL_BODY_BUFFERED;
54     }
55
56     public int doEndTag() throws JspException {
57       try {
58     String JavaDoc s = cached;
59         if (s == null) {
60           if (bodyContent == null || bodyContent.getString() == null)
61         s = "";
62           else
63             s = bodyContent.getString().trim();
64           cache.put(key, s);
65         }
66         pageContext.getOut().write(s);
67       } catch (java.io.IOException JavaDoc ex) {
68         throw new JspException(ex);
69       }
70       return EVAL_PAGE;
71     }
72
73     //*********************************************************************
74
// Attribute accessors
75

76     public void setScope(String JavaDoc scope) {
77       if (scope.equalsIgnoreCase("page"))
78     this.scope = PageContext.PAGE_SCOPE;
79       else if (scope.equalsIgnoreCase("request"))
80         this.scope = PageContext.REQUEST_SCOPE;
81       else if (scope.equalsIgnoreCase("session"))
82         this.scope = PageContext.SESSION_SCOPE;
83       else if (scope.equalsIgnoreCase("application"))
84         this.scope = PageContext.APPLICATION_SCOPE;
85       else
86         throw new IllegalArgumentException JavaDoc("invalid scope");
87     }
88
89     public void setName(String JavaDoc nameExpr) {
90       this.nameExpr = nameExpr;
91     }
92
93     public void setKey(String JavaDoc keyExpr) {
94       this.keyExpr = keyExpr;
95     }
96
97
98     //*********************************************************************
99
// Constructor and initialization
100

101     public CacheTag() {
102       super();
103       init();
104     }
105
106     private void init() {
107       scope = PageContext.APPLICATION_SCOPE;
108       name = nameExpr = "";
109       key = keyExpr = "";
110     }
111     
112
113     //*********************************************************************
114
// Private utility methods
115

116     private void evaluateExpressions() throws JspException {
117       name = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
118         "name",
119         nameExpr,
120         String JavaDoc.class,
121         this,
122         pageContext);
123       key = (String JavaDoc) ExpressionEvaluatorManager.evaluate(
124         "key",
125         keyExpr,
126         String JavaDoc.class,
127         this,
128         pageContext);
129     }
130
131 }
132
Popular Tags