From 0a9b8cbb411142b0c24379f32d41b2071f19a2e4 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 13 May 2015 18:03:47 +0200 Subject: [PATCH] Polish Infinispan support Updated to the `EmbeddedCacheManager` interface and added support for default cache configuration. Added dependencies management for the JCache support with tests Fixes gh-2906, see gh-2633 --- spring-boot-autoconfigure/pom.xml | 5 ++ .../cache/InfinispanCacheConfiguration.java | 45 ++++++++-- .../cache/CacheAutoConfigurationTests.java | 86 ++++++++++++++----- spring-boot-dependencies/pom.xml | 7 +- 4 files changed, 113 insertions(+), 30 deletions(-) diff --git a/spring-boot-autoconfigure/pom.xml b/spring-boot-autoconfigure/pom.xml index b849689b3a..3cb4302ac8 100644 --- a/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-autoconfigure/pom.xml @@ -235,6 +235,11 @@ hornetq-jms-server true + + org.infinispan + infinispan-jcache + true + org.infinispan infinispan-spring4 diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/InfinispanCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/InfinispanCacheConfiguration.java index 7b5ec6456d..2ffe80d23b 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/InfinispanCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/InfinispanCacheConfiguration.java @@ -16,8 +16,15 @@ package org.springframework.boot.autoconfigure.cache; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +import org.infinispan.configuration.cache.ConfigurationBuilder; import org.infinispan.manager.DefaultCacheManager; +import org.infinispan.manager.EmbeddedCacheManager; import org.infinispan.spring.provider.SpringEmbeddedCacheManager; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -28,13 +35,11 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; import org.springframework.util.CollectionUtils; -import java.io.IOException; -import java.util.List; - /** * Infinispan cache configuration. * * @author EddĂș MelĂ©ndez + * @author Stephane Nicoll * @since 1.3.0 */ @Configuration @@ -46,24 +51,46 @@ public class InfinispanCacheConfiguration { @Autowired private CacheProperties cacheProperties; + @Autowired(required = false) + private ConfigurationBuilder defaultConfigurationBuilder; + @Bean - public CacheManager cacheManager() throws IOException { - DefaultCacheManager defaultCacheManager = createCacheManager(); + public CacheManager cacheManager(EmbeddedCacheManager embeddedCacheManager) { + return new SpringEmbeddedCacheManager(embeddedCacheManager); + } + + @Bean(destroyMethod = "stop") + @ConditionalOnMissingBean + public EmbeddedCacheManager infinispanCacheManager() throws IOException { + EmbeddedCacheManager infinispanCacheManager = createEmbeddedCacheManager(); List cacheNames = this.cacheProperties.getCacheNames(); if (!CollectionUtils.isEmpty(cacheNames)) { for (String cacheName : cacheNames) { - defaultCacheManager.startCache(cacheName); + infinispanCacheManager.defineConfiguration(cacheName, getDefaultCacheConfiguration()); } } - return new SpringEmbeddedCacheManager(defaultCacheManager); + return infinispanCacheManager; } - private DefaultCacheManager createCacheManager() throws IOException { + private EmbeddedCacheManager createEmbeddedCacheManager() throws IOException { Resource location = this.cacheProperties.resolveConfigLocation(); if (location != null) { - return new DefaultCacheManager(this.cacheProperties.getConfig().getInputStream()); + InputStream in = this.cacheProperties.getConfig().getInputStream(); + try { + return new DefaultCacheManager(in); + } + finally { + in.close(); + } } return new DefaultCacheManager(); } + private org.infinispan.configuration.cache.Configuration getDefaultCacheConfiguration() { + if (this.defaultConfigurationBuilder != null) { + return defaultConfigurationBuilder.build(); + } + return new ConfigurationBuilder().build(); + } + } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java index 8773787844..6c656f2426 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java @@ -29,6 +29,9 @@ import com.hazelcast.cache.HazelcastCachingProvider; import com.hazelcast.core.HazelcastInstance; import com.hazelcast.spring.cache.HazelcastCacheManager; import net.sf.ehcache.Status; +import org.infinispan.configuration.cache.ConfigurationBuilder; +import org.infinispan.jcache.embedded.JCachingProvider; +import org.infinispan.spring.provider.SpringEmbeddedCacheManager; import org.junit.After; import org.junit.Rule; import org.junit.Test; @@ -71,6 +74,7 @@ import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -381,29 +385,58 @@ public class CacheAutoConfigurationTests { } @Test - public void infinispanCacheWithCaches() { - SpringEmbeddedCacheManager cacheManager = null; - try { - load(DefaultCacheConfiguration.class, "spring.cache.type=infinispan", - "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar"); - cacheManager = validateCacheManager(SpringEmbeddedCacheManager.class); - assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar")); - } finally { - cacheManager.stop(); - } + public void infinispanCacheWithConfig() { + load(DefaultCacheConfiguration.class, "spring.cache.type=infinispan", + "spring.cache.config=infinispan.xml"); + SpringEmbeddedCacheManager cacheManager = validateCacheManager(SpringEmbeddedCacheManager.class); + assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar")); } @Test - public void infinispanCacheWithConfig() { - SpringEmbeddedCacheManager cacheManager = null; - try { - load(DefaultCacheConfiguration.class, "spring.cache.type=infinispan", - "spring.cache.config=infinispan.xml"); - cacheManager = validateCacheManager(SpringEmbeddedCacheManager.class); - assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar")); - } finally { - cacheManager.stop(); - } + public void infinispanCacheWithCaches() { + load(DefaultCacheConfiguration.class, "spring.cache.type=infinispan", + "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar"); + SpringEmbeddedCacheManager cacheManager = validateCacheManager(SpringEmbeddedCacheManager.class); + assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar")); + assertThat(cacheManager.getCacheNames(), hasSize(2)); + } + + @Test + public void infinispanCacheWithCachesAndCustomConfig() { + load(InfinispanCustomConfiguration.class, "spring.cache.type=infinispan", + "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar"); + SpringEmbeddedCacheManager cacheManager = validateCacheManager(SpringEmbeddedCacheManager.class); + assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar")); + assertThat(cacheManager.getCacheNames(), hasSize(2)); + + ConfigurationBuilder defaultConfigurationBuilder = this.context + .getBean(ConfigurationBuilder.class); + verify(defaultConfigurationBuilder, times(2)).build(); + } + + @Test + public void infinispanAsJCacheWithCaches() { + String cachingProviderFqn = JCachingProvider.class.getName(); + load(DefaultCacheConfiguration.class, "spring.cache.type=jcache", + "spring.cache.jcache.provider=" + cachingProviderFqn, + "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar"); + JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class); + assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar")); + assertThat(cacheManager.getCacheNames(), hasSize(2)); + } + + @Test + public void infinispanAsJCacheWithConfig() throws IOException { + String cachingProviderFqn = JCachingProvider.class.getName(); + String configLocation = "infinispan.xml"; + load(DefaultCacheConfiguration.class, "spring.cache.type=jcache", + "spring.cache.jcache.provider=" + cachingProviderFqn, + "spring.cache.config=" + configLocation); + JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class); + + Resource configResource = new ClassPathResource(configLocation); + assertThat(cacheManager.getCacheManager().getURI(), + is(configResource.getURI())); } @Test @@ -581,6 +614,19 @@ public class CacheAutoConfigurationTests { } + @Configuration + @EnableCaching + static class InfinispanCustomConfiguration { + + @Bean + public ConfigurationBuilder configurationBuilder() { + ConfigurationBuilder builder = mock(ConfigurationBuilder.class); + when(builder.build()).thenReturn(new ConfigurationBuilder().build()); + return builder; + } + + } + @Configuration @Import({ GenericCacheConfiguration.class, RedisCacheConfiguration.class }) static class CustomCacheManagerConfiguration { diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index a7a5ef66ec..cbbee2e307 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -1277,6 +1277,11 @@ hsqldb ${hsqldb.version} + + org.infinispan + infinispan-jcache + ${infinispan.version} + org.infinispan infinispan-spring4 @@ -1855,4 +1860,4 @@ integration-test - + \ No newline at end of file