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