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.ArrayList; |
21 |
|
import java.util.List; |
22 |
|
|
23 |
|
import net.sf.ehcache.Ehcache; |
24 |
|
import net.sf.ehcache.Element; |
25 |
|
|
26 |
|
import org.apache.jetspeed.cache.CacheElement; |
27 |
|
import org.apache.jetspeed.cache.ContentCacheKey; |
28 |
|
import org.apache.jetspeed.cache.JetspeedCache; |
29 |
|
import org.apache.jetspeed.cache.JetspeedCacheEventListener; |
30 |
|
import org.apache.jetspeed.request.RequestContext; |
31 |
|
|
32 |
|
public class EhCacheImpl implements JetspeedCache |
33 |
|
{ |
34 |
|
protected Ehcache ehcache; |
35 |
0 |
protected List localListeners = new ArrayList(); |
36 |
0 |
protected List remoteListeners = new ArrayList(); |
37 |
|
|
38 |
|
public EhCacheImpl(Ehcache ehcache) |
39 |
0 |
{ |
40 |
0 |
this.ehcache = ehcache; |
41 |
0 |
} |
42 |
|
|
43 |
|
public CacheElement get(Object key) |
44 |
|
{ |
45 |
0 |
Element element = ehcache.get(key); |
46 |
0 |
if (element == null) |
47 |
0 |
return null; |
48 |
0 |
return new EhCacheElementImpl(element); |
49 |
|
} |
50 |
|
|
51 |
|
public int getTimeToIdleSeconds() |
52 |
|
{ |
53 |
0 |
return (int)ehcache.getTimeToIdleSeconds(); |
54 |
|
} |
55 |
|
|
56 |
|
public int getTimeToLiveSeconds() |
57 |
|
{ |
58 |
0 |
return (int)ehcache.getTimeToLiveSeconds(); |
59 |
|
} |
60 |
|
|
61 |
|
public boolean isKeyInCache(Object key) |
62 |
|
{ |
63 |
0 |
return ehcache.isKeyInCache(key); |
64 |
|
} |
65 |
|
|
66 |
|
|
67 |
|
public void put(CacheElement element) |
68 |
|
{ |
69 |
0 |
EhCacheElementImpl impl = (EhCacheElementImpl)element; |
70 |
0 |
ehcache.put(impl.getImplElement()); |
71 |
0 |
notifyListeners(true, CacheElement.ActionAdded,impl.getKey(),impl.getContent()); |
72 |
0 |
} |
73 |
|
|
74 |
|
public CacheElement createElement(Object key, Object content) |
75 |
|
{ |
76 |
0 |
if (!((key instanceof Serializable) || !(content instanceof Serializable))) |
77 |
0 |
throw new IllegalArgumentException("The cache key and the object to cache must be serializable."); |
78 |
0 |
return new EhCacheElementImpl((Serializable)key, (Serializable)content); |
79 |
|
} |
80 |
|
|
81 |
|
public boolean remove(Object key) |
82 |
|
{ |
83 |
0 |
Element element = ehcache.get(key); |
84 |
0 |
if (element == null) |
85 |
0 |
return false; |
86 |
0 |
boolean isRemoved = ehcache.remove(key); |
87 |
0 |
if (isRemoved) |
88 |
0 |
notifyListeners(true, CacheElement.ActionRemoved,key,null); |
89 |
0 |
return isRemoved; |
90 |
|
} |
91 |
|
|
92 |
|
public boolean removeQuiet(Object key) |
93 |
|
{ |
94 |
0 |
Element element = ehcache.get(key); |
95 |
0 |
if (element == null) |
96 |
0 |
return false; |
97 |
0 |
return ehcache.removeQuiet(key); |
98 |
|
} |
99 |
|
|
100 |
|
public void clear() |
101 |
|
{ |
102 |
0 |
ehcache.removeAll(); |
103 |
0 |
notifyListeners(true, CacheElement.ActionRemoved,null,class="keyword">null); |
104 |
0 |
} |
105 |
|
|
106 |
|
public void evictContentForUser(String username) |
107 |
|
{ |
108 |
0 |
return; |
109 |
|
} |
110 |
|
|
111 |
|
public void evictContentForSession(String session) |
112 |
|
{ |
113 |
0 |
return; |
114 |
|
} |
115 |
|
|
116 |
|
public void addEventListener(JetspeedCacheEventListener listener, boolean local) |
117 |
|
{ |
118 |
0 |
if (local) |
119 |
0 |
localListeners.add(listener); |
120 |
|
else |
121 |
0 |
remoteListeners.add(listener); |
122 |
|
|
123 |
0 |
} |
124 |
|
|
125 |
|
public void removeEventListener(JetspeedCacheEventListener listener, boolean local) |
126 |
|
{ |
127 |
0 |
if (local) |
128 |
0 |
localListeners.remove(listener); |
129 |
|
else |
130 |
0 |
remoteListeners.remove(listener); |
131 |
|
|
132 |
0 |
} |
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
public Object clone() throws CloneNotSupportedException |
137 |
|
{ |
138 |
0 |
return super.clone(); |
139 |
|
} |
140 |
|
|
141 |
|
public void dispose() |
142 |
|
{ |
143 |
0 |
} |
144 |
|
|
145 |
|
protected void notifyListeners(boolean local, int action, Object key, Object value) |
146 |
|
{ |
147 |
0 |
List listeners = (local?localListeners:remoteListeners); |
148 |
0 |
for (int ix = 0; ix < listeners.size(); ix++) |
149 |
|
{ |
150 |
|
try |
151 |
|
{ |
152 |
0 |
JetspeedCacheEventListener listener = (JetspeedCacheEventListener)listeners.get(ix); |
153 |
0 |
switch (action) |
154 |
|
{ |
155 |
|
case CacheElement.ActionAdded: |
156 |
0 |
listener.notifyElementAdded(this,local, key,value); |
157 |
0 |
break; |
158 |
|
case CacheElement.ActionChanged: |
159 |
0 |
listener.notifyElementChanged(this,local, key,value); |
160 |
0 |
break; |
161 |
|
case CacheElement.ActionRemoved: |
162 |
0 |
listener.notifyElementRemoved(this,local, key,value); |
163 |
0 |
break; |
164 |
|
case CacheElement.ActionEvicted: |
165 |
0 |
listener.notifyElementEvicted(this,local, key,value); |
166 |
0 |
break; |
167 |
|
case CacheElement.ActionExpired: |
168 |
0 |
listener.notifyElementExpired(this,local, key,value); |
169 |
|
break; |
170 |
|
} |
171 |
|
} |
172 |
0 |
catch (Exception e) |
173 |
|
{ |
174 |
0 |
e.printStackTrace(); |
175 |
|
|
176 |
0 |
} |
177 |
|
} |
178 |
0 |
} |
179 |
|
|
180 |
|
public ContentCacheKey createCacheKey(RequestContext rc, String windowId) |
181 |
|
{ |
182 |
0 |
return null; |
183 |
|
} |
184 |
|
|
185 |
|
} |