1 7 package com.jofti.cache.adapter.listener; 8 9 import net.sf.ehcache.Cache; 10 import net.sf.ehcache.CacheException; 11 import net.sf.ehcache.Ehcache; 12 import net.sf.ehcache.Element; 13 import net.sf.ehcache.event.CacheEventListener; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 18 import com.jofti.cache.IBaseAdaptor; 19 import com.jofti.core.InternalIndex; 20 import com.jofti.exception.JoftiException; 21 22 30 public class EhEventListener implements CacheEventListener{ 31 32 35 IBaseAdaptor base = null; 36 37 private static Log log = LogFactory 38 .getLog(EhEventListener.class); 39 40 41 42 public EhEventListener(IBaseAdaptor base){ 43 this.base =base; 44 } 45 46 47 50 public void notifyElementRemoved(Ehcache arg0, Element arg1) { 51 if (arg1 == null){ 52 return; 53 } 54 Object key = arg1.getKey(); 55 56 key = base.decorateKey(key); 57 try { 58 base.acquireUpdateLock(); 59 try { 60 61 synchronized(base.getCacheLock(key)) 64 { 65 base.getIndex().removeByKey((Comparable )key); 66 } 67 if (log.isDebugEnabled()){ 68 log.debug("Remove event: removed from index "+key ); 69 } 70 } catch (JoftiException e){ 71 log.error("Remove event: Unable to remove value for key "+key,e); 72 }finally { 73 base.releaseUpdateLock(); 74 } 75 76 } catch (Exception e){ 77 log.error("unable to aquire update lock",e); 78 } 79 80 } 81 82 85 public void notifyElementPut(Ehcache arg0, Element arg1) { 86 if (arg1 == null){ 87 return; 88 } 89 90 Object key = arg1.getKey(); 91 Object value = arg1.getValue(); 92 key = base.decorateKey(key); 93 94 if (key == null){ 95 throw new RuntimeException ("key is null for put "+ arg1); 96 } 97 try { 98 base.acquireUpdateLock(); 99 try { 100 InternalIndex index = base.getIndex(); 101 synchronized(base.getCacheLock(key)) 102 { 103 104 index.insert(key,value); 106 } 107 if (log.isDebugEnabled()){ 108 log.debug("Add Event: entry added to index "+key + " value: "+ value); 109 } 110 } catch (JoftiException e){ 111 log.error("Add Event: Unable to add index value for key "+key,e); 112 }finally { 114 base.releaseUpdateLock(); 115 } 116 117 118 } catch (Exception e){ 119 log.error(e); 120 } 121 122 123 } 124 125 128 public void notifyElementExpired(Ehcache arg0, Element arg1) { 129 if (arg1 == null){ 130 return; 131 } 132 Object key = arg1.getKey(); 133 key = base.decorateKey(key); 135 try { 136 base.acquireUpdateLock(); 137 try { 138 139 synchronized(base.getCacheLock(key)) 142 { 143 base.getIndex().removeByKey((Comparable )key); 144 } 145 if (log.isDebugEnabled()){ 147 log.debug("Expire Event: removed from index "+key ); 148 } 149 } catch (JoftiException e){ 150 log.error("Expire event: Unable to remove value for key "+key,e); 151 }finally { 152 base.releaseUpdateLock(); 153 } 154 155 } catch (Exception e){ 156 log.error("unable to aquire update lock",e); 157 } 158 159 } 160 161 162 165 public void notifyElementUpdated(Ehcache cache, Element element) throws CacheException 166 { 167 if (element == null){ 168 return; 169 } 170 Object key = element.getKey(); 171 Object value = element.getValue(); 172 key = base.decorateKey(key); 173 174 if (key == null){ 175 throw new RuntimeException ("key is null for put "+ element); 176 } 177 try { 178 base.acquireUpdateLock(); 179 try { 180 InternalIndex index = base.getIndex(); 181 synchronized(base.getCacheLock(key)) 182 { 183 184 if (log.isDebugEnabled()) { 185 log.debug("Object does exist so remove " + key); 186 } 187 188 index.removeByKey(key); 190 191 index.insert(key,value); 193 } 194 if (log.isDebugEnabled()){ 195 log.debug("Add Event: entry added to index "+key + " value: "+ value); 196 } 197 } catch (JoftiException e){ 198 log.error("Add Event: Unable to add index value for key "+key,e); 199 }finally { 201 base.releaseUpdateLock(); 202 } 203 204 205 } catch (Exception e){ 206 log.error(e); 207 } 208 209 210 } 211 212 public Object clone(){ 213 return null; 214 } 215 216 public void dispose() 217 { 218 220 } 221 222 223 public void notifyElementEvicted(Ehcache arg0, Element arg1) 224 { 225 227 } 228 229 230 public void notifyRemoveAll(Ehcache arg0) 231 { 232 try { 233 base.acquireUpdateLock(); 234 try { 235 InternalIndex index = base.getIndex(); 236 index.removeAll(); 237 } catch (JoftiException e){ 238 log.error("Add Event: Unable to remove all values "); 239 }finally { 241 base.releaseUpdateLock(); 242 } 243 } catch (Exception e){ 244 log.error(e); 245 } 246 } 247 248 } 249 250 251 | Popular Tags |