From c6aadd145ac8b83af809d8471501484b313b801c Mon Sep 17 00:00:00 2001 From: Nishchay Date: Tue, 10 Jan 2023 16:03:38 +0530 Subject: [PATCH] issue-2494 : fix cache visiblity for javaxCacheManager --- .../org/ehcache/jsr107/Eh107CacheManager.java | 119 +++++++----------- .../java/org/ehcache/jsr107/UnwrapTest.java | 37 ++++-- 2 files changed, 73 insertions(+), 83 deletions(-) diff --git a/107/src/main/java/org/ehcache/jsr107/Eh107CacheManager.java b/107/src/main/java/org/ehcache/jsr107/Eh107CacheManager.java index d073791aea..6a5d1e11e0 100644 --- a/107/src/main/java/org/ehcache/jsr107/Eh107CacheManager.java +++ b/107/src/main/java/org/ehcache/jsr107/Eh107CacheManager.java @@ -17,10 +17,7 @@ import java.lang.management.ManagementFactory; import java.net.URI; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Map; -import java.util.Properties; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -55,8 +52,8 @@ class Eh107CacheManager implements CacheManager { private static MBeanServer MBEAN_SERVER = ManagementFactory.getPlatformMBeanServer(); private final Object cachesLock = new Object(); - private final ConcurrentMap> caches = new ConcurrentHashMap>(); - private final EhcacheManager ehCacheManager; + private final ConcurrentMap> lazilyLoadedCaches = new ConcurrentHashMap<>(); + private final org.ehcache.CacheManager ehCacheManager; private final EhcacheCachingProvider cachingProvider; private final ClassLoader classLoader; private final URI uri; @@ -75,21 +72,25 @@ class Eh107CacheManager implements CacheManager { this.managementRegistry = managementRegistry; this.configurationMerger = configurationMerger; - refreshAllCaches(); } - EhcacheManager getEhCacheManager() { - return ehCacheManager; - } - private void refreshAllCaches() { - for (Map.Entry> entry : ehCacheManager.getRuntimeConfiguration().getCacheConfigurations().entrySet()) { - String name = entry.getKey(); - CacheConfiguration config = entry.getValue(); - caches.putIfAbsent(name, wrapEhcacheCache(name, config)); - } - for (Map.Entry> namedCacheEntry : caches.entrySet()) { - namedCacheEntry.getValue().isClosed(); + private void loadCache(String cacheName) { + Map> cacheConfigurations = ehCacheManager.getRuntimeConfiguration().getCacheConfigurations(); + CacheConfiguration cacheConfiguration; + + if (null != (cacheConfiguration = cacheConfigurations.get(cacheName))) { + Eh107Cache wrappedCache = wrapEhcacheCache(cacheName, cacheConfiguration); + if (lazilyLoadedCaches.putIfAbsent(cacheName, wrappedCache) == null) { + @SuppressWarnings("unchecked") + Eh107Configuration configuration = wrappedCache.getConfiguration(Eh107Configuration.class); + if (configuration.isManagementEnabled()) { + enableManagement(wrappedCache, true); + } + if (configuration.isStatisticsEnabled()) { + enableStatistics(wrappedCache, true); + } + } } } @@ -158,7 +159,7 @@ public > Cache createCache(String cach } Eh107Cache cache = wrapEhcacheCache(cacheName, ehcache); assert safeCacheRetrieval(cacheName) == null; - caches.put(cacheName, cache); + lazilyLoadedCaches.put(cacheName, cache); return cache; } @@ -189,7 +190,7 @@ public > Cache createCache(String cach cache = new Eh107Cache(cacheName, new Eh107CompleteConfiguration(configHolder.jsr107Configuration, ehCache .getRuntimeConfiguration()), cacheResources, ehCache, this, managementRegistry); - caches.put(cacheName, cache); + lazilyLoadedCaches.put(cacheName, cache); if (configHolder.jsr107Configuration.isManagementEnabled()) { enableManagement(cacheName, true); @@ -227,6 +228,7 @@ public String toString() { @Override public Cache getCache(String cacheName, Class keyType, Class valueType) { checkClosed(); + loadCache(cacheName); if (cacheName == null || keyType == null || valueType == null) { throw new NullPointerException(); @@ -257,6 +259,7 @@ public Cache getCache(String cacheName, Class keyType, Class @Override public Cache getCache(String cacheName) { checkClosed(); + loadCache(cacheName); if (cacheName == null) { throw new NullPointerException(); @@ -278,7 +281,7 @@ public Cache getCache(String cacheName) { @SuppressWarnings("unchecked") private Eh107Cache safeCacheRetrieval(final String cacheName) { - final Eh107Cache eh107Cache = caches.get(cacheName); + final Eh107Cache eh107Cache = lazilyLoadedCaches.get(cacheName); if(eh107Cache != null && eh107Cache.isClosed()) { return null; } @@ -287,8 +290,8 @@ private Eh107Cache safeCacheRetrieval(final String cacheName) { @Override public Iterable getCacheNames() { - refreshAllCaches(); - return Collections.unmodifiableList(new ArrayList(caches.keySet())); + checkClosed(); + return Collections.unmodifiableList(new ArrayList<>(lazilyLoadedCaches.keySet())); } @Override @@ -301,7 +304,7 @@ public void destroyCache(String cacheName) { synchronized (cachesLock) { checkClosed(); - Eh107Cache cache = caches.remove(cacheName); + Eh107Cache cache = lazilyLoadedCaches.remove(cacheName); if (cache == null) { // TCK expects this method to return w/o exception if named cache does // not exist @@ -429,63 +432,27 @@ public void close() { closeException.throwIfNotEmpty(); } - void closeInternal(MultiCacheException closeException) { - try { - synchronized (cachesLock) { - for (Eh107Cache cache : caches.values()) { - try { - close(cache, closeException); - } catch (Throwable t) { - closeException.addThrowable(t); - } - } - - try { - caches.clear(); - } catch (Throwable t) { - closeException.addThrowable(t); - } - - try { - ehCacheManager.close(); - } catch (Throwable t) { - closeException.addThrowable(t); - } + void closeInternal() { + synchronized (cachesLock) { + try { + closeAll(lazilyLoadedCaches.values(), (Closeable) lazilyLoadedCaches::clear, ehCacheManager); + } catch (IOException e) { + throw new CacheException(e); } - } catch (Throwable t) { - closeException.addThrowable(t); } } - void close(Eh107Cache cache, MultiCacheException closeException) { - try { - if (caches.remove(cache.getName(), cache)) { - try { - unregisterObject(cache.getManagementMBean()); - } catch (Throwable t) { - closeException.addThrowable(t); - } - - try { - unregisterObject(cache.getStatisticsMBean()); - } catch (Throwable t) { - closeException.addThrowable(t); - } - - try { - cache.closeInternal(closeException); - } catch (Throwable t) { - closeException.addThrowable(t); - } - - try { - ehCacheManager.removeCache(cache.getName()); - } catch (Throwable t) { - closeException.addThrowable(t); - } + void close(Eh107Cache cache) { + if (lazilyLoadedCaches.remove(cache.getName(), cache)) { + try { + chain( + () -> unregisterObject(cache.getManagementMBean()), + () -> unregisterObject(cache.getStatisticsMBean()), + () -> cache.closeInternal(), + () -> ehCacheManager.removeCache(cache.getName())); + } catch (Throwable t) { + throw new CacheException(t); } - } catch (Throwable t) { - closeException.addThrowable(t); } } } diff --git a/107/src/test/java/org/ehcache/jsr107/UnwrapTest.java b/107/src/test/java/org/ehcache/jsr107/UnwrapTest.java index 9719af1d4d..5289d2411b 100644 --- a/107/src/test/java/org/ehcache/jsr107/UnwrapTest.java +++ b/107/src/test/java/org/ehcache/jsr107/UnwrapTest.java @@ -15,9 +15,8 @@ */ package org.ehcache.jsr107; -import org.ehcache.EhcacheManager; -import org.ehcache.config.CacheConfiguration; -import org.ehcache.config.CacheRuntimeConfiguration; +import org.ehcache.config.builders.CacheConfigurationBuilder; +import org.ehcache.core.EhcacheManager; import org.ehcache.event.CacheEvent; import org.junit.After; import org.junit.Before; @@ -30,9 +29,9 @@ import javax.cache.event.EventType; import javax.cache.spi.CachingProvider; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsInstanceOf.instanceOf; -import static org.junit.Assert.assertThat; +import static org.ehcache.config.builders.ResourcePoolsBuilder.heap; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; /** * @author rism @@ -79,7 +78,31 @@ public void testCacheEntryEventUnwrap() { assertThat(cacheEntryEvent.unwrap(cacheEntryEvent.getClass()), is(instanceOf(Eh107CacheEntryEvent.NormalEvent.class))); } - @SuppressWarnings("unchecked") + @Test + public void testCacheVisibilityPostUnwrap() { + + CacheManager javaxCacheManager = Caching.getCachingProvider().getCacheManager(); + + org.ehcache.CacheManager cacheManager = javaxCacheManager.unwrap(org.ehcache.CacheManager.class); + CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class, heap(5)); + cacheManager.createCache("jcache", cacheConfigurationBuilder); + + Cache javaxCache = javaxCacheManager.getCache("jcache", Integer.class, String.class); + assertThat(javaxCache, is(notNullValue())); + + CacheManager javaxCacheManager1 = javaxCacheManager.unwrap(javax.cache.CacheManager.class); + Cache javaxCache1 = javaxCacheManager1.getCache("jcache", Integer.class, String.class); + assertThat(javaxCache1, is(notNullValue())); + + org.ehcache.Cache cache = cacheManager.getCache("jcache", Integer.class, String.class); + assertThat(cache, is(notNullValue())); + + cache.put(1,"one"); + assertThat(javaxCache.get(1), is("one")); + assertThat(javaxCache1.get(1), is("one")); + + } + private class EhEvent implements CacheEvent { @Override public org.ehcache.event.EventType getType() {