35
35
import java .io .IOException ;
36
36
import java .lang .management .ManagementFactory ;
37
37
import java .net .URI ;
38
- import java .util .*;
38
+ import java .util .Collections ;
39
+ import java .util .HashSet ;
40
+ import java .util .Map ;
41
+ import java .util .Properties ;
39
42
import java .util .concurrent .ConcurrentHashMap ;
40
43
import java .util .concurrent .ConcurrentMap ;
41
44
48
51
import javax .management .InstanceNotFoundException ;
49
52
import javax .management .MBeanServer ;
50
53
54
+ import org .ehcache .core .events .CacheManagerListener ;
55
+ import org .ehcache .core .spi .store .InternalCacheManager ;
51
56
import static org .ehcache .jsr107 .CloseUtil .chain ;
52
57
import static org .ehcache .jsr107 .CloseUtil .closeAll ;
53
58
@@ -61,7 +66,7 @@ class Eh107CacheManager implements CacheManager {
61
66
private static final MBeanServer MBEAN_SERVER = ManagementFactory .getPlatformMBeanServer ();
62
67
63
68
private final Object cachesLock = new Object ();
64
- private final ConcurrentMap <String , Eh107Cache <?, ?>> lazilyLoadedCaches = new ConcurrentHashMap <>();
69
+ private final ConcurrentMap <String , Eh107Cache <?, ?>> caches = new ConcurrentHashMap <>();
65
70
private final org .ehcache .CacheManager ehCacheManager ;
66
71
private final EhcacheCachingProvider cachingProvider ;
67
72
private final ClassLoader classLoader ;
@@ -80,31 +85,71 @@ class Eh107CacheManager implements CacheManager {
80
85
this .configurationMerger = configurationMerger ;
81
86
this .statisticsService = jsr107Service .getStatistics ();
82
87
88
+ ((InternalCacheManager ) ehCacheManager ).registerListener (new CacheManagerListener () {
89
+ @ Override
90
+ public void cacheAdded (String alias , org .ehcache .Cache <?, ?> cache ) {
91
+ loadCache (alias , cache );
92
+ }
93
+
94
+ @ Override
95
+ public void cacheRemoved (String alias , org .ehcache .Cache <?, ?> cache ) {
96
+ Eh107Cache <?, ?> jcache = caches .get (alias );
97
+ if (jcache != null ) {
98
+ close (jcache );
99
+ }
100
+ }
101
+
102
+ @ Override
103
+ public void stateTransition (Status from , Status to ) {
104
+ }
105
+
106
+ });
107
+ loadAllCaches ();
83
108
}
84
109
110
+ private void loadAllCaches () {
111
+ for (Map .Entry <String , CacheConfiguration <?, ?>> entry : ehCacheManager .getRuntimeConfiguration ().getCacheConfigurations ().entrySet ()) {
112
+ CacheConfiguration <?, ?> config = entry .getValue ();
113
+ InternalCache <?, ?> cache = (InternalCache <?, ?>) ehCacheManager .getCache (entry .getKey (), config .getKeyType (), config .getValueType ());
85
114
86
- private void loadCache (String cacheName ) {
87
- Map <String , CacheConfiguration <?, ?>> cacheConfigurations = ehCacheManager .getRuntimeConfiguration ().getCacheConfigurations ();
88
- CacheConfiguration <?, ?> cacheConfiguration ;
115
+ loadCache (entry .getKey (), cache );
116
+ }
89
117
90
- if (null != (cacheConfiguration = cacheConfigurations .get (cacheName ))) {
91
- Eh107Cache <?, ?> wrappedCache = wrapEhcacheCache (cacheName , cacheConfiguration );
92
- if (lazilyLoadedCaches .putIfAbsent (cacheName , wrappedCache ) == null ) {
93
- @ SuppressWarnings ("unchecked" )
94
- Eh107Configuration <?, ?> configuration = wrappedCache .getConfiguration (Eh107Configuration .class );
95
- if (configuration .isManagementEnabled ()) {
96
- enableManagement (wrappedCache , true );
97
- }
98
- if (configuration .isStatisticsEnabled ()) {
99
- enableStatistics (wrappedCache , true );
100
- }
101
- }
118
+ for (Eh107Cache <?, ?> wrappedCache : caches .values ()) {
119
+ wrappedCache .isClosed ();
102
120
}
103
121
}
104
122
105
- private <K , V > Eh107Cache <K , V > wrapEhcacheCache (String alias , CacheConfiguration <K , V > ehConfig ) {
106
- org .ehcache .Cache <K , V > cache = ehCacheManager .getCache (alias , ehConfig .getKeyType (), ehConfig .getValueType ());
107
- return wrapEhcacheCache (alias , (InternalCache <K , V >)cache );
123
+ @ SuppressWarnings ("unchecked" )
124
+ private <K , V > Cache <K , V > loadCache (String alias , org .ehcache .Cache <K , V > cache ) {
125
+ return (Cache <K , V >) caches .computeIfAbsent (alias , name -> {
126
+ Eh107Cache <?, ?> wrappedCache = wrapEhcacheCache (name , (InternalCache <K , V >) cache );
127
+ @ SuppressWarnings ("unchecked" )
128
+ Eh107Configuration <?, ?> configuration = wrappedCache .getConfiguration (Eh107Configuration .class );
129
+ if (configuration .isManagementEnabled ()) {
130
+ enableManagement (wrappedCache , true );
131
+ }
132
+ if (configuration .isStatisticsEnabled ()) {
133
+ enableStatistics (wrappedCache , true );
134
+ }
135
+ return wrappedCache ;
136
+ });
137
+ }
138
+
139
+ @ SuppressWarnings ("unchecked" )
140
+ private <K , V > Cache <K , V > reloadCache (String alias , Eh107Cache <K , V > jcache ) {
141
+ return (Cache <K , V >) caches .computeIfPresent (alias , (name , existing ) -> {
142
+ @ SuppressWarnings ("unchecked" )
143
+ Eh107Configuration <?, ?> oldConfiguration = existing .getConfiguration (Eh107Configuration .class );
144
+ Eh107Configuration <?, ?> newConfiguration = jcache .getConfiguration (Eh107Configuration .class );
145
+ if (oldConfiguration .isManagementEnabled () != newConfiguration .isManagementEnabled ()) {
146
+ enableManagement (jcache , newConfiguration .isManagementEnabled ());
147
+ }
148
+ if (oldConfiguration .isStatisticsEnabled () != newConfiguration .isStatisticsEnabled ()) {
149
+ enableStatistics (jcache , newConfiguration .isStatisticsEnabled ());
150
+ }
151
+ return jcache ;
152
+ });
108
153
}
109
154
110
155
private <K , V > Eh107Cache <K , V > wrapEhcacheCache (String alias , InternalCache <K , V > cache ) {
@@ -167,67 +212,42 @@ public <K, V, C extends Configuration<K, V>> Cache<K, V> createCache(String cach
167
212
@ SuppressWarnings ("unchecked" )
168
213
Eh107Configuration .Eh107ConfigurationWrapper <K , V > configurationWrapper = (Eh107Configuration .Eh107ConfigurationWrapper <K , V >)config ;
169
214
CacheConfiguration <K , V > unwrap = configurationWrapper .getCacheConfiguration ();
170
- final org .ehcache .Cache <K , V > ehcache ;
171
215
try {
172
- ehcache = ehCacheManager .createCache (cacheName , unwrap );
216
+ ehCacheManager .createCache (cacheName , unwrap );
173
217
} catch (IllegalArgumentException e ) {
174
218
throw new CacheException ("A Cache named [" + cacheName + "] already exists" );
175
219
}
176
- Eh107Cache <K , V > cache = wrapEhcacheCache (cacheName , (InternalCache <K , V >)ehcache );
177
- assert safeCacheRetrieval (cacheName ) == null ;
178
- lazilyLoadedCaches .put (cacheName , cache );
179
-
180
- @ SuppressWarnings ("unchecked" )
181
- Eh107Configuration <?, ?> configuration = cache .getConfiguration (Eh107Configuration .class );
182
- if (configuration .isManagementEnabled ()) {
183
- enableManagement (cacheName , true );
184
- }
185
-
186
- if (configuration .isStatisticsEnabled ()) {
187
- enableStatistics (cacheName , true );
188
- }
189
-
190
- return cache ;
191
- }
192
-
193
- ConfigurationMerger .ConfigHolder <K , V > configHolder = configurationMerger .mergeConfigurations (cacheName , config );
194
-
195
- final InternalCache <K , V > ehCache ;
196
- try {
197
- ehCache = (InternalCache <K , V >)ehCacheManager .createCache (cacheName , configHolder .cacheConfiguration );
198
- } catch (IllegalArgumentException e ) {
199
- throw configHolder .cacheResources .closeResourcesAfter (new CacheException ("A Cache named [" + cacheName + "] already exists" ));
200
- } catch (Throwable t ) {
201
- // something went wrong in ehcache land, make sure to clean up our stuff
202
- throw configHolder .cacheResources .closeResourcesAfter (new CacheException (t ));
203
- }
204
-
205
- Eh107Cache <K , V > cache = null ;
206
- CacheResources <K , V > cacheResources = configHolder .cacheResources ;
207
- try {
208
- if (configHolder .useEhcacheLoaderWriter ) {
209
- cacheResources = new CacheResources <>(cacheName , wrapCacheLoaderWriter (ehCache .getCacheLoaderWriter ()),
210
- cacheResources .getExpiryPolicy (), cacheResources .getListenerResources ());
211
- }
212
- cache = new Eh107Cache <>(cacheName , new Eh107CompleteConfiguration <>(configHolder .jsr107Configuration , ehCache
213
- .getRuntimeConfiguration ()), cacheResources , ehCache , statisticsService , this );
214
-
215
- lazilyLoadedCaches .put (cacheName , cache );
216
-
217
- if (configHolder .jsr107Configuration .isManagementEnabled ()) {
218
- enableManagement (cacheName , true );
219
- }
220
+ return safeCacheRetrieval (cacheName );
221
+ } else {
222
+ ConfigurationMerger .ConfigHolder <K , V > configHolder = configurationMerger .mergeConfigurations (cacheName , config );
220
223
221
- if (configHolder .jsr107Configuration .isStatisticsEnabled ()) {
222
- enableStatistics (cacheName , true );
224
+ final InternalCache <K , V > ehCache ;
225
+ try {
226
+ ehCache = (InternalCache <K , V >)ehCacheManager .createCache (cacheName , configHolder .cacheConfiguration );
227
+ } catch (IllegalArgumentException e ) {
228
+ throw configHolder .cacheResources .closeResourcesAfter (new CacheException ("A Cache named [" + cacheName + "] already exists" ));
229
+ } catch (Throwable t ) {
230
+ // something went wrong in ehcache land, make sure to clean up our stuff
231
+ throw configHolder .cacheResources .closeResourcesAfter (new CacheException (t ));
223
232
}
224
233
225
- return cache ;
226
- } catch (Throwable t ) {
227
- if (cache != null ) {
228
- throw cache .closeInternalAfter (new CacheException (t ));
229
- } else {
230
- throw cacheResources .closeResourcesAfter (new CacheException (t ));
234
+ Eh107Cache <K , V > cache = null ;
235
+ CacheResources <K , V > cacheResources = configHolder .cacheResources ;
236
+ try {
237
+ if (configHolder .useEhcacheLoaderWriter ) {
238
+ cacheResources = new CacheResources <>(cacheName , wrapCacheLoaderWriter (ehCache .getCacheLoaderWriter ()),
239
+ cacheResources .getExpiryPolicy (), cacheResources .getListenerResources ());
240
+ }
241
+ cache = new Eh107Cache <>(cacheName , new Eh107CompleteConfiguration <>(configHolder .jsr107Configuration , ehCache
242
+ .getRuntimeConfiguration ()), cacheResources , ehCache , statisticsService , this );
243
+
244
+ return reloadCache (cacheName , cache );
245
+ } catch (Throwable t ) {
246
+ if (cache != null ) {
247
+ throw cache .closeInternalAfter (new CacheException (t ));
248
+ } else {
249
+ throw cacheResources .closeResourcesAfter (new CacheException (t ));
250
+ }
231
251
}
232
252
}
233
253
}
@@ -248,7 +268,6 @@ public String toString() {
248
268
@ Override
249
269
public <K , V > Cache <K , V > getCache (String cacheName , Class <K > keyType , Class <V > valueType ) {
250
270
checkClosed ();
251
- loadCache (cacheName );
252
271
253
272
if (cacheName == null || keyType == null || valueType == null ) {
254
273
throw new NullPointerException ();
@@ -279,7 +298,6 @@ public <K, V> Cache<K, V> getCache(String cacheName, Class<K> keyType, Class<V>
279
298
@ Override
280
299
public <K , V > Cache <K , V > getCache (String cacheName ) {
281
300
checkClosed ();
282
- loadCache (cacheName );
283
301
284
302
if (cacheName == null ) {
285
303
throw new NullPointerException ();
@@ -290,7 +308,7 @@ public <K, V> Cache<K, V> getCache(String cacheName) {
290
308
291
309
@ SuppressWarnings ("unchecked" )
292
310
private <K , V > Eh107Cache <K , V > safeCacheRetrieval (final String cacheName ) {
293
- final Eh107Cache <?, ?> eh107Cache = lazilyLoadedCaches .get (cacheName );
311
+ final Eh107Cache <?, ?> eh107Cache = caches .get (cacheName );
294
312
if (eh107Cache != null && eh107Cache .isClosed ()) {
295
313
return null ;
296
314
}
@@ -300,7 +318,7 @@ private <K, V> Eh107Cache<K, V> safeCacheRetrieval(final String cacheName) {
300
318
@ Override
301
319
public Iterable <String > getCacheNames () {
302
320
checkClosed ();
303
- return Collections .unmodifiableList (new ArrayList <>(lazilyLoadedCaches .keySet ()));
321
+ return Collections .unmodifiableSet (new HashSet <>(ehCacheManager . getRuntimeConfiguration (). getCacheConfigurations () .keySet ()));
304
322
}
305
323
306
324
@ Override
@@ -312,7 +330,7 @@ public void destroyCache(String cacheName) {
312
330
synchronized (cachesLock ) {
313
331
checkClosed ();
314
332
315
- Eh107Cache <?, ?> cache = lazilyLoadedCaches .remove (cacheName );
333
+ Eh107Cache <?, ?> cache = caches .remove (cacheName );
316
334
if (cache == null ) {
317
335
// TCK expects this method to return w/o exception if named cache does
318
336
// not exist
@@ -440,15 +458,15 @@ public void close() {
440
458
void closeInternal () {
441
459
synchronized (cachesLock ) {
442
460
try {
443
- closeAll (lazilyLoadedCaches .values (), (Closeable ) lazilyLoadedCaches ::clear , ehCacheManager );
461
+ closeAll (caches .values (), (Closeable ) caches ::clear , ehCacheManager );
444
462
} catch (IOException e ) {
445
463
throw new CacheException (e );
446
464
}
447
465
}
448
466
}
449
467
450
468
void close (Eh107Cache <?, ?> cache ) {
451
- if (lazilyLoadedCaches .remove (cache .getName (), cache )) {
469
+ if (caches .remove (cache .getName (), cache )) {
452
470
try {
453
471
chain (
454
472
() -> unregisterObject (cache .getManagementMBean ()),
0 commit comments