Source code: ojb/broker/cache/MetaObjectCacheImpl.java
1 package ojb.broker.cache;
2
3 import ojb.broker.Identity;
4
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.Map;
8
9 /**
10 * This cache makes it possible to have separate cache implementations for
11 * each class. When an object is cached / looked up the MetaObjectCacheImpl
12 * checks if a special ObjectCache has been set for this class. It recursively
13 * looks up the superclasses of the given object to look for a special cache. If
14 * no special cache is found it uses the ObjectCacheDefaultImpl to cache the
15 * object.<br>
16 * It is also possible to switch off caching for a specific class by setting
17 * the object cache to null.
18 *
19 * @author rzj7l2
20 * @created 8. November 2001
21 */
22 public class MetaObjectCacheImpl implements ObjectCache
23 {
24 private Map cachesByClass = new HashMap();
25
26 /**
27 * Constructor for the MetaObjectCacheImpl object
28 */
29 public MetaObjectCacheImpl()
30 {
31 setClassCache(Object.class, new ObjectCacheDefaultImpl());
32 }
33
34 /**
35 * Sets the ObjectCache implementation to use for objects with the given
36 * type and subclasses
37 *
38 * @param objectClass The object's class, use java.lang.Object to alter
39 * default caching for all objects which have no special
40 * caching defined
41 * @param cache The new ObjectCache implementation to use for this
42 * class and subclasses, null to switch off caching
43 * for the given class
44 */
45 public void setClassCache(Class objectClass, ObjectCache cache)
46
47 {
48 setClassCache(objectClass.getName(), cache);
49 }
50
51 /**
52 * Caches the given Object
53 *
54 * @param obj The object to cache
55 */
56 public void cache(Object obj)
57 {
58 if (obj != null)
59 {
60 Identity oid = new Identity(obj);
61 cache(oid, obj);
62 }
63 }
64
65 /**
66 * Caches the given object using the given Identity as key
67 *
68 * @param oid The Identity key
69 * @param obj The object o cache
70 */
71 public void cache(Identity oid, Object obj)
72 {
73 if (oid != null && obj != null)
74 {
75 ObjectCache cache = getCache(oid.getObjectsClass());
76 if (cache != null)
77 {
78 cache.cache(oid, obj);
79 }
80 }
81 }
82
83 /**
84 * Looks up the object from the cache
85 *
86 * @param oid The Identity to look up the object for
87 * @return The object if found, otherwise null
88 */
89 public Object lookup(Identity oid)
90 {
91 Object ret = null;
92 if (oid != null)
93 {
94 ObjectCache cache = getCache(oid.getObjectsClass());
95 if (cache != null)
96 {
97 ret = cache.lookup(oid);
98 }
99 }
100 return ret;
101 }
102
103 /**
104 * Removes the object identified by the given Identity from the cache
105 *
106 * @param oid The identity of the object to remove
107 */
108 protected void removeByOID(Identity oid)
109 {
110 if (oid != null)
111 {
112 ObjectCache cache = getCache(oid.getObjectsClass());
113 if (cache != null)
114 {
115 cache.remove(oid);
116 }
117 }
118 }
119
120 /**
121 * Removes the given object from the cache
122 *
123 * @param obj The object to remove
124 */
125 public void remove(Object obj)
126 {
127 if (obj != null)
128 {
129 if (obj instanceof Identity)
130 {
131 removeByOID((Identity) obj);
132 return;
133 }
134
135 ObjectCache cache = getCache(obj.getClass());
136 if (cache != null)
137 {
138 cache.remove(obj);
139 }
140 }
141 }
142
143 /**
144 * Clears the cache
145 */
146 public void clear()
147 {
148 Iterator it = cachesByClass.values().iterator();
149 while (it.hasNext())
150 {
151 ObjectCache cache = (ObjectCache) it.next();
152 if (cache != null)
153 {
154 cache.clear();
155 }
156 }
157
158 }
159
160 /**
161 * Sets the ObjectCache implementation for the given class name
162 *
163 * @param className The name of the class to cache
164 * @param cache The ObjectCache to use for this class and subclasses
165 */
166 private void setClassCache(String className, ObjectCache cache)
167 {
168 cachesByClass.put(className, cache);
169 }
170
171 /**
172 * Gets the cache for the given class
173 *
174 * @param objectClass The class to look up the cache for
175 * @return The cache
176 */
177 private ObjectCache getCache(Class objectClass)
178 {
179 ObjectCache cache = (ObjectCache) cachesByClass.get(objectClass.getName());
180 if (cache == null)
181 {
182 if (cachesByClass.containsKey(objectClass.getName()))
183 {
184 //there is a null-cache present => do not cache this class
185 }
186 else if (!objectClass.equals(Object.class))
187
188 {
189 //check for superclasses cache
190 cache = getCache(objectClass.getSuperclass());
191 }
192 }
193 return cache;
194 }
195
196 }