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.

This commit is contained in:
John Blum
2017-12-14 19:14:25 -08:00
parent 387b120dcb
commit 42bbfacaf3
6 changed files with 173 additions and 25 deletions

View File

@@ -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<? extends Annotation> getAnnotationType();
/**
* Returns the fully-qualified {@link Class#getName() class name} of the cache application

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<String, PeerCacheConfigurer> 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<String, Object> 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<? extends Annotation> getAnnotationType() {
return PeerCacheApplication.class;
}

View File

@@ -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 {
}
}