KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.jofti.cache.adapter;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Properties JavaDoc;
5
6 import net.sf.ehcache.CacheException;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import com.jofti.api.IndexQuery;
12 import com.jofti.cache.BaseAdaptor;
13 import com.jofti.cache.CacheAdapter;
14 import com.jofti.cache.adapter.listener.OSEventListener;
15 import com.jofti.core.InternalIndex;
16 import com.jofti.exception.JoftiException;
17 import com.jofti.util.PropertyLoader;
18 import com.opensymphony.oscache.base.NeedsRefreshException;
19 import com.opensymphony.oscache.base.events.CacheEntryEventListener;
20 import com.opensymphony.oscache.general.GeneralCacheAdministrator;
21
22 /**
23
24  *
25  * The adapter implementation specific to OSCache.
26  *
27  * The adapter takes care of the implementation specific details for converting
28  * between the process the index expects and the behaviour of OSCache.
29  *
30  * For OSCache this means that the behaviour where a NeedsRefreshExeption is
31  * generated on a get is no longer visible to the caller. Instead, the entry is flushed
32  * as it has expired. <p>
33  *
34  * OSCache has a full set of callbacks for the object in its cache so updates to the
35  * index are driven by these callbacks.<p>
36  *
37  * On start up this adpater will <b>not</b> try and index existing elements in the cache as OSCache provides no way to obtain a set of exsting
38  * keys.<p>
39  *
40   * @author Steve Woodcock
41  * <p>
42  * @version 1.0
43  * <p>
44  */

45
46
47 public class OSCacheAdapter extends BaseAdaptor implements CacheAdapter {
48
49     private GeneralCacheAdministrator admin = null;
50     
51     
52     private static Log log = LogFactory.getLog(OSCacheAdapter.class);
53     
54     private String JavaDoc name;
55     
56
57     
58     private int expiryTime =3600; //seconds
59

60     private OSEventListener eventListener =null;
61     /**
62      * @return Returns the expiryTime.
63      */

64     public int getExpiryTime() {
65         return expiryTime;
66     }
67     /**
68      * @param expiryTime The expiryTime to set.
69      */

70     public void setExpiryTime(int expiryTime) {
71         this.expiryTime = expiryTime;
72     }
73     public OSCacheAdapter() {
74     }
75
76     public OSCacheAdapter(Object JavaDoc cache) {
77         if (! (cache instanceof GeneralCacheAdministrator) ){
78             throw new RuntimeException JavaDoc("OSCacheAdapter requires GeneralCacheAdministrator ");
79         }
80         this.admin = (GeneralCacheAdministrator)cache;
81     }
82     
83     public void setCacheImpl(Object JavaDoc cache){
84         this.admin = (GeneralCacheAdministrator)cache;
85         
86     }
87     
88     /* (non-Javadoc)
89      * @see com.jofti.api.IndexCache#get(java.lang.Object)
90      */

91     public Object JavaDoc get(Object JavaDoc key) {
92         if (key != null && (!(key instanceof String JavaDoc))) {
93             log.warn("key value for OSCache must by of type String " + key);
94             return null;
95         }
96         Object JavaDoc obj = null;
97         synchronized (getCacheLock(key)) {
98             try {
99                 obj = admin.getFromCache((String JavaDoc) key, expiryTime);
100             } catch (NeedsRefreshException nre) {
101                 // we do this because of the bizarre way OSCache throws
102
// exceptions for gets
103
admin.cancelUpdate((String JavaDoc) key);
104                 // it has expired so lets flush the entry
105
admin.flushEntry((String JavaDoc) key);
106
107             }
108         }
109         // we return null here if we got an exception because it is consistent
110
// with most other cache implementations
111
return obj;
112     }
113
114     
115
116     /* (non-Javadoc)
117      * @see com.jofti.api.IndexCache#put(java.lang.Object, java.lang.Object)
118      */

119     public void put(Object JavaDoc key, Object JavaDoc value) throws JoftiException {
120
121         if (key != null && (!(key instanceof String JavaDoc))) {
122             throw new JoftiException(
123                     "key value for OSCache must by of type String " + key);
124         }
125
126         // get a lock for the key that we are trying to add
127
// we do not care about other parts of the cache or the index
128
synchronized (getCacheLock(key)) {
129
130             // rely on call back listener to populate index entry
131
admin.putInCache((String JavaDoc) key, value);
132
133         }
134
135     }
136
137     /**
138      * Removes the element which matches the key.
139      * <p>
140      * If no element matches, nothing is removed and no Exception is thrown.
141      *
142      * @param key
143      * the key of the element to remove
144      * @throws CacheException
145      */

146     public void remove(Object JavaDoc key) throws JoftiException{
147         
148             // remove this entry and rely on call back listener to remove index entry
149
if (key != null && (!(key instanceof String JavaDoc))){
150                 throw new JoftiException("key value for OSCache must by of type String "+ key);
151             }
152            admin.flushEntry((String JavaDoc)key);
153        
154     }
155
156     /**
157      * Remove all elements in the cache, but leave the cache
158      * in a useable state.
159      * @throws CacheException
160      */

161     public void removeAll() throws JoftiException{
162         //rely on call back listener to remove index entry
163
admin.flushAll();
164     }
165
166     /* (non-Javadoc)
167      * @see com.jofti.cache.LifeCycleAdapter#init(java.util.Properties)
168      */

169     public synchronized void init(Properties JavaDoc properties) throws JoftiException {
170         try {
171             
172             
173             
174                 String JavaDoc cacheConfigFile = null;
175                 if (properties != null){
176                     String JavaDoc key =null;
177                     for (Iterator JavaDoc it = properties.keySet().iterator();it.hasNext();){
178                         key = (String JavaDoc)it.next();
179                         if (MUTABLE_VALUES.equalsIgnoreCase(key)){
180                             checkMutable = Boolean.valueOf(properties.getProperty(key)).booleanValue();
181                             if (log.isInfoEnabled()){
182                                 log.info("Mutability checking is set to " + checkMutable);
183                             }
184                         }
185                         if ("file".equalsIgnoreCase(key)){
186                             cacheConfigFile = properties.getProperty(key);
187                         }
188                     }
189                 }
190                 log.debug("looking up GeneralCacheAdministrator");
191                 if (admin == null){
192                 
193                     if (cacheConfigFile == null){
194                         log.debug("not found - creating default GeneralCacheAdministrator");
195                         admin = new GeneralCacheAdministrator();
196                     }else{
197                         // we need to construct a properties object
198
log.debug("not found - creating default GeneralCacheAdministrator");
199                         Properties JavaDoc props =null;
200                         try {
201                             props= PropertyLoader.loadProperties(cacheConfigFile);
202                         } catch (Throwable JavaDoc t){
203                             log.warn("Unable to load config file " + cacheConfigFile + " from classpath");
204                         }
205                         if (props == null){
206                             throw new JoftiException("Unable to load config file " + cacheConfigFile + " from classpath");
207                         }
208                         admin = new GeneralCacheAdministrator(props);
209                     }
210                 }else{
211                     log.debug("using supplied GeneralCacheAdministrator");
212                 }
213                 if (properties != null){
214                     String JavaDoc expiryParam = properties.getProperty("expiryTime");
215                     if(expiryParam != null){
216                         try {
217                             expiryTime = Integer.parseInt(expiryParam);
218                         }catch (NumberFormatException JavaDoc nfe){
219                             log.error("Unable to set expiry time - using default of " + expiryTime,nfe);
220                         }
221                     }
222                 }
223             
224             
225             eventListener = new OSEventListener(this);
226             
227             admin.getCache().addCacheEventListener(eventListener,CacheEntryEventListener.class);
228
229         } catch (Exception JavaDoc e){
230             throw new JoftiException(e);
231         }
232         
233         
234     }
235     
236     /* (non-Javadoc)
237      * @see com.jofti.cache.LifeCycleAdapter#destroy()
238      */

239     public void destroy() throws JoftiException {
240           if (admin != null) {
241              admin.destroy();
242            
243         }
244           if (index != null){
245             index.removeAll();
246           }
247       
248     }
249
250    
251
252     public String JavaDoc getName() {
253         return name;
254     }
255
256     public void setName(String JavaDoc name) {
257         this.name = name;
258     }
259
260     public String JavaDoc toString() {
261         return "OSCache(" + getName() + ')';
262     }
263     
264     
265
266     /* (non-Javadoc)
267      * @see com.jofti.api.IndexCache#getCacheImpl()
268      */

269     public Object JavaDoc getCacheImpl() {
270         return admin;
271     }
272
273     
274
275     /* (non-Javadoc)
276      * @see com.jofti.cache.CacheAdapter#setInternalIndex(com.jofti.core.InternalIndex)
277      */

278     public void setInternalIndex(InternalIndex index) {
279         this.index = index;
280         
281     }
282     
283     /* (non-Javadoc)
284      * @see com.jofti.cache.CacheAdapter#start()
285      */

286     public void start() throws JoftiException {
287
288     // do nothing here
289

290         
291     }
292     
293     
294
295     protected Object JavaDoc getCacheValue(Object JavaDoc key) {
296         return get(key);
297     }
298     /* (non-Javadoc)
299      * @see com.jofti.cache.CacheLocking#getIndex()
300      */

301     public InternalIndex getIndex() {
302         return index;
303     }
304     
305     public IndexQuery addQuery(String JavaDoc name, IndexQuery query)throws JoftiException {
306         
307         return index.getParserManager().addQuery(name, query);
308     }
309
310     /* (non-Javadoc)
311      * @see com.jofti.api.Index#getQuery(java.lang.String)
312      */

313     public IndexQuery getQuery(String JavaDoc name) {
314         
315         return index.getParserManager().getQuery(name);
316     }
317 }
Popular Tags