KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > cache > adapter > CoherenceListenerAdapter


1 package com.jofti.cache.adapter;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.Comparator JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.Properties JavaDoc;
9 import java.util.Set JavaDoc;
10
11
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import com.jofti.api.CacheAccessor;
17 import com.jofti.api.IndexQuery;
18 import com.jofti.cache.CacheListenerAdapter;
19 import com.jofti.cache.BaseAdaptor;
20 import com.jofti.cache.adapter.listener.CoherenceEventListener;
21 import com.jofti.core.INameSpaceAware;
22 import com.jofti.core.IParsedQuery;
23 import com.jofti.core.InternalIndex;
24 import com.jofti.core.QueryId;
25 import com.jofti.core.QueryType;
26 import com.jofti.exception.JoftiException;
27 import com.jofti.util.CompositeComparator;
28 import com.tangosol.net.NamedCache;
29 import com.tangosol.util.MapEvent;
30
31 /**
32
33  *
34  * The adapter implementation specific to Coherence.</p>
35  *
36  * The adapter takes care of the implementation specific details for converting
37  * between the process the index expects and the behaviour of Coherence.</p>
38  *
39  * The Listener adapter is for use as a listener to Coherence and does not provide the get, set, remove methods
40  * that the wrapper adapter provides. The addition, removal and getting of values from the Cache must be
41  * done on the Cache implementation directly. This is the preferred way of using an already existing Cache.</p>
42  *
43  * The start up of the adapter will also try and index any elements already in
44  * the cache.<P>
45  *
46  * @author Steve Woodcock
47  * <p>
48  * @version 1.0
49  * @since 1.1
50  */

51 public class CoherenceListenerAdapter extends BaseAdaptor implements CacheAccessor, CacheListenerAdapter
52 {
53
54
55     private static Log log = LogFactory
56                                                     .getLog(CoherenceListenerAdapter.class);
57
58
59     private String JavaDoc name;
60
61     public InternalIndex index;
62
63     private CoherenceEventListener eventListener = null;
64     
65     
66     private NamedCache cache =null;
67
68
69     public CoherenceListenerAdapter()
70     {
71     }
72
73     public CoherenceListenerAdapter(Object JavaDoc cache)
74     {
75         this.cache = (NamedCache) cache;
76
77     }
78     /* (non-Javadoc)
79      * @see com.jofti.cache.LifeCycleAdapter#init(java.util.Properties)
80      */

81     public synchronized void init(Properties JavaDoc properties) throws JoftiException
82     {
83         if (properties != null) {
84             String JavaDoc key = null;
85             for (Iterator JavaDoc it = properties.keySet().iterator(); it.hasNext();) {
86                 key = (String JavaDoc) it.next();
87                 if (MUTABLE_VALUES.equalsIgnoreCase(key)) {
88                     checkMutable = Boolean.valueOf(
89                             properties.getProperty(key)).booleanValue();
90                     if (log.isInfoEnabled()) {
91                         log.info("Mutability checking is set to "
92                                 + checkMutable);
93                     }
94                 }
95                 if ("file".equalsIgnoreCase(key)) {
96                     log.warn("Listener adapters cannot be used to start up a Cache - check docs for Adapter Type");
97                     throw new JoftiException("Listener adapters cannot be used to start up a Cache - check docs for Adapter Type");
98                 }
99             }
100         }
101
102      
103         if (cache == null) {
104             throw new JoftiException("Cache cannot be null for listener adapter");
105         } else {
106                 log.info("Index started using cache "+ cache.getCacheName());
107         }
108         eventListener = new CoherenceEventListener(this);
109     
110     }
111
112     
113  
114    
115
116     /* (non-Javadoc)
117      * @see com.jofti.cache.LifeCycleAdapter#destroy()
118      */

119     public synchronized void destroy() throws JoftiException
120     {
121         try {
122             cache.removeMapListener(eventListener);
123            if (index != null){
124                 index.removeAll();
125            }
126         } catch (IllegalStateException JavaDoc e) {
127             throw new JoftiException(e);
128         }
129     }
130
131     /* (non-Javadoc)
132      * @see com.jofti.cache.LifeCycleAdapter#getName()
133      */

134     public String JavaDoc getName()
135     {
136         return name;
137     }
138
139     /* (non-Javadoc)
140      * @see com.jofti.cache.LifeCycleAdapter#setName(java.lang.String)
141      */

142     public void setName(String JavaDoc name)
143     {
144         this.name = name;
145     }
146
147     public String JavaDoc toString()
148     {
149         return "CoherenceCache(" + getName() + ')';
150     }
151
152   
153
154
155     /*
156      * (non-Javadoc)
157      *
158      * @see com.jofticache.CacheAdapter#setInternalIndex(com.jofti.core.InternalIndex)
159      */

160     public void setInternalIndex(InternalIndex index)
161     {
162         this.index = index;
163
164     }
165
166     /* (non-Javadoc)
167      * @see com.jofti.api.IndexCache#query(com.jofti.api.IndexQuery)
168      */

169     public Map JavaDoc query(IndexQuery query) throws JoftiException
170     {
171         
172         Map JavaDoc temp = null;
173         
174         query = processQuery(query,index.getParserManager());
175        
176         acquireQueryLock();
177         try {
178         
179         temp=index.query(query);
180         } finally {
181             releaseQueryLock();
182         }
183         return getCacheValues(temp, (IParsedQuery)query,index.getIntrospector());
184
185     }
186
187     
188
189     /* (non-Javadoc)
190      * @see com.jofti.cache.CacheLocking#checkMutable(java.lang.Object, java.lang.Object)
191      */

192     protected Object JavaDoc checkMutable(Object JavaDoc key, Object JavaDoc result)
193     {
194         try {
195             // first parse the object - again
196
Map JavaDoc cacheObjectValues = index.getIntrospector().getAttributeValues(
197                     result);
198
199             Map JavaDoc indexObjectValues = index.getAttributesByKey(decorateKey( key));
200             
201             if (cacheObjectValues.equals(indexObjectValues)) {
202                 return result;
203             } else {
204                 if (log.isDebugEnabled()) {
205                     log.debug("Object under Key " + key
206                             + " - attributes changed without re-insert");
207                 }
208             }
209             // then get the values for the key
210

211         } catch (JoftiException e) {
212             log.warn("Error checking mutability", e);
213         }
214
215         return null;
216     }
217
218     /**
219      * @param cache
220      */

221     public void setCacheImpl(Object JavaDoc cache){
222         CoherenceListenerAdapter.this.cache = (NamedCache)cache;
223         if (this.cache != null){
224             this.cache.addMapListener(eventListener);
225         }
226         
227     }
228     
229     protected Object JavaDoc getCacheValue(Object JavaDoc key) {
230         
231         return cache.get(key);
232         
233     }
234
235     /* (non-Javadoc)
236      * @see com.jofti.cache.LifeCycleAdapter#start()
237      */

238     public void start() throws JoftiException {
239         if (cache != null){
240             cache.addMapListener(eventListener);
241
242                 loadInitialValues(cache);
243             
244               
245             
246         }
247         
248         
249     }
250
251       private void loadInitialValues(NamedCache cache)
252             throws JoftiException {
253         Set JavaDoc entries = cache.entrySet();
254          int size = entries.size();
255             Iterator JavaDoc it = entries.iterator();
256         for (int i=0;i<size;i++) {
257             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
258
259             eventListener.entryInserted(new MapEvent(cache,1,entry.getKey(),entry.getValue(),entry.getValue()));
260
261         }
262     }
263
264
265     /* (non-Javadoc)
266      * @see com.jofti.cache.CacheLocking#getIndex()
267      */

268     public InternalIndex getIndex() {
269         
270         return index;
271     }
272
273     /* (non-Javadoc)
274      * @see com.jofti.api.CacheAccessor#getCacheImpl()
275      */

276     public Object JavaDoc getCacheImpl() {
277         
278         return cache;
279     }
280
281     public IndexQuery addQuery(String JavaDoc name, IndexQuery query)throws JoftiException {
282         
283         return index.getParserManager().addQuery(name, query);
284     }
285
286     /* (non-Javadoc)
287      * @see com.jofti.api.Index#getQuery(java.lang.String)
288      */

289     public IndexQuery getQuery(String JavaDoc name) {
290         
291         return index.getParserManager().getQuery(name);
292     }
293     
294 }
Popular Tags