1 17 18 package org.apache.lenya.ac.cache; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.net.MalformedURLException ; 23 24 import org.apache.avalon.framework.activity.Disposable; 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.excalibur.source.Source; 30 import org.apache.excalibur.source.SourceNotFoundException; 31 import org.apache.excalibur.source.SourceResolver; 32 import org.apache.excalibur.source.SourceValidity; 33 import org.apache.lenya.util.CacheMap; 34 35 39 public class SourceCacheImpl 40 extends AbstractLogEnabled 41 implements SourceCache, Serviceable, Disposable { 42 43 47 public ServiceManager getManager() { 48 return manager; 49 } 50 51 55 public SourceResolver getResolver() { 56 return resolver; 57 } 58 59 62 public SourceCacheImpl() { 63 cache = new CacheMap(CAPACITY); 64 } 65 66 public static final int CAPACITY = 1000; 67 private CacheMap cache; 68 69 73 protected CacheMap getCache() { 74 return cache; 75 } 76 77 80 public Object get(String sourceUri, InputStreamBuilder builder) throws CachingException { 81 82 String key = sourceUri; 83 Object value = null; 84 85 CachedObject cachedObject = (CachedObject) getCache().get(key); 86 boolean usedCache = false; 87 SourceValidity sourceValidity = null; 88 89 try { 90 91 if (cachedObject != null) { 92 if (getLogger().isDebugEnabled()){ 93 getLogger().debug("Found cached object [" + cachedObject + "]"); 94 } 95 SourceValidity cachedValidity = cachedObject.getValidityObject(); 96 97 int result = cachedValidity.isValid(); 98 boolean valid = false; 99 if (result == 0) { 100 101 103 sourceValidity = getSourceValidity(sourceUri); 104 105 if (sourceValidity != null) { 106 result = cachedValidity.isValid(sourceValidity); 107 if (result == 0) { 108 sourceValidity = null; 109 } else { 110 valid = (result == 1); 111 } 112 } 113 } else { 114 valid = (result > 0); 115 } 116 117 if (valid) { 118 if (this.getLogger().isDebugEnabled()) { 119 this.getLogger().debug( 120 "Using valid cached source for '" + sourceUri + "'."); 121 } 122 usedCache = true; 123 value = cachedObject.getValue(); 124 } else { 125 if (this.getLogger().isDebugEnabled()) { 126 this.getLogger().debug( 127 "Cached content is invalid for '" + sourceUri + "'."); 128 } 129 getCache().remove(key); 131 } 132 133 } else { 134 getLogger().debug("Did not find cached object."); 135 } 136 137 if (!usedCache) { 138 getLogger().debug("Did not use cache."); 139 if (key != null) { 140 if (sourceValidity == null) { 141 sourceValidity = getSourceValidity(sourceUri); 142 } 143 if (sourceValidity != null) { 144 if (getLogger().isDebugEnabled()) { 145 getLogger().debug("Source validity is not null."); 146 } 147 } else { 148 if (getLogger().isDebugEnabled()) { 149 getLogger().debug("Source validity is null - not caching."); 150 } 151 key = null; 152 } 153 } 154 155 value = buildObject(sourceUri, builder); 156 157 if (key != null) { 159 if (this.getLogger().isDebugEnabled()) { 160 this.getLogger().debug( 161 "Caching object [" 162 + value 163 + "] for further requests of [" 164 + sourceUri 165 + "]."); 166 } 167 getCache().put(key, new CachedObject(sourceValidity, value)); 168 } 169 } 170 171 } catch (Exception e) { 172 throw new CachingException(e); 173 } 174 175 return value; 176 } 177 178 188 protected Object buildObject(String sourceUri, InputStreamBuilder builder) 189 throws MalformedURLException , IOException , SourceNotFoundException, BuildException { 190 Object value = null; 191 Source source = null; 192 try { 193 source = getResolver().resolveURI(sourceUri); 194 if (source.exists()) { 195 InputStream stream = source.getInputStream(); 196 value = builder.build(stream); 197 } 198 } finally { 199 if (source != null) { 200 getResolver().release(source); 201 } 202 } 203 return value; 204 } 205 206 213 protected SourceValidity getSourceValidity(String sourceUri) 214 throws MalformedURLException , IOException { 215 SourceValidity sourceValidity; 216 Source source = null; 217 try { 218 source = getResolver().resolveURI(sourceUri); 219 sourceValidity = source.getValidity(); 220 } finally { 221 if (source != null) { 222 getResolver().release(source); 223 } 224 } 225 return sourceValidity; 226 } 227 228 private ServiceManager manager; 229 private SourceResolver resolver; 230 231 234 public void service(ServiceManager manager) throws ServiceException { 235 this.manager = manager; 236 this.resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 237 } 238 239 242 public void dispose() { 243 if (getResolver() != null) { 244 getManager().release(getResolver()); 245 } 246 } 247 248 } 249 | Popular Tags |