1 23 24 package com.sun.appserv.web.cache; 25 26 import java.util.Map ; 27 28 import java.util.logging.Logger ; 29 import java.util.logging.Level ; 30 import java.util.ResourceBundle ; 31 32 import javax.servlet.ServletContext ; 33 import javax.servlet.http.HttpServletRequest ; 34 35 import com.sun.enterprise.web.logging.pwc.LogDomains; 36 37 import com.sun.appserv.web.cache.mapping.Constants; 38 import com.sun.appserv.web.cache.mapping.CacheMapping; 39 import com.sun.appserv.web.cache.mapping.ConstraintField; 40 import com.sun.appserv.web.cache.mapping.Field; 41 42 47 public class DefaultCacheHelper implements CacheHelper { 48 49 public static final String ATTR_CACHING_FILTER_NAME = 50 "com.sun.ias.web.cachingFilterName"; 51 public static final String PROP_KEY_GENERATOR_ATTR_NAME = 52 "cacheKeyGeneratorAttrName"; 53 private static Logger _logger; 55 private static boolean _isTraceEnabled = false; 56 57 60 private static ResourceBundle _rb = null; 61 62 ServletContext context; 63 64 CacheManager manager; 66 67 String attrKeyGenerator = null; 68 boolean isKeyGeneratorChecked = false; 69 CacheKeyGenerator keyGenerator; 70 71 75 public void setCacheManager(CacheManager manager) { 76 this.manager = manager; 77 } 78 79 80 81 86 public void init(ServletContext context, Map props) { 87 this.context = context; 88 attrKeyGenerator = (String )props.get(PROP_KEY_GENERATOR_ATTR_NAME); 89 90 _logger = LogDomains.getLogger(LogDomains.PWC_LOGGER); 92 _isTraceEnabled = _logger.isLoggable(Level.FINE); 93 _rb = _logger.getResourceBundle(); 94 } 95 96 102 private CacheMapping lookupCacheMapping(HttpServletRequest request) { 103 String name = (String )request.getAttribute(ATTR_CACHING_FILTER_NAME); 104 return manager.getCacheMapping(name); 105 } 106 107 113 public String getCacheKey(HttpServletRequest request) { 114 115 CacheMapping mapping = lookupCacheMapping(request); 117 118 if (isKeyGeneratorChecked == false && attrKeyGenerator != null) { 119 try { 120 keyGenerator = (CacheKeyGenerator) 121 context.getAttribute(attrKeyGenerator); 122 } catch (ClassCastException cce){ 123 _logger.log(Level.WARNING, "cache.defaultHelp.illegalKeyGenerator", cce); 124 } 125 126 isKeyGeneratorChecked = true; 127 } 128 129 if (keyGenerator != null) { 130 String key = keyGenerator.getCacheKey(context, request); 131 if (key != null) 132 return key; 133 } 134 135 StringBuffer sb = new StringBuffer (128); 136 137 140 sb.append(request.getServletPath()); 141 142 Field[] keys = mapping.getKeyFields(); 144 for (int i = 0; i < keys.length; i++) { 145 Object value = keys[i].getValue(context, request); 146 147 if (value == null) { 149 if (_isTraceEnabled) { 150 _logger.fine("DefaultCacheHelper: cannot find all the required key fields in the request " + request.getServletPath()); 151 } 152 return null; 153 } 154 155 sb.append(";"); 156 sb.append(Constants.KEY_PREFIXES[keys[i].getScope()]); 157 sb.append(keys[i].getName()); 158 sb.append("="); 159 sb.append(value); 160 } 161 162 return sb.toString(); 163 } 164 165 174 public boolean isCacheable(HttpServletRequest request) { 175 boolean result = false; 176 177 CacheMapping mapping = lookupCacheMapping(request); 179 180 if (mapping.findMethod(request.getMethod())) { 182 result = true; 183 184 ConstraintField fields[] = mapping.getConstraintFields(); 185 for (int i = 0; i < fields.length; i++) { 187 if (!fields[i].applyConstraints(context, request)) { 188 result = false; 189 break; 190 } 191 } 192 } 193 return result; 194 } 195 196 206 public boolean isRefreshNeeded(HttpServletRequest request) { 207 boolean result = false; 208 209 CacheMapping mapping = lookupCacheMapping(request); 211 Field field = mapping.getRefreshField(); 212 if (field != null) { 213 Object value = field.getValue(context, request); 214 if (value != null && "true".equals(value.toString())) { 216 result = true; 217 } 218 } 219 return result; 220 } 221 222 228 public int getTimeout(HttpServletRequest request) { 229 CacheMapping mapping = lookupCacheMapping(request); 231 232 int result = mapping.getTimeout(); 234 235 Field field = mapping.getTimeoutField(); 237 if (field != null) { 238 Object value = field.getValue(context, request); 239 if (value != null) { 240 try { 241 Integer timeoutAttr = new Integer (value.toString()); 243 result = timeoutAttr.intValue(); 244 } catch (NumberFormatException cce) { } 245 } 246 } 247 248 return result; 250 } 251 252 256 public void destroy() throws Exception { 257 } 258 } 259 | Popular Tags |