3535import java .io .IOException ;
3636import java .lang .management .ManagementFactory ;
3737import java .net .URI ;
38- import java .util .*;
38+ import java .util .ArrayList ;
39+ import java .util .Collections ;
40+ import java .util .Map ;
41+ import java .util .Properties ;
3942import java .util .concurrent .ConcurrentHashMap ;
4043import java .util .concurrent .ConcurrentMap ;
4144
@@ -61,7 +64,7 @@ class Eh107CacheManager implements CacheManager {
6164 private static final MBeanServer MBEAN_SERVER = ManagementFactory .getPlatformMBeanServer ();
6265
6366 private final Object cachesLock = new Object ();
64- private final ConcurrentMap <String , Eh107Cache <?, ?>> lazilyLoadedCaches = new ConcurrentHashMap <>();
67+ private final ConcurrentMap <String , Eh107Cache <?, ?>> caches = new ConcurrentHashMap <>();
6568 private final org .ehcache .CacheManager ehCacheManager ;
6669 private final EhcacheCachingProvider cachingProvider ;
6770 private final ClassLoader classLoader ;
@@ -80,26 +83,32 @@ class Eh107CacheManager implements CacheManager {
8083 this .configurationMerger = configurationMerger ;
8184 this .statisticsService = jsr107Service .getStatistics ();
8285
86+ refreshAllCaches ();
8387 }
8488
85-
86- private void loadCache (String cacheName ) {
87- Map <String , CacheConfiguration <?, ?>> cacheConfigurations = ehCacheManager .getRuntimeConfiguration ().getCacheConfigurations ();
88- CacheConfiguration <?, ?> cacheConfiguration ;
89-
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 );
89+ private void refreshAllCaches () {
90+ for (Map .Entry <String , CacheConfiguration <?, ?>> entry : ehCacheManager .getRuntimeConfiguration ().getCacheConfigurations ().entrySet ()) {
91+ String name = entry .getKey ();
92+ CacheConfiguration <?, ?> config = entry .getValue ();
93+
94+ if (!caches .containsKey (name )) {
95+ Eh107Cache <?, ?> wrappedCache = wrapEhcacheCache (name , config );
96+ if (caches .putIfAbsent (name , wrappedCache ) == null ) {
97+ @ SuppressWarnings ("unchecked" )
98+ Eh107Configuration <?, ?> configuration = wrappedCache .getConfiguration (Eh107Configuration .class );
99+ if (configuration .isManagementEnabled ()) {
100+ enableManagement (wrappedCache , true );
101+ }
102+ if (configuration .isStatisticsEnabled ()) {
103+ enableStatistics (wrappedCache , true );
104+ }
100105 }
101106 }
102107 }
108+
109+ for (Eh107Cache <?, ?> wrappedCache : caches .values ()) {
110+ wrappedCache .isClosed ();
111+ }
103112 }
104113
105114 private <K , V > Eh107Cache <K , V > wrapEhcacheCache (String alias , CacheConfiguration <K , V > ehConfig ) {
@@ -175,7 +184,7 @@ public <K, V, C extends Configuration<K, V>> Cache<K, V> createCache(String cach
175184 }
176185 Eh107Cache <K , V > cache = wrapEhcacheCache (cacheName , (InternalCache <K , V >)ehcache );
177186 assert safeCacheRetrieval (cacheName ) == null ;
178- lazilyLoadedCaches .put (cacheName , cache );
187+ caches .put (cacheName , cache );
179188
180189 @ SuppressWarnings ("unchecked" )
181190 Eh107Configuration <?, ?> configuration = cache .getConfiguration (Eh107Configuration .class );
@@ -212,7 +221,7 @@ public <K, V, C extends Configuration<K, V>> Cache<K, V> createCache(String cach
212221 cache = new Eh107Cache <>(cacheName , new Eh107CompleteConfiguration <>(configHolder .jsr107Configuration , ehCache
213222 .getRuntimeConfiguration ()), cacheResources , ehCache , statisticsService , this );
214223
215- lazilyLoadedCaches .put (cacheName , cache );
224+ caches .put (cacheName , cache );
216225
217226 if (configHolder .jsr107Configuration .isManagementEnabled ()) {
218227 enableManagement (cacheName , true );
@@ -248,7 +257,6 @@ public String toString() {
248257 @ Override
249258 public <K , V > Cache <K , V > getCache (String cacheName , Class <K > keyType , Class <V > valueType ) {
250259 checkClosed ();
251- loadCache (cacheName );
252260
253261 if (cacheName == null || keyType == null || valueType == null ) {
254262 throw new NullPointerException ();
@@ -279,7 +287,6 @@ public <K, V> Cache<K, V> getCache(String cacheName, Class<K> keyType, Class<V>
279287 @ Override
280288 public <K , V > Cache <K , V > getCache (String cacheName ) {
281289 checkClosed ();
282- loadCache (cacheName );
283290
284291 if (cacheName == null ) {
285292 throw new NullPointerException ();
@@ -290,7 +297,7 @@ public <K, V> Cache<K, V> getCache(String cacheName) {
290297
291298 @ SuppressWarnings ("unchecked" )
292299 private <K , V > Eh107Cache <K , V > safeCacheRetrieval (final String cacheName ) {
293- final Eh107Cache <?, ?> eh107Cache = lazilyLoadedCaches .get (cacheName );
300+ final Eh107Cache <?, ?> eh107Cache = caches .get (cacheName );
294301 if (eh107Cache != null && eh107Cache .isClosed ()) {
295302 return null ;
296303 }
@@ -300,7 +307,8 @@ private <K, V> Eh107Cache<K, V> safeCacheRetrieval(final String cacheName) {
300307 @ Override
301308 public Iterable <String > getCacheNames () {
302309 checkClosed ();
303- return Collections .unmodifiableList (new ArrayList <>(lazilyLoadedCaches .keySet ()));
310+ refreshAllCaches ();
311+ return Collections .unmodifiableList (new ArrayList <>(caches .keySet ()));
304312 }
305313
306314 @ Override
@@ -312,7 +320,7 @@ public void destroyCache(String cacheName) {
312320 synchronized (cachesLock ) {
313321 checkClosed ();
314322
315- Eh107Cache <?, ?> cache = lazilyLoadedCaches .remove (cacheName );
323+ Eh107Cache <?, ?> cache = caches .remove (cacheName );
316324 if (cache == null ) {
317325 // TCK expects this method to return w/o exception if named cache does
318326 // not exist
@@ -440,15 +448,15 @@ public void close() {
440448 void closeInternal () {
441449 synchronized (cachesLock ) {
442450 try {
443- closeAll (lazilyLoadedCaches .values (), (Closeable ) lazilyLoadedCaches ::clear , ehCacheManager );
451+ closeAll (caches .values (), (Closeable ) caches ::clear , ehCacheManager );
444452 } catch (IOException e ) {
445453 throw new CacheException (e );
446454 }
447455 }
448456 }
449457
450458 void close (Eh107Cache <?, ?> cache ) {
451- if (lazilyLoadedCaches .remove (cache .getName (), cache )) {
459+ if (caches .remove (cache .getName (), cache )) {
452460 try {
453461 chain (
454462 () -> unregisterObject (cache .getManagementMBean ()),
0 commit comments