1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.components.portletregistry;
18
19 import java.util.List;
20 import java.util.Properties;
21
22 import org.apache.jetspeed.cache.CacheElement;
23 import org.apache.jetspeed.cache.DistributedCacheObject;
24 import org.apache.jetspeed.cache.JetspeedCache;
25 import org.apache.jetspeed.cache.impl.EhCacheElementImpl;
26 import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
27 import org.apache.ojb.broker.Identity;
28 import org.apache.ojb.broker.PersistenceBroker;
29 import org.apache.ojb.broker.cache.ObjectCache;
30
31 /***
32 * OJB cache
33 *
34 * @author dtaylor
35 *
36 */
37 public class RegistryApplicationCache implements ObjectCache
38 {
39 private static JetspeedCache oidCache;
40 private static JetspeedCache nameCache;
41 private static PortletRegistry registry;
42 private static List listeners = null;
43
44 public RegistryApplicationCache(PersistenceBroker broker, Properties props)
45 {
46 }
47
48 public synchronized static void cacheInit(PortletRegistry r, JetspeedCache o, JetspeedCache n, List l)
49 {
50 registry = r;
51 oidCache = o;
52 nameCache = n;
53 listeners = l;
54 }
55
56 public Object lookup(Identity oid)
57 {
58 return cacheLookup(oid);
59 }
60 public synchronized static Object cacheLookup(Identity oid)
61 {
62 CacheElement element = oidCache.get(oid);
63 if (element != null)
64 {
65 return element.getContent();
66 }
67 return null;
68 }
69
70
71
72
73 public void cache(Identity oid, Object obj)
74 {
75 cacheAdd(oid, obj);
76 }
77 public synchronized static void cacheAdd(Identity oid, Object obj)
78 {
79 oidCache.remove(oid);
80 CacheElement entry = new EhCacheElementImpl(oid, obj);
81 oidCache.put(entry);
82
83 MutablePortletApplication pa = (MutablePortletApplication)obj;
84 DistributedCacheObject wrapper = new RegistryCacheObjectWrapper(oid, pa.getName());
85 nameCache.remove(pa.getName());
86 CacheElement nameEntry = nameCache.createElement(pa.getName(), wrapper);
87 nameCache.put(nameEntry);
88
89 if (listeners != null)
90 {
91 for (int ix=0; ix < listeners.size(); ix++)
92 {
93 RegistryEventListener listener = (RegistryEventListener)listeners.get(ix);
94 listener.applicationUpdated((MutablePortletApplication)obj);
95 }
96 }
97 }
98
99
100
101
102 public void clear()
103 {
104 cacheClear();
105 }
106 public synchronized static void cacheClear()
107 {
108 oidCache.clear();
109 nameCache.clear();
110 }
111
112
113
114
115
116 public void remove(Identity oid)
117 {
118 cacheRemove(oid);
119 }
120 /***
121 * cacheRemove
122 *
123 * Remove identified object from object and node caches.
124 *
125 * @param oid object identity
126 */
127 public synchronized static void cacheRemove(Identity oid)
128 {
129 MutablePortletApplication pd = (MutablePortletApplication)cacheLookup(oid);
130 if (pd == null)
131 return;
132
133 oidCache.remove(oid);
134 nameCache.remove(pd.getName());
135
136 if (listeners != null)
137 {
138 for (int ix=0; ix < listeners.size(); ix++)
139 {
140 RegistryEventListener listener = (RegistryEventListener)listeners.get(ix);
141 listener.applicationRemoved(pd);
142 }
143 }
144
145 }
146
147 public synchronized static void cacheRemoveQuiet(String key, RegistryCacheObjectWrapper w)
148 {
149 RegistryCacheObjectWrapper wrapper = w;
150 if (wrapper == null)
151 {
152 wrapper = (RegistryCacheObjectWrapper)nameCache.get(key);
153 if (wrapper == null)
154 return;
155 }
156 Identity oid = wrapper.getId();
157
158 MutablePortletApplication pd = (MutablePortletApplication)cacheLookup(oid);
159 if (pd == null)
160 return;
161
162 oidCache.removeQuiet(oid);
163 nameCache.removeQuiet(pd.getName());
164 }
165
166 }