1 16 package org.apache.cocoon.transformation.helpers; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.logger.Logger; 24 import org.apache.avalon.framework.service.ServiceManager; 25 import org.apache.cocoon.caching.CachedResponse; 26 import org.apache.cocoon.components.sax.XMLSerializer; 27 import org.apache.cocoon.components.source.SourceUtil; 28 import org.apache.excalibur.source.Source; 29 import org.apache.excalibur.source.SourceResolver; 30 import org.apache.excalibur.source.SourceValidity; 31 import org.apache.excalibur.source.impl.validity.ExpiresValidity; 32 33 41 public final class PreemptiveLoader { 42 43 private static final PreemptiveLoader instance = new PreemptiveLoader(); 44 45 46 private Map cacheStorageProxyMap = new HashMap (20); 47 48 private List loadList = new ArrayList (50); 49 50 boolean alive = false; 51 52 56 static PreemptiveLoader getInstance() { 57 return instance; 58 } 59 60 66 public void add(IncludeCacheStorageProxy proxy, String uri, long expires) { 67 boolean addItem = true; 68 List uriList = (List )this.cacheStorageProxyMap.get(proxy); 69 if ( null == uriList ) { 70 uriList = new ArrayList (50); 71 this.cacheStorageProxyMap.put(proxy, uriList); 72 } else { 73 synchronized (uriList) { 74 if (uriList.contains(uri)) { 76 addItem = false; 77 } 78 } 79 } 80 if ( addItem ) { 81 uriList.add(uri); 82 this.loadList.add(new Object [] {proxy, uri, new Long (expires), uriList}); 83 } 84 85 synchronized (this.cacheStorageProxyMap) { 86 this.cacheStorageProxyMap.notify(); 87 } 88 } 89 90 96 public void process(ServiceManager manager, 97 SourceResolver resolver, 98 Logger logger) { 99 this.alive = true; 100 if (logger.isDebugEnabled()) { 101 logger.debug("PreemptiveLoader: Starting preemptive loading"); 102 } 103 104 while (this.alive) { 105 while (this.loadList.size() > 0) { 106 Object [] object = (Object [])this.loadList.get(0); 107 final String uri = (String )object[1]; 108 this.loadList.remove(0); 109 synchronized (object[3]) { 110 ((List )object[3]).remove(uri); 111 } 112 113 Source source = null; 114 XMLSerializer serializer = null; 115 116 try { 117 if (logger.isDebugEnabled()) { 118 logger.debug("PreemptiveLoader: Loading " + uri); 119 } 120 121 source = resolver.resolveURI(uri); 122 serializer = (XMLSerializer)manager.lookup(XMLSerializer.ROLE); 123 124 SourceUtil.toSAX(source, serializer); 125 126 SourceValidity[] validities = new SourceValidity[1]; 127 validities[0] = new ExpiresValidity(((Long )object[2]).longValue() * 1000); CachedResponse response = new CachedResponse(validities, 129 (byte[])serializer.getSAXFragment()); 130 ((IncludeCacheStorageProxy)object[0]).put(uri, response); 131 132 } catch (Exception ignore) { 133 } finally { 135 resolver.release( source ); 136 manager.release( serializer ); 137 } 138 if (logger.isDebugEnabled()) { 139 logger.debug("PreemptiveLoader: Finished loading " + uri); 140 } 141 } 142 synchronized (this.cacheStorageProxyMap) { 143 try { 144 this.cacheStorageProxyMap.wait(); 145 } catch (InterruptedException e) { 146 } 147 } 148 } 149 if (logger.isDebugEnabled()) { 150 logger.debug("PreemptiveLoader: Finished preemptive loading"); 151 } 152 } 153 154 158 synchronized public void stop() { 159 this.alive = false; 160 synchronized (this.cacheStorageProxyMap) { 161 this.cacheStorageProxyMap.notify(); 162 } 163 } 164 } 165 | Popular Tags |