1 package org.apache.ojb.broker.cache;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.Map;
22 import java.util.Properties;
23
24 import org.apache.ojb.broker.Identity;
25 import org.apache.ojb.broker.PersistenceBroker;
26
27
28
29
30
31
32
33 public class ObjectCachePerClassImpl extends AbstractMetaCache
34 {
35 private static Map cachesByClass = Collections.synchronizedMap(new HashMap());
36
37
38
39
40 public ObjectCachePerClassImpl(PersistenceBroker broker, Properties prop)
41 {
42 setClassCache(Object.class, new ObjectCacheDefaultImpl(broker, null));
43 }
44
45 public ObjectCache getCache(Identity oid, Object obj, int methodCall)
46 {
47 if(oid.getObjectsRealClass() == null)
48 {
49 return null;
50 }
51 else
52 {
53 return getCachePerClass(oid.getObjectsRealClass(), methodCall);
54 }
55 }
56
57
58
59
60 public void clear()
61 {
62 Iterator it = cachesByClass.values().iterator();
63 while (it.hasNext())
64 {
65 ObjectCache cache = (ObjectCache) it.next();
66 if (cache != null)
67 {
68 cache.clear();
69 }
70 }
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84 public void setClassCache(Class objectClass, ObjectCache cache)
85
86 {
87 setClassCache(objectClass.getName(), cache);
88 }
89
90
91
92
93
94
95
96 private void setClassCache(String className, ObjectCache cache)
97 {
98 cachesByClass.put(className, cache);
99 }
100
101
102
103
104
105
106
107 private ObjectCache getCachePerClass(Class objectClass, int methodCall)
108 {
109 ObjectCache cache = (ObjectCache) cachesByClass.get(objectClass.getName());
110 if (cache == null && AbstractMetaCache.METHOD_CACHE == methodCall
111 && !cachesByClass.containsKey(objectClass.getName()))
112 {
113 cache = new ObjectCacheDefaultImpl(null, null);
114 setClassCache(objectClass.getName(), cache);
115 }
116 return cache;
117 }
118 }