KickJava   Java API By Example, From Geeks To Geeks.

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


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.tangosol.util.MapEvent;
16 import com.tangosol.util.MapListener;
17
18 /**
19  * The Class used to provide the connection between the adapter and the Cache for Listener adapters. </p>
20  * The Listener is for Coherence 3.x and above. </p>
21  * @author xenephon
22  * @version 1.4
23  * @since 1.2
24  *
25  */

26 public class CoherenceEventListener implements MapListener{
27
28     
29     IBaseAdaptor base = null;
30     
31     private static Log log = LogFactory
32     .getLog(CoherenceEventListener.class);
33     
34     /**
35      *
36      */

37     public CoherenceEventListener(IBaseAdaptor base) {
38         this.base = base;
39     }
40
41     /* (non-Javadoc)
42      * @see com.tangosol.util.MapListener#entryInserted(com.tangosol.util.MapEvent)
43      */

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

79     public void entryUpdated(MapEvent arg0) {
80         Object JavaDoc key = arg0.getKey();
81         Object JavaDoc value = arg0.getNewValue();
82         
83         key = base.decorateKey(key);
84         
85         try {
86             base.acquireUpdateLock();
87             try {
88                 InternalIndex index = base.getIndex();
89                 synchronized(base.getCacheLock(key))
90                 {
91                     //remove the original value
92
index.removeByKey(key);
93                     // insert into the index
94
index.insert(key,value);
95                 }
96                 if (log.isDebugEnabled()){
97                     log.debug("Update Event: entry added to index "+key + " value: "+ value);
98                 }
99             } catch (JoftiException e){
100                 log.error("Update Event: Unable to add index value for key "+key,e);
101                 // should we remove the value if we cannot add it?
102
}finally {
103                 base.releaseUpdateLock();
104             }
105             
106             
107         } catch (Exception JavaDoc e){
108             log.error(e);
109         }
110         
111         
112         
113     }
114
115     /* (non-Javadoc)
116      * @see com.tangosol.util.MapListener#entryDeleted(com.tangosol.util.MapEvent)
117      */

118     public void entryDeleted(MapEvent arg0) {
119         Object JavaDoc key = arg0.getKey();
120         key = base.decorateKey(key);
121         
122
123         try {
124             base.acquireUpdateLock();
125             try {
126                 InternalIndex index = base.getIndex();
127                 synchronized(base.getCacheLock(key))
128                 {
129                     // insert into the index
130
index.removeByKey(key);
131                 }
132                 if (log.isDebugEnabled()){
133                     log.debug("Remove Event: entry added to index "+key);
134                 }
135             } catch (JoftiException e){
136                 log.error("Add Event: Unable to add index value for key "+key,e);
137                 // should we remove the value if we cannot add it?
138
}finally {
139                 base.releaseUpdateLock();
140             }
141             
142             
143         } catch (Exception JavaDoc e){
144             log.error(e);
145         }
146         
147         
148         
149         
150     }
151
152 }
153
Popular Tags