1 16 package org.apache.cocoon.caching.impl; 17 18 import java.io.Serializable ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 import java.util.Set ; 22 23 import org.apache.avalon.framework.activity.Disposable; 24 import org.apache.avalon.framework.activity.Initializable; 25 import org.apache.avalon.framework.logger.AbstractLogEnabled; 26 import org.apache.avalon.framework.thread.ThreadSafe; 27 import org.apache.cocoon.caching.EventRegistry; 28 import org.apache.cocoon.caching.validity.Event; 29 import org.apache.commons.collections.MultiHashMap; 30 31 46 47 public abstract class AbstractDoubleMapEventRegistry 48 extends AbstractLogEnabled 49 implements Initializable, EventRegistry, Disposable, ThreadSafe { 50 51 private boolean m_init_success = false; 52 private MultiHashMap m_keyMMap; 53 private MultiHashMap m_eventMMap; 54 55 62 public void register(Event e, Serializable key) { 63 synchronized(this) { 64 m_keyMMap.put(key,e); 65 m_eventMMap.put(e,key); 66 } 67 } 68 69 72 public void clear() { 73 synchronized(this) { 74 m_keyMMap.clear(); 75 m_eventMMap.clear(); 76 } 77 } 78 79 82 public Serializable [] keysForEvent(Event e) { 83 Collection coll = (Collection )m_eventMMap.get(e); 84 if (coll==null || coll.isEmpty()) { 85 if (getLogger().isDebugEnabled()) { 86 getLogger().debug("The event map returned empty"); 87 } 88 return null; 89 } else { 90 return (Serializable [])coll.toArray(new Serializable [coll.size()]); 91 } 92 } 93 94 97 public Serializable [] allKeys() { 98 Set keys = this.m_keyMMap.keySet(); 99 return (Serializable [])keys.toArray( 100 new Serializable [keys.size()]); 101 } 102 103 107 public void removeKey(Serializable key) { 108 Collection coll = (Collection )m_keyMMap.get(key); 109 if (coll==null || coll.isEmpty()) { 110 return; 111 } 112 synchronized(this) { 115 Iterator it = coll.iterator(); 116 while (it.hasNext()) { 117 120 Object o = it.next(); 121 if (o != null) { 122 if (getLogger().isDebugEnabled()) { 123 getLogger().debug("Removing from event mapping: " + o.toString()); 124 } 125 m_eventMMap.remove(o,key); 126 } 127 } 128 129 m_keyMMap.remove(key); 132 } 133 } 134 135 140 public void initialize() throws Exception { 141 if (recover()) { 142 m_init_success = true; 143 } 144 } 145 146 149 public void dispose() { 150 EventRegistryDataWrapper ecdw = wrapRegistry(); 151 persist(ecdw); 152 m_keyMMap.clear(); 153 m_keyMMap = null; 154 m_eventMMap.clear(); 155 m_eventMMap = null; 156 } 157 158 161 public boolean wasRecoverySuccessful() { 162 return m_init_success; 163 } 164 165 protected EventRegistryDataWrapper wrapRegistry() { 166 EventRegistryDataWrapper ecdw = new EventRegistryDataWrapper(); 167 ecdw.setupMaps(this.m_keyMMap, this.m_eventMMap); 168 return ecdw; 169 } 170 171 protected void unwrapRegistry(EventRegistryDataWrapper ecdw) { 172 this.m_eventMMap = ecdw.get_eventMap(); 173 this.m_keyMMap = ecdw.get_keyMap(); 174 } 175 176 protected final void createBlankCache() { 177 this.m_eventMMap = new MultiHashMap(100); 179 this.m_keyMMap = new MultiHashMap(100); 180 } 181 182 190 protected abstract boolean recover(); 191 192 195 protected abstract void persist(EventRegistryDataWrapper wrapper); 196 197 } 198 | Popular Tags |