1 23 24 package com.sun.appserv.web.cache; 25 26 import java.text.MessageFormat ; 27 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.Enumeration ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 import java.util.Properties ; 35 import java.util.Set ; 36 import java.io.PrintWriter ; 37 import java.io.StringWriter ; 38 39 import java.util.logging.Logger ; 40 import java.util.logging.Level ; 41 import java.util.ResourceBundle ; 42 43 import javax.servlet.ServletContext ; 44 import org.apache.catalina.LifecycleException; 45 46 import com.sun.appserv.util.cache.Cache; 47 import com.sun.appserv.web.cache.mapping.CacheMapping; 48 49 import com.sun.enterprise.web.logging.pwc.LogDomains; 50 51 public class CacheManager { 52 53 public static final String CACHE_MANAGER_ATTR_NAME = 54 "com.sun.appserv.web.CacheManager"; 55 public static final int DEFAULT_CACHE_MAX_ENTRIES = 4096; 56 public static final int DEFAULT_CACHE_TIMEOUT = 30; 57 public static final String DEFAULT_CACHE_CLASSNAME = 58 "com.sun.appserv.util.cache.LruCache"; 59 60 private static Logger _logger; 62 65 private static ResourceBundle _rb = null; 66 67 int maxEntries = DEFAULT_CACHE_MAX_ENTRIES; 69 int defaultTimeout = DEFAULT_CACHE_TIMEOUT; 70 String cacheClassName = DEFAULT_CACHE_CLASSNAME; 71 72 boolean enabled = false; 73 74 ServletContext context; 76 77 Properties cacheProps; 79 Cache defaultCache; 80 81 HashMap cacheMappings = new HashMap (); 83 84 Map defaultHelperProps; 86 DefaultCacheHelper defaultHelper; 87 88 HashMap helperDefs = new HashMap (); 90 HashMap cacheHelpers = new HashMap (); 91 HashMap cacheHelpersByFilterName = new HashMap (); 92 93 ArrayList listeners = new ArrayList (); 95 96 99 public CacheManager() { } 100 101 105 public void setMaxEntries(int maxEntries) { 106 this.maxEntries = maxEntries; 107 } 108 109 113 public void setDefaultTimeout(int defaultTimeout) { 114 this.defaultTimeout = defaultTimeout; 115 } 116 117 121 public void setEnabled(boolean enabled) { 122 this.enabled = enabled; 123 } 124 125 128 public boolean isEnabled() { 129 return enabled; 130 } 131 132 137 public void addProperty(String name, String value) { 138 if (name.equalsIgnoreCase("cacheClassName")) { 139 cacheClassName = value; 140 } else { 141 if (cacheProps == null) { 142 cacheProps = new Properties (); 143 } 144 cacheProps.setProperty(name, value); 145 } 146 } 147 148 153 public void addCacheHelperDef(String name, HashMap helperDef) { 154 helperDefs.put(name, helperDef); 155 } 156 157 161 public void setDefaultHelperProps(Map map) { 162 this.defaultHelperProps = map; 163 } 164 165 169 public void setServletContext(ServletContext context) { 170 this.context = context; 171 } 172 173 178 private CacheHelper loadCacheHelper(String className) 179 throws Exception { 180 181 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 184 Class helperClass = cl.loadClass(className); 185 186 CacheHelper helper = (CacheHelper) helperClass.newInstance(); 187 188 return helper; 189 } 190 191 195 public void start() throws LifecycleException { 196 197 if (!enabled) 198 return; 199 200 _logger = LogDomains.getLogger(LogDomains.PWC_LOGGER); 202 _rb = _logger.getResourceBundle(); 203 204 try { 206 defaultCache = createCache(maxEntries, cacheClassName); 207 } catch (Exception e) { 208 _logger.log(Level.WARNING, "cache.manager.excep_createCache", e); 209 210 String msg = _rb.getString("cache.manager.excep_createCache"); 211 throw new LifecycleException(msg, e); 212 } 213 214 defaultHelper = new DefaultCacheHelper(); 216 defaultHelper.setCacheManager(this); 217 defaultHelper.init(context, defaultHelperProps); 218 219 Iterator helperNames = helperDefs.keySet().iterator(); 221 while(helperNames.hasNext()) { 222 String name = (String ) helperNames.next(); 223 HashMap map = (HashMap )helperDefs.get(name); 224 225 try { 226 String className = (String )map.get("class-name"); 227 CacheHelper helper = loadCacheHelper(className); 228 helper.init(context, map); 229 cacheHelpers.put(name, helper); 230 231 } catch (Exception e) { 232 String msg = _rb.getString("cache.manager.excep_initCacheHelper"); 233 Object [] params = { name }; 234 msg = MessageFormat.format(msg, params); 235 236 throw new LifecycleException(msg, e); 237 } 238 } 239 240 Iterator filterNames = cacheMappings.keySet().iterator(); 242 while(filterNames.hasNext()) { 243 String name = (String ) filterNames.next(); 244 CacheMapping mapping = (CacheMapping)cacheMappings.get(name); 245 246 String helperNameRef = mapping.getHelperNameRef(); 247 CacheHelper helper; 248 if (helperNameRef == null || helperNameRef.equals("default")) { 249 helper = defaultHelper; 250 } else { 251 helper = (CacheHelper) cacheHelpers.get(helperNameRef); 252 } 253 cacheHelpersByFilterName.put(name, helper); 254 } 255 } 256 257 261 public String getCacheClassName() { 262 return cacheClassName; 263 } 264 265 270 public Cache createCache() throws Exception { 271 return createCache(maxEntries, DEFAULT_CACHE_CLASSNAME); 272 } 273 274 279 public Cache createCache(int cacacity, String className) 280 throws Exception { 281 282 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 285 Class cacheClass = cl.loadClass(className); 286 287 Cache cacheImpl = (Cache)cacheClass.newInstance(); 288 cacheImpl.init(maxEntries, cacheProps); 289 290 return cacheImpl; 291 } 292 293 297 public int getDefaultTimeout() { 298 return defaultTimeout; 299 } 300 301 305 public Cache getDefaultCache() { 306 return defaultCache; 307 } 308 309 314 public void addCacheMapping(String name, CacheMapping mapping) { 315 cacheMappings.put(name, mapping); 316 } 317 318 323 public CacheMapping getCacheMapping(String name) { 324 return (CacheMapping)cacheMappings.get(name); 325 } 326 327 332 public CacheHelper getCacheHelper(String name) { 333 return (CacheHelper)cacheHelpers.get(name); 334 } 335 336 341 public CacheHelper getCacheHelperByFilterName(String filterName) { 342 return (CacheHelper)cacheHelpersByFilterName.get(filterName); 343 } 344 345 349 public void addCacheManagerListener(CacheManagerListener listener) { 350 synchronized (listeners) { 351 listeners.add(listener); 352 } 353 } 354 355 359 public void removeCacheManagerListener(CacheManagerListener listener) { 360 synchronized (listeners) { 361 listeners.remove(listener); 362 } 363 } 364 365 368 public void enable() { 369 for (int i = 0; i < listeners.size(); i++) { 370 CacheManagerListener listener = (CacheManagerListener) 371 listeners.get(i); 372 listener.cacheManagerEnabled(); 373 } 374 } 375 376 379 public void disable() { 380 for (int i = 0; i < listeners.size(); i++) { 381 CacheManagerListener listener = (CacheManagerListener) 382 listeners.get(i); 383 listener.cacheManagerDisabled(); 384 } 385 } 386 387 392 public void stop() throws LifecycleException { 393 disable(); 394 395 try { 396 defaultHelper.destroy(); 397 } catch (Exception e) { 398 } 400 401 Enumeration helpers = Collections.enumeration(cacheHelpers.values()); 403 while(helpers.hasMoreElements()) { 404 CacheHelper cacheHelper = (CacheHelper)helpers.nextElement(); 405 try { 406 cacheHelper.destroy(); 407 } catch (Exception e) { 408 } 410 } 411 cacheHelpers.clear(); 412 cacheMappings.clear(); 413 cacheHelpersByFilterName.clear(); 414 listeners.clear(); 415 } 416 } 417 | Popular Tags |