View Javadoc

1   /*
2    * Copyright 2001-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  package org.apache.commons.cache.tagext;
17  
18  import javax.servlet.*;
19  import javax.servlet.http.*;
20  import javax.servlet.jsp.*;
21  import javax.servlet.jsp.tagext.*;
22  import java.io.*;
23  import java.util.Hashtable;
24  import org.apache.commons.cache.*;
25  import org.apache.commons.jocl.JOCLContentHandler;
26  
27  /**
28   * ...tk...
29   * @version $Id: CacheTag.java 155435 2005-02-26 13:17:27Z dirkv $
30   * @author Rodney Waldhoff
31   */
32  public class CacheTag extends BodyTagSupport {
33     private static final boolean DEBUG = false;
34     private static final boolean VERBOSE_DEBUG = DEBUG && false;
35     private static final boolean CHECKING = false;
36  
37     protected Serializable _key = null;
38     protected Serializable _group = null;
39     protected Long _expiresAt = null;
40     protected static Cache _cache = null;
41     protected String _content = null;
42     protected boolean _mustRevalidate = false;
43     protected String _name = null;
44  
45     public void release() {
46        super.release();
47        _key = null;
48        _group = null;
49        _name = null;
50        _expiresAt = null;
51        _content = null;
52        _mustRevalidate = false;
53     }
54  
55     public void setMustRevalidate(String obj) {
56        _mustRevalidate = Boolean.valueOf(obj).booleanValue();
57     }
58  
59  // weblogic reflection doesn't work very well
60  /*
61     public void setMustRevalidate(Object obj) {
62        if(obj instanceof Boolean) {
63           _mustRevalidate = ((Boolean)obj).booleanValue();
64        } else {
65           _mustRevalidate = Boolean.valueOf(String.valueOf(obj)).booleanValue();
66        }
67     }
68  */
69     public void setMustRevalidate(boolean val) {
70        _mustRevalidate = val;
71     }
72  
73     public void setName(String name) {
74        _name = name;
75     }
76  
77     public void setGroup(String group) {
78        _group = group;
79     }
80  
81  // weblogic reflection doesn't work very well
82  /*
83     public void setGroup(Serializable group) {
84        _group = group;
85     }
86  */
87  
88     public void setKey(String key) {
89        _key = key;
90     }
91  
92  // weblogic reflection doesn't work very well
93  /*
94     public void setKey(Serializable key) {
95        _key = key;
96     }
97  */
98  
99     public void setTtl(String ttl) {
100       if(ttl != null) {
101          if(ttl.endsWith("s") || ttl.endsWith("S")) {
102             setTtl(1000L * Long.parseLong(ttl.substring(0,ttl.length() - 1)));
103          } else if(ttl.endsWith("m") || ttl.endsWith("M")) {
104             setTtl(60L * 1000L * Long.parseLong(ttl.substring(0,ttl.length() - 1)));
105          } else if(ttl.endsWith("h") || ttl.endsWith("H")) {
106             setTtl(60L * 60L * 1000L * Long.parseLong(ttl.substring(0,ttl.length() - 1)));
107          } else if(ttl.endsWith("d") || ttl.endsWith("D")) {
108             setTtl(24L * 60L * 60L * 1000L * Long.parseLong(ttl.substring(0,ttl.length() - 1)));
109          } else {
110             setTtl(1000L * Long.parseLong(ttl));
111          }
112       }
113    }
114 
115    private void setTtl(long ttl) {
116       _expiresAt = new Long(System.currentTimeMillis() + ttl);
117    }
118 
119    private void setTtl(Long ttl) {
120       if(null == ttl) {
121          _expiresAt = null;
122       } else {
123          _expiresAt = new Long(System.currentTimeMillis() + ttl.longValue());
124       }
125    }
126 
127    public void setLastModified(String lastmodstr) {
128       long lastmod = 0L;
129       try {
130          lastmod = Long.parseLong(lastmodstr);
131          _expiresAt = new Long( System.currentTimeMillis() + ((System.currentTimeMillis() - lastmod)/2) );
132       } catch(Exception e) {
133          // do something?
134          _expiresAt = null;
135       }
136    }
137 
138 // weblogic reflection doesn't work very well
139 /*
140    public void setLastModified(long lastmod) {
141       _expiresAt = new Long( System.currentTimeMillis() + ((System.currentTimeMillis() - lastmod)/2) );
142    }
143 
144    public void setLastModified(Long lastmod) {
145       if(null != lastmod) {
146          _expiresAt = new Long( System.currentTimeMillis() + ((System.currentTimeMillis() - lastmod.longValue())/2) );
147       } else {
148          _expiresAt = null;
149       }
150    }
151 */
152    public void setExpiresAt(String expires) {
153       try {
154          _expiresAt = new Long(Long.parseLong(expires));
155       } catch(Exception e) {
156          // do something?
157          _expiresAt = null;
158       }
159    }
160 
161 // weblogic reflection doesn't work very well
162 /*
163    public void setExpiresAt(long expires) {
164       _expiresAt = new Long(expires);
165    }
166 
167    public void setExpiresAt(Long expires) {
168       _expiresAt = expires;
169    }
170 */
171    public int doStartTag() throws JspException {
172       if(!_mustRevalidate) {
173          try {
174             Object obj = getCache(_name).retrieve(_key);
175             if(null == obj) {
176                return EVAL_BODY_TAG;
177             } else {
178                _content = obj.toString();
179                return SKIP_BODY;
180             }
181          } catch(Exception e) {
182             e.printStackTrace();
183             throw new JspException(e.toString());
184          }
185       } else {
186          getCache(_name).clear(_key);
187          return EVAL_BODY_TAG;
188       }
189    }
190 
191    public int doAfterBody() throws JspException {
192       _content = getBodyContent().getString();
193       getCache(_name).store(_key,_content,_expiresAt,null,_group);
194       return SKIP_BODY;
195    }
196 
197    public int doEndTag() throws JspException {
198       try {
199          pageContext.getOut().write(_content);
200       } catch(Exception e) {
201          e.printStackTrace();
202          throw new JspException(e.toString());
203       }
204       return EVAL_PAGE;
205    }
206 
207    private static final CacheTag _instance = new CacheTag(); // used in getCache
208 
209    public synchronized static Cache getCache(String name) throws JspException {
210       if(CacheSingleton.hasCache(name)) {
211          return CacheSingleton.getCache(name);
212       } else {
213          Cache cache = null;
214 
215          String serfile = System.getProperty("org.apache.commons.cache.tagext.CacheTag.cache." + name + ".serialized-cache-file");
216          if(null != serfile) {
217             try {
218                cache = SimpleCache.readFromFile(serfile);
219             } catch(Exception e) {
220                cache = null;
221             }
222          }
223 
224          if(null != cache) {
225             CacheSingleton.putCache(name,cache);
226             return cache;
227          } else {
228             String config = System.getProperty("org.apache.commons.cache.tagext.CacheTag.cache." + name + ".configuration");
229             if(null == config) {
230                throw new JspException("Cache \"" + name + "\" not found.");
231             } else {
232                try {
233                   JOCLContentHandler jocl = JOCLContentHandler.parse(_instance.getClass().getClassLoader().getResourceAsStream(config));
234                   cache = (Cache)(jocl.getValue(0));
235                   CacheSingleton.putCache(name,cache);
236                   return cache;
237                } catch(Exception e) {
238                   e.printStackTrace();
239                   throw new JspException(e.toString());
240                }
241             }
242          }
243       }
244    }
245 }