KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > cache > adapter > listener > EhEventListener


1 /*
2  * Created on 30-Oct-2005
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

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 /**
23  * The Class used to provide the connection between the adapter and the Cache for Listener adapters. </p>
24  * The Listener is for EHCache 1.2 and above. </p>
25  * @author xenephon
26  * @version 1.8
27  * @since 1.1
28  *
29  */

30 public class EhEventListener implements CacheEventListener{
31
32     /* (non-Javadoc)
33      * @see net.sf.ehcache.event.CacheEventListener#notifyElementRemoved(net.sf.ehcache.Cache, net.sf.ehcache.Element)
34      */

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     /* (non-Javadoc)
48      * @see net.sf.ehcache.event.CacheEventListener#notifyElementRemoved(net.sf.ehcache.Cache, net.sf.ehcache.Element)
49      */

50     public void notifyElementRemoved(Ehcache arg0, Element arg1) {
51         if (arg1 == null){
52             return;
53         }
54         Object JavaDoc key = arg1.getKey();
55         
56         key = base.decorateKey(key);
57         try {
58             base.acquireUpdateLock();
59             try {
60                 
61                 // lock on the key lock - so we do not get other threads interfering for the same/similar
62
// key
63
synchronized(base.getCacheLock(key))
64                 {
65                     base.getIndex().removeByKey((Comparable JavaDoc)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 JavaDoc e){
77             log.error("unable to aquire update lock",e);
78         }
79         
80     }
81
82     /* (non-Javadoc)
83      * @see net.sf.ehcache.event.CacheEventListener#notifyElementPut(net.sf.ehcache.Cache, net.sf.ehcache.Element)
84      */

85     public void notifyElementPut(Ehcache arg0, Element arg1) {
86         if (arg1 == null){
87             return;
88         }
89         
90         Object JavaDoc key = arg1.getKey();
91         Object JavaDoc value = arg1.getValue();
92         key = base.decorateKey(key);
93         
94         if (key == null){
95             throw new RuntimeException JavaDoc("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                     // insert into the index
105
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                 // should we remove the value if we cannot add it?
113
}finally {
114                 base.releaseUpdateLock();
115             }
116             
117             
118         } catch (Exception JavaDoc e){
119             log.error(e);
120         }
121         
122         
123     }
124
125     /* (non-Javadoc)
126      * @see net.sf.ehcache.event.CacheEventListener#notifyElementExpired(net.sf.ehcache.Cache, net.sf.ehcache.Element)
127      */

128     public void notifyElementExpired(Ehcache arg0, Element arg1) {
129         if (arg1 == null){
130             return;
131         }
132         Object JavaDoc key = arg1.getKey();
133         //Object value = arg1.getValue();
134
key = base.decorateKey(key);
135         try {
136             base.acquireUpdateLock();
137             try {
138                 
139                 // lock on the key lock - so we do not get other threads interfering for the same/similar
140
// key
141
synchronized(base.getCacheLock(key))
142                 {
143                     base.getIndex().removeByKey((Comparable JavaDoc)key);
144                 }
145             // log.info("Flush Event: removed from index "+key + " value "+ value);
146
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 JavaDoc e){
156             log.error("unable to aquire update lock",e);
157         }
158         
159     }
160
161
162     /* (non-Javadoc)
163      * @see net.sf.ehcache.event.CacheEventListener#notifyElementUpdated(net.sf.ehcache.Cache, net.sf.ehcache.Element)
164      */

165     public void notifyElementUpdated(Ehcache cache, Element element) throws CacheException
166     {
167         if (element == null){
168             return;
169         }
170         Object JavaDoc key = element.getKey();
171         Object JavaDoc value = element.getValue();
172         key = base.decorateKey(key);
173         
174         if (key == null){
175             throw new RuntimeException JavaDoc("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                         // null op if it does not exist
189
index.removeByKey(key);
190                                  
191                         // insert into the index
192
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                 // should we remove the value if we cannot add it?
200
}finally {
201                 base.releaseUpdateLock();
202             }
203             
204             
205         } catch (Exception JavaDoc e){
206             log.error(e);
207         }
208         
209         
210     }
211
212     public Object JavaDoc clone(){
213         return null;
214     }
215
216     public void dispose()
217     {
218        // we do not have to do anything here
219

220     }
221
222
223     public void notifyElementEvicted(Ehcache arg0, Element arg1)
224     {
225         // we do not care
226

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                 // should we remove the value if we cannot add it?
240
}finally {
241                 base.releaseUpdateLock();
242             }
243         } catch (Exception JavaDoc e){
244             log.error(e);
245         }
246     }
247     
248 }
249
250
251
Popular Tags