1 23 package com.sun.enterprise.web; 24 25 import java.util.HashMap ; 26 import java.util.logging.Logger ; 27 import java.util.logging.Level ; 28 29 import javax.servlet.ServletContext ; 30 31 import org.apache.catalina.Context; 32 import org.apache.catalina.LifecycleException; 33 import org.apache.catalina.deploy.FilterDef; 34 import org.apache.catalina.deploy.FilterMap; 35 36 import com.sun.logging.LogDomains; 37 38 import com.sun.enterprise.config.ConfigBean; 39 import com.sun.enterprise.deployment.runtime.web.WebProperty; 40 import com.sun.enterprise.deployment.runtime.web.SunWebApp; 41 import com.sun.enterprise.deployment.runtime.web.Cache; 42 import com.sun.enterprise.deployment.runtime.web.CacheHelper; 43 import com.sun.enterprise.deployment.runtime.web.DefaultHelper; 44 47 import com.sun.appserv.web.cache.mapping.CacheMapping; 48 import com.sun.appserv.web.cache.mapping.ConstraintField; 49 import com.sun.appserv.web.cache.CacheManager; 50 import com.sun.appserv.web.cache.DefaultCacheHelper; 51 import com.sun.appserv.web.cache.mapping.Field; 52 import com.sun.appserv.web.cache.mapping.ValueConstraint; 53 54 57 public final class CacheModule { 58 public final static String CACHING_FILTER_CLASSNAME = 59 "com.sun.appserv.web.cache.filter.CachingFilter"; 60 public final static String DEFAULT_CACHE_HELPER_CLASSNAME = 61 "com.sun.appserv.web.cache.DefaultCacheHelper"; 62 63 private static String trim(String str) { 64 if (str != null) 65 return str.trim(); 66 return str; 67 } 68 69 78 public static CacheManager configureResponseCache(WebModule app, 79 SunWebApp bean) throws Exception { 80 Logger logger = LogDomains.getLogger(LogDomains.WEB_LOGGER); 81 82 Cache cacheConfig = bean.getCache(); 83 84 if (cacheConfig == null) { 86 return null; 87 } 88 89 if (logger.isLoggable(Level.FINE)) { 90 logger.fine("configuring cache for web application " + app.getPath()); 91 } 92 93 CacheManager manager = new CacheManager(); 95 96 String name, value; 97 value = cacheConfig.getAttributeValue(Cache.ENABLED); 98 if (value != null) { 99 boolean enabled = ConfigBean.toBoolean(value); 100 manager.setEnabled(enabled); 101 } 102 103 value = cacheConfig.getAttributeValue(Cache.MAX_ENTRIES); 105 if (value != null) { 106 try { 107 int maxEntries = Integer.parseInt(value.trim()); 108 manager.setMaxEntries(maxEntries); 109 } catch (NumberFormatException e) { 110 throw new Exception ("invalid max-entries", e); 112 } 113 } 114 115 value = cacheConfig.getAttributeValue(Cache.TIMEOUT_IN_SECONDS); 116 if (value != null) { 117 try { 118 int defaultTimeout = Integer.parseInt(value.trim()); 119 manager.setDefaultTimeout(defaultTimeout); 120 } catch (NumberFormatException e) { 121 throw new Exception ("invalid timeout", e); 123 } 124 } 125 126 WebProperty[] props = cacheConfig.getWebProperty(); 127 for (int i = 0; i < props.length; i++) { 128 name = props[i].getAttributeValue(WebProperty.NAME); 129 value = props[i].getAttributeValue(WebProperty.VALUE); 130 131 manager.addProperty(name, value); 132 } 133 134 DefaultHelper defHelperConfig = cacheConfig.getDefaultHelper(); 136 137 HashMap map = new HashMap (); 138 if (defHelperConfig != null) { 139 props = defHelperConfig.getWebProperty(); 140 for (int i = 0; i < props.length; i++) { 141 name = props[i].getAttributeValue(WebProperty.NAME); 142 value = props[i].getAttributeValue(WebProperty.VALUE); 143 144 map.put(name, value); 145 } 146 } 147 manager.setDefaultHelperProps(map); 148 149 for (int i = 0; i < cacheConfig.sizeCacheHelper(); i++) { 151 CacheHelper helperConfig = cacheConfig.getCacheHelper(i); 152 153 String helperName = helperConfig.getAttributeValue( 154 CacheHelper.NAME); 155 HashMap helperProps = new HashMap (); 156 props = helperConfig.getWebProperty(); 157 for (int j = 0; j < props.length; j++) { 158 name = props[i].getAttributeValue(WebProperty.NAME); 159 value = props[i].getAttributeValue(WebProperty.VALUE); 160 161 helperProps.put(name, value); 162 } 163 helperProps.put("class-name", 164 helperConfig.getAttributeValue( 165 CacheHelper.CLASS_NAME)); 166 167 manager.addCacheHelperDef(helperName, helperProps); 168 } 169 170 for (int i = 0; i < cacheConfig.sizeCacheMapping(); i++) { 172 com.sun.enterprise.deployment.runtime.web.CacheMapping 173 mapConfig = cacheConfig.getCacheMapping(i); 174 175 CacheMapping mapping = new CacheMapping(); 176 configureCacheMapping(mapConfig, mapping, logger); 177 178 String filterName = CACHING_FILTER_CLASSNAME + i; 180 181 185 manager.addCacheMapping(filterName, mapping); 186 187 FilterDef filterDef = new FilterDef(); 189 filterDef.setFilterName(filterName); 190 filterDef.setFilterClass(CACHING_FILTER_CLASSNAME); 191 192 filterDef.addInitParameter("servletName", mapping.getServletName()); 193 filterDef.addInitParameter("URLPattern", mapping.getURLPattern()); 194 app.addFilterDef(filterDef); 195 196 FilterMap filterMap = new FilterMap(); 198 filterMap.setServletName(mapping.getServletName()); 199 filterMap.setURLPattern(mapping.getURLPattern()); 200 String [] dispatchers = mapConfig.getDispatcher(); 201 for (int j=0; dispatchers!=null && j<dispatchers.length; j++) { 202 filterMap.setDispatcher(dispatchers[j]); 204 } 205 filterMap.setFilterName(filterName); 206 app.addFilterMap(filterMap); 207 208 if (logger.isLoggable(Level.FINE)) { 209 logger.fine("added a caching filter for servlet-name = " + mapping.getServletName() + " url-pattern = " + mapping.getURLPattern()); 210 } 211 } 212 213 manager.setServletContext(app.getServletContext()); 214 return manager; 215 } 216 217 224 private static void configureCacheMapping( 225 com.sun.enterprise.deployment.runtime.web.CacheMapping mapConfig, 226 CacheMapping mapping, 227 Logger logger) throws Exception { 228 String name, scope, value, expr; 229 230 233 mapping.setServletName(trim(mapConfig.getServletName())); 234 mapping.setURLPattern(trim(mapConfig.getUrlPattern())); 235 236 String helperRef = mapConfig.getCacheHelperRef(); 238 if (helperRef == null) { 239 helperRef = "default"; 240 } 241 mapping.setHelperNameRef(helperRef); 242 243 247 value = mapConfig.getTimeout(); 248 if (value != null) { 249 try { 250 mapping.setTimeout(Integer.parseInt(value.trim())); 251 } catch (NumberFormatException e) { 252 throw new Exception ("invalid timeout", e); 253 } 254 } else { 255 name = mapConfig.getAttributeValue( 257 com.sun.enterprise.deployment.runtime.web.CacheMapping.TIMEOUT, 258 com.sun.enterprise.deployment.runtime.web.CacheMapping.NAME); 259 scope = mapConfig.getAttributeValue( 260 com.sun.enterprise.deployment.runtime.web.CacheMapping.TIMEOUT, 261 com.sun.enterprise.deployment.runtime.web.CacheMapping.SCOPE); 262 if (name != null && scope != null) 263 mapping.setTimeoutField(new Field(name, scope)); 264 } 265 266 269 270 name = mapConfig.getAttributeValue( 271 com.sun.enterprise.deployment.runtime.web.CacheMapping.REFRESH_FIELD, 272 com.sun.enterprise.deployment.runtime.web.CacheMapping.NAME); 273 scope = mapConfig.getAttributeValue( 274 com.sun.enterprise.deployment.runtime.web.CacheMapping.REFRESH_FIELD, 275 com.sun.enterprise.deployment.runtime.web.CacheMapping.SCOPE); 276 if (name != null && scope != null) { 277 Field refreshField = new Field(name, scope); 278 mapping.setRefreshField(refreshField); 279 } 280 281 284 if (mapConfig.sizeHttpMethod() > 0) { 285 mapping.setMethods(mapConfig.getHttpMethod()); 286 } 287 288 291 for (int i = 0; i < mapConfig.sizeKeyField(); i++) { 292 name = mapConfig.getAttributeValue( 293 com.sun.enterprise.deployment.runtime.web.CacheMapping.KEY_FIELD, 294 i, com.sun.enterprise.deployment.runtime.web.CacheMapping.NAME); 295 scope = mapConfig.getAttributeValue( 296 com.sun.enterprise.deployment.runtime.web.CacheMapping.KEY_FIELD, 297 i, com.sun.enterprise.deployment.runtime.web.CacheMapping.SCOPE); 298 if (name != null && scope != null) { 299 mapping.addKeyField(new Field(name, scope)); 300 301 if (logger.isLoggable(Level.FINE)) { 302 logger.fine("added a key-field : name = " + name 303 + " scope = " + scope); 304 } 305 } 306 } 307 308 312 for (int i = 0; i < mapConfig.sizeConstraintField(); i++) { 313 com.sun.enterprise.deployment.runtime.web.ConstraintField 314 fieldConfig = mapConfig.getConstraintField(i); 315 316 name = fieldConfig.getAttributeValue( 317 com.sun.enterprise.deployment.runtime.web.ConstraintField.NAME); 318 scope = fieldConfig.getAttributeValue( 319 com.sun.enterprise.deployment.runtime.web.ConstraintField.SCOPE); 320 ConstraintField constraintField = 321 new ConstraintField(name, scope); 322 323 value = fieldConfig.getAttributeValue(com.sun.enterprise.deployment.runtime.web.ConstraintField.CACHE_ON_MATCH); 324 if (value != null) 325 constraintField.setCacheOnMatch(ConfigBean.toBoolean(value)); 326 327 value = fieldConfig.getAttributeValue(com.sun.enterprise.deployment.runtime.web.ConstraintField.CACHE_ON_MATCH_FAILURE); 328 if (value != null) 329 constraintField.setCacheOnMatchFailure( 330 ConfigBean.toBoolean(value)); 331 332 333 for (int j = 0; j < fieldConfig.sizeValue(); j++) { 335 value = fieldConfig.getValue(j).trim(); 336 expr = fieldConfig.getAttributeValue( 337 com.sun.enterprise.deployment.runtime.web.ConstraintField.VALUE, j, com.sun.enterprise.deployment.runtime.web.ConstraintField.MATCH_EXPR); 338 339 ValueConstraint constraint = new ValueConstraint(value, expr); 340 value = fieldConfig.getAttributeValue(com.sun.enterprise.deployment.runtime.web.ConstraintField.VALUE, j, com.sun.enterprise.deployment.runtime.web.ConstraintField.CACHE_ON_MATCH); 341 if (value != null) { 342 constraint.setCacheOnMatch(ConfigBean.toBoolean(value)); 343 } 344 value = fieldConfig.getAttributeValue(com.sun.enterprise.deployment.runtime.web.ConstraintField.VALUE, j, com.sun.enterprise.deployment.runtime.web.ConstraintField.CACHE_ON_MATCH_FAILURE); 345 if (value != null) { 346 constraint.setCacheOnMatchFailure( 347 ConfigBean.toBoolean(value)); 348 } 349 constraintField.addConstraint(constraint); 350 351 if (logger.isLoggable(Level.FINE)) { 352 logger.fine("added a constraint: " + constraint.toString()); 353 } 354 } 355 356 mapping.addConstraintField(constraintField); 357 358 if (logger.isLoggable(Level.FINE)) { 359 logger.fine("added a constraint-field name = " + name + " scope = " + scope + " cache-on-match = " + constraintField.getCacheOnMatch() + " cache-on-match-failure = " + constraintField.getCacheOnMatchFailure()); 360 } 361 } 362 } 363 } 364 | Popular Tags |