1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.spi.config.cache.infinispan;
17
18 import java.util.HashSet;
19 import java.util.Set;
20 import java.util.concurrent.locks.Lock;
21 import java.util.concurrent.locks.ReentrantReadWriteLock;
22
23 import org.infinispan.configuration.cache.Configuration;
24 import org.infinispan.manager.EmbeddedCacheManager;
25
26 import de.smartics.properties.spi.config.cache.Cache;
27 import de.smartics.properties.spi.config.cache.CacheManager;
28 import de.smartics.properties.spi.config.cache.UnawareCache;
29 import de.smartics.properties.spi.config.cache.guava.GuavaCacheManager;
30 import de.smartics.util.lang.StaticAnalysis;
31
32
33
34
35 public abstract class AbstractInfinispanCompoundKeyCacheManager implements
36 CacheManager
37 {
38
39
40
41
42
43
44
45
46
47
48
49 private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
50
51
52
53
54
55
56 private final Lock readLock = lock.readLock();
57
58
59
60
61
62
63 private final Lock writeLock = lock.writeLock();
64
65
66
67
68 private final EmbeddedCacheManager manager;
69
70
71
72
73 private final GuavaCacheManager configManager;
74
75
76
77
78 private final org.infinispan.Cache<Object, Object> cache;
79
80
81
82
83
84
85
86
87
88
89 public AbstractInfinispanCompoundKeyCacheManager(
90 final CacheManagerProxy cacheManager)
91 {
92 this.manager = cacheManager.getManager();
93 configManager = new GuavaCacheManager();
94
95 manager.getDefaultCacheConfiguration().jmxStatistics();
96
97 final String cacheName = cacheManager.getCacheName();
98 this.cache = manager.getCache(cacheName);
99 if (!manager.isRunning(cacheName))
100 {
101 final Configuration cacheConfig = this.cache.getCacheConfiguration();
102 cacheConfig.jmxStatistics();
103 cacheConfig.indexing().indexLocalOnly();
104 cache.start();
105 }
106
107 cache.clear();
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 @Override
124 public final void clearAll()
125 {
126 writeLock.lock();
127 try
128 {
129 internalClearAll();
130 }
131 finally
132 {
133 writeLock.unlock();
134 }
135 }
136
137
138
139
140
141
142 @Override
143 public final void clear(final String cacheName)
144 {
145 writeLock.lock();
146 try
147 {
148 internalClear(cacheName);
149 }
150 finally
151 {
152 writeLock.unlock();
153 }
154 }
155
156
157
158
159
160
161 @Override
162 public final void discard(final String cacheName)
163 {
164 writeLock.lock();
165 try
166 {
167 if (isManagementCache(cacheName))
168 {
169 configManager.discard(cacheName);
170 }
171 internalClear(cacheName);
172 internalStop(cacheName);
173 }
174 finally
175 {
176 writeLock.unlock();
177 }
178
179 }
180
181
182
183
184 @Override
185 public final void stopAll()
186 {
187 writeLock.lock();
188 try
189 {
190 internalStopAll();
191 }
192 finally
193 {
194 writeLock.unlock();
195 }
196 }
197
198
199
200
201
202
203 @Override
204 public final void stop(final String cacheName)
205 {
206 writeLock.lock();
207 try
208 {
209 internalStop(cacheName);
210 }
211 finally
212 {
213 writeLock.unlock();
214 }
215 }
216
217
218
219
220
221
222
223
224 @SuppressWarnings({ StaticAnalysis.RAWTYPES, StaticAnalysis.UNCHECKED })
225 public final UnawareCache getPropertiesCache(final String cacheName)
226 {
227 writeLock.lock();
228 try
229 {
230 final UnawareCache propertiesCache =
231 internalGetPropertiesCache(cacheName);
232 return propertiesCache;
233 }
234 finally
235 {
236 writeLock.unlock();
237 }
238 }
239
240
241
242
243
244
245
246
247 @SuppressWarnings({ StaticAnalysis.RAWTYPES, StaticAnalysis.UNCHECKED })
248 public final Cache getConfigurationsCache(final String cacheName)
249 {
250 writeLock.lock();
251 try
252 {
253 return configManager.getConfigurationsCache(cacheName);
254 }
255 finally
256 {
257 writeLock.unlock();
258 }
259 }
260
261
262
263
264
265
266 @SuppressWarnings({ StaticAnalysis.RAWTYPES, StaticAnalysis.UNCHECKED })
267 @Override
268 public final Set<String> getCacheNames()
269 {
270 readLock.lock();
271 try
272 {
273 final Cache infinispanDelegatingCacheCache =
274 configManager.getInfinispanDelegatingCache();
275 final Set<String> cacheNames = infinispanDelegatingCacheCache.keySet();
276
277 final HashSet allCacheNames = new HashSet<String>();
278 if (cacheNames != null)
279 {
280 allCacheNames.addAll(cacheNames);
281 }
282 final Set inMemoryCacheNames = configManager.getCacheNames();
283 if (inMemoryCacheNames != null)
284 {
285 allCacheNames.addAll(inMemoryCacheNames);
286 }
287 return allCacheNames;
288 }
289 finally
290 {
291 readLock.unlock();
292 }
293 }
294
295
296
297
298
299
300 private void internalClearAll()
301 {
302 configManager.clearAll();
303 cache.clear();
304 }
305
306
307
308
309
310
311 private void internalClear(final String cacheName)
312 {
313 if (isManagementCache(cacheName))
314 {
315 configManager.clear(cacheName);
316 }
317 else if (isInfinispanDelegatingCache(cacheName))
318 {
319 internalClearAll();
320 }
321 else
322 {
323
324 cache.clear();
325 }
326 }
327
328 private boolean isInfinispanDelegatingCache(final String cacheName)
329 {
330 return GuavaCacheManager.INFINISPAN_DELEGATING_CACHE_CACHE_NAME
331 .equals(cacheName);
332 }
333
334 private boolean isManagementCache(final String cacheName)
335 {
336 return cacheName.startsWith("Management");
337 }
338
339
340
341
342 private void internalStopAll()
343 {
344 configManager.stopAll();
345 cache.stop();
346 }
347
348
349
350
351
352
353 private void internalStop(final String cacheName)
354 {
355 if (isManagementCache(cacheName))
356 {
357 configManager.stop(cacheName);
358 }
359 }
360
361
362
363
364
365
366
367
368 @SuppressWarnings({ StaticAnalysis.RAWTYPES, StaticAnalysis.UNCHECKED })
369 private UnawareCache<?, ?> internalGetPropertiesCache(final String cacheName)
370 {
371 final Cache infinispanCacheCache =
372 configManager.getInfinispanDelegatingCache();
373 InfinispanCompoundKeyDelegatingCache infinispanCache =
374 (InfinispanCompoundKeyDelegatingCache) infinispanCacheCache
375 .get(cacheName);
376
377 if (infinispanCache == null)
378 {
379 infinispanCache =
380 new InfinispanCompoundKeyDelegatingCache(cacheName, this.cache);
381 infinispanCacheCache.put(cacheName, infinispanCache);
382 }
383 return infinispanCache;
384 }
385
386
387
388 }