Skip to content

Commit b02f2e7

Browse files
committed
Revert "Merge branch 'issue-2494-daggy' into issue-2494-daggy-integrate"
This reverts commit 5fc4dd8, reversing changes made to cafa5bf.
1 parent be0e98c commit b02f2e7

File tree

2 files changed

+36
-54
lines changed

2 files changed

+36
-54
lines changed

ehcache-107/src/main/java/org/ehcache/jsr107/Eh107CacheManager.java

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535
import java.io.IOException;
3636
import java.lang.management.ManagementFactory;
3737
import 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;
3942
import java.util.concurrent.ConcurrentHashMap;
4043
import 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()),

ehcache-107/src/test/java/org/ehcache/jsr107/UnwrapTest.java

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package org.ehcache.jsr107;
1818

19-
import org.ehcache.config.builders.CacheConfigurationBuilder;
2019
import org.ehcache.core.EhcacheManager;
2120
import org.ehcache.event.CacheEvent;
2221
import org.junit.After;
@@ -30,9 +29,9 @@
3029
import javax.cache.event.EventType;
3130
import javax.cache.spi.CachingProvider;
3231

33-
import static org.ehcache.config.builders.ResourcePoolsBuilder.heap;
3432
import static org.hamcrest.MatcherAssert.assertThat;
35-
import static org.hamcrest.Matchers.*;
33+
import static org.hamcrest.Matchers.instanceOf;
34+
import static org.hamcrest.Matchers.is;
3635

3736
/**
3837
* @author rism
@@ -79,31 +78,6 @@ public void testCacheEntryEventUnwrap() {
7978
assertThat(cacheEntryEvent.unwrap(cacheEntryEvent.getClass()), is(instanceOf(Eh107CacheEntryEvent.NormalEvent.class)));
8079
}
8180

82-
@Test
83-
public void testCacheVisibilityPostUnwrap() {
84-
85-
CacheManager javaxCacheManager = Caching.getCachingProvider().getCacheManager();
86-
87-
org.ehcache.CacheManager cacheManager = javaxCacheManager.unwrap(org.ehcache.CacheManager.class);
88-
CacheConfigurationBuilder<Integer, String> cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class, heap(5));
89-
cacheManager.createCache("jcache", cacheConfigurationBuilder);
90-
91-
Cache<Integer, String> javaxCache = javaxCacheManager.getCache("jcache", Integer.class, String.class);
92-
assertThat(javaxCache, is(notNullValue()));
93-
94-
CacheManager javaxCacheManager1 = javaxCacheManager.unwrap(javax.cache.CacheManager.class);
95-
Cache<Integer, String> javaxCache1 = javaxCacheManager1.getCache("jcache", Integer.class, String.class);
96-
assertThat(javaxCache1, is(notNullValue()));
97-
98-
org.ehcache.Cache<Integer, String> cache = cacheManager.getCache("jcache", Integer.class, String.class);
99-
assertThat(cache, is(notNullValue()));
100-
101-
cache.put(1,"one");
102-
assertThat(javaxCache.get(1), is("one"));
103-
assertThat(javaxCache1.get(1), is("one"));
104-
105-
}
106-
10781
private class EhEvent implements CacheEvent<String,String> {
10882
@Override
10983
public org.ehcache.event.EventType getType() {

0 commit comments

Comments
 (0)