KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12 import com.jofti.cache.IBaseAdaptor;
13 import com.jofti.core.InternalIndex;
14 import com.jofti.exception.JoftiException;
15 import com.opensymphony.oscache.base.events.CacheEntryEvent;
16 import com.opensymphony.oscache.base.events.CacheEntryEventListener;
17 import com.opensymphony.oscache.base.events.CacheGroupEvent;
18 import com.opensymphony.oscache.base.events.CachePatternEvent;
19 import com.opensymphony.oscache.base.events.CachewideEvent;
20
21 /**
22  * The Class used to provide the connection between the adapter and the Cache for Listener adapters. </p>
23  * The Listener is for OSCache. </p>
24  *
25  * @author xenephon
26  * @version 1.0
27  */

28 public class OSEventListener implements CacheEntryEventListener {
29
30     /* (non-Javadoc)
31      * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheEntryAdded(com.opensymphony.oscache.base.events.CacheEntryEvent)
32      */

33     IBaseAdaptor base = null;
34
35     private static Log log = LogFactory.getLog(OSEventListener.class);
36
37     public OSEventListener(IBaseAdaptor base) {
38         this.base = base;
39     }
40
41     /* (non-Javadoc)
42      * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheEntryAdded(com.opensymphony.oscache.base.events.CacheEntryEvent)
43      */

44     public void cacheEntryAdded(CacheEntryEvent arg0) {
45         // get the key
46
String JavaDoc key = arg0.getKey();
47         Object JavaDoc value = arg0.getEntry().getContent();
48
49         try {
50             base.acquireUpdateLock();
51             try {
52
53                 synchronized (base.getCacheLock(key)) {
54                     // insert into the index
55
base.getIndex().insert(key, value);
56                 }
57                 if (log.isDebugEnabled()) {
58                     log.debug("Add Event: entry added to index " + key
59                             + " value: " + value);
60                 }
61             } catch (JoftiException e) {
62                 log.error(
63                         "Add Event: Unable to add index value for key " + key,
64                         e);
65                 // should we remove the value if we cannot add it?
66
} finally {
67                 base.releaseUpdateLock();
68             }
69
70         } catch (Exception JavaDoc e) {
71             log.warn(e);
72         }
73
74     }
75
76     /* (non-Javadoc)
77      * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheEntryFlushed(com.opensymphony.oscache.base.events.CacheEntryEvent)
78      */

79     public void cacheEntryFlushed(CacheEntryEvent arg0) {
80         String JavaDoc key = arg0.getKey();
81
82         try {
83             base.acquireUpdateLock();
84             try {
85
86                 // lock on the key lock - so we do not get other threads interfering for the same/similar
87
// key
88
synchronized (base.getCacheLock(key)) {
89                     base.getIndex().removeByKey((Comparable JavaDoc) key);
90                 }
91                 // log.info("Flush Event: removed from index "+key + " value "+ value);
92
if (log.isDebugEnabled()) {
93                     log.debug("Flush Event: removed from index " + key);
94                 }
95             } catch (JoftiException e) {
96                 log.error("Flush event: Unable to remove value for key " + key,
97                         e);
98             } finally {
99                 base.releaseUpdateLock();
100             }
101
102         } catch (Exception JavaDoc e) {
103             log.error("unable to aquire update lock", e);
104         }
105
106     }
107
108     /* (non-Javadoc)
109      * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheEntryRemoved(com.opensymphony.oscache.base.events.CacheEntryEvent)
110      */

111     public void cacheEntryRemoved(CacheEntryEvent arg0) {
112         String JavaDoc key = arg0.getKey();
113
114         try {
115             base.acquireUpdateLock();
116             try {
117
118                 // lock on the key lock - so we do not get other threads interfering for the same/similar
119
// key
120
synchronized (base.getCacheLock(key)) {
121                     base.getIndex().removeByKey((Comparable JavaDoc) key);
122                 }
123                 if (log.isDebugEnabled()) {
124                     log.debug("Remove event: removed from index " + key);
125                 }
126             } catch (JoftiException e) {
127                 log.error(
128                         "Remove event: Unable to remove value for key " + key,
129                         e);
130             } finally {
131                 base.releaseUpdateLock();
132             }
133
134         } catch (Exception JavaDoc e) {
135             log.error("unable to aquire update lock", e);
136         }
137     }
138
139     /* (non-Javadoc)
140      * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheEntryUpdated(com.opensymphony.oscache.base.events.CacheEntryEvent)
141      */

142     public void cacheEntryUpdated(CacheEntryEvent arg0) {
143         String JavaDoc key = arg0.getKey();
144         Object JavaDoc value = arg0.getEntry().getContent();
145
146         InternalIndex index = base.getIndex();
147         try {
148             base.acquireUpdateLock();
149             try {
150
151                 //lock on the key lock - so we do not get other threads interfering for the same/similar
152
// key
153
synchronized (base.getCacheLock(key)) {
154                     index.removeByKey(key);
155                     index.insert(key, value);
156                 }
157                 if (log.isDebugEnabled()) {
158                     log.debug("Update event: inserted into index " + key
159                             + " value " + value);
160                 }
161             } catch (JoftiException e) {
162                 log.error("Update event: Unable to index value for key " + key,
163                         e);
164             } finally {
165                 base.releaseUpdateLock();
166             }
167
168         } catch (Exception JavaDoc e) {
169             log.error("unable to aquire update lock", e);
170         }
171     }
172
173     /* (non-Javadoc)
174      * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheGroupFlushed(com.opensymphony.oscache.base.events.CacheGroupEvent)
175      */

176     public void cacheGroupFlushed(CacheGroupEvent arg0) {
177         // we do not care about this
178
log.debug("IndexCache group flushed");
179
180     }
181
182     /* (non-Javadoc)
183      * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cachePatternFlushed(com.opensymphony.oscache.base.events.CachePatternEvent)
184      */

185     public void cachePatternFlushed(CachePatternEvent arg0) {
186         // we do not care about this
187
log.debug("IndexCache pattern flushed");
188     }
189
190     /* (non-Javadoc)
191      * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheFlushed(com.opensymphony.oscache.base.events.CachewideEvent)
192      */

193     public void cacheFlushed(CachewideEvent arg0) {
194
195         log.debug("All IndexCache flushed");
196
197     }
198
199 }
200
Popular Tags