001    /*
002     * Copyright 2001-2004 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.cache.tagext;
017    
018    import javax.servlet.*;
019    import javax.servlet.http.*;
020    import javax.servlet.jsp.*;
021    import javax.servlet.jsp.tagext.*;
022    import java.io.*;
023    import java.util.Hashtable;
024    import org.apache.commons.cache.*;
025    import org.apache.commons.jocl.JOCLContentHandler;
026    
027    /**
028     * ...tk...
029     * @version $Id: CacheTag.java 155435 2005-02-26 13:17:27Z dirkv $
030     * @author Rodney Waldhoff
031     */
032    public class CacheTag extends BodyTagSupport {
033       private static final boolean DEBUG = false;
034       private static final boolean VERBOSE_DEBUG = DEBUG && false;
035       private static final boolean CHECKING = false;
036    
037       protected Serializable _key = null;
038       protected Serializable _group = null;
039       protected Long _expiresAt = null;
040       protected static Cache _cache = null;
041       protected String _content = null;
042       protected boolean _mustRevalidate = false;
043       protected String _name = null;
044    
045       public void release() {
046          super.release();
047          _key = null;
048          _group = null;
049          _name = null;
050          _expiresAt = null;
051          _content = null;
052          _mustRevalidate = false;
053       }
054    
055       public void setMustRevalidate(String obj) {
056          _mustRevalidate = Boolean.valueOf(obj).booleanValue();
057       }
058    
059    // weblogic reflection doesn't work very well
060    /*
061       public void setMustRevalidate(Object obj) {
062          if(obj instanceof Boolean) {
063             _mustRevalidate = ((Boolean)obj).booleanValue();
064          } else {
065             _mustRevalidate = Boolean.valueOf(String.valueOf(obj)).booleanValue();
066          }
067       }
068    */
069       public void setMustRevalidate(boolean val) {
070          _mustRevalidate = val;
071       }
072    
073       public void setName(String name) {
074          _name = name;
075       }
076    
077       public void setGroup(String group) {
078          _group = group;
079       }
080    
081    // weblogic reflection doesn't work very well
082    /*
083       public void setGroup(Serializable group) {
084          _group = group;
085       }
086    */
087    
088       public void setKey(String key) {
089          _key = key;
090       }
091    
092    // weblogic reflection doesn't work very well
093    /*
094       public void setKey(Serializable key) {
095          _key = key;
096       }
097    */
098    
099       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    }