KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cintoo > messages > context > DefaultContextCache


1 /*
2  * Copyright 2006 cintoo, Berlin, Germany
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package cintoo.messages.context;
17
18 import api.cintoo.messages.bundle.BaseBundle;
19 import api.cintoo.messages.context.Context;
20 import api.cintoo.messages.context.ContextCache;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.concurrent.locks.ReentrantReadWriteLock JavaDoc;
26
27 /**
28  * Default implementation for a context cache. Uses
29  * maps to store bundle and context caching information.
30  *
31  * @author Stephan J. Schmidt
32  * @version $id$
33  * @since 1.0
34  */

35 public class DefaultContextCache implements ContextCache {
36   private Map JavaDoc<Long JavaDoc, BaseBundle> contextCache;
37   private ReentrantReadWriteLock JavaDoc lock = new ReentrantReadWriteLock JavaDoc();
38
39   /**
40    * Construct cache with default settings
41    */

42   public DefaultContextCache() {
43     contextCache = new HashMap JavaDoc<Long JavaDoc, BaseBundle>();
44   }
45
46   /**
47    * Add bundle and context to cache
48    *
49    * @param context context with the scope of the bundle
50    * @param locale locale for the bundle
51    * @param bundle bundle to add
52    */

53   public void addToCache(Context context, Locale JavaDoc locale, BaseBundle bundle) {
54     try {
55       lock.writeLock().lock();
56       contextCache.put(ContextKey.generate(context, locale), bundle);
57     } finally {
58       lock.writeLock().unlock();
59     }
60   }
61
62   /**
63    * Retrieve a cached bundle
64    *
65    * @param context context for the wanted bundle
66    * @param locale locale for the bundle
67    * @return cached bundle name
68    */

69   public BaseBundle getFromCache(Context context, Locale JavaDoc locale) {
70     BaseBundle bundle = null;
71     try {
72       lock.readLock().lock();
73       bundle = contextCache.get(ContextKey.generate(context, locale));
74     } finally {
75       lock.readLock().unlock();
76     }
77     return bundle;
78   }
79
80   /**
81    * Check if a bundle is cached
82    *
83    * @param context context to check if it is cached
84    * @param locale locale for the bundle
85    * @return true if the context is cached
86    */

87   public boolean isCached(Context context, Locale JavaDoc locale) {
88     boolean contains = false;
89     try {
90       lock.readLock().lock();
91       contains = contextCache.containsKey(ContextKey.generate(context, locale));
92     } finally {
93       lock.readLock().unlock();
94     }
95     return contains;
96   }
97
98   /**
99    * Clear the cache
100    */

101   public void clear() {
102     try {
103       lock.writeLock().lock();
104       try {
105         lock.writeLock().lock();
106         contextCache.clear();
107       } finally {
108         lock.writeLock().unlock();
109       }
110     } finally {
111       lock.writeLock().unlock();
112     }
113   }
114 }
115
Popular Tags