1 16 package org.apache.cocoon.caching.impl; 17 18 import java.io.Serializable ; 19 import java.util.Iterator ; 20 21 import org.apache.avalon.framework.activity.Initializable; 22 import org.apache.avalon.framework.service.ServiceException; 23 import org.apache.avalon.framework.service.ServiceManager; 24 import org.apache.cocoon.ProcessingException; 25 import org.apache.cocoon.caching.CachedResponse; 26 import org.apache.cocoon.caching.EventAware; 27 import org.apache.cocoon.caching.EventRegistry; 28 import org.apache.cocoon.caching.validity.Event; 29 import org.apache.cocoon.caching.validity.EventValidity; 30 import org.apache.cocoon.components.source.impl.SitemapSource; 31 import org.apache.excalibur.source.SourceValidity; 32 import org.apache.excalibur.source.impl.validity.AbstractAggregatedValidity; 33 34 41 public class EventAwareCacheImpl extends CacheImpl implements Initializable, 42 EventAware { 43 44 private ServiceManager m_manager; 45 46 private EventRegistry m_eventRegistry; 47 48 52 public void clear() { 53 super.clear(); 54 m_eventRegistry.clear(); 55 } 56 57 65 public void store(Serializable key, 66 CachedResponse response) 67 throws ProcessingException { 68 SourceValidity[] validities = response.getValidityObjects(); 69 for (int i=0; i< validities.length;i++) { 70 SourceValidity val = validities[i]; 71 examineValidity(val, key); 72 } 73 super.store(key, response); 74 } 75 76 84 85 88 public void service(ServiceManager manager) throws ServiceException { 89 this.m_manager = manager; 90 super.service(manager); 91 this.m_eventRegistry = (EventRegistry)manager.lookup(EventRegistry.ROLE); 92 } 93 94 98 public void remove(Serializable key) { 99 super.remove(key); 100 m_eventRegistry.removeKey(key); 101 } 102 103 109 public void processEvent(Event e) { 110 if (e == null) return; 111 Serializable [] keys = m_eventRegistry.keysForEvent(e); 112 if (keys == null) return; 113 for (int i=0;i<keys.length; i++) { 114 if (keys[i] != null) { 115 if (getLogger().isDebugEnabled()) { 116 getLogger().debug("Processing cache event, found Pipeline key: " + keys[i].toString()); 117 } 118 123 remove(keys[i]); 124 } 125 } 126 } 127 128 132 public void initialize() throws Exception { 133 if (!m_eventRegistry.wasRecoverySuccessful()) { 134 super.clear(); 135 } else { 136 veryifyEventCache(); 138 } 139 } 140 141 149 public void veryifyEventCache() { 150 Serializable [] keys = m_eventRegistry.allKeys(); 151 if (keys == null) return; 152 for (int i=0; i<keys.length; i++) { 153 if (!this.containsKey(keys[i])) { 154 m_eventRegistry.removeKey(keys[i]); 155 if (getLogger().isDebugEnabled()) { 156 getLogger().debug("Cache key no longer valid: " + 157 keys[i]); 158 } 159 } 160 } 161 } 162 163 166 public void dispose() { 167 m_manager.release(m_eventRegistry); 168 super.dispose(); 169 m_manager = null; 170 m_eventRegistry = null; 171 } 172 173 private void examineValidity(SourceValidity val, Serializable key) { 174 if (val instanceof AbstractAggregatedValidity) { 175 handleAggregatedValidity((AbstractAggregatedValidity)val, key); 176 } else if (val instanceof EventValidity) { 177 handleEventValidity((EventValidity)val, key); 178 } else if (val instanceof SitemapSource.SitemapSourceValidity) { 179 examineValidity(((SitemapSource.SitemapSourceValidity) val).getNestedValidity(), key); 180 } 181 } 182 183 private void handleAggregatedValidity( 184 AbstractAggregatedValidity val, 185 Serializable key) { 186 Iterator it = val.getValidities().iterator(); 188 while (it.hasNext()) { 189 SourceValidity thisVal = (SourceValidity)it.next(); 190 examineValidity(thisVal, key); 192 } 193 } 194 195 private void handleEventValidity(EventValidity val, Serializable key) { 196 if (getLogger().isDebugEnabled()) { 197 getLogger().debug("Found EventValidity: " + val.toString()); 198 } 199 m_eventRegistry.register(val.getEvent(),key); 200 } 201 202 } 203 | Popular Tags |