Coverage Report - org.apache.commons.cache.tagext.CacheTag
 
Classes in this File Line Coverage Branch Coverage Complexity
CacheTag
0%
0/98
0%
0/32
2.8
 
 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  0
 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  0
    protected Serializable _key = null;
 38  0
    protected Serializable _group = null;
 39  0
    protected Long _expiresAt = null;
 40  0
    protected static Cache _cache = null;
 41  0
    protected String _content = null;
 42  0
    protected boolean _mustRevalidate = false;
 43  0
    protected String _name = null;
 44  
 
 45  
    public void release() {
 46  0
       super.release();
 47  0
       _key = null;
 48  0
       _group = null;
 49  0
       _name = null;
 50  0
       _expiresAt = null;
 51  0
       _content = null;
 52  0
       _mustRevalidate = false;
 53  0
    }
 54  
 
 55  
    public void setMustRevalidate(String obj) {
 56  0
       _mustRevalidate = Boolean.valueOf(obj).booleanValue();
 57  0
    }
 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  0
       _mustRevalidate = val;
 71  0
    }
 72  
 
 73  
    public void setName(String name) {
 74  0
       _name = name;
 75  0
    }
 76  
 
 77  
    public void setGroup(String group) {
 78  0
       _group = group;
 79  0
    }
 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  0
       _key = key;
 90  0
    }
 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  0
       if(ttl != null) {
 101  0
          if(ttl.endsWith("s") || ttl.endsWith("S")) {
 102  0
             setTtl(1000L * Long.parseLong(ttl.substring(0,ttl.length() - 1)));
 103  0
          } else if(ttl.endsWith("m") || ttl.endsWith("M")) {
 104  0
             setTtl(60L * 1000L * Long.parseLong(ttl.substring(0,ttl.length() - 1)));
 105  0
          } else if(ttl.endsWith("h") || ttl.endsWith("H")) {
 106  0
             setTtl(60L * 60L * 1000L * Long.parseLong(ttl.substring(0,ttl.length() - 1)));
 107  0
          } else if(ttl.endsWith("d") || ttl.endsWith("D")) {
 108  0
             setTtl(24L * 60L * 60L * 1000L * Long.parseLong(ttl.substring(0,ttl.length() - 1)));
 109  
          } else {
 110  0
             setTtl(1000L * Long.parseLong(ttl));
 111  
          }
 112  
       }
 113  0
    }
 114  
 
 115  
    private void setTtl(long ttl) {
 116  0
       _expiresAt = new Long(System.currentTimeMillis() + ttl);
 117  0
    }
 118  
 
 119  
    private void setTtl(Long ttl) {
 120  0
       if(null == ttl) {
 121  0
          _expiresAt = null;
 122  
       } else {
 123  0
          _expiresAt = new Long(System.currentTimeMillis() + ttl.longValue());
 124  
       }
 125  0
    }
 126  
 
 127  
    public void setLastModified(String lastmodstr) {
 128  0
       long lastmod = 0L;
 129  
       try {
 130  0
          lastmod = Long.parseLong(lastmodstr);
 131  0
          _expiresAt = new Long( System.currentTimeMillis() + ((System.currentTimeMillis() - lastmod)/2) );
 132  0
       } catch(Exception e) {
 133  
          // do something?
 134  0
          _expiresAt = null;
 135  0
       }
 136  0
    }
 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  0
          _expiresAt = new Long(Long.parseLong(expires));
 155  0
       } catch(Exception e) {
 156  
          // do something?
 157  0
          _expiresAt = null;
 158  0
       }
 159  0
    }
 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  0
       if(!_mustRevalidate) {
 173  
          try {
 174  0
             Object obj = getCache(_name).retrieve(_key);
 175  0
             if(null == obj) {
 176  0
                return EVAL_BODY_TAG;
 177  
             } else {
 178  0
                _content = obj.toString();
 179  0
                return SKIP_BODY;
 180  
             }
 181  0
          } catch(Exception e) {
 182  0
             e.printStackTrace();
 183  0
             throw new JspException(e.toString());
 184  
          }
 185  
       } else {
 186  0
          getCache(_name).clear(_key);
 187  0
          return EVAL_BODY_TAG;
 188  
       }
 189  
    }
 190  
 
 191  
    public int doAfterBody() throws JspException {
 192  0
       _content = getBodyContent().getString();
 193  0
       getCache(_name).store(_key,_content,_expiresAt,null,_group);
 194  0
       return SKIP_BODY;
 195  
    }
 196  
 
 197  
    public int doEndTag() throws JspException {
 198  
       try {
 199  0
          pageContext.getOut().write(_content);
 200  0
       } catch(Exception e) {
 201  0
          e.printStackTrace();
 202  0
          throw new JspException(e.toString());
 203  0
       }
 204  0
       return EVAL_PAGE;
 205  
    }
 206  
 
 207  0
    private static final CacheTag _instance = new CacheTag(); // used in getCache
 208  
 
 209  
    public synchronized static Cache getCache(String name) throws JspException {
 210  0
       if(CacheSingleton.hasCache(name)) {
 211  0
          return CacheSingleton.getCache(name);
 212  
       } else {
 213  0
          Cache cache = null;
 214  
 
 215  0
          String serfile = System.getProperty("org.apache.commons.cache.tagext.CacheTag.cache." + name + ".serialized-cache-file");
 216  0
          if(null != serfile) {
 217  
             try {
 218  0
                cache = SimpleCache.readFromFile(serfile);
 219  0
             } catch(Exception e) {
 220  0
                cache = null;
 221  0
             }
 222  
          }
 223  
 
 224  0
          if(null != cache) {
 225  0
             CacheSingleton.putCache(name,cache);
 226  0
             return cache;
 227  
          } else {
 228  0
             String config = System.getProperty("org.apache.commons.cache.tagext.CacheTag.cache." + name + ".configuration");
 229  0
             if(null == config) {
 230  0
                throw new JspException("Cache \"" + name + "\" not found.");
 231  
             } else {
 232  
                try {
 233  0
                   JOCLContentHandler jocl = JOCLContentHandler.parse(_instance.getClass().getClassLoader().getResourceAsStream(config));
 234  0
                   cache = (Cache)(jocl.getValue(0));
 235  0
                   CacheSingleton.putCache(name,cache);
 236  0
                   return cache;
 237  0
                } catch(Exception e) {
 238  0
                   e.printStackTrace();
 239  0
                   throw new JspException(e.toString());
 240  
                }
 241  
             }
 242  
          }
 243  
       }
 244  
    }
 245  
 }