From bb2802208b85553688b2c7f9244f9af6b7e0ab82 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 17 Oct 2013 19:35:21 +0200 Subject: [PATCH] Upgraded to JCache 0.11 for Spring Framework 4.0 RC1 Unfortunately, the JCache API changed quite a bit since 0.6. We're building against a snapshot of JCache 0.11 now, tracking its way to final after the Public Review Ballot. --- build.gradle | 7 ++-- .../cache/jcache/JCacheCache.java | 9 ++-- .../cache/jcache/JCacheCacheManager.java | 23 +++++++---- .../jcache/JCacheManagerFactoryBean.java | 41 +++++++++++++------ 4 files changed, 49 insertions(+), 31 deletions(-) diff --git a/build.gradle b/build.gradle index 4cef73ac42..bf53b9381f 100644 --- a/build.gradle +++ b/build.gradle @@ -77,6 +77,7 @@ configure(allprojects) { project -> maven { url "https://repository.apache.org/content/repositories/releases" } // tomcat 8 RC3 maven { url "https://repository.apache.org/content/repositories/snapshots" } // tomcat-websocket-* snapshots maven { url "https://maven.java.net/content/repositories/releases" } // javax.websocket, tyrus + maven { url "https://oss.sonatype.org/content/repositories/snapshots" } // javax.cache } dependencies { @@ -349,7 +350,7 @@ project("spring-context") { compile(files(project(":spring-core").cglibRepackJar)) optional("javax.ejb:ejb-api:3.0") optional("javax.inject:javax.inject:1") - optional("javax.enterprise.concurrent:javax.enterprise.concurrent-api:1.0-b06") + optional("javax.enterprise.concurrent:javax.enterprise.concurrent-api:1.0") optional("org.apache.geronimo.specs:geronimo-jms_1.1_spec:1.1") optional("org.eclipse.persistence:javax.persistence:2.0.0") optional("org.beanshell:bsh:2.0b4") @@ -419,7 +420,7 @@ project("spring-tx") { compile("aopalliance:aopalliance:1.0") provided("com.ibm.websphere:uow:6.0.2.17") optional("javax.resource:connector-api:1.5") - optional("javax.transaction:javax.transaction-api:1.2-b03") + optional("javax.transaction:javax.transaction-api:1.2") optional("javax.ejb:ejb-api:3.0") testCompile("org.eclipse.persistence:javax.persistence:2.0.0") testCompile("org.aspectj:aspectjweaver:${aspectjVersion}") @@ -505,7 +506,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:0.6") + optional("javax.cache:cache-api:0.11-SNAPSHOT") optional("net.sf.ehcache:ehcache-core:2.6.5") optional("org.quartz-scheduler:quartz:1.8.6") { exclude group: "org.slf4j", module: "slf4j-log4j12" diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCache.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCache.java index 8b0a731c4e..536f9fc20b 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCache.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCache.java @@ -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. @@ -18,8 +18,6 @@ package org.springframework.cache.jcache; import java.io.Serializable; -import javax.cache.Status; - import org.springframework.cache.Cache; import org.springframework.cache.support.SimpleValueWrapper; import org.springframework.util.Assert; @@ -28,6 +26,8 @@ import org.springframework.util.Assert; * {@link org.springframework.cache.Cache} implementation on top of a * {@link javax.cache.Cache} instance. * + *

Note: This class has been updated for JCache 0.11, as of Spring 4.0. + * * @author Juergen Hoeller * @since 3.2 */ @@ -56,9 +56,6 @@ public class JCacheCache implements Cache { */ public JCacheCache(javax.cache.Cache jcache, boolean allowNullValues) { Assert.notNull(jcache, "Cache must not be null"); - Status status = jcache.getStatus(); - Assert.isTrue(Status.STARTED.equals(status), - "A 'started' cache is required - current cache is " + status.toString()); this.cache = jcache; this.allowNullValues = allowNullValues; } diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCacheManager.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCacheManager.java index e06d8492d2..6e3b30e458 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCacheManager.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCacheManager.java @@ -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. @@ -19,16 +19,17 @@ package org.springframework.cache.jcache; import java.util.Collection; import java.util.LinkedHashSet; import javax.cache.CacheManager; -import javax.cache.Status; +import javax.cache.Caching; import org.springframework.cache.Cache; import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager; -import org.springframework.util.Assert; /** * {@link org.springframework.cache.CacheManager} implementation * backed by a JCache {@link javax.cache.CacheManager}. * + *

Note: This class has been updated for JCache 0.11, as of Spring 4.0. + * * @author Juergen Hoeller * @since 3.2 */ @@ -87,16 +88,20 @@ public class JCacheCacheManager extends AbstractTransactionSupportingCacheManage return this.allowNullValues; } + @Override + public void afterPropertiesSet() { + if (this.cacheManager == null) { + this.cacheManager = Caching.getCachingProvider().getCacheManager(); + } + super.afterPropertiesSet(); + } + @Override protected Collection loadCaches() { - Assert.notNull(this.cacheManager, "A backing CacheManager is required"); - Status status = this.cacheManager.getStatus(); - Assert.isTrue(Status.STARTED.equals(status), - "A 'started' JCache CacheManager is required - current cache is " + status.toString()); - Collection caches = new LinkedHashSet(); - for (javax.cache.Cache jcache : this.cacheManager.getCaches()) { + for (String cacheName : this.cacheManager.getCacheNames()) { + javax.cache.Cache jcache = this.cacheManager.getCache(cacheName); caches.add(new JCacheCache(jcache, this.allowNullValues)); } return caches; diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheManagerFactoryBean.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheManagerFactoryBean.java index 0bf1c5cb6c..a7787ac8fc 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheManagerFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheManagerFactoryBean.java @@ -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. @@ -16,8 +16,11 @@ package org.springframework.cache.jcache; +import java.net.URI; +import java.util.Properties; import javax.cache.CacheManager; import javax.cache.Caching; +import javax.cache.spi.CachingProvider; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.DisposableBean; @@ -29,15 +32,19 @@ import org.springframework.beans.factory.InitializingBean; * obtaining a pre-defined CacheManager by name through the standard * JCache {@link javax.cache.Caching} class. * + *

Note: This class has been updated for JCache 0.11, as of Spring 4.0. + * * @author Juergen Hoeller * @since 3.2 - * @see javax.cache.Caching#getCacheManager() - * @see javax.cache.Caching#getCacheManager(String) + * @see javax.cache.Caching#getCachingProvider() + * @see javax.cache.spi.CachingProvider#getCacheManager() */ public class JCacheManagerFactoryBean implements FactoryBean, BeanClassLoaderAware, InitializingBean, DisposableBean { - private String cacheManagerName = Caching.DEFAULT_CACHE_MANAGER_NAME; + private URI cacheManagerUri; + + private Properties cacheManagerProperties; private ClassLoader beanClassLoader; @@ -45,12 +52,20 @@ public class JCacheManagerFactoryBean /** - * Specify the name of the desired CacheManager. - * Default is JCache's default. - * @see Caching#DEFAULT_CACHE_MANAGER_NAME + * Specify the URI for the desired CacheManager. + * Default is {@code null} (i.e. JCache's default). + */ + public void setCacheManagerUri(URI cacheManagerUri) { + this.cacheManagerUri = cacheManagerUri; + } + + /** + * Specify properties for the to-be-created CacheManager. + * Default is {@code null} (i.e. no special properties to apply). + * @see javax.cache.spi.CachingProvider#getCacheManager(URI, ClassLoader, Properties) */ - public void setCacheManagerName(String cacheManagerName) { - this.cacheManagerName = cacheManagerName; + public void setCacheManagerProperties(Properties cacheManagerProperties) { + this.cacheManagerProperties = cacheManagerProperties; } @Override @@ -60,9 +75,9 @@ public class JCacheManagerFactoryBean @Override public void afterPropertiesSet() { - this.cacheManager = (this.beanClassLoader != null ? - Caching.getCacheManager(this.beanClassLoader, this.cacheManagerName) : - Caching.getCacheManager(this.cacheManagerName)); + CachingProvider provider = Caching.getCachingProvider(); + this.cacheManager = provider.getCacheManager( + this.cacheManagerUri, this.beanClassLoader, this.cacheManagerProperties); } @@ -84,7 +99,7 @@ public class JCacheManagerFactoryBean @Override public void destroy() { - this.cacheManager.shutdown(); + this.cacheManager.close(); } } -- GitLab