1 16 package org.apache.cocoon.caching.impl; 17 18 import java.io.IOException ; 19 import java.io.Serializable ; 20 21 import org.apache.avalon.framework.activity.Disposable; 22 import org.apache.avalon.framework.logger.AbstractLogEnabled; 23 import org.apache.avalon.framework.parameters.ParameterException; 24 import org.apache.avalon.framework.parameters.Parameterizable; 25 import org.apache.avalon.framework.parameters.Parameters; 26 import org.apache.avalon.framework.service.ServiceException; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.avalon.framework.service.Serviceable; 29 import org.apache.avalon.framework.thread.ThreadSafe; 30 import org.apache.cocoon.ProcessingException; 31 import org.apache.cocoon.caching.Cache; 32 import org.apache.cocoon.caching.CachedResponse; 33 import org.apache.excalibur.store.Store; 34 35 45 public class CacheImpl 46 extends AbstractLogEnabled 47 implements Cache, ThreadSafe, Serviceable, Disposable, Parameterizable { 48 49 50 protected Store store; 51 52 53 protected ServiceManager manager; 54 55 58 public void service (ServiceManager manager) throws ServiceException { 59 this.manager = manager; 60 } 61 62 65 public void dispose() { 66 this.manager.release(this.store); 67 this.store = null; 68 this.manager = null; 69 } 70 71 77 public void store(Serializable key, 78 CachedResponse response) 79 throws ProcessingException { 80 if ( this.getLogger().isInfoEnabled()) { 81 this.getLogger().info("Caching new response for " + key); 82 } 83 try { 84 this.store.store(key, response); 85 } catch (IOException ioe) { 86 throw new ProcessingException("Unable to cache response.", ioe); 87 } 88 } 89 90 96 public CachedResponse get(Serializable key) { 97 final CachedResponse r = (CachedResponse)this.store.get(key); 98 if ( this.getLogger().isDebugEnabled()) { 99 this.getLogger().debug("Cached response for " + key + " : " + 100 (r == null ? "not found" : "found")); 101 } 102 return r; 103 } 104 105 111 public void remove(Serializable key) { 112 if ( this.getLogger().isInfoEnabled()) { 113 this.getLogger().info("Removing cached response for " + key); 114 } 115 this.store.remove(key); 116 } 117 118 121 public void clear() { 122 if ( this.getLogger().isInfoEnabled()) { 123 this.getLogger().info("Clearing cache"); 124 } 125 this.store.clear(); 127 } 128 129 132 public boolean containsKey(Serializable key) { 133 return this.store.containsKey(key); 134 } 135 136 139 public void parameterize(Parameters parameters) throws ParameterException { 140 String storeName = parameters.getParameter("store", Store.ROLE); 141 try { 142 this.store = (Store)this.manager.lookup(storeName); 143 } catch (ServiceException e) { 144 throw new ParameterException("Unable to lookup store: " + storeName, e); 145 } 146 } 147 148 } 149 | Popular Tags |