From 42bbfacaf343a595da88f859216fb22dbb2ed151 Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 14 Dec 2017 19:14:25 -0800 Subject: [PATCH] DATAGEODE-75 - Enable the spring.data.gemfire.name property to be used in addition to spring.data.gemfire.cache.name for naming members of the cluster. --- .../AbstractCacheConfiguration.java | 7 +- .../annotation/CacheServerApplication.java | 3 +- .../annotation/ClientCacheApplication.java | 3 +- .../annotation/PeerCacheApplication.java | 3 +- .../annotation/PeerCacheConfiguration.java | 44 +++--- ...actCacheConfigurationIntegrationTests.java | 138 ++++++++++++++++++ 6 files changed, 173 insertions(+), 25 deletions(-) create mode 100644 src/test/java/org/springframework/data/gemfire/config/annotation/AbstractCacheConfigurationIntegrationTests.java diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/AbstractCacheConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/AbstractCacheConfiguration.java index b2e35b64..1e0f05b4 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/AbstractCacheConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/AbstractCacheConfiguration.java @@ -274,8 +274,9 @@ public abstract class AbstractCacheConfiguration extends AbstractAnnotationConfi setLogLevel(resolveProperty(cacheProperty("log-level"), (String) cacheMetadataAttributes.get("logLevel"))); - setName(resolveProperty(cacheProperty("name"), - (String) cacheMetadataAttributes.get("name"))); + setName(resolveProperty(propertyName("name"), + resolveProperty(cacheProperty("name"), + (String) cacheMetadataAttributes.get("name")))); setUseBeanFactoryLocator(resolveProperty(propertyName("use-bean-factory-locator"), Boolean.TRUE.equals(cacheMetadataAttributes.get("useBeanFactoryLocator")))); @@ -473,7 +474,7 @@ public abstract class AbstractCacheConfiguration extends AbstractAnnotationConfi * @see org.springframework.data.gemfire.config.annotation.ClientCacheApplication * @see org.springframework.data.gemfire.config.annotation.PeerCacheApplication */ - protected abstract Class getAnnotationType(); + protected abstract Class getAnnotationType(); /** * Returns the fully-qualified {@link Class#getName() class name} of the cache application diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/CacheServerApplication.java b/src/main/java/org/springframework/data/gemfire/config/annotation/CacheServerApplication.java index c95cf5c1..0ac08fad 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/CacheServerApplication.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/CacheServerApplication.java @@ -235,7 +235,8 @@ public @interface CacheServerApplication { * * Defaults to {@literal SpringBasedCacheServerApplication}. * - * Use {@literal spring.data.gemfire.cache.name} property in {@literal application.properties}. + * Use either the {@literal spring.data.gemfire.name} or the {@literal spring.data.gemfire.cache.name} property + * in {@literal application.properties}. */ String name() default CacheServerConfiguration.DEFAULT_NAME; diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/ClientCacheApplication.java b/src/main/java/org/springframework/data/gemfire/config/annotation/ClientCacheApplication.java index 26b059fa..973e0c01 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/ClientCacheApplication.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/ClientCacheApplication.java @@ -195,7 +195,8 @@ public @interface ClientCacheApplication { * * Defaults to {@literal SpringBasedCacheClientApplication}. * - * Use {@literal spring.data.gemfire.cache.name} property in {@literal application.properties}. + * Use either the {@literal spring.data.gemfire.name} or the {@literal spring.data.gemfire.cache.name} property + * in {@literal application.properties}. */ String name() default ClientCacheConfiguration.DEFAULT_NAME; diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/PeerCacheApplication.java b/src/main/java/org/springframework/data/gemfire/config/annotation/PeerCacheApplication.java index fe61435d..67e83a5d 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/PeerCacheApplication.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/PeerCacheApplication.java @@ -142,7 +142,8 @@ public @interface PeerCacheApplication { * * Defaults to {@literal SpringBasedPeerCacheApplication}. * - * Use {@literal spring.data.gemfire.cache.name} property in {@literal application.properties}. + * Use either the {@literal spring.data.gemfire.name} or the {@literal spring.data.gemfire.cache.name} property + * in {@literal application.properties}. */ String name() default PeerCacheConfiguration.DEFAULT_NAME; diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/PeerCacheConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/PeerCacheConfiguration.java index 9403e5f7..cbf64836 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/PeerCacheConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/PeerCacheConfiguration.java @@ -19,6 +19,7 @@ package org.springframework.data.gemfire.config.annotation; import static org.springframework.data.gemfire.util.CollectionUtils.nullSafeMap; +import java.lang.annotation.Annotation; import java.util.Collections; import java.util.List; import java.util.Map; @@ -100,10 +101,12 @@ public class PeerCacheConfiguration extends AbstractCacheConfiguration { Optional.of(this.getBeanFactory()) .filter(beanFactory -> beanFactory instanceof ListableBeanFactory) .map(beanFactory -> { + Map beansOfType = ((ListableBeanFactory) beanFactory) .getBeansOfType(PeerCacheConfigurer.class, true, true); return nullSafeMap(beansOfType).values().stream().collect(Collectors.toList()); + }) .orElseGet(Collections::emptyList) ); @@ -140,31 +143,34 @@ public class PeerCacheConfiguration extends AbstractCacheConfiguration { Map peerCacheApplicationAttributes = importMetadata.getAnnotationAttributes(getAnnotationTypeName()); - setEnableAutoReconnect(resolveProperty(cachePeerProperty("enable-auto-reconnect"), - Boolean.TRUE.equals(peerCacheApplicationAttributes.get("enableAutoReconnect")))); + if (peerCacheApplicationAttributes != null) { - setLockLease(resolveProperty(cachePeerProperty("lock-lease"), - (Integer) peerCacheApplicationAttributes.get("lockLease"))); + setEnableAutoReconnect(resolveProperty(cachePeerProperty("enable-auto-reconnect"), + Boolean.TRUE.equals(peerCacheApplicationAttributes.get("enableAutoReconnect")))); - setLockTimeout(resolveProperty(cachePeerProperty("lock-timeout"), - (Integer) peerCacheApplicationAttributes.get("lockTimeout"))); + setLockLease(resolveProperty(cachePeerProperty("lock-lease"), + (Integer) peerCacheApplicationAttributes.get("lockLease"))); - setMessageSyncInterval(resolveProperty(cachePeerProperty("message-sync-interval"), - (Integer) peerCacheApplicationAttributes.get("messageSyncInterval"))); + setLockTimeout(resolveProperty(cachePeerProperty("lock-timeout"), + (Integer) peerCacheApplicationAttributes.get("lockTimeout"))); - setSearchTimeout(resolveProperty(cachePeerProperty("search-timeout"), - (Integer) peerCacheApplicationAttributes.get("searchTimeout"))); + setMessageSyncInterval(resolveProperty(cachePeerProperty("message-sync-interval"), + (Integer) peerCacheApplicationAttributes.get("messageSyncInterval"))); - setUseClusterConfiguration(resolveProperty(cachePeerProperty("use-cluster-configuration"), - Boolean.TRUE.equals(peerCacheApplicationAttributes.get("useClusterConfiguration")))); + setSearchTimeout(resolveProperty(cachePeerProperty("search-timeout"), + (Integer) peerCacheApplicationAttributes.get("searchTimeout"))); - Optional.ofNullable((String) peerCacheApplicationAttributes.get("locators")) - .filter(PeerCacheConfiguration::hasValue) - .ifPresent(this::setLocators); + setUseClusterConfiguration(resolveProperty(cachePeerProperty("use-cluster-configuration"), + Boolean.TRUE.equals(peerCacheApplicationAttributes.get("useClusterConfiguration")))); - Optional.ofNullable(resolveProperty(cachePeerProperty("locators"), (String) null)) - .filter(StringUtils::hasText) - .ifPresent(this::setLocators); + Optional.ofNullable((String) peerCacheApplicationAttributes.get("locators")) + .filter(PeerCacheConfiguration::hasValue) + .ifPresent(this::setLocators); + + Optional.ofNullable(resolveProperty(cachePeerProperty("locators"), (String) null)) + .filter(StringUtils::hasText) + .ifPresent(this::setLocators); + } } } @@ -172,7 +178,7 @@ public class PeerCacheConfiguration extends AbstractCacheConfiguration { * {@inheritDoc} */ @Override - protected Class getAnnotationType() { + protected Class getAnnotationType() { return PeerCacheApplication.class; } diff --git a/src/test/java/org/springframework/data/gemfire/config/annotation/AbstractCacheConfigurationIntegrationTests.java b/src/test/java/org/springframework/data/gemfire/config/annotation/AbstractCacheConfigurationIntegrationTests.java new file mode 100644 index 00000000..f8a12d30 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/config/annotation/AbstractCacheConfigurationIntegrationTests.java @@ -0,0 +1,138 @@ +/* + * Copyright 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.gemfire.config.annotation; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Optional; + +import org.apache.geode.cache.Cache; +import org.apache.geode.cache.GemFireCache; +import org.apache.geode.cache.client.ClientCache; +import org.junit.After; +import org.junit.Test; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertySource; +import org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects; +import org.springframework.mock.env.MockPropertySource; + +/** + * Integration tests for {@link AbstractCacheConfiguration}. + * + * @author John Blum + * @see org.junit.Test + * @see org.springframework.data.gemfire.config.annotation.AbstractCacheConfiguration + * @since 2.0.2 + */ +public class AbstractCacheConfigurationIntegrationTests { + + private ConfigurableApplicationContext applicationContext; + + @After + public void tearDown() { + Optional.ofNullable(this.applicationContext).ifPresent(ConfigurableApplicationContext::close); + } + + private void assertName(GemFireCache gemfireCache, String name) { + + assertThat(gemfireCache).isNotNull(); + assertThat(gemfireCache.getDistributedSystem()).isNotNull(); + assertThat(gemfireCache.getDistributedSystem().getProperties()).isNotNull(); + assertThat(gemfireCache.getDistributedSystem().getProperties().getProperty("name")).isEqualTo(name); + } + + private ConfigurableApplicationContext newApplicationContext(Class... annotatedClasses) { + return newApplicationContext(null, annotatedClasses); + } + + private ConfigurableApplicationContext newApplicationContext(PropertySource testPropertySource, + Class... annotatedClasses) { + + AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); + + Optional.ofNullable(testPropertySource).ifPresent(it -> { + + MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources(); + + propertySources.addFirst(testPropertySource); + }); + + applicationContext.registerShutdownHook(); + applicationContext.register(annotatedClasses); + applicationContext.refresh(); + + return applicationContext; + } + + @Test + public void clientCacheNameUsesAnnotationNameAttributeDefaultValue() { + + this.applicationContext = newApplicationContext(TestClientCacheConfiguration.class); + + GemFireCache peerCache = this.applicationContext.getBean("gemfireCache", ClientCache.class); + + assertName(peerCache, ClientCacheConfiguration.DEFAULT_NAME); + } + + @Test + public void clientCacheNameUsesSpringDataGemFireNameProperty() { + + MockPropertySource testPropertySource = new MockPropertySource() + .withProperty("spring.data.gemfire.name", "TestClient"); + + this.applicationContext = newApplicationContext(testPropertySource, TestClientCacheConfiguration.class); + + GemFireCache peerCache = this.applicationContext.getBean("gemfireCache", ClientCache.class); + + assertName(peerCache, "TestClient"); + } + + @Test + public void peerCacheNameUsesAnnotationNameAttributeConfiguredValue() { + + this.applicationContext = newApplicationContext(TestPeerCacheConfiguration.class); + + GemFireCache peerCache = this.applicationContext.getBean("gemfireCache", Cache.class); + + assertName(peerCache, "TestPeerCacheApp"); + } + + @Test + public void peerCacheNameUsesSpringDataGemFireCacheNameProperty() { + + MockPropertySource testPropertySource = new MockPropertySource() + .withProperty("spring.data.gemfire.cache.name", "TestPeer"); + + this.applicationContext = newApplicationContext(testPropertySource, TestPeerCacheConfiguration.class); + + GemFireCache peerCache = this.applicationContext.getBean("gemfireCache", Cache.class); + + assertName(peerCache, "TestPeer"); + } + + @ClientCacheApplication + @EnableGemFireMockObjects + static class TestClientCacheConfiguration { + } + + @PeerCacheApplication(name = "TestPeerCacheApp") + @EnableGemFireMockObjects + static class TestPeerCacheConfiguration { + } +}