KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.jofti.cache.adapter;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.Properties JavaDoc;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10 import com.jofti.api.CacheAccessor;
11 import com.jofti.api.IndexQuery;
12 import com.jofti.cache.CacheListenerAdapter;
13 import com.jofti.cache.BaseAdaptor;
14 import com.jofti.cache.adapter.listener.OSEventListener;
15 import com.jofti.core.INameSpaceAware;
16 import com.jofti.core.IParsedQuery;
17 import com.jofti.core.InternalIndex;
18 import com.jofti.core.QueryId;
19 import com.jofti.core.QueryType;
20 import com.jofti.exception.JoftiException;
21 import com.jofti.util.CompositeComparator;
22 import com.opensymphony.oscache.base.Cache;
23 import com.opensymphony.oscache.base.NeedsRefreshException;
24 import com.opensymphony.oscache.base.events.CacheEntryEventListener;
25
26 /**
27
28  *
29  * The adapter implementation specific to OSCache that acts as a Listener to the Cache.
30  *
31  * The adapter takes care of the implementation specific details for converting
32  * between the process the index expects and the behaviour of OSCache.
33  *
34  * For OSCache this means that the behaviour where a NeedsRefreshExeption is
35  * generated on a get is no longer visible to the caller. Instead, the entry is flushed
36  * as it has expired. <p>
37  *
38  * Similarly, a put will also overwrite the previous value - making the first expired if it needs refreshing.<p>
39  *
40  * OSCache has a full set of callbacks for the object in its cache so updates to the
41  * index are driven by these callbacks.<p>
42  *
43  * On start up this adpater will <b>not</b> try and index existing elements int he cache as OSCache provides no way to obtain a set og exsting
44  * keys.<p>
45  *
46   * @author Steve Woodcock
47  * <p>
48  * @version 1.0
49  * <p>
50  */

51
52
53 public class OSCacheListenerAdapter extends BaseAdaptor implements CacheAccessor, CacheListenerAdapter {
54
55     private Cache cache = null;
56     
57     
58     private static Log log = LogFactory.getLog(OSCacheListenerAdapter.class);
59     
60     private String JavaDoc name;
61     
62
63     
64     private int expiryTime =3600; //seconds
65

66     private OSEventListener eventListener =null;
67     /**
68      * @return Returns the expiryTime.
69      */

70     public int getExpiryTime() {
71         return expiryTime;
72     }
73     /**
74     */

75     public OSCacheListenerAdapter() {
76     }
77
78     public OSCacheListenerAdapter(Object JavaDoc cache) {
79         if (!(cache instanceof Cache)){
80             throw new RuntimeException JavaDoc("Cache is not instance of "+ Cache.class.getName());
81         }
82         this.cache = (Cache)cache;
83     }
84     
85   
86  
87
88     /* (non-Javadoc)
89      * @see com.jofti.cache.LifeCycleAdapter#init(java.util.Properties)
90      */

91     public synchronized void init(Properties JavaDoc properties) throws JoftiException {
92         try {
93             
94                 if (properties != null){
95                     String JavaDoc key =null;
96                     for (Iterator JavaDoc it = properties.keySet().iterator();it.hasNext();){
97                         key = (String JavaDoc)it.next();
98                         if (MUTABLE_VALUES.equalsIgnoreCase(key)){
99                             checkMutable = Boolean.valueOf(properties.getProperty(key)).booleanValue();
100                             if (log.isInfoEnabled()){
101                                 log.info("Mutability checking is set to " + checkMutable);
102                             }
103                         }
104                                     
105                     }
106                       if ("file".equalsIgnoreCase(key)) {
107                         log.warn("Listener adapters cannot be used to start up a Cache - please use addCache method");
108                         throw new JoftiException("Listener adapters cannot be used to start up a Cache - please use addCache method");
109                     }
110                 }
111                  if (cache == null) {
112                     throw new JoftiException("Cache cannot be null for listener adapter");
113                 } else {
114                     log.info("Index started using cache "+ cache);
115                 }
116                  // fix for missing expiry time config
117
if (properties != null){
118                         String JavaDoc expiryParam = properties.getProperty("expiryTime");
119                         if(expiryParam != null){
120                             try {
121                                 expiryTime = Integer.parseInt(expiryParam);
122                             }catch (NumberFormatException JavaDoc nfe){
123                                 log.error("Unable to set expiry time - using default of " + expiryTime,nfe);
124                             }
125                         }
126                     }
127             eventListener = new OSEventListener(this);
128             
129         
130         } catch (Exception JavaDoc e){
131             throw new JoftiException(e);
132         }
133         
134         
135     }
136     
137     /* (non-Javadoc)
138      * @see com.jofti.cache.LifeCycleAdapter#destroy()
139      */

140     public void destroy() throws JoftiException {
141         
142           index.removeAll();
143       
144     }
145
146    
147
148     public String JavaDoc getName() {
149         return name;
150     }
151
152     public void setName(String JavaDoc name) {
153         this.name = name;
154     }
155
156     public String JavaDoc toString() {
157         return "OSCache(" + getName() + ')';
158     }
159     
160     
161
162     /* (non-Javadoc)
163      * @see com.jofti.api.IndexCache#getCacheImpl()
164      */

165     public Object JavaDoc getCacheImpl() {
166         return cache;
167     }
168
169     
170     /* (non-Javadoc)
171      * @see com.jofti.cache.CacheAdapter#setInternalIndex(com.jofti.core.InternalIndex)
172      */

173     public void setInternalIndex(InternalIndex index) {
174         this.index = index;
175         
176     }
177     
178     /* (non-Javadoc)
179      * @see com.jofti.cache.CacheAdapter#start()
180      */

181     public void start() throws JoftiException {
182
183         if (cache != null){
184             cache.addCacheEventListener(eventListener,CacheEntryEventListener.class);
185         }
186         
187         
188     }
189     
190
191
192     
193
194     protected Object JavaDoc getCacheValue(Object JavaDoc key) {
195         Object JavaDoc obj=null;
196         try {
197             obj =cache.getFromCache((String JavaDoc)key, expiryTime);
198         }catch (NeedsRefreshException nre){
199             //we do this because of the bizarre way OSCache throws exceptions for gets
200
cache.cancelUpdate((String JavaDoc)key);
201             // it has expired so lets flush the entry
202
cache.flushEntry((String JavaDoc)key);
203            
204         }
205         return obj;
206     }
207     
208     public void warnOutOfDate(Object JavaDoc key){
209         if (log.isWarnEnabled()) {
210             log
211                     .warn("Index and cache have become out of date for key - Probable cause element expiry - flushing entry for '"
212                             + key +"'");
213         }
214     }
215     
216     /* (non-Javadoc)
217      * @see com.jofti.cache.CacheLocking#getIndex()
218      */

219     public InternalIndex getIndex() {
220         return index;
221     }
222     
223     /* (non-Javadoc)
224      * @see com.jofti.api.IndexedListener#setCacheImpl(java.lang.Object)
225      */

226     public void setCacheImpl(Object JavaDoc cache) {
227         this.cache = (Cache)cache;
228         this.cache.addCacheEventListener(eventListener,CacheEntryEventListener.class);
229         
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 }
Popular Tags