1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.jetspeed.cache.impl; |
19 |
|
|
20 |
|
import java.util.Collections; |
21 |
|
import java.util.HashMap; |
22 |
|
import java.util.Iterator; |
23 |
|
import java.util.Map; |
24 |
|
import java.util.StringTokenizer; |
25 |
|
|
26 |
|
import net.sf.ehcache.Ehcache; |
27 |
|
import net.sf.ehcache.Element; |
28 |
|
|
29 |
|
import org.apache.jetspeed.cache.CacheElement; |
30 |
|
import org.apache.jetspeed.cache.ContentCacheElement; |
31 |
|
import org.apache.jetspeed.cache.ContentCacheKey; |
32 |
|
import org.apache.jetspeed.cache.ContentCacheKeyGenerator; |
33 |
|
import org.apache.jetspeed.cache.JetspeedCache; |
34 |
|
import org.apache.jetspeed.cache.JetspeedCacheEventListener; |
35 |
|
import org.apache.jetspeed.cache.JetspeedContentCache; |
36 |
|
import org.apache.jetspeed.decoration.Theme; |
37 |
|
import org.apache.jetspeed.request.RequestContext; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
public class EhPortletContentCacheImpl extends EhCacheImpl implements JetspeedContentCache, JetspeedCacheEventListener |
46 |
|
{ |
47 |
|
|
48 |
0 |
JetspeedCache preferenceCache = null; |
49 |
0 |
ContentCacheKeyGenerator keyGenerator = null; |
50 |
|
|
51 |
|
public EhPortletContentCacheImpl(Ehcache ehcache, JetspeedCache preferenceCache, ContentCacheKeyGenerator keyGenerator) |
52 |
|
{ |
53 |
0 |
this(ehcache); |
54 |
0 |
this.preferenceCache = preferenceCache; |
55 |
0 |
this.keyGenerator = keyGenerator; |
56 |
0 |
preferenceCache.addEventListener(this,false); |
57 |
0 |
} |
58 |
|
|
59 |
|
public EhPortletContentCacheImpl(Ehcache ehcache, JetspeedCache preferenceCache) |
60 |
|
{ |
61 |
0 |
this(ehcache); |
62 |
0 |
this.preferenceCache = preferenceCache; |
63 |
0 |
preferenceCache.addEventListener(this,false); |
64 |
0 |
} |
65 |
|
|
66 |
|
public EhPortletContentCacheImpl(Ehcache ehcache) |
67 |
|
{ |
68 |
0 |
super(ehcache); |
69 |
0 |
} |
70 |
|
|
71 |
|
public EhPortletContentCacheImpl(Ehcache ehcache, ContentCacheKeyGenerator keyGenerator) |
72 |
|
{ |
73 |
0 |
this(ehcache); |
74 |
0 |
this.keyGenerator = keyGenerator; |
75 |
0 |
} |
76 |
|
|
77 |
|
|
78 |
|
public void notifyElementAdded(JetspeedCache cache, boolean local, Object key, Object element) |
79 |
|
{ |
80 |
0 |
} |
81 |
|
|
82 |
|
public void notifyElementChanged(JetspeedCache cache, boolean local, Object key, Object element) |
83 |
|
{ |
84 |
0 |
} |
85 |
|
|
86 |
|
public void notifyElementEvicted(JetspeedCache cache, boolean local, Object key, Object element) |
87 |
|
{ |
88 |
0 |
} |
89 |
|
|
90 |
|
public void notifyElementExpired(JetspeedCache cache, boolean local, Object key, Object element) |
91 |
|
{ |
92 |
0 |
notifyElementRemoved(cache,local,key,element); |
93 |
0 |
} |
94 |
|
|
95 |
|
|
96 |
|
public static final String KEY_ENTITY_KEY = |
97 |
|
EhPortletContentCacheElementImpl.KEY_SEPARATOR + "portlet_entity" + EhPortletContentCacheElementImpl.KEY_SEPARATOR ; |
98 |
0 |
public static final int KEY_ENTITY_KEY_LENGTH = KEY_ENTITY_KEY.length(); |
99 |
|
|
100 |
|
public void notifyElementRemoved(JetspeedCache cache, boolean local, |
101 |
|
Object key, Object element) |
102 |
|
{ |
103 |
0 |
if (local) return; |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
0 |
if (!(key instanceof String)) return; |
108 |
0 |
String s = (String) key; |
109 |
0 |
if (!(s.startsWith(KEY_ENTITY_KEY))) return; |
110 |
0 |
StringTokenizer st = new StringTokenizer(s, |
111 |
|
EhPortletContentCacheElementImpl.KEY_SEPARATOR); |
112 |
0 |
int count = 0; |
113 |
0 |
String pe = null; |
114 |
0 |
String user = null; |
115 |
0 |
while (st.hasMoreTokens()) |
116 |
|
{ |
117 |
0 |
String temp = st.nextToken(); |
118 |
0 |
switch (count) |
119 |
|
{ |
120 |
|
case 0: |
121 |
0 |
break; |
122 |
|
case 1: |
123 |
0 |
pe = temp; |
124 |
0 |
break; |
125 |
|
case 2: |
126 |
0 |
user = temp; |
127 |
|
break; |
128 |
|
} |
129 |
0 |
count++; |
130 |
0 |
if (count > 2) break; |
131 |
0 |
} |
132 |
0 |
if ((pe != null) && (user != class="keyword">null)) |
133 |
|
{ |
134 |
0 |
removeUserEntry(user, "portal", pe); |
135 |
0 |
removeUserEntry(user, "desktop", pe); |
136 |
|
} |
137 |
0 |
} |
138 |
|
|
139 |
|
void removeUserEntry(String username, String pipeline, String windowId) |
140 |
|
{ |
141 |
0 |
ContentCacheKey key = keyGenerator.createUserCacheKey(username, pipeline, windowId); |
142 |
0 |
if (ehcache.remove(key.getKey())) |
143 |
|
{ |
144 |
0 |
Element userElement = ehcache.get(username); |
145 |
|
|
146 |
0 |
if (userElement != null) |
147 |
|
{ |
148 |
0 |
Map map = (Map)userElement.getObjectValue(); |
149 |
0 |
if (map != null) |
150 |
|
{ |
151 |
0 |
map.remove(windowId); |
152 |
|
} |
153 |
|
} |
154 |
|
} |
155 |
0 |
} |
156 |
|
|
157 |
|
public CacheElement get(Object key) |
158 |
|
{ |
159 |
0 |
ContentCacheKey cckey = (ContentCacheKey)key; |
160 |
0 |
Element element = ehcache.get(cckey.getKey()); |
161 |
0 |
if (element == null) |
162 |
0 |
return null; |
163 |
0 |
return new EhPortletContentCacheElementImpl(element, cckey); |
164 |
|
} |
165 |
|
|
166 |
|
public int getTimeToIdleSeconds() |
167 |
|
{ |
168 |
0 |
return (int)ehcache.getTimeToIdleSeconds(); |
169 |
|
} |
170 |
|
|
171 |
|
public int getTimeToLiveSeconds() |
172 |
|
{ |
173 |
0 |
return (int)ehcache.getTimeToLiveSeconds(); |
174 |
|
} |
175 |
|
|
176 |
|
public boolean isKeyInCache(Object key) |
177 |
|
{ |
178 |
0 |
ContentCacheKey cckey = (ContentCacheKey)key; |
179 |
0 |
return ehcache.isKeyInCache(cckey.getKey()); |
180 |
|
} |
181 |
|
|
182 |
|
public void put(CacheElement element) |
183 |
|
{ |
184 |
0 |
ContentCacheElement ccElement = (ContentCacheElement)element; |
185 |
0 |
EhPortletContentCacheElementImpl impl = (EhPortletContentCacheElementImpl)element; |
186 |
0 |
Element ehl = impl.getImplElement(); |
187 |
0 |
String userKey = ccElement.getContentCacheKey().getSessionId(); |
188 |
0 |
if (userKey == null) |
189 |
|
{ |
190 |
0 |
userKey = ccElement.getContentCacheKey().getUsername(); |
191 |
|
} |
192 |
0 |
String windowId = ccElement.getContentCacheKey().getWindowId(); |
193 |
|
try |
194 |
|
{ |
195 |
0 |
ehcache.put(ehl); |
196 |
|
} |
197 |
0 |
catch (Exception e) |
198 |
|
{ |
199 |
0 |
e.printStackTrace(); |
200 |
0 |
} |
201 |
0 |
Element userElement = ehcache.get(userKey); |
202 |
0 |
if (userElement == null) |
203 |
|
{ |
204 |
0 |
Map map = Collections.synchronizedMap(new HashMap()); |
205 |
0 |
map.put(windowId, ccElement.getContentCacheKey()); |
206 |
0 |
userElement = new Element(userKey, map); |
207 |
0 |
ehcache.put(userElement); |
208 |
0 |
} |
209 |
|
else |
210 |
|
{ |
211 |
0 |
Map map = (Map)userElement.getObjectValue(); |
212 |
0 |
map.put(windowId, ccElement.getContentCacheKey()); |
213 |
|
} |
214 |
0 |
} |
215 |
|
|
216 |
|
public CacheElement createElement(Object key, Object content) |
217 |
|
{ |
218 |
0 |
ContentCacheKey cckey = (ContentCacheKey)key; |
219 |
0 |
Element cachedElement = new Element(cckey.getKey(), content); |
220 |
0 |
return new EhPortletContentCacheElementImpl(cachedElement, cckey); |
221 |
|
} |
222 |
|
|
223 |
|
public boolean remove(Object key) |
224 |
|
{ |
225 |
0 |
CacheElement element = this.get(key); |
226 |
0 |
boolean removed = false; |
227 |
0 |
if (element == null) |
228 |
0 |
return false; |
229 |
|
|
230 |
0 |
ContentCacheElement ccElement = (ContentCacheElement)element; |
231 |
0 |
EhPortletContentCacheElementImpl impl = (EhPortletContentCacheElementImpl)element; |
232 |
0 |
Element ehl = impl.getImplElement(); |
233 |
0 |
String userKey = ccElement.getContentCacheKey().getSessionId(); |
234 |
0 |
if (userKey == null) |
235 |
|
{ |
236 |
0 |
userKey = ccElement.getContentCacheKey().getUsername(); |
237 |
|
} |
238 |
0 |
String windowId = ccElement.getContentCacheKey().getWindowId(); |
239 |
0 |
removed = ehcache.remove(ccElement.getContentCacheKey().getKey()); |
240 |
0 |
Element userElement = ehcache.get(userKey); |
241 |
0 |
if (userElement != null) |
242 |
|
{ |
243 |
0 |
Map map = (Map)userElement.getObjectValue(); |
244 |
0 |
if (map != null) |
245 |
|
{ |
246 |
0 |
map.remove(windowId); |
247 |
|
} |
248 |
|
} |
249 |
0 |
return removed; |
250 |
|
} |
251 |
|
|
252 |
|
public void evictContentForUser(String username) |
253 |
|
{ |
254 |
0 |
Element userElement = saveGet(username); |
255 |
0 |
if (userElement != null) |
256 |
|
{ |
257 |
0 |
Map map = (Map)userElement.getObjectValue(); |
258 |
0 |
if (map != null) |
259 |
|
{ |
260 |
0 |
Iterator entities = map.values().iterator(); |
261 |
0 |
while (entities.hasNext()) |
262 |
|
{ |
263 |
0 |
ContentCacheKey ccKey = (ContentCacheKey)entities.next(); |
264 |
0 |
ehcache.remove(ccKey.getKey()); |
265 |
0 |
} |
266 |
|
} |
267 |
0 |
ehcache.remove(username); |
268 |
|
} |
269 |
0 |
} |
270 |
|
|
271 |
|
public void evictContentForSession(String session) |
272 |
|
{ |
273 |
0 |
Element userElement = saveGet(session); |
274 |
0 |
if (userElement != null) |
275 |
|
{ |
276 |
0 |
Map map = (Map)userElement.getObjectValue(); |
277 |
0 |
if (map != null) |
278 |
|
{ |
279 |
0 |
Iterator entities = map.values().iterator(); |
280 |
0 |
while (entities.hasNext()) |
281 |
|
{ |
282 |
0 |
ContentCacheKey ccKey = (ContentCacheKey)entities.next(); |
283 |
0 |
ehcache.remove(ccKey.getKey()); |
284 |
0 |
} |
285 |
|
} |
286 |
0 |
ehcache.remove(session); |
287 |
|
} |
288 |
0 |
} |
289 |
|
|
290 |
|
public void clear() |
291 |
|
{ |
292 |
0 |
ehcache.removeAll(); |
293 |
0 |
} |
294 |
|
|
295 |
|
public ContentCacheKey createCacheKey(RequestContext context, String windowId) |
296 |
|
{ |
297 |
0 |
return this.keyGenerator.createCacheKey(context, windowId); |
298 |
|
} |
299 |
|
|
300 |
|
protected Element saveGet(Object key) |
301 |
|
{ |
302 |
|
try |
303 |
|
{ |
304 |
0 |
return ehcache.get(key); |
305 |
|
} |
306 |
0 |
catch (IllegalStateException ise) |
307 |
|
{ |
308 |
|
|
309 |
0 |
return null; |
310 |
|
} |
311 |
|
} |
312 |
|
|
313 |
|
public String createSessionKey(RequestContext context) |
314 |
|
{ |
315 |
0 |
boolean isAjaxRequest = (context == null); |
316 |
0 |
String mode = isAjaxRequest ? "-d-" : "-p-"; |
317 |
0 |
String user = context.getRequest().getRemoteUser(); |
318 |
0 |
if (user == null) |
319 |
0 |
user = "guest"; |
320 |
0 |
return user + mode + context.getPage().getId(); |
321 |
|
} |
322 |
|
|
323 |
|
public void invalidate(RequestContext context) |
324 |
|
{ |
325 |
0 |
String themeCacheKey = createSessionKey(context); |
326 |
0 |
Theme theme = (Theme)context.getRequest().getSession().getAttribute(themeCacheKey); |
327 |
0 |
if (theme != null) |
328 |
|
{ |
329 |
0 |
theme.setInvalidated(true); |
330 |
|
} |
331 |
0 |
} |
332 |
|
} |