Author: olamy Date: Tue May 6 07:16:01 2014 New Revision: 1592674 URL: http://svn.apache.org/r1592674 Log: [JCS-126] JCache allows Closeable for components
Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCache.java commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCachingManager.java commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSListener.java Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCache.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCache.java?rev=1592674&r1=1592673&r2=1592674&view=diff ============================================================================== --- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCache.java (original) +++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCache.java Tue May 6 07:16:01 2014 @@ -20,6 +20,8 @@ package org.apache.commons.jcs.jcache; import static org.apache.commons.jcs.jcache.Asserts.assertNotNull; +import java.io.Closeable; +import java.io.IOException; import java.io.Serializable; import java.util.Arrays; import java.util.HashMap; @@ -38,6 +40,7 @@ import javax.cache.configuration.Complet import javax.cache.configuration.Configuration; import javax.cache.configuration.Factory; import javax.cache.event.CacheEntryEvent; +import javax.cache.event.CacheEntryListener; import javax.cache.event.EventType; import javax.cache.expiry.Duration; import javax.cache.expiry.EternalExpiryPolicy; @@ -54,8 +57,12 @@ import javax.management.ObjectName; import org.apache.commons.jcs.access.CacheAccess; import org.apache.commons.jcs.access.exception.CacheException; +import org.apache.commons.jcs.engine.ElementAttributes; import org.apache.commons.jcs.engine.behavior.ICacheElement; +import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes; import org.apache.commons.jcs.engine.behavior.IElementSerializer; +import org.apache.commons.jcs.engine.control.event.behavior.IElementEvent; +import org.apache.commons.jcs.engine.control.event.behavior.IElementEventHandler; import org.apache.commons.jcs.engine.control.CompositeCache; import org.apache.commons.jcs.jcache.jmx.JCSCacheMXBean; import org.apache.commons.jcs.jcache.jmx.JCSCacheStatisticsMXBean; @@ -84,10 +91,16 @@ public class JCSCache<K extends Serializ private final IElementSerializer serializer = new StandardSerializer(); private final ExecutorService pool; - public JCSCache(final ClassLoader classLoader, final CacheManager mgr, final JCSConfiguration<K, V> configuration, - final CompositeCache<K, JCSElement<V>> cache, final Properties properties) + public JCSCache(final ClassLoader classLoader, final CacheManager mgr, + final String cacheName, + final JCSConfiguration<K, V> configuration, + final CompositeCache<K, JCSElement<V>> cache, final Properties properties) { manager = mgr; + + final ICompositeCacheAttributes cacheAttributes = cache.getCacheAttributes(); + cacheAttributes.setCacheName(cacheName); + delegate = new CacheAccess<K, JCSElement<V>>(cache); config = configuration; DaemonThreadFactory threadFactory = new DaemonThreadFactory("JCS-JCache-"); @@ -825,12 +838,27 @@ public class JCSCache<K extends Serializ delegate.dispose(); manager.destroyCache(getName()); + close(loader); + close(writer); + close(expiryPolicy); + for (final JCSListener<K, V> listener : listeners.values()) + { + close(listener); + } closed = true; listeners.clear(); JMXs.unregister(cacheConfigObjectName); JMXs.unregister(cacheStatsObjectName); } + private static void close(final Object potentiallyCloseable) + { + if (Closeable.class.isInstance(potentiallyCloseable)) + { + Closeable.class.cast(potentiallyCloseable); + } + } + @Override public boolean isClosed() { Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCachingManager.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCachingManager.java?rev=1592674&r1=1592673&r2=1592674&view=diff ============================================================================== --- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCachingManager.java (original) +++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSCachingManager.java Tue May 6 07:16:01 2014 @@ -113,8 +113,11 @@ public class JCSCachingManager implement final Class<?> valueType = configuration == null ? Object.class : configuration.getValueType(); if (!caches.containsKey(cacheName)) { - final Cache<K, V> cache = ClassLoaderAwareHandler.newProxy(loader, new JCSCache(loader, this, new JCSConfiguration( - configuration, keyType, valueType), instance.getCache(cacheName), instance.getConfigurationProperties()), Cache.class); + final Cache<K, V> cache = ClassLoaderAwareHandler.newProxy( + loader, new JCSCache(loader, this, cacheName, + new JCSConfiguration(configuration, keyType, valueType), + instance.getCache(cacheName), + instance.getConfigurationProperties()), Cache.class); caches.putIfAbsent(cacheName, cache); } else Modified: commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSListener.java URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSListener.java?rev=1592674&r1=1592673&r2=1592674&view=diff ============================================================================== --- commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSListener.java (original) +++ commons/proper/jcs/trunk/commons-jcs-jcache/src/main/java/org/apache/commons/jcs/jcache/JCSListener.java Tue May 6 07:16:01 2014 @@ -28,11 +28,12 @@ import javax.cache.event.CacheEntryListe import javax.cache.event.CacheEntryListenerException; import javax.cache.event.CacheEntryRemovedListener; import javax.cache.event.CacheEntryUpdatedListener; +import java.io.Closeable; import java.io.Serializable; import java.util.ArrayList; import java.util.List; -public class JCSListener<K extends Serializable, V extends Serializable> +public class JCSListener<K extends Serializable, V extends Serializable> implements Closeable { private final boolean oldValue; private final boolean synchronous; @@ -117,8 +118,11 @@ public class JCSListener<K extends Seria return filtered; } + @Override public void close() { - // no-op + if (Closeable.class.isInstance(delegate)) { + Closeable.class.cast(delegate); + } } }