1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.cache.impl; |
18 |
|
|
19 |
|
import java.io.Serializable; |
20 |
|
import java.util.Collections; |
21 |
|
import java.util.HashMap; |
22 |
|
import java.util.Iterator; |
23 |
|
import java.util.Map; |
24 |
|
|
25 |
|
import net.sf.ehcache.Cache; |
26 |
|
import net.sf.ehcache.CacheException; |
27 |
|
import net.sf.ehcache.Ehcache; |
28 |
|
import net.sf.ehcache.Element; |
29 |
|
import net.sf.ehcache.event.CacheEventListener; |
30 |
|
import net.sf.ehcache.event.RegisteredEventListeners; |
31 |
|
|
32 |
|
import org.apache.jetspeed.cache.CacheElement; |
33 |
|
import org.apache.jetspeed.cache.DistributedCacheObject; |
34 |
|
import org.apache.jetspeed.cache.JetspeedCache; |
35 |
|
import org.apache.jetspeed.request.RequestContext; |
36 |
|
|
37 |
|
public class EhCacheDistributedImpl extends EhCacheImpl implements JetspeedCache, CacheEventListener |
38 |
|
{ |
39 |
|
|
40 |
|
|
41 |
0 |
private Map refList = Collections.synchronizedMap(new HashMap()); |
42 |
|
|
43 |
|
|
44 |
|
public EhCacheDistributedImpl(Ehcache ehcache) |
45 |
|
{ |
46 |
0 |
super(ehcache); |
47 |
0 |
RegisteredEventListeners listeners = ehcache |
48 |
|
.getCacheEventNotificationService(); |
49 |
0 |
listeners.registerListener(this); |
50 |
|
|
51 |
0 |
} |
52 |
|
|
53 |
|
public CacheElement get(Object key) |
54 |
|
{ |
55 |
0 |
return get((Serializable)key); |
56 |
|
} |
57 |
|
|
58 |
|
|
59 |
|
public CacheElement get(Serializable key) |
60 |
|
{ |
61 |
0 |
Element element = ehcache.get(key); |
62 |
0 |
if (element == null) |
63 |
0 |
return null; |
64 |
0 |
return new EhCacheDistributedElementImpl(element); |
65 |
|
} |
66 |
|
|
67 |
|
|
68 |
|
public boolean isKeyInCache(Object key) |
69 |
|
{ |
70 |
0 |
if ((key == null) || (!(key instanceof Serializable))) |
71 |
0 |
return false; |
72 |
0 |
return ehcache.isKeyInCache(key); |
73 |
|
} |
74 |
|
|
75 |
|
public boolean isKeyInCache(Serializable key) |
76 |
|
{ |
77 |
0 |
return ehcache.isKeyInCache(key); |
78 |
|
} |
79 |
|
|
80 |
|
public void put(CacheElement element) |
81 |
|
{ |
82 |
0 |
EhCacheDistributedElementImpl impl = (EhCacheDistributedElementImpl) element; |
83 |
0 |
ehcache.put(impl.getImplElement()); |
84 |
0 |
refList.put(impl.getKey(), impl); |
85 |
0 |
notifyListeners(true, CacheElement.ActionAdded,impl.getKey(),impl.getContent()); |
86 |
0 |
} |
87 |
|
|
88 |
|
public CacheElement createElement(Object key, Object content) |
89 |
|
{ |
90 |
0 |
return new EhCacheDistributedElementImpl((Serializable)key, (DistributedCacheObject)content); |
91 |
|
} |
92 |
|
|
93 |
|
public CacheElement createElement(Serializable key, DistributedCacheObject content) |
94 |
|
{ |
95 |
0 |
return new EhCacheDistributedElementImpl(key, content); |
96 |
|
} |
97 |
|
|
98 |
|
|
99 |
|
public boolean remove(Object key) |
100 |
|
{ |
101 |
0 |
return remove ((Serializable) key); |
102 |
|
} |
103 |
|
|
104 |
|
public boolean remove(Serializable key) |
105 |
|
{ |
106 |
0 |
Element element = ehcache.get(key); |
107 |
0 |
refList.remove(key); |
108 |
0 |
if (element == null) |
109 |
0 |
return false; |
110 |
0 |
boolean isRemoved = ehcache.remove(key); |
111 |
0 |
if (isRemoved) |
112 |
0 |
notifyListeners(true, CacheElement.ActionRemoved,key,null); |
113 |
0 |
return isRemoved; |
114 |
|
} |
115 |
|
|
116 |
|
public boolean removeQuiet(Object key) |
117 |
|
{ |
118 |
0 |
Element element = ehcache.get(key); |
119 |
0 |
refList.remove(key); |
120 |
0 |
if (element == null) |
121 |
0 |
return false; |
122 |
0 |
return ehcache.removeQuiet(key); |
123 |
|
|
124 |
|
} |
125 |
|
|
126 |
|
public void evictContentForUser(RequestContext context) |
127 |
|
{ |
128 |
0 |
return; |
129 |
|
} |
130 |
|
|
131 |
|
public String createCacheKey(String primary, String secondary) |
132 |
|
{ |
133 |
0 |
return primary; |
134 |
|
} |
135 |
|
|
136 |
|
public Object clone() throws CloneNotSupportedException |
137 |
|
{ |
138 |
0 |
return null; |
139 |
|
} |
140 |
|
|
141 |
|
public void dispose() |
142 |
|
{ |
143 |
0 |
if (refList != null) |
144 |
|
{ |
145 |
0 |
Map temp = refList; |
146 |
0 |
refList = null; |
147 |
0 |
temp.clear(); |
148 |
0 |
} |
149 |
|
else |
150 |
0 |
return; |
151 |
0 |
if (this.ehcache != null) |
152 |
|
{ |
153 |
0 |
ehcache = null; |
154 |
|
} |
155 |
0 |
} |
156 |
|
|
157 |
|
public void notifyElement( Ehcache cache, boolean local,Element arg1, int action) |
158 |
|
{ |
159 |
0 |
if (cache != this.ehcache) |
160 |
|
{ |
161 |
0 |
System.out.println ("Cache=" + cache.getName() + " is not my cache=" + this.ehcache.getName()); |
162 |
0 |
return; |
163 |
|
} |
164 |
|
try |
165 |
|
{ |
166 |
0 |
EhCacheDistributedElementImpl e = (EhCacheDistributedElementImpl) refList |
167 |
|
.get(arg1.getKey()); |
168 |
0 |
if (e != null) |
169 |
|
{ |
170 |
0 |
if (action < 0) |
171 |
0 |
refList.remove(arg1.getKey()); |
172 |
0 |
else if (action == CacheElement.ActionAdded) |
173 |
0 |
refList.put(arg1.getKey(), arg1); |
174 |
0 |
e.notifyChange(action); |
175 |
0 |
notifyListeners(local, action,arg1.getKey(),arg1.getObjectValue()); |
176 |
|
} |
177 |
0 |
} catch (Exception e) |
178 |
|
{ |
179 |
0 |
e.printStackTrace(); |
180 |
0 |
} |
181 |
0 |
} |
182 |
|
|
183 |
|
public void notifyElementEvicted(Ehcache cache, Element arg1) |
184 |
|
{ |
185 |
0 |
notifyElement(cache, false, arg1,CacheElement.ActionEvicted); |
186 |
0 |
} |
187 |
|
|
188 |
|
public void notifyElementExpired(Ehcache cache, Element arg1) |
189 |
|
{ |
190 |
0 |
notifyElement(cache, false, arg1,CacheElement.ActionExpired); |
191 |
0 |
} |
192 |
|
|
193 |
|
public void notifyElementPut(Ehcache cache, Element arg1) |
194 |
|
throws CacheException |
195 |
|
{ |
196 |
|
|
197 |
0 |
notifyElement(cache, false, arg1, CacheElement.ActionAdded); |
198 |
0 |
} |
199 |
|
|
200 |
|
public void notifyElementRemoved(Ehcache cache, Element arg1) |
201 |
|
throws CacheException |
202 |
|
{ |
203 |
0 |
notifyElement(cache, false, arg1,CacheElement.ActionRemoved); |
204 |
0 |
} |
205 |
|
|
206 |
|
public void notifyElementUpdated(Ehcache cache, Element arg1) |
207 |
|
throws CacheException |
208 |
|
{ |
209 |
0 |
notifyElement(cache, false,arg1,CacheElement.ActionChanged); |
210 |
0 |
} |
211 |
|
public void notifyRemoveAll(Ehcache cache) |
212 |
|
{ |
213 |
0 |
if (cache != this.ehcache) |
214 |
|
{ |
215 |
0 |
System.out.println ("Cache=" + cache.getName() + " is not my cache=" + this.ehcache.getName()); |
216 |
0 |
return; |
217 |
|
} |
218 |
|
try |
219 |
|
{ |
220 |
0 |
Iterator it = refList.entrySet().iterator(); |
221 |
0 |
while (it.hasNext()) |
222 |
|
{ |
223 |
0 |
EhCacheDistributedElementImpl e = (EhCacheDistributedElementImpl)it.next(); |
224 |
0 |
notifyListeners(false, CacheElement.ActionRemoved,e.getKey(),e); |
225 |
0 |
e.notifyChange(CacheElement.ActionRemoved); |
226 |
0 |
} |
227 |
0 |
refList.clear(); |
228 |
0 |
} catch (Exception e) |
229 |
|
{ |
230 |
0 |
e.printStackTrace(); |
231 |
0 |
} |
232 |
|
|
233 |
|
|
234 |
0 |
} |
235 |
|
|
236 |
|
} |