/
AbstractJCacheManager.java
295 lines (245 loc) · 9.2 KB
/
AbstractJCacheManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package org.infinispan.jcache;
import static org.infinispan.jcache.RIMBeanServerRegistrationUtility.ObjectNameType.CONFIGURATION;
import static org.infinispan.jcache.RIMBeanServerRegistrationUtility.ObjectNameType.STATISTICS;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.configuration.Configuration;
import javax.cache.spi.CachingProvider;
import org.infinispan.commons.api.BasicCache;
import org.infinispan.commons.logging.LogFactory;
import org.infinispan.commons.util.InfinispanCollections;
import org.infinispan.jcache.logging.Log;
/**
* Infinispan's implementation of {@link javax.cache.CacheManager}.
*
* @author Vladimir Blagojevic
* @author Galder Zamarreño
*/
public abstract class AbstractJCacheManager implements CacheManager {
private static final Log log = LogFactory.getLog(AbstractJCacheManager.class, Log.class);
private final URI uri;
private final CachingProvider provider;
private final Properties properties;
private final HashMap<String, AbstractJCache<?, ?>> caches = new HashMap<String, AbstractJCache<?, ?>>();
private final StackTraceElement[] allocationStackTrace;
/**
* Boolean flag tracking down whether the underlying Infinispan cache
* manager used by JCacheManager is unmanaged or managed. Unmanaged means
* that this JCacheManager instance controls the lifecycle of the
* Infinispan Cache Manager. When managed, it means that the cache manager
* is injected and hence JCacheManager is not the owner of the lifecycle
* of this cache manager.
*/
private final boolean managedCacheManager;
/**
* A flag indicating whether the cache manager is closed or not.
* Cache manager's status does not fit well here because even if an
* trying to stop a cache manager whose status is {@link ComponentStatus#INSTANTIATED}
* does not change it to {@link ComponentStatus#TERMINATED}
*/
private volatile boolean isClosed;
public AbstractJCacheManager(URI uri, ClassLoader classLoader, CachingProvider provider, Properties properties, boolean managedCacheManager) {
// Track allocation time
this.allocationStackTrace = Thread.currentThread().getStackTrace();
this.uri = uri;
this.provider = provider;
this.properties = properties;
this.managedCacheManager = managedCacheManager;
isClosed = false;
}
@Override
public CachingProvider getCachingProvider() {
return provider;
}
@Override
public URI getURI() {
return uri;
}
@Override
public Properties getProperties() {
return properties;
}
@Override
public <K, V, C extends Configuration<K, V>> Cache<K, V> createCache(
String cacheName, C configuration) {
checkNotClosed().checkNull(cacheName, "cacheName").checkNull(configuration, "configuration");
synchronized (caches) {
AbstractJCache<?, ?> cache = caches.get(cacheName);
if (cache == null) {
cache = create(cacheName, configuration);
caches.put(cache.getName(), cache);
} else {
throw log.cacheAlreadyRegistered(cacheName,
cache.getConfiguration(Configuration.class), configuration);
}
return unchecked(cache);
}
}
@Override
public <K, V> Cache<K, V> getCache(String cacheName, Class<K> keyType, Class<V> valueType) {
checkNotClosed().checkNull(keyType, "keyType").checkNull(valueType, "valueType");
synchronized (caches) {
Cache<K, V> cache = unchecked(caches.get(cacheName));
if (cache != null) {
Configuration<?, ?> configuration = cache.getConfiguration(Configuration.class);
Class<?> cfgKeyType = configuration.getKeyType();
if (verifyType(keyType, cfgKeyType)) {
Class<?> cfgValueType = configuration.getValueType();
if (verifyType(valueType, cfgValueType))
return cache;
throw log.incompatibleType(valueType, cfgValueType);
}
throw log.incompatibleType(keyType, cfgKeyType);
}
return null;
}
}
@Override
public <K, V> Cache<K, V> getCache(String cacheName) {
checkNotClosed();
synchronized (caches) {
Cache<K, V> cache = unchecked(caches.get(cacheName));
if (cache != null) {
Configuration<K, V> configuration = cache.getConfiguration(Configuration.class);
Class<K> keyType = configuration.getKeyType();
Class<V> valueType = configuration.getValueType();
if (Object.class.equals(keyType) && Object.class.equals(valueType))
return cache;
throw log.unsafeTypedCacheRequest(cacheName, keyType, valueType);
}
return null;
}
}
@Override
public Iterable<String> getCacheNames() {
return isClosed ? InfinispanCollections.<String>emptyList() : delegateCacheNames();
}
@Override
public void destroyCache(String cacheName) {
checkNotClosed().checkNull(cacheName, "cacheName");
AbstractJCache<?, ?> destroyedCache;
synchronized (caches) {
destroyedCache = caches.remove(cacheName);
}
if (destroyedCache != null) {
/* Don't destroy caches not created through jcache. */
delegateRemoveCache(destroyedCache);
}
unregisterCacheMBeans(destroyedCache);
}
@Override
public void enableManagement(String cacheName, boolean enabled) {
checkNotClosed();
caches.get(cacheName).setManagementEnabled(enabled);
}
@Override
public void enableStatistics(String cacheName, boolean enabled) {
checkNotClosed();
caches.get(cacheName).setStatisticsEnabled(enabled);
}
@Override
public void close() {
if (!isClosed()) {
ArrayList<AbstractJCache<?, ?>> cacheList;
synchronized (caches) {
cacheList = new ArrayList<AbstractJCache<?, ?>>(caches.values());
caches.clear();
}
for (AbstractJCache<?, ?> cache : cacheList) {
try {
cache.close();
unregisterCacheMBeans(cache);
} catch (Exception e) {
// log?
}
}
delegateStop();
isClosed = true;
}
}
@Override
public boolean isClosed() {
return delegateIsClosed() || isClosed;
}
/**
* Avoid weak references to this cache manager
* being garbage collected without being shutdown.
*/
@Override
protected void finalize() throws Throwable {
try {
if(!managedCacheManager && !isClosed) {
// Create the leak description
Throwable t = log.cacheManagerNotClosed();
t.setStackTrace(allocationStackTrace);
log.leakedCacheManager(t);
// Close
delegateStop();
}
} finally {
super.finalize();
}
}
public <K, V, I extends BasicCache<K, V> > Cache<K, V> getOrCreateCache(String cacheName, I ispnCache) {
synchronized (caches) {
AbstractJCache<?, ?> cache = caches.get(cacheName);
if (cache == null) {
cache = create(ispnCache);
caches.put(cacheName, cache);
}
return unchecked(cache);
}
}
protected <K, V> void registerPredefinedCache(String cacheName, AbstractJCache<K, V> cache) {
caches.put(cacheName, cache);
}
//TODO: proper JMX!
protected abstract boolean supportsJmx();
protected abstract void delegateLogIsClosed();
protected abstract Iterable<String> delegateCacheNames();
protected abstract void delegateStop();
protected abstract boolean delegateIsClosed();
protected abstract <K, V> void delegateRemoveCache(AbstractJCache<K, V> cacheName);
protected abstract <K, V, C extends Configuration<K, V>> AbstractJCache<K, V> create(String cacheName, C configuration);
protected abstract <K, V, I extends BasicCache<K, V> > AbstractJCache<K, V> create(I ispnCache);
protected Set<String> getManagedCacheNames() {
HashSet<String> result = new HashSet<String>();
synchronized (caches) {
result.addAll(caches.keySet());
}
return Collections.unmodifiableSet(result);
}
private void unregisterCacheMBeans(AbstractJCache<?, ?> cache) {
if (!supportsJmx()) {
return;
}
if (cache != null) {
RIMBeanServerRegistrationUtility.unregisterCacheObject(cache, STATISTICS);
RIMBeanServerRegistrationUtility.unregisterCacheObject(cache, CONFIGURATION);
}
}
private AbstractJCacheManager checkNotClosed() {
if (isClosed())
delegateLogIsClosed();
return this;
}
private AbstractJCacheManager checkNull(Object obj, String name) {
if (obj == null)
throw log.parameterMustNotBeNull(name);
return this;
}
@SuppressWarnings("unchecked")
private <K, V> Cache<K, V> unchecked(Cache<?, ?> cache) {
return (Cache<K, V>) cache;
}
private <K> boolean verifyType(Class<K> type, Class<?> cfgType) {
return cfgType != null && cfgType.equals(type);
}
}