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.PortletDefinitionComposite;
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 RegistryPortletCache 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 RegistryPortletCache(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 PortletDefinitionComposite pd = (PortletDefinitionComposite)obj;
84 DistributedCacheObject wrapper = new RegistryCacheObjectWrapper(oid, pd.getUniqueName());
85 nameCache.remove(pd.getUniqueName());
86 CacheElement nameEntry = nameCache.createElement(pd.getUniqueName(), 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.portletUpdated((PortletDefinitionComposite)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 PortletDefinitionComposite pd = (PortletDefinitionComposite)cacheLookup(oid);
130 if (pd == null)
131 return;
132
133 oidCache.remove(oid);
134 nameCache.remove(pd.getUniqueName());
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.portletRemoved(pd);
142 }
143 }
144 }
145
146 public synchronized static void cacheRemoveQuiet(String key, RegistryCacheObjectWrapper w)
147 {
148 RegistryCacheObjectWrapper wrapper = w;
149 if (wrapper == null)
150 {
151 wrapper = (RegistryCacheObjectWrapper)nameCache.get(key);
152 if (wrapper == null)
153 return;
154 }
155 Identity oid = wrapper.getId();
156
157 PortletDefinitionComposite pd = (PortletDefinitionComposite)cacheLookup(oid);
158 if (pd == null)
159 return;
160
161 oidCache.removeQuiet(oid);
162 nameCache.removeQuiet(pd.getUniqueName());
163 }
164
165 }