Upgraded to JCache 1.0 RC1

Also completing 4.0's consistency efforts between Spring's cache adapters.
This commit is contained in:
Juergen Hoeller 2013-12-20 01:28:16 +01:00
parent b1460742c3
commit a884cde18c
10 changed files with 82 additions and 55 deletions

View File

@ -65,7 +65,6 @@ configure(allprojects) { project ->
maven { url "http://repo.spring.io/libs-release" }
maven { url "http://repo.spring.io/milestone" } // for AspectJ 1.8.0.M1
maven { url "https://repository.apache.org/content/repositories/releases" } // tomcat 8
// maven { url "https://repository.apache.org/content/repositories/snapshots" } // tomcat 8 snapshots
maven { url "https://maven.java.net/content/repositories/releases" } // javax.websocket, tyrus
maven { url "https://oss.sonatype.org/content/repositories/releases" } // javax.cache
}
@ -506,7 +505,7 @@ project("spring-context-support") {
optional(project(":spring-jdbc")) // for Quartz support
optional(project(":spring-tx")) // for Quartz support
optional("javax.mail:mail:1.4.7")
optional("javax.cache:cache-api:1.0.0-PFD")
optional("javax.cache:cache-api:1.0.0-RC1")
optional("com.google.guava:guava:14.0.1")
optional("net.sf.ehcache:ehcache-core:2.6.5")
optional("org.quartz-scheduler:quartz:1.8.6") {

View File

@ -50,12 +50,12 @@ public class EhCacheCache implements Cache {
@Override
public String getName() {
public final String getName() {
return this.cache.getName();
}
@Override
public Ehcache getNativeCache() {
public final Ehcache getNativeCache() {
return this.cache;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -46,7 +46,7 @@ public class EhCacheCacheManager extends AbstractTransactionSupportingCacheManag
}
/**
* Create a new EhCacheCacheManager for the given backing EhCache.
* Create a new EhCacheCacheManager for the given backing EhCache CacheManager.
* @param cacheManager the backing EhCache {@link net.sf.ehcache.CacheManager}
*/
public EhCacheCacheManager(net.sf.ehcache.CacheManager cacheManager) {
@ -71,15 +71,16 @@ public class EhCacheCacheManager extends AbstractTransactionSupportingCacheManag
@Override
protected Collection<Cache> loadCaches() {
Assert.notNull(this.cacheManager, "A backing EhCache CacheManager is required");
Status status = this.cacheManager.getStatus();
net.sf.ehcache.CacheManager cacheManager = getCacheManager();
Assert.notNull(cacheManager, "A backing EhCache CacheManager is required");
Status status = cacheManager.getStatus();
Assert.isTrue(Status.STATUS_ALIVE.equals(status),
"An 'alive' EhCache CacheManager is required - current cache is " + status.toString());
String[] names = this.cacheManager.getCacheNames();
String[] names = cacheManager.getCacheNames();
Collection<Cache> caches = new LinkedHashSet<Cache>(names.length);
for (String name : names) {
caches.add(new EhCacheCache(this.cacheManager.getEhcache(name)));
caches.add(new EhCacheCache(cacheManager.getEhcache(name)));
}
return caches;
}
@ -90,7 +91,7 @@ public class EhCacheCacheManager extends AbstractTransactionSupportingCacheManag
if (cache == null) {
// check the EhCache cache again
// (in case the cache was added at runtime)
Ehcache ehcache = this.cacheManager.getEhcache(name);
Ehcache ehcache = getCacheManager().getEhcache(name);
if (ehcache != null) {
cache = new EhCacheCache(ehcache);
addCache(cache);

View File

@ -67,16 +67,16 @@ public class GuavaCache implements Cache {
@Override
public String getName() {
public final String getName() {
return this.name;
}
@Override
public com.google.common.cache.Cache<Object, Object> getNativeCache() {
public final com.google.common.cache.Cache<Object, Object> getNativeCache() {
return this.cache;
}
public boolean isAllowNullValues() {
public final boolean isAllowNullValues() {
return this.allowNullValues;
}

View File

@ -26,7 +26,6 @@ import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheBuilderSpec;
import com.google.common.cache.CacheLoader;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.util.Assert;
@ -49,7 +48,7 @@ import org.springframework.util.Assert;
* @since 4.0
* @see GuavaCache
*/
public class GuavaCacheManager implements CacheManager, DisposableBean {
public class GuavaCacheManager implements CacheManager {
private final ConcurrentMap<String, Cache> cacheMap = new ConcurrentHashMap<String, Cache>(16);
@ -59,6 +58,8 @@ public class GuavaCacheManager implements CacheManager, DisposableBean {
private CacheLoader<Object, Object> cacheLoader;
private boolean allowNullValues = true;
/**
* Construct a dynamic GuavaCacheManager,
@ -133,6 +134,24 @@ public class GuavaCacheManager implements CacheManager, DisposableBean {
this.cacheLoader = cacheLoader;
}
/**
* Specify whether to accept and convert {@code null} values for all caches
* in this cache manager.
* <p>Default is "true", despite Guava itself not supporting {@code null} values.
* An internal holder object will be used to store user-level {@code null}s.
*/
public void setAllowNullValues(boolean allowNullValues) {
this.allowNullValues = allowNullValues;
}
/**
* Return whether this cache manager accepts and converts {@code null} values
* for all of its caches.
*/
public boolean isAllowNullValues() {
return this.allowNullValues;
}
@Override
public Collection<String> getCacheNames() {
@ -160,7 +179,7 @@ public class GuavaCacheManager implements CacheManager, DisposableBean {
* @return the Spring GuavaCache adapter (or a decorator thereof)
*/
protected Cache createGuavaCache(String name) {
return new GuavaCache(name, createNativeGuavaCache(name));
return new GuavaCache(name, createNativeGuavaCache(name), isAllowNullValues());
}
/**
@ -177,12 +196,4 @@ public class GuavaCacheManager implements CacheManager, DisposableBean {
}
}
@Override
public void destroy() {
for (Cache cache : this.cacheMap.values()) {
}
}
}

View File

@ -26,7 +26,7 @@ import org.springframework.util.Assert;
* {@link org.springframework.cache.Cache} implementation on top of a
* {@link javax.cache.Cache} instance.
*
* <p>Note: This class has been updated for JCache 0.11, as of Spring 4.0.
* <p>Note: This class has been updated for JCache 1.0, as of Spring 4.0.
*
* @author Juergen Hoeller
* @since 3.2
@ -35,8 +35,7 @@ public class JCacheCache implements Cache {
private static final Object NULL_HOLDER = new NullHolder();
@SuppressWarnings("rawtypes")
private final javax.cache.Cache cache;
private final javax.cache.Cache<Object, Object> cache;
private final boolean allowNullValues;
@ -45,7 +44,7 @@ public class JCacheCache implements Cache {
* Create an {@link org.springframework.cache.jcache.JCacheCache} instance.
* @param jcache backing JCache Cache instance
*/
public JCacheCache(javax.cache.Cache<?,?> jcache) {
public JCacheCache(javax.cache.Cache<Object, Object> jcache) {
this(jcache, true);
}
@ -54,7 +53,7 @@ public class JCacheCache implements Cache {
* @param jcache backing JCache Cache instance
* @param allowNullValues whether to accept and convert null values for this cache
*/
public JCacheCache(javax.cache.Cache<?,?> jcache, boolean allowNullValues) {
public JCacheCache(javax.cache.Cache<Object, Object> jcache, boolean allowNullValues) {
Assert.notNull(jcache, "Cache must not be null");
this.cache = jcache;
this.allowNullValues = allowNullValues;
@ -62,21 +61,20 @@ public class JCacheCache implements Cache {
@Override
public String getName() {
public final String getName() {
return this.cache.getName();
}
@Override
public javax.cache.Cache<?,?> getNativeCache() {
public final javax.cache.Cache<Object, Object> getNativeCache() {
return this.cache;
}
public boolean isAllowNullValues() {
public final boolean isAllowNullValues() {
return this.allowNullValues;
}
@Override
@SuppressWarnings("unchecked")
public ValueWrapper get(Object key) {
Object value = this.cache.get(key);
return (value != null ? new SimpleValueWrapper(fromStoreValue(value)) : null);
@ -93,13 +91,11 @@ public class JCacheCache implements Cache {
}
@Override
@SuppressWarnings("unchecked")
public void put(Object key, Object value) {
this.cache.put(key, toStoreValue(value));
}
@Override
@SuppressWarnings("unchecked")
public void evict(Object key) {
this.cache.remove(key);
}

View File

@ -28,7 +28,7 @@ import org.springframework.cache.transaction.AbstractTransactionSupportingCacheM
* {@link org.springframework.cache.CacheManager} implementation
* backed by a JCache {@link javax.cache.CacheManager}.
*
* <p>Note: This class has been updated for JCache 0.11, as of Spring 4.0.
* <p>Note: This class has been updated for JCache 1.0, as of Spring 4.0.
*
* @author Juergen Hoeller
* @since 3.2
@ -71,17 +71,17 @@ public class JCacheCacheManager extends AbstractTransactionSupportingCacheManage
}
/**
* Specify whether to accept and convert null values for all caches
* Specify whether to accept and convert {@code null} values for all caches
* in this cache manager.
* <p>Default is "true", despite JSR-107 itself not supporting null values.
* An internal holder object will be used to store user-level null values.
* <p>Default is "true", despite JSR-107 itself not supporting {@code null} values.
* An internal holder object will be used to store user-level {@code null}s.
*/
public void setAllowNullValues(boolean allowNullValues) {
this.allowNullValues = allowNullValues;
}
/**
* Return whether this cache manager accepts and converts null values
* Return whether this cache manager accepts and converts {@code null} values
* for all of its caches.
*/
public boolean isAllowNullValues() {
@ -90,8 +90,8 @@ public class JCacheCacheManager extends AbstractTransactionSupportingCacheManage
@Override
public void afterPropertiesSet() {
if (this.cacheManager == null) {
this.cacheManager = Caching.getCachingProvider().getCacheManager();
if (getCacheManager() == null) {
setCacheManager(Caching.getCachingProvider().getCacheManager());
}
super.afterPropertiesSet();
}
@ -100,9 +100,9 @@ public class JCacheCacheManager extends AbstractTransactionSupportingCacheManage
@Override
protected Collection<Cache> loadCaches() {
Collection<Cache> caches = new LinkedHashSet<Cache>();
for (String cacheName : this.cacheManager.getCacheNames()) {
javax.cache.Cache<Object, Object> jcache = this.cacheManager.getCache(cacheName);
caches.add(new JCacheCache(jcache, this.allowNullValues));
for (String cacheName : getCacheManager().getCacheNames()) {
javax.cache.Cache<Object, Object> jcache = getCacheManager().getCache(cacheName);
caches.add(new JCacheCache(jcache, isAllowNullValues()));
}
return caches;
}
@ -111,11 +111,10 @@ public class JCacheCacheManager extends AbstractTransactionSupportingCacheManage
public Cache getCache(String name) {
Cache cache = super.getCache(name);
if (cache == null) {
// check the JCache cache again
// (in case the cache was added at runtime)
javax.cache.Cache<?,?> jcache = this.cacheManager.getCache(name);
// Check the JCache cache again (in case the cache was added at runtime)
javax.cache.Cache<Object, Object> jcache = getCacheManager().getCache(name);
if (jcache != null) {
cache = new JCacheCache(jcache, this.allowNullValues);
cache = new JCacheCache(jcache, isAllowNullValues());
addCache(cache);
}
}

View File

@ -32,7 +32,7 @@ import org.springframework.beans.factory.InitializingBean;
* obtaining a pre-defined CacheManager by name through the standard
* JCache {@link javax.cache.Caching} class.
*
* <p>Note: This class has been updated for JCache 0.11, as of Spring 4.0.
* <p>Note: This class has been updated for JCache 1.0, as of Spring 4.0.
*
* @author Juergen Hoeller
* @since 3.2

View File

@ -87,16 +87,16 @@ public class ConcurrentMapCache implements Cache {
@Override
public String getName() {
public final String getName() {
return this.name;
}
@Override
public ConcurrentMap<Object, Object> getNativeCache() {
public final ConcurrentMap<Object, Object> getNativeCache() {
return this.store;
}
public boolean isAllowNullValues() {
public final boolean isAllowNullValues() {
return this.allowNullValues;
}

View File

@ -47,6 +47,8 @@ public class ConcurrentMapCacheManager implements CacheManager {
private boolean dynamic = true;
private boolean allowNullValues = true;
/**
* Construct a dynamic ConcurrentMapCacheManager,
@ -78,6 +80,25 @@ public class ConcurrentMapCacheManager implements CacheManager {
}
}
/**
* Specify whether to accept and convert {@code null} values for all caches
* in this cache manager.
* <p>Default is "true", despite ConcurrentHashMap itself not supporting {@code null}
* values. An internal holder object will be used to store user-level {@code null}s.
*/
public void setAllowNullValues(boolean allowNullValues) {
this.allowNullValues = allowNullValues;
}
/**
* Return whether this cache manager accepts and converts {@code null} values
* for all of its caches.
*/
public boolean isAllowNullValues() {
return this.allowNullValues;
}
@Override
public Collection<String> getCacheNames() {
return Collections.unmodifiableSet(this.cacheMap.keySet());
@ -104,7 +125,7 @@ public class ConcurrentMapCacheManager implements CacheManager {
* @return the ConcurrentMapCache (or a decorator thereof)
*/
protected Cache createConcurrentMapCache(String name) {
return new ConcurrentMapCache(name);
return new ConcurrentMapCache(name, isAllowNullValues());
}
}