Skip to content

Commit

Permalink
issue-2494 : fix cache visiblity for javaxCacheManager
Browse files Browse the repository at this point in the history
  • Loading branch information
nnares committed Jan 25, 2023
1 parent cafa5bf commit be30a76
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 41 deletions.
68 changes: 29 additions & 39 deletions ehcache-107/src/main/java/org/ehcache/jsr107/Eh107CacheManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@
import java.io.IOException;
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;

Expand All @@ -63,7 +60,7 @@ class Eh107CacheManager implements CacheManager {
private static final MBeanServer MBEAN_SERVER = ManagementFactory.getPlatformMBeanServer();

private final Object cachesLock = new Object();
private final ConcurrentMap<String, Eh107Cache<?, ?>> caches = new ConcurrentHashMap<>();
private final ConcurrentMap<String, Eh107Cache<?, ?>> lazilyLoadedCaches = new ConcurrentHashMap<>();
private final org.ehcache.CacheManager ehCacheManager;
private final EhcacheCachingProvider cachingProvider;
private final ClassLoader classLoader;
Expand All @@ -82,37 +79,31 @@ class Eh107CacheManager implements CacheManager {
this.configurationMerger = configurationMerger;
this.statisticsService = jsr107Service.getStatistics();

refreshAllCaches();
}

private void refreshAllCaches() {
for (Map.Entry<String, CacheConfiguration<?, ?>> entry : ehCacheManager.getRuntimeConfiguration().getCacheConfigurations().entrySet()) {
String name = entry.getKey();
CacheConfiguration<?, ?> config = entry.getValue();

if (!caches.containsKey(name)) {
Eh107Cache<?, ?> wrappedCache = wrapEhcacheCache(name, config);
if (caches.putIfAbsent(name, wrappedCache) == null) {
@SuppressWarnings("unchecked")
Eh107Configuration<?, ?> configuration = wrappedCache.getConfiguration(Eh107Configuration.class);
if (configuration.isManagementEnabled()) {
enableManagement(wrappedCache, true);
}
if (configuration.isStatisticsEnabled()) {
enableStatistics(wrappedCache, true);
}
private <K, V> Eh107Cache<K, V> wrapEhcacheCache(String alias, CacheConfiguration<K, V> ehConfig) {
org.ehcache.Cache<K, V> cache = ehCacheManager.getCache(alias, ehConfig.getKeyType(), ehConfig.getValueType());
return wrapEhcacheCache(alias, (InternalCache<K, V>)cache);
}

private void loadCache(String cacheName) {
Map<String, CacheConfiguration<?, ?>> 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);
}
}
}

for (Eh107Cache<?, ?> wrappedCache : caches.values()) {
wrappedCache.isClosed();
}
}

private <K, V> Eh107Cache<K, V> wrapEhcacheCache(String alias, CacheConfiguration<K, V> ehConfig) {
org.ehcache.Cache<K, V> cache = ehCacheManager.getCache(alias, ehConfig.getKeyType(), ehConfig.getValueType());
return wrapEhcacheCache(alias, (InternalCache<K, V>)cache);
}

private <K, V> Eh107Cache<K, V> wrapEhcacheCache(String alias, InternalCache<K, V> cache) {
Expand Down Expand Up @@ -182,8 +173,7 @@ public <K, V, C extends Configuration<K, V>> Cache<K, V> createCache(String cach
throw new CacheException("A Cache named [" + cacheName + "] already exists");
}
Eh107Cache<K, V> cache = wrapEhcacheCache(cacheName, (InternalCache<K, V>)ehcache);
assert safeCacheRetrieval(cacheName) == null;
caches.put(cacheName, cache);
lazilyLoadedCaches.put(cacheName, cache);

@SuppressWarnings("unchecked")
Eh107Configuration<?, ?> configuration = cache.getConfiguration(Eh107Configuration.class);
Expand Down Expand Up @@ -220,7 +210,7 @@ public <K, V, C extends Configuration<K, V>> Cache<K, V> createCache(String cach
cache = new Eh107Cache<>(cacheName, new Eh107CompleteConfiguration<>(configHolder.jsr107Configuration, ehCache
.getRuntimeConfiguration()), cacheResources, ehCache, statisticsService, this);

caches.put(cacheName, cache);
lazilyLoadedCaches.put(cacheName, cache);

if (configHolder.jsr107Configuration.isManagementEnabled()) {
enableManagement(cacheName, true);
Expand Down Expand Up @@ -296,7 +286,8 @@ public <K, V> Cache<K, V> getCache(String cacheName) {

@SuppressWarnings("unchecked")
private <K, V> Eh107Cache<K, V> safeCacheRetrieval(final String cacheName) {
final Eh107Cache<?, ?> eh107Cache = caches.get(cacheName);
loadCache(cacheName);
final Eh107Cache<?, ?> eh107Cache = lazilyLoadedCaches.get(cacheName);
if(eh107Cache != null && eh107Cache.isClosed()) {
return null;
}
Expand All @@ -306,8 +297,7 @@ private <K, V> Eh107Cache<K, V> safeCacheRetrieval(final String cacheName) {
@Override
public Iterable<String> getCacheNames() {
checkClosed();
refreshAllCaches();
return Collections.unmodifiableList(new ArrayList<>(caches.keySet()));
return Collections.unmodifiableList(new ArrayList<>(lazilyLoadedCaches.keySet()));
}

@Override
Expand All @@ -319,7 +309,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
Expand Down Expand Up @@ -447,15 +437,15 @@ public void close() {
void closeInternal() {
synchronized (cachesLock) {
try {
closeAll(caches.values(), (Closeable) caches::clear, ehCacheManager);
closeAll(lazilyLoadedCaches.values(), (Closeable) lazilyLoadedCaches::clear, ehCacheManager);
} catch (IOException e) {
throw new CacheException(e);
}
}
}

void close(Eh107Cache<?, ?> cache) {
if (caches.remove(cache.getName(), cache)) {
if (lazilyLoadedCaches.remove(cache.getName(), cache)) {
try {
chain(
() -> unregisterObject(cache.getManagementMBean()),
Expand Down
31 changes: 29 additions & 2 deletions ehcache-107/src/test/java/org/ehcache/jsr107/UnwrapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.ehcache.jsr107;

import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.core.EhcacheManager;
import org.ehcache.event.CacheEvent;
import org.junit.After;
Expand All @@ -28,9 +29,9 @@
import javax.cache.event.EventType;
import javax.cache.spi.CachingProvider;

import static org.ehcache.config.builders.ResourcePoolsBuilder.heap;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;

/**
* @author rism
Expand Down Expand Up @@ -77,6 +78,32 @@ public void testCacheEntryEventUnwrap() {
assertThat(cacheEntryEvent.unwrap(cacheEntryEvent.getClass()), is(instanceOf(Eh107CacheEntryEvent.NormalEvent.class)));
}

@Test
public void testCacheVisibilityPostUnwrap() {

CacheManager javaxCacheManager = Caching.getCachingProvider().getCacheManager();

org.ehcache.CacheManager cacheManager = javaxCacheManager.unwrap(org.ehcache.CacheManager.class);
CacheConfigurationBuilder<Integer, String> cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class, heap(5));
cacheManager.createCache("jcache", cacheConfigurationBuilder);

Cache<Integer, String> javaxCache = javaxCacheManager.getCache("jcache", Integer.class, String.class);
assertThat(javaxCache, is(notNullValue()));

CacheManager javaxCacheManager1 = javaxCacheManager.unwrap(javax.cache.CacheManager.class);
Cache<Integer, String> javaxCache1 = javaxCacheManager1.getCache("jcache", Integer.class, String.class);
assertThat(javaxCache1, is(notNullValue()));

org.ehcache.Cache<Integer, String> 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<String,String> {
@Override
public org.ehcache.event.EventType getType() {
Expand Down

0 comments on commit be30a76

Please sign in to comment.