KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

84     public Object JavaDoc get(Object JavaDoc key) {
85
86         Object JavaDoc result = null;
87         if (key != null) {
88
89
90                 result = map.get(key);
91
92         }
93         return result;
94
95     }
96
97     
98
99     /*
100      * (non-Javadoc)
101      *
102      * @see com.jofti.api.IndexCache#put(java.lang.Object, java.lang.Object)
103      */

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

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

161     public void removeAll() throws JoftiException{
162         acquireUpdateLock();
163         try {
164             if (map != null){
165                 map.clear();
166             }
167             if (index != null){
168                 index.removeAll();
169             }
170         }
171         catch (Exception JavaDoc e) {
172             throw new JoftiException(e);
173         } finally{
174             releaseUpdateLock();
175         }
176        
177     }
178
179     /* (non-Javadoc)
180      * @see com.jofti.cache.LifeCycleAdapter#init(java.util.Properties)
181      */

182     public synchronized void init(Properties JavaDoc properties) throws JoftiException {
183         
184         
185         if (properties != null){
186             String JavaDoc key = null;
187             for (Iterator JavaDoc it = properties.keySet().iterator();it.hasNext();){
188                 key = (String JavaDoc)it.next();
189                 if (MUTABLE_VALUES.equalsIgnoreCase(key)){
190                     checkMutable = Boolean.valueOf(properties.getProperty(key)).booleanValue();
191                     if (log.isInfoEnabled()){
192                         log.info("Mutability checking is set to " + checkMutable);
193                     }
194                 }
195             
196             }
197         }
198     }
199     
200     /* (non-Javadoc)
201      * @see com.jofti.cache.LifeCycleAdapter#destroy()
202      */

203     public void destroy() throws JoftiException {
204         
205         map = new ConcurrentHashMap();
206         
207         if (index != null){
208             index.removeAll();
209         }
210     }
211
212    
213
214     /* (non-Javadoc)
215      * @see com.jofti.cache.LifeCycleAdapter#getName()
216      */

217     public String JavaDoc getName() {
218         return name;
219     }
220
221     /* (non-Javadoc)
222      * @see com.jofti.cache.LifeCycleAdapter#setName(java.lang.String)
223      */

224     public void setName(String JavaDoc name) {
225         this.name = name;
226     }
227
228     public String JavaDoc toString() {
229         return "HashMapCache(" + getName() + ')';
230     }
231     
232
233     /* (non-Javadoc)
234      * @see com.jofti.api.IndexCache#getCacheImpl()
235      */

236     public Object JavaDoc getCacheImpl() {
237
238         return map;
239     }
240
241
242
243     /* (non-Javadoc)
244      * @see com.jofti.cache.CacheAdapter#setInternalIndex(com.jofti.core.InternalIndex)
245      */

246     public void setInternalIndex(InternalIndex index) {
247         this.index = index;
248         
249     }
250
251     /* (non-Javadoc)
252      * @see com.jofti.cache.CacheAdapter#start()
253      */

254     public void start() throws JoftiException {
255
256             loadInitialValues(map);
257         
258         
259     }
260     
261     private void loadInitialValues(Map JavaDoc temp) throws JoftiException {
262         if (map ==null){
263             log.info("No initial values to index as map is null");
264             return;
265         }
266         Set JavaDoc entries = temp.entrySet();
267          int size = entries.size();
268             Iterator JavaDoc it = entries.iterator();
269             for (int i=0;i<size;i++) {
270
271             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
272
273             if (entry.getKey() instanceof Comparable JavaDoc) {
274                 if (entry.getValue() != null) {
275                     // then we must remove from index
276
index.insert((Comparable JavaDoc) entry.getKey(), entry.getValue());
277                 }
278             } else {
279                 log.warn("unable to index value at " + entry.getKey()
280                         + "as key is not Comparable");
281             }
282         }
283     }
284     
285     
286
287     
288     
289     /* (non-Javadoc)
290      * @see com.jofti.cache.CacheLocking#getCacheValue(java.lang.Object)
291      */

292     protected Object JavaDoc getCacheValue(Object JavaDoc key) {
293         synchronized (getCacheLock(key)) {
294             return map.get(key);
295         }
296     }
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
318 }
Popular Tags