1 16 package org.apache.cocoon.woody; 17 18 import java.io.IOException ; 19 20 import org.apache.avalon.framework.activity.Disposable; 21 import org.apache.avalon.framework.component.Component; 22 import org.apache.avalon.framework.configuration.Configurable; 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 import org.apache.avalon.framework.logger.AbstractLogEnabled; 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.commons.collections.FastHashMap; 31 import org.apache.excalibur.source.Source; 32 import org.apache.excalibur.source.SourceValidity; 33 34 39 public class DefaultCacheManager 40 extends AbstractLogEnabled 41 implements CacheManager, ThreadSafe, Serviceable, Disposable, Configurable, Component { 42 43 protected ServiceManager manager; 44 protected Configuration configuration; 45 protected FastHashMap cache = new FastHashMap(); 46 47 public void service(ServiceManager serviceManager) throws ServiceException { 48 this.manager = serviceManager; 49 } 50 51 54 public void configure(Configuration configuration) throws ConfigurationException { 55 this.configuration = configuration; 56 } 57 58 public Object get(Source source, String prefix) { 59 String key = prefix + source.getURI(); 60 SourceValidity newValidity = source.getValidity(); 61 62 if (newValidity == null) { 63 cache.remove(key); 64 return null; 65 } 66 67 Object [] objectAndValidity = (Object [])cache.get(key); 68 if (objectAndValidity == null) 69 return null; 70 71 SourceValidity storedValidity = (SourceValidity)objectAndValidity[1]; 72 int valid = storedValidity.isValid(); 73 boolean isValid; 74 if (valid == 0) { 75 valid = storedValidity.isValid(newValidity); 76 isValid = (valid == 1); 77 } else { 78 isValid = (valid == 1); 79 } 80 81 if (!isValid) { 82 cache.remove(key); 83 return null; 84 } 85 86 return objectAndValidity[0]; 87 } 88 89 public void set(Object object, Source source, String prefix) throws IOException { 90 String key = prefix + source.getURI(); 91 SourceValidity validity = source.getValidity(); 92 if (validity != null) { 93 Object [] objectAndValidity = {object, validity}; 94 cache.put(key, objectAndValidity); 95 } 96 } 97 98 101 public void dispose() { 102 this.manager = null; 103 this.cache = null; 104 } 105 } 106 | Popular Tags |