1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.jboss.cache.pojo.impl; |
9 |
| |
10 |
| import java.util.Collection; |
11 |
| import java.util.Map; |
12 |
| import java.util.WeakHashMap; |
13 |
| import java.util.regex.Pattern; |
14 |
| |
15 |
| import org.apache.commons.logging.Log; |
16 |
| import org.apache.commons.logging.LogFactory; |
17 |
| import org.jboss.cache.Cache; |
18 |
| import org.jboss.cache.CacheException; |
19 |
| import org.jboss.cache.CacheSPI; |
20 |
| import org.jboss.cache.DefaultCacheFactory; |
21 |
| import org.jboss.cache.Fqn; |
22 |
| import org.jboss.cache.Version; |
23 |
| import org.jboss.cache.config.Configuration; |
24 |
| import org.jboss.cache.factories.XmlConfigurationParser; |
25 |
| import org.jboss.cache.pojo.PojoCache; |
26 |
| import org.jboss.cache.pojo.PojoCacheException; |
27 |
| import org.jboss.cache.pojo.annotation.Attach; |
28 |
| import org.jboss.cache.pojo.annotation.Detach; |
29 |
| import org.jboss.cache.pojo.annotation.Find; |
30 |
| import org.jboss.cache.pojo.notification.annotation.PojoCacheListener; |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| public class PojoCacheImpl implements PojoCache |
39 |
| { |
40 |
| private CacheSPI<Object, Object> cache = null; |
41 |
| protected final Log log_ = LogFactory.getLog(PojoCacheImpl.this.getClass()); |
42 |
| private PojoCacheDelegate delegate_; |
43 |
| |
44 |
| |
45 |
| private Map cachedTypes_ = new WeakHashMap(); |
46 |
| private boolean hasCreate_ = false; |
47 |
| private CacheListenerAdaptor listenerAdaptor = new CacheListenerAdaptor(this); |
48 |
| |
49 |
231
| public PojoCacheImpl(String configStr, boolean toStart)
|
50 |
| { |
51 |
231
| try
|
52 |
| { |
53 |
| |
54 |
| |
55 |
| |
56 |
231
| XmlConfigurationParser parser = new XmlConfigurationParser();
|
57 |
231
| Configuration expected = parser.parseFile(configStr);
|
58 |
| |
59 |
231
| init(expected, toStart);
|
60 |
| } |
61 |
| catch (Exception e) |
62 |
| { |
63 |
0
| throw new PojoCacheException("Failed to start " + configStr, e);
|
64 |
| } |
65 |
| } |
66 |
| |
67 |
268
| public PojoCacheImpl(Configuration config, boolean toStart)
|
68 |
| { |
69 |
268
| init(config, toStart);
|
70 |
| } |
71 |
| |
72 |
499
| private void init(Configuration config, boolean toStart)
|
73 |
| { |
74 |
499
| try
|
75 |
| { |
76 |
499
| cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache(config, toStart);
|
77 |
| } |
78 |
| catch (Exception e) |
79 |
| { |
80 |
0
| throw new PojoCacheException("init " + config + " failed", e);
|
81 |
| } |
82 |
| |
83 |
499
| delegate_ = new PojoCacheDelegate(this);
|
84 |
| } |
85 |
| |
86 |
25470
| public CacheSPI getCacheSPI()
|
87 |
| { |
88 |
25470
| return cache;
|
89 |
| } |
90 |
| |
91 |
1557
| public Object attach(String id, Object pojo) throws PojoCacheException
|
92 |
| { |
93 |
1557
| return attach(Fqn.fromString(id), pojo);
|
94 |
| } |
95 |
| |
96 |
5260
| @Attach
|
97 |
| public Object attach(Fqn id, Object pojo) throws PojoCacheException |
98 |
| { |
99 |
5260
| try
|
100 |
| { |
101 |
5260
| Object obj = putObject(id, pojo, null);
|
102 |
5259
| return obj;
|
103 |
| } |
104 |
| catch (CacheException e) |
105 |
| { |
106 |
0
| throw new PojoCacheException("putObject failed " + id, e);
|
107 |
| } |
108 |
| } |
109 |
| |
110 |
2379
| @Attach
|
111 |
| public Object attach(Fqn id, Object pojo, String field) throws PojoCacheException |
112 |
| { |
113 |
2379
| try
|
114 |
| { |
115 |
2379
| Object obj = putObject(id, pojo, field);
|
116 |
2379
| return obj;
|
117 |
| } |
118 |
| catch (CacheException e) |
119 |
| { |
120 |
0
| throw new PojoCacheException("putObject failed " + id, e);
|
121 |
| } |
122 |
| } |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
7639
| public Object putObject(Fqn id, Object pojo, String field)
|
128 |
| throws CacheException |
129 |
| { |
130 |
7639
| Object obj = null;
|
131 |
| |
132 |
| |
133 |
7639
| obj = delegate_.putObjectI(id, pojo, field);
|
134 |
2
| if (obj != null) return obj;
|
135 |
| |
136 |
7637
| obj = delegate_.putObjectII(id, pojo, field);
|
137 |
7636
| return obj;
|
138 |
| } |
139 |
| |
140 |
990
| public Object detach(String id) throws PojoCacheException
|
141 |
| { |
142 |
990
| return detach(Fqn.fromString(id));
|
143 |
| } |
144 |
| |
145 |
17479
| @Detach
|
146 |
| public Object detach(Fqn id, String field) throws PojoCacheException |
147 |
| { |
148 |
17474
| try
|
149 |
| { |
150 |
17479
| Object pojo = getObject(id, field);
|
151 |
11162
| if (pojo == null) return pojo;
|
152 |
| |
153 |
6316
| Object obj = removeObject(id, field);
|
154 |
6316
| return obj;
|
155 |
| } |
156 |
| catch (CacheException e) |
157 |
| { |
158 |
1
| throw new PojoCacheException("detach " + id + " failed", e);
|
159 |
| } |
160 |
| } |
161 |
| |
162 |
4065
| public Object detach(Fqn id) throws PojoCacheException
|
163 |
| { |
164 |
4065
| return detach(id, null);
|
165 |
| } |
166 |
| |
167 |
6316
| public Object removeObject(Fqn id, String field) throws CacheException
|
168 |
| { |
169 |
6316
| delegate_.setBulkRemove(false);
|
170 |
6316
| return delegate_.removeObject(id, field);
|
171 |
| } |
172 |
| |
173 |
0
| public String getPojoID(Object pojo)
|
174 |
| { |
175 |
0
| throw new PojoCacheException("getPojoID not yet implemented");
|
176 |
| } |
177 |
| |
178 |
714
| public Object find(String id) throws PojoCacheException
|
179 |
| { |
180 |
714
| return find(Fqn.fromString(id));
|
181 |
| } |
182 |
| |
183 |
714
| @Find
|
184 |
| public Object find(Fqn id) throws PojoCacheException |
185 |
| { |
186 |
714
| try
|
187 |
| { |
188 |
714
| return getObject(id);
|
189 |
| } |
190 |
| catch (CacheException e) |
191 |
| { |
192 |
1
| throw new PojoCacheException("find " + id + " failed ", e);
|
193 |
| } |
194 |
| } |
195 |
| |
196 |
8412
| public Object getObject(Fqn id) throws CacheException
|
197 |
| { |
198 |
8412
| return getObject(id, null);
|
199 |
| } |
200 |
| |
201 |
27985
| public Object getObject(Fqn id, String field) throws CacheException
|
202 |
| { |
203 |
27985
| return delegate_.getObject(id, field);
|
204 |
| } |
205 |
| |
206 |
| |
207 |
9
| public Map findAll(String id) throws PojoCacheException
|
208 |
| { |
209 |
9
| return findAll(Fqn.fromString(id));
|
210 |
| } |
211 |
| |
212 |
9
| @Find
|
213 |
| public Map findAll(Fqn id) throws PojoCacheException |
214 |
| { |
215 |
| |
216 |
0
| if (id == null) id = Fqn.ROOT;
|
217 |
| |
218 |
9
| try
|
219 |
| { |
220 |
9
| return delegate_.findObjects(id);
|
221 |
| } |
222 |
| catch (CacheException e) |
223 |
| { |
224 |
0
| throw new PojoCacheException("findAll " + id + " failed", e);
|
225 |
| } |
226 |
| } |
227 |
| |
228 |
943
| public String getVersion()
|
229 |
| { |
230 |
943
| return Version.printVersion();
|
231 |
| } |
232 |
| |
233 |
475
| public void create() throws PojoCacheException
|
234 |
| { |
235 |
475
| log_.info("PojoCache version: " + getVersion());
|
236 |
475
| try
|
237 |
| { |
238 |
475
| cache.create();
|
239 |
| } |
240 |
| catch (Exception e) |
241 |
| { |
242 |
0
| throw new PojoCacheException("PojoCache create exception", e);
|
243 |
| } |
244 |
| |
245 |
475
| hasCreate_ = true;
|
246 |
| } |
247 |
| |
248 |
468
| public void start() throws PojoCacheException
|
249 |
| { |
250 |
468
| if (!hasCreate_)
|
251 |
| { |
252 |
433
| create();
|
253 |
| } |
254 |
| |
255 |
468
| try
|
256 |
| { |
257 |
468
| log_.info("PojoCache version: " + getVersion());
|
258 |
468
| cache.start();
|
259 |
| } |
260 |
| catch (Exception e) |
261 |
| { |
262 |
1
| throw new PojoCacheException("Failed starting " + e, e);
|
263 |
| } |
264 |
| } |
265 |
| |
266 |
485
| public void stop() throws PojoCacheException
|
267 |
| { |
268 |
485
| cache.stop();
|
269 |
| } |
270 |
| |
271 |
33
| public void destroy() throws PojoCacheException
|
272 |
| { |
273 |
33
| cache.destroy();
|
274 |
| } |
275 |
| |
276 |
0
| public Collection<Object> getListeners()
|
277 |
| { |
278 |
0
| return listenerAdaptor.getListeners();
|
279 |
| } |
280 |
| |
281 |
50
| public void addListener(Object listener)
|
282 |
| { |
283 |
50
| addListener(listener, null);
|
284 |
| } |
285 |
| |
286 |
50
| public void addListener(Object listener, Pattern pattern)
|
287 |
| { |
288 |
| |
289 |
| |
290 |
| |
291 |
50
| synchronized (listenerAdaptor)
|
292 |
| { |
293 |
50
| try
|
294 |
| { |
295 |
50
| listenerAdaptor.addListener(listener, pattern);
|
296 |
| } |
297 |
| catch (IllegalArgumentException e) |
298 |
| { |
299 |
| |
300 |
0
| e.fillInStackTrace();
|
301 |
0
| throw e;
|
302 |
| } |
303 |
50
| cache.addCacheListener(listenerAdaptor);
|
304 |
| } |
305 |
| } |
306 |
| |
307 |
0
| public void removeListener(Object listener)
|
308 |
| { |
309 |
0
| synchronized (listenerAdaptor)
|
310 |
| { |
311 |
0
| listenerAdaptor.removeListener(listener);
|
312 |
0
| if (listenerAdaptor.isEmpty())
|
313 |
0
| cache.removeCacheListener(listenerAdaptor);
|
314 |
| } |
315 |
| } |
316 |
| |
317 |
18642
| public Cache<Object,Object> getCache()
|
318 |
| { |
319 |
18642
| return cache;
|
320 |
| } |
321 |
| |
322 |
| |
323 |
| |
324 |
| |
325 |
0
| public Object _evictObject(Fqn fqn) throws CacheException
|
326 |
| { |
327 |
0
| boolean evict = true;
|
328 |
0
| boolean removeCacheInterceptor = false;
|
329 |
| |
330 |
| |
331 |
| |
332 |
| |
333 |
0
| delegate_.setBulkRemove(false);
|
334 |
| |
335 |
0
| return null;
|
336 |
| } |
337 |
| |
338 |
| |
339 |
| |
340 |
| |
341 |
| |
342 |
| |
343 |
| |
344 |
| |
345 |
41006
| public synchronized CachedType getCachedType(Class clazz)
|
346 |
| { |
347 |
41006
| CachedType type = (CachedType) cachedTypes_.get(clazz);
|
348 |
41006
| if (type == null)
|
349 |
| { |
350 |
1997
| type = new CachedType(clazz);
|
351 |
1997
| cachedTypes_.put(clazz, type);
|
352 |
1997
| return type;
|
353 |
| } |
354 |
| else |
355 |
| { |
356 |
39009
| return type;
|
357 |
| } |
358 |
| } |
359 |
| |
360 |
0
| public String toString()
|
361 |
| { |
362 |
0
| return getClass().getName() +
|
363 |
| " cache=" + cache + |
364 |
| " delegate=" + delegate_ + |
365 |
| " types=" + cachedTypes_.size(); |
366 |
| } |
367 |
| } |