KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.jofti.cache.adapter;
2
3
4
5
6 import java.util.Map JavaDoc;
7 import java.util.Properties JavaDoc;
8
9
10
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15
16 import com.jofti.api.IndexQuery;
17 import com.jofti.cache.CacheAdapter;
18 import com.jofti.cache.BaseAdaptor;
19 import com.jofti.core.INameSpaceAware;
20 import com.jofti.core.IParsedQuery;
21 import com.jofti.core.InternalIndex;
22 import com.jofti.core.QueryId;
23 import com.jofti.core.QueryType;
24 import com.jofti.exception.JoftiException;
25 import com.jofti.store.Counter;
26 import com.jofti.util.CompositeComparator;
27
28
29 /**
30
31  *
32  * The adapter implementation specific to the Map interface.
33  *
34  * The adapter takes care of the implementation specific details for converting
35  * between the process the index expects and the behaviour of the Map.
36  *
37  * Although it is possible to use this adapter with any Map implementation, it
38  * does not deal with the LinkedHashMap where it has been configured to run as
39  * an LRU cache. As values will dispear from the Map without callback to the index. This also applies to other Map
40  * type structures that silently expire elements.<p>
41  *
42  * Care must be taken when using Maps with similar expiry functionality as it <b> will</b> lead to memory leaks.
43  *
44  *
45  * The start up of the adapter will also try and index any elements already in
46  * the Map.<p>
47  *
48  * @author Steve Woodcock
49  * <p>
50  * @version 1.0
51  * <p>
52  */

53
54 public class MockAdapter extends BaseAdaptor implements CacheAdapter {
55
56      private static Log log = LogFactory.getLog(MapAdapter.class);
57     
58    
59     private String JavaDoc name;
60     
61     
62
63     
64     public MockAdapter() {
65     }
66
67     public MockAdapter(Object JavaDoc cache) {
68         
69     }
70     
71     public void setCacheImpl(Object JavaDoc cache){
72         
73     }
74     
75     /* (non-Javadoc)
76      * @see com.jofti.api.IndexCache#get(java.lang.Object)
77      */

78     public Object JavaDoc get(Object JavaDoc key) {
79
80         Object JavaDoc result = null;
81         if (key != null) {
82
83
84                 result = "mock";
85
86         }
87         return result;
88
89     }
90
91     
92
93     /*
94      * (non-Javadoc)
95      *
96      * @see com.jofti.api.IndexCache#put(java.lang.Object, java.lang.Object)
97      */

98     public void put(Object JavaDoc key, Object JavaDoc value) throws JoftiException{
99         
100         Comparable JavaDoc newKey = (Comparable JavaDoc)decorateKey(key);
101         
102         acquireUpdateLock();
103         try{
104             synchronized(getCacheLock(key))
105             {
106                 
107                 if (index.contains(newKey)){
108                     Counter.removeCommands++;
109                         index.removeByKey(newKey);
110                  }
111                             
112                 // put the value back in
113
index.insert(newKey, value);
114                    
115           
116             }
117         } finally {
118             releaseUpdateLock();
119         }
120     }
121
122     /**
123      * Removes the element which matches the key.
124      * <p>
125      * If no element matches, nothing is removed and no Exception is thrown.
126      * @param key the key of the element to remove
127      * @throws CacheException
128      */

129     public void remove(Object JavaDoc key) throws JoftiException {
130         Comparable JavaDoc newKey = (Comparable JavaDoc)decorateKey(key);
131
132         acquireUpdateLock();
133         try {
134             synchronized (getCacheLock(key)) {
135     
136                 
137                     index.removeByKey(newKey);
138
139             }
140
141         } catch (Exception JavaDoc e) {
142             throw new JoftiException(e);
143         } finally {
144             releaseUpdateLock();
145         }
146
147     }
148
149     /**
150      * Remove all elements in the cache, but leave the cache in a useable state.
151      *
152      * @throws CacheException
153      */

154     public void removeAll() throws JoftiException{
155         acquireUpdateLock();
156         try {
157         if (index != null){
158                 index.removeAll();
159             }
160         }
161         catch (Exception JavaDoc e) {
162             throw new JoftiException(e);
163         } finally{
164             releaseUpdateLock();
165         }
166        
167     }
168
169     /* (non-Javadoc)
170      * @see com.jofti.cache.LifeCycleAdapter#init(java.util.Properties)
171      */

172     public synchronized void init(Properties JavaDoc properties) throws JoftiException {
173         
174         
175         
176     }
177     
178     /* (non-Javadoc)
179      * @see com.jofti.cache.LifeCycleAdapter#destroy()
180      */

181     public void destroy() throws JoftiException {
182         
183         
184         if (index != null){
185             index.removeAll();
186         }
187     }
188
189    
190
191     /* (non-Javadoc)
192      * @see com.jofti.cache.LifeCycleAdapter#getName()
193      */

194     public String JavaDoc getName() {
195         return name;
196     }
197
198     /* (non-Javadoc)
199      * @see com.jofti.cache.LifeCycleAdapter#setName(java.lang.String)
200      */

201     public void setName(String JavaDoc name) {
202         this.name = name;
203     }
204
205     public String JavaDoc toString() {
206         return "MockCache(" + getName() + ')';
207     }
208     
209
210     /* (non-Javadoc)
211      * @see com.jofti.api.IndexCache#getCacheImpl()
212      */

213     public Object JavaDoc getCacheImpl() {
214
215         return null;
216     }
217
218
219
220     /* (non-Javadoc)
221      * @see com.jofti.cache.CacheAdapter#setInternalIndex(com.jofti.core.InternalIndex)
222      */

223     public void setInternalIndex(InternalIndex index) {
224         this.index = index;
225         
226     }
227
228     /* (non-Javadoc)
229      * @see com.jofti.cache.CacheAdapter#start()
230      */

231     public void start() throws JoftiException {
232
233         
234         
235     }
236     
237     
238     
239     /*
240      * (non-Javadoc)
241      *
242      * @see com.jofti.api.IndexCache#query(com.jofti.api.IndexQuery)
243      */

244     
245     protected Object JavaDoc checkMutable(Object JavaDoc key, Object JavaDoc result) {
246     
247
248         return result;
249     }
250     
251     
252     /* (non-Javadoc)
253      * @see com.jofti.cache.CacheLocking#getCacheValue(java.lang.Object)
254      */

255     protected Object JavaDoc getCacheValue(Object JavaDoc key) {
256         synchronized (getCacheLock(key)) {
257             return "mock";
258         }
259     }
260
261     /* (non-Javadoc)
262      * @see com.jofti.cache.CacheLocking#getIndex()
263      */

264     public InternalIndex getIndex() {
265         return index;
266     }
267     
268     public IndexQuery addQuery(String JavaDoc name, IndexQuery query)throws JoftiException {
269         
270         return index.getParserManager().addQuery(name, query);
271     }
272
273     /* (non-Javadoc)
274      * @see com.jofti.api.Index#getQuery(java.lang.String)
275      */

276     public IndexQuery getQuery(String JavaDoc name) {
277         
278         return index.getParserManager().getQuery(name);
279     }
280
281 }
Popular Tags