KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.jofti.cache.adapter;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.Properties JavaDoc;
8
9 import net.sf.ehcache.Cache;
10 import net.sf.ehcache.CacheException;
11 import net.sf.ehcache.Element;
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.EhEventListener;
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
29 /**
30
31  *
32  * The adapter implementation specific to EHCache.</p>
33  *
34  * The adapter takes care of the implementation specific details for converting
35  * between the process the index expects and the behaviour of EHCache.</p>
36  *
37  * The Listener adapter is for use as a listener to EHCache and does not provide the get, set, remove methods
38  * that the wrapper adapter provides. The addition, removal and getting of values from the Cache must be
39  * done on the Cache implementation directly. This is the preferred way of using an already existing Cache.</p>
40  *
41  * The start up of the adapter will also try and index any elements already in
42  * the cache.<P>
43  *
44  * @author Steve Woodcock
45  * <p>
46  * @version 1.0
47  * @since 1.1
48  */

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

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

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

130     public String JavaDoc getName()
131     {
132         return name;
133     }
134
135     /* (non-Javadoc)
136      * @see com.jofti.cache.LifeCycleAdapter#setName(java.lang.String)
137      */

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

156     public void setInternalIndex(InternalIndex index)
157     {
158         this.index = index;
159
160     }
161
162    
163
164     /**
165      * @param cache
166      */

167     public void setCacheImpl(Object JavaDoc cache){
168         EhCacheListenerAdapter.this.cache = (Cache)cache;
169         if (cache != null){
170             ((Cache)cache).getCacheEventNotificationService().registerListener(eventListener);
171         }
172         
173     }
174     
175     protected Object JavaDoc getCacheValue(Object JavaDoc key) {
176         try {
177             return cache.get((Serializable JavaDoc) key).getValue();
178         } catch (CacheException e) {
179             log.warn(e);
180         }
181         return null;
182     }
183
184     /* (non-Javadoc)
185      * @see com.jofti.cache.LifeCycleAdapter#start()
186      */

187     public void start() throws JoftiException {
188         if (cache != null){
189             ((Cache)cache).getCacheEventNotificationService().registerListener(eventListener);
190               try {
191                 loadInitialValues(cache);
192             } catch (CacheException ce) {
193                 throw new JoftiException(ce);
194             }
195         }
196         
197         
198     }
199
200       private void loadInitialValues(net.sf.ehcache.Cache cache)
201             throws CacheException, JoftiException {
202         List JavaDoc keys = cache.getKeys();
203          int size = keys.size();
204             Iterator JavaDoc it = keys.iterator();
205         for (int i=0;i<size;i++) {
206         
207             Object JavaDoc key = it.next();
208             Element element = cache.get((Serializable JavaDoc) key);
209             // then we must remove from index
210
eventListener.notifyElementPut(cache, element);
211
212         }
213     }
214
215
216     /* (non-Javadoc)
217      * @see com.jofti.cache.CacheLocking#getIndex()
218      */

219     public InternalIndex getIndex() {
220         
221         return index;
222     }
223
224     /* (non-Javadoc)
225      * @see com.jofti.api.CacheAccessor#getCacheImpl()
226      */

227     public Object JavaDoc getCacheImpl() {
228         
229         return cache;
230     }
231
232     public IndexQuery addQuery(String JavaDoc name, IndexQuery query)throws JoftiException {
233         
234         return index.getParserManager().addQuery(name, query);
235     }
236
237     /* (non-Javadoc)
238      * @see com.jofti.api.Index#getQuery(java.lang.String)
239      */

240     public IndexQuery getQuery(String JavaDoc name) {
241         
242         return index.getParserManager().getQuery(name);
243     }
244     
245 }
Popular Tags