diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/AddCacheServerConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/AddCacheServerConfiguration.java index 8cd38a01..77bbed3d 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/AddCacheServerConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/AddCacheServerConfiguration.java @@ -19,7 +19,6 @@ package org.springframework.data.gemfire.config.annotation; import java.lang.annotation.Annotation; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; @@ -30,6 +29,7 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; import org.springframework.data.gemfire.config.annotation.support.AbstractAnnotationConfigSupport; import org.springframework.data.gemfire.config.xml.GemfireConstants; @@ -74,10 +74,9 @@ public class AddCacheServerConfiguration extends AbstractAnnotationConfigSupport @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { - if (importingClassMetadata.hasAnnotation(EnableCacheServer.class.getName())) { + if (isAnnotationPresent(importingClassMetadata)) { - Map enableCacheServerAttributes = - importingClassMetadata.getAnnotationAttributes(EnableCacheServer.class.getName()); + AnnotationAttributes enableCacheServerAttributes = getAnnotationAttributes(importingClassMetadata); registerCacheServerFactoryBeanDefinition(enableCacheServerAttributes, registry); } @@ -94,13 +93,13 @@ public class AddCacheServerConfiguration extends AbstractAnnotationConfigSupport * @see org.springframework.beans.factory.support.BeanDefinitionRegistry * @see org.springframework.data.gemfire.server.CacheServerFactoryBean */ - protected void registerCacheServerFactoryBeanDefinition(Map enableCacheServerAttributes, + protected void registerCacheServerFactoryBeanDefinition(AnnotationAttributes enableCacheServerAttributes, BeanDefinitionRegistry registry) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CacheServerFactoryBean.class); String beanName = registerCacheServerFactoryBeanDefinition(builder.getBeanDefinition(), - (String) enableCacheServerAttributes.get("name"), registry); + enableCacheServerAttributes.getString("name"), registry); builder.addPropertyReference("cache", GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME); builder.addPropertyValue("cacheServerConfigurers", resolveCacheServerConfigurers()); @@ -108,77 +107,77 @@ public class AddCacheServerConfiguration extends AbstractAnnotationConfigSupport builder.addPropertyValue("autoStartup", resolveProperty(namedCacheServerProperty(beanName, "auto-startup"), resolveProperty(cacheServerProperty("auto-startup"), - (Boolean) enableCacheServerAttributes.get("autoStartup")))); + enableCacheServerAttributes.getBoolean("autoStartup")))); builder.addPropertyValue("bindAddress", resolveProperty(namedCacheServerProperty(beanName, "bind-address"), resolveProperty(cacheServerProperty("bind-address"), - (String) enableCacheServerAttributes.get("bindAddress")))); + enableCacheServerAttributes.getString("bindAddress")))); builder.addPropertyValue("hostNameForClients", resolveProperty(namedCacheServerProperty(beanName, "hostname-for-clients"), resolveProperty(cacheServerProperty("hostname-for-clients"), - (String) enableCacheServerAttributes.get("hostnameForClients")))); + enableCacheServerAttributes.getString("hostnameForClients")))); builder.addPropertyValue("loadPollInterval", resolveProperty(namedCacheServerProperty(beanName, "load-poll-interval"), resolveProperty(cacheServerProperty("load-poll-interval"), - (Long) enableCacheServerAttributes.get("loadPollInterval")))); + enableCacheServerAttributes.getNumber("loadPollInterval")))); builder.addPropertyValue("maxConnections", resolveProperty(namedCacheServerProperty(beanName, "max-connections"), resolveProperty(cacheServerProperty("max-connections"), - (Integer) enableCacheServerAttributes.get("maxConnections")))); + enableCacheServerAttributes.getNumber("maxConnections")))); builder.addPropertyValue("maxMessageCount", resolveProperty(namedCacheServerProperty(beanName, "max-message-count"), resolveProperty(cacheServerProperty("max-message-count"), - (Integer) enableCacheServerAttributes.get("maxMessageCount")))); + enableCacheServerAttributes.getNumber("maxMessageCount")))); builder.addPropertyValue("maxThreads", resolveProperty(namedCacheServerProperty(beanName, "max-threads"), resolveProperty(cacheServerProperty("max-threads"), - (Integer) enableCacheServerAttributes.get("maxThreads")))); + enableCacheServerAttributes.getNumber("maxThreads")))); builder.addPropertyValue("maxTimeBetweenPings", resolveProperty(namedCacheServerProperty(beanName, "max-time-between-pings"), resolveProperty(cacheServerProperty("max-time-between-pings"), - (Integer) enableCacheServerAttributes.get("maxTimeBetweenPings")))); + enableCacheServerAttributes.getNumber("maxTimeBetweenPings")))); builder.addPropertyValue("messageTimeToLive", resolveProperty(namedCacheServerProperty(beanName, "message-time-to-live"), resolveProperty(cacheServerProperty("message-time-to-live"), - (Integer) enableCacheServerAttributes.get("messageTimeToLive")))); + enableCacheServerAttributes.getNumber("messageTimeToLive")))); builder.addPropertyValue("port", resolveProperty(namedCacheServerProperty(beanName, "port"), resolveProperty(cacheServerProperty("port"), - (Integer) enableCacheServerAttributes.get("port")))); + enableCacheServerAttributes.getNumber("port")))); builder.addPropertyValue("socketBufferSize", resolveProperty(namedCacheServerProperty(beanName, "socket-buffer-size"), resolveProperty(cacheServerProperty("socket-buffer-size"), - (Integer) enableCacheServerAttributes.get("socketBufferSize")))); + enableCacheServerAttributes.getNumber("socketBufferSize")))); builder.addPropertyValue("subscriptionCapacity", resolveProperty(namedCacheServerProperty(beanName, "subscription-capacity"), resolveProperty(cacheServerProperty("subscription-capacity"), - (Integer) enableCacheServerAttributes.get("subscriptionCapacity")))); + enableCacheServerAttributes.getNumber("subscriptionCapacity")))); builder.addPropertyValue("subscriptionDiskStore", resolveProperty(namedCacheServerProperty(beanName, "subscription-disk-store-name"), resolveProperty(cacheServerProperty("subscription-disk-store-name"), - (String) enableCacheServerAttributes.get("subscriptionDiskStoreName")))); + enableCacheServerAttributes.getString("subscriptionDiskStoreName")))); builder.addPropertyValue("subscriptionEvictionPolicy", resolveProperty(namedCacheServerProperty(beanName, "subscription-eviction-policy"), SubscriptionEvictionPolicy.class, resolveProperty(cacheServerProperty("subscription-eviction-policy"), - SubscriptionEvictionPolicy.class, (SubscriptionEvictionPolicy) enableCacheServerAttributes.get("subscriptionEvictionPolicy")))); + SubscriptionEvictionPolicy.class, enableCacheServerAttributes.getEnum("subscriptionEvictionPolicy")))); builder.addPropertyValue("tcpNoDelay", resolveProperty(namedCacheServerProperty(beanName, "tcp-no-delay"), resolveProperty(cacheServerProperty("tcp-no-delay"), - (Boolean) enableCacheServerAttributes.get("tcpNoDelay")))); + enableCacheServerAttributes.getBoolean("tcpNoDelay")))); } private List resolveCacheServerConfigurers() { @@ -194,8 +193,10 @@ public class AddCacheServerConfiguration extends AbstractAnnotationConfigSupport BeanDefinitionRegistry registry) { if (StringUtils.hasText(beanName)) { - BeanDefinitionReaderUtils.registerBeanDefinition( - newBeanDefinitionHolder(beanDefinition, beanName), registry); + + BeanDefinitionHolder beanDefinitionHolder = newBeanDefinitionHolder(beanDefinition, beanName); + + BeanDefinitionReaderUtils.registerBeanDefinition(beanDefinitionHolder, registry); return beanName; } diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/AddCacheServersConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/AddCacheServersConfiguration.java index 566a95c8..fe70e34c 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/AddCacheServersConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/AddCacheServersConfiguration.java @@ -14,14 +14,14 @@ * limitations under the License. * */ - package org.springframework.data.gemfire.config.annotation; -import java.util.Map; +import java.util.Arrays; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; +import org.springframework.data.gemfire.util.ArrayUtils; /** * The {@link AddCacheServersConfiguration} class registers {@link org.springframework.data.gemfire.server.CacheServerFactoryBean} @@ -41,16 +41,18 @@ public class AddCacheServersConfiguration extends AddCacheServerConfiguration { */ @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { + if (importingClassMetadata.hasAnnotation(EnableCacheServers.class.getName())) { - Map enableCacheServersAttributes = importingClassMetadata.getAnnotationAttributes( - EnableCacheServers.class.getName()); + + AnnotationAttributes enableCacheServersAttributes = + getAnnotationAttributes(importingClassMetadata, EnableCacheServers.class.getName()); AnnotationAttributes[] serversAttributes = - (AnnotationAttributes[]) enableCacheServersAttributes.get("servers"); + enableCacheServersAttributes.getAnnotationArray("servers"); - for (AnnotationAttributes enableCacheServerAttributes : serversAttributes) { - registerCacheServerFactoryBeanDefinition(enableCacheServerAttributes, registry); - } + Arrays.stream(ArrayUtils.nullSafeArray(serversAttributes, AnnotationAttributes.class)) + .forEach(enableCacheServerAttributes -> + registerCacheServerFactoryBeanDefinition(enableCacheServerAttributes, registry)); } } } diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/AddPoolConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/AddPoolConfiguration.java index b77a9769..12d60dda 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/AddPoolConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/AddPoolConfiguration.java @@ -73,10 +73,9 @@ public class AddPoolConfiguration extends AbstractAnnotationConfigSupport @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { - if (importingClassMetadata.hasAnnotation(EnablePool.class.getName())) { + if (isAnnotationPresent(importingClassMetadata)) { - Map enablePoolAttributes = - importingClassMetadata.getAnnotationAttributes(EnablePool.class.getName()); + AnnotationAttributes enablePoolAttributes = getAnnotationAttributes(importingClassMetadata); registerPoolFactoryBeanDefinition(enablePoolAttributes, registry); } @@ -93,7 +92,7 @@ public class AddPoolConfiguration extends AbstractAnnotationConfigSupport * @see org.springframework.data.gemfire.config.annotation.EnablePool * @see java.util.Map */ - protected void registerPoolFactoryBeanDefinition(Map enablePoolAttributes, + protected void registerPoolFactoryBeanDefinition(AnnotationAttributes enablePoolAttributes, BeanDefinitionRegistry registry) { String poolName = getAndValidatePoolName(enablePoolAttributes); @@ -103,94 +102,94 @@ public class AddPoolConfiguration extends AbstractAnnotationConfigSupport poolFactoryBean.addPropertyValue("freeConnectionTimeout", resolveProperty(namedPoolProperty(poolName, "free-connection-timeout"), resolveProperty(poolProperty("free-connection-timeout"), - (Integer) enablePoolAttributes.get("freeConnectionTimeout")))); + enablePoolAttributes.getNumber("freeConnectionTimeout")))); poolFactoryBean.addPropertyValue("idleTimeout", resolveProperty(namedPoolProperty(poolName, "idle-timeout"), resolveProperty(poolProperty("idle-timeout"), - (Long) enablePoolAttributes.get("idleTimeout")))); + enablePoolAttributes.getNumber("idleTimeout")))); poolFactoryBean.addPropertyValue("loadConditioningInterval", resolveProperty(namedPoolProperty(poolName, "load-conditioning-interval"), resolveProperty(poolProperty("load-conditioning-interval"), - (Integer) enablePoolAttributes.get("loadConditioningInterval")))); + enablePoolAttributes.getNumber("loadConditioningInterval")))); poolFactoryBean.addPropertyValue("maxConnections", resolveProperty(namedPoolProperty(poolName, "max-connections"), resolveProperty(poolProperty("max-connections"), - (Integer) enablePoolAttributes.get("maxConnections")))); + enablePoolAttributes.getNumber("maxConnections")))); poolFactoryBean.addPropertyValue("minConnections", resolveProperty(namedPoolProperty(poolName, "min-connections"), resolveProperty(poolProperty("min-connections"), - (Integer) enablePoolAttributes.get("minConnections")))); + enablePoolAttributes.getNumber("minConnections")))); poolFactoryBean.addPropertyValue("multiUserAuthentication", resolveProperty(namedPoolProperty(poolName, "multi-user-authentication"), resolveProperty(poolProperty("multi-user-authentication"), - (Boolean) enablePoolAttributes.get("multiUserAuthentication")))); + enablePoolAttributes.getBoolean("multiUserAuthentication")))); poolFactoryBean.addPropertyValue("pingInterval", resolveProperty(namedPoolProperty(poolName, "ping-interval"), resolveProperty(poolProperty("ping-interval"), - (Long) enablePoolAttributes.get("pingInterval")))); + enablePoolAttributes.getNumber("pingInterval")))); poolFactoryBean.addPropertyValue("poolConfigurers", resolvePoolConfigurers()); poolFactoryBean.addPropertyValue("prSingleHopEnabled", resolveProperty(namedPoolProperty(poolName, "pr-single-hop-enabled"), resolveProperty(poolProperty("pr-single-hop-enabled"), - (Boolean) enablePoolAttributes.get("prSingleHopEnabled")))); + enablePoolAttributes.getBoolean("prSingleHopEnabled")))); poolFactoryBean.addPropertyValue("readTimeout", resolveProperty(namedPoolProperty(poolName, "read-timeout"), resolveProperty(poolProperty("read-timeout"), - (Integer) enablePoolAttributes.get("readTimeout")))); + enablePoolAttributes.getNumber("readTimeout")))); poolFactoryBean.addPropertyValue("retryAttempts", resolveProperty(namedPoolProperty(poolName, "retry-attempts"), resolveProperty(poolProperty("retry-attempts"), - (Integer) enablePoolAttributes.get("retryAttempts")))); + enablePoolAttributes.getNumber("retryAttempts")))); poolFactoryBean.addPropertyValue("serverGroup", resolveProperty(namedPoolProperty(poolName, "server-group"), resolveProperty(poolProperty("server-group"), - (String) enablePoolAttributes.get("serverGroup")))); + enablePoolAttributes.getString("serverGroup")))); poolFactoryBean.addPropertyValue("socketBufferSize", resolveProperty(namedPoolProperty(poolName, "socket-buffer-size"), resolveProperty(poolProperty("socket-buffer-size"), - (Integer) enablePoolAttributes.get("socketBufferSize")))); + enablePoolAttributes.getNumber("socketBufferSize")))); poolFactoryBean.addPropertyValue("statisticInterval", resolveProperty(namedPoolProperty(poolName, "statistic-interval"), resolveProperty(poolProperty("statistic-interval"), - (Integer) enablePoolAttributes.get("statisticInterval")))); + enablePoolAttributes.getNumber("statisticInterval")))); poolFactoryBean.addPropertyValue("subscriptionAckInterval", resolveProperty(namedPoolProperty(poolName, "subscription-ack-interval"), resolveProperty(poolProperty("subscription-ack-interval"), - (Integer) enablePoolAttributes.get("subscriptionAckInterval")))); + enablePoolAttributes.getNumber("subscriptionAckInterval")))); poolFactoryBean.addPropertyValue("subscriptionEnabled", resolveProperty(namedPoolProperty(poolName, "subscription-enabled"), resolveProperty(poolProperty("subscription-enabled"), - (Boolean) enablePoolAttributes.get("subscriptionEnabled")))); + enablePoolAttributes.getBoolean("subscriptionEnabled")))); poolFactoryBean.addPropertyValue("subscriptionMessageTrackingTimeout", resolveProperty(namedPoolProperty(poolName, "subscription-message-tracking-timeout"), resolveProperty(poolProperty("subscription-message-tracking-timeout"), - (Integer) enablePoolAttributes.get("subscriptionMessageTrackingTimeout")))); + enablePoolAttributes.getNumber("subscriptionMessageTrackingTimeout")))); poolFactoryBean.addPropertyValue("subscriptionRedundancy", resolveProperty(namedPoolProperty(poolName, "subscription-redundancy"), resolveProperty(poolProperty("subscription-redundancy"), - (Integer) enablePoolAttributes.get("subscriptionRedundancy")))); + enablePoolAttributes.getNumber("subscriptionRedundancy")))); poolFactoryBean.addPropertyValue("threadLocalConnections", resolveProperty(namedPoolProperty(poolName, "thread-local-connections"), resolveProperty(poolProperty("thread-local-connections"), - (Boolean) enablePoolAttributes.get("threadLocalConnections")))); + enablePoolAttributes.getBoolean("threadLocalConnections")))); configurePoolConnections(poolName, enablePoolAttributes, poolFactoryBean); @@ -221,7 +220,7 @@ public class AddPoolConfiguration extends AbstractAnnotationConfigSupport * @see org.springframework.data.gemfire.config.annotation.ClientCacheApplication * @see java.util.Map */ - protected BeanDefinitionBuilder configurePoolConnections(String poolName, Map enablePoolAttributes, + protected BeanDefinitionBuilder configurePoolConnections(String poolName, AnnotationAttributes enablePoolAttributes, BeanDefinitionBuilder poolFactoryBean) { configurePoolLocators(poolName, enablePoolAttributes, poolFactoryBean); @@ -230,7 +229,7 @@ public class AddPoolConfiguration extends AbstractAnnotationConfigSupport return poolFactoryBean; } - protected BeanDefinitionBuilder configurePoolLocators(String poolName, Map enablePoolAttributes, + protected BeanDefinitionBuilder configurePoolLocators(String poolName, AnnotationAttributes enablePoolAttributes, BeanDefinitionBuilder poolFactoryBean) { String locatorsFromProperty = resolveProperty(namedPoolProperty(poolName, "locators"), @@ -247,7 +246,7 @@ public class AddPoolConfiguration extends AbstractAnnotationConfigSupport return poolFactoryBean; } - protected BeanDefinitionBuilder configurePoolServers(String poolName, Map enablePoolAttributes, + protected BeanDefinitionBuilder configurePoolServers(String poolName, AnnotationAttributes enablePoolAttributes, BeanDefinitionBuilder poolFactoryBean) { String serversFromProperty = resolveProperty(namedPoolProperty(poolName, "servers"), @@ -264,11 +263,11 @@ public class AddPoolConfiguration extends AbstractAnnotationConfigSupport return poolFactoryBean; } - protected ConnectionEndpointList parseConnectionEndpoints(Map enablePoolAttributes, + protected ConnectionEndpointList parseConnectionEndpoints(AnnotationAttributes enablePoolAttributes, String arrayAttributeName, String stringAttributeName, int defaultPort) { AnnotationAttributes[] connectionEndpointsMetaData = - (AnnotationAttributes[]) enablePoolAttributes.get(arrayAttributeName); + enablePoolAttributes.getAnnotationArray(arrayAttributeName); ConnectionEndpointList connectionEndpoints = new ConnectionEndpointList(); @@ -277,7 +276,7 @@ public class AddPoolConfiguration extends AbstractAnnotationConfigSupport connectionEndpoints.add(newConnectionEndpoint((String) annotationAttributes.get("host"), (Integer) annotationAttributes.get("port")))); - Optional.ofNullable((String) enablePoolAttributes.get(stringAttributeName)) + Optional.ofNullable(enablePoolAttributes.getString(stringAttributeName)) .filter(StringUtils::hasText) .ifPresent(hostsPorts -> connectionEndpoints.add(ConnectionEndpointList.parse(defaultPort, hostsPorts.split(",")))); diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/AddPoolsConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/AddPoolsConfiguration.java index 711bc3dc..72d51022 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/AddPoolsConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/AddPoolsConfiguration.java @@ -14,14 +14,14 @@ * limitations under the License. * */ - package org.springframework.data.gemfire.config.annotation; -import java.util.Map; +import java.util.Arrays; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; +import org.springframework.data.gemfire.util.ArrayUtils; /** * The {@link AddPoolsConfiguration} class registers {@link org.springframework.data.gemfire.client.PoolFactoryBean} @@ -42,15 +42,17 @@ public class AddPoolsConfiguration extends AddPoolConfiguration { */ @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { + if (importingClassMetadata.hasAnnotation(EnablePools.class.getName())) { - Map enablePoolsAttributes = importingClassMetadata.getAnnotationAttributes( - EnablePools.class.getName()); - AnnotationAttributes[] serversAttributes = (AnnotationAttributes[]) enablePoolsAttributes.get("pools"); + AnnotationAttributes enablePoolsAttributes = + getAnnotationAttributes(importingClassMetadata, EnablePools.class.getName()); - for (AnnotationAttributes enablePoolAttributes : serversAttributes) { - registerPoolFactoryBeanDefinition(enablePoolAttributes, registry); - } + AnnotationAttributes[] serversAttributes = + enablePoolsAttributes.getAnnotationArray("pools"); + + Arrays.stream(ArrayUtils.nullSafeArray(serversAttributes, AnnotationAttributes.class)) + .forEach(enablePoolAttributes -> registerPoolFactoryBeanDefinition(enablePoolAttributes, registry)); } } } diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/DiskStoreConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/DiskStoreConfiguration.java index cca5c83f..de6c971d 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/DiskStoreConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/DiskStoreConfiguration.java @@ -99,10 +99,9 @@ public class DiskStoreConfiguration extends AbstractAnnotationConfigSupport @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { - if (importingClassMetadata.hasAnnotation(getAnnotationTypeName())) { + if (isAnnotationPresent(importingClassMetadata)) { - AnnotationAttributes enableDiskStoreAttributes = - AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(getAnnotationTypeName())); + AnnotationAttributes enableDiskStoreAttributes = getAnnotationAttributes(importingClassMetadata); registerDiskStoreBeanDefinition(enableDiskStoreAttributes, registry); } @@ -332,7 +331,8 @@ public class DiskStoreConfiguration extends AbstractAnnotationConfigSupport private BeanDefinitionBuilder setPropertyValueIfNotDefault(BeanDefinitionBuilder beanDefinitionBuilder, String propertyName, T value, T defaultValue) { - return (value != null && !value.equals(defaultValue) ? - beanDefinitionBuilder.addPropertyValue(propertyName, value) : beanDefinitionBuilder); + return value != null && !value.equals(defaultValue) + ? beanDefinitionBuilder.addPropertyValue(propertyName, value) + : beanDefinitionBuilder; } } diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/DiskStoresConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/DiskStoresConfiguration.java index e19d0bba..a38ca297 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/DiskStoresConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/DiskStoresConfiguration.java @@ -14,15 +14,14 @@ * limitations under the License. * */ - package org.springframework.data.gemfire.config.annotation; -import static java.util.Arrays.stream; -import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray; +import java.util.Arrays; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; +import org.springframework.data.gemfire.util.ArrayUtils; /** * The {@link DiskStoresConfiguration} class is a Spring {@link org.springframework.context.annotation.ImportBeanDefinitionRegistrar} @@ -38,25 +37,23 @@ import org.springframework.core.type.AnnotationMetadata; */ public class DiskStoresConfiguration extends DiskStoreConfiguration { - /* (non-Javadoc) */ @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { if (importingClassMetadata.hasAnnotation(EnableDiskStores.class.getName())) { - AnnotationAttributes enableDiskStoresAttributes = AnnotationAttributes.fromMap( - importingClassMetadata.getAnnotationAttributes(EnableDiskStores.class.getName())); + AnnotationAttributes enableDiskStoresAttributes = + getAnnotationAttributes(importingClassMetadata, EnableDiskStores.class.getName()); AnnotationAttributes[] diskStores = enableDiskStoresAttributes.getAnnotationArray("diskStores"); - stream(nullSafeArray(diskStores, AnnotationAttributes.class)).forEach(diskStoreAttributes -> - registerDiskStoreBeanDefinition( + Arrays.stream(ArrayUtils.nullSafeArray(diskStores, AnnotationAttributes.class)) + .forEach(diskStoreAttributes -> registerDiskStoreBeanDefinition( mergeDiskStoreAttributes(enableDiskStoresAttributes, diskStoreAttributes), registry)); } } - /* (non-Javadoc) */ protected AnnotationAttributes mergeDiskStoreAttributes(AnnotationAttributes enableDiskStoresAttributes, AnnotationAttributes diskStoreAttributes) { @@ -72,7 +69,6 @@ public class DiskStoresConfiguration extends DiskStoreConfiguration { return diskStoreAttributes; } - /* (non-Javadoc) */ private void setAttributeIfNotDefault(AnnotationAttributes diskStoreAttributes, String attributeName, T newValue, T defaultValue) { @@ -83,7 +79,6 @@ public class DiskStoresConfiguration extends DiskStoreConfiguration { } } - /* (non-Javadoc) */ private String toString(Object value) { return String.valueOf(value); } diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/EvictionConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/EvictionConfiguration.java index cdc4913d..e2acee4b 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/EvictionConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/EvictionConfiguration.java @@ -14,7 +14,6 @@ * limitations under the License. * */ - package org.springframework.data.gemfire.config.annotation; import static org.springframework.data.gemfire.config.annotation.EnableEviction.EvictionPolicy; @@ -34,6 +33,7 @@ import java.util.function.Supplier; import org.apache.geode.cache.EvictionAttributes; import org.apache.geode.cache.Region; import org.apache.geode.cache.util.ObjectSizer; + import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.ApplicationContext; diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/ExpirationConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/ExpirationConfiguration.java index b7bba4e2..cb00028b 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/ExpirationConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/ExpirationConfiguration.java @@ -14,7 +14,6 @@ * limitations under the License. * */ - package org.springframework.data.gemfire.config.annotation; import static org.springframework.data.gemfire.config.annotation.EnableExpiration.ExpirationPolicy; @@ -33,6 +32,7 @@ import java.util.function.Supplier; import org.apache.geode.cache.ExpirationAction; import org.apache.geode.cache.ExpirationAttributes; import org.apache.geode.cache.Region; + import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.annotation.Bean; @@ -138,7 +138,6 @@ public class ExpirationConfiguration extends AbstractAnnotationConfigSupport imp return new BeanPostProcessor() { @Override - @SuppressWarnings("unchecked") public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return isRegionFactoryBean(bean) ? getExpirationPolicyConfigurer().configure(bean) : bean; } diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/GemFireAsLastResourceConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/GemFireAsLastResourceConfiguration.java index aa2351df..a6c4e28b 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/GemFireAsLastResourceConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/GemFireAsLastResourceConfiguration.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.gemfire.config.annotation; import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException; @@ -23,7 +22,7 @@ import java.util.Map; import java.util.Optional; import org.apache.geode.cache.GemFireCache; -import org.aspectj.lang.annotation.Aspect; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -34,6 +33,8 @@ import org.springframework.data.gemfire.config.annotation.support.GemFireAsLastR import org.springframework.data.gemfire.config.annotation.support.GemFireAsLastResourceConnectionClosingAspect; import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.aspectj.lang.annotation.Aspect; + /** * The {@link GemFireAsLastResourceConfiguration} class is a Spring {@link Configuration @Configuration} * annotated class used to configure the GemFire "Last Resource" Spring Data GemFire {@link Aspect Aspects}. @@ -58,14 +59,11 @@ public class GemFireAsLastResourceConfiguration implements ImportAware { private Integer enableTransactionManagementOrder; - /* (non-Javadoc) */ @Override public void setImportMetadata(AnnotationMetadata importMetadata) { this.enableTransactionManagementOrder = resolveEnableTransactionManagementOrder(importMetadata); } - /* (non-Javadoc) */ - @SuppressWarnings("all") protected int resolveEnableTransactionManagementOrder(AnnotationMetadata importMetadata) { AnnotationAttributes enableTransactionManagementAttributes = @@ -81,8 +79,6 @@ public class GemFireAsLastResourceConfiguration implements ImportAware { EnableTransactionManagement.class.getSimpleName(), String.valueOf(order))); } - /* (non-Javadoc) */ - @SuppressWarnings("all") protected AnnotationAttributes resolveEnableTransactionManagementAttributes( AnnotationMetadata importMetadata) { @@ -98,7 +94,6 @@ public class GemFireAsLastResourceConfiguration implements ImportAware { EnableTransactionManagement.class.getSimpleName())); } - /* (non-Javadoc) */ protected Integer getEnableTransactionManagementOrder() { return Optional.ofNullable(this.enableTransactionManagementOrder) @@ -109,14 +104,15 @@ public class GemFireAsLastResourceConfiguration implements ImportAware { Configuration.class.getSimpleName(), EnableGemFireAsLastResource.class.getSimpleName())); } - /* (non-Javadoc) */ @Bean public Object gemfireCachePostProcessor(@Autowired(required = false) GemFireCache gemfireCache) { - Optional.ofNullable(gemfireCache).ifPresent(cache -> cache.setCopyOnRead(true)); + + Optional.ofNullable(gemfireCache) + .ifPresent(cache -> cache.setCopyOnRead(true)); + return null; } - /* (non-Javadoc) */ @Bean public GemFireAsLastResourceConnectionAcquiringAspect gemfireJcaConnectionAcquiringAspect() { @@ -130,7 +126,6 @@ public class GemFireAsLastResourceConfiguration implements ImportAware { return connectionAcquiringAspect; } - /* (non-Javadoc) */ @Bean public GemFireAsLastResourceConnectionClosingAspect gemfireJcaConnectionClosingAspect() { diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/GemFirePropertiesConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/GemFirePropertiesConfiguration.java index 86ddbf69..944ff4d9 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/GemFirePropertiesConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/GemFirePropertiesConfiguration.java @@ -14,7 +14,6 @@ * limitations under the License. * */ - package org.springframework.data.gemfire.config.annotation; import java.lang.annotation.Annotation; diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/LocatorConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/LocatorConfiguration.java index bba363d6..beafe402 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/LocatorConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/LocatorConfiguration.java @@ -14,7 +14,6 @@ * limitations under the License. * */ - package org.springframework.data.gemfire.config.annotation; import java.lang.annotation.Annotation; @@ -23,6 +22,7 @@ import java.util.Optional; import java.util.Properties; import org.apache.geode.distributed.Locator; + import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.data.gemfire.GemfireUtils; import org.springframework.data.gemfire.config.annotation.support.EmbeddedServiceConfigurationSupport; diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/LoggingConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/LoggingConfiguration.java index 07f439a8..cf7b0fe4 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/LoggingConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/LoggingConfiguration.java @@ -14,7 +14,6 @@ * limitations under the License. * */ - package org.springframework.data.gemfire.config.annotation; import java.lang.annotation.Annotation; diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/ManagerConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/ManagerConfiguration.java index 6b51d7cb..d18b76b1 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/ManagerConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/ManagerConfiguration.java @@ -14,7 +14,6 @@ * limitations under the License. * */ - package org.springframework.data.gemfire.config.annotation; import java.lang.annotation.Annotation; diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/McastConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/McastConfiguration.java index 521f7df8..c83adee1 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/McastConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/McastConfiguration.java @@ -14,7 +14,6 @@ * limitations under the License. * */ - package org.springframework.data.gemfire.config.annotation; import java.lang.annotation.Annotation; diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/MemcachedServerConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/MemcachedServerConfiguration.java index 60ad5be1..e385b56c 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/MemcachedServerConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/MemcachedServerConfiguration.java @@ -14,7 +14,6 @@ * limitations under the License. * */ - package org.springframework.data.gemfire.config.annotation; import java.lang.annotation.Annotation; diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/OffHeapConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/OffHeapConfiguration.java index b8951f3a..58ff1046 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/OffHeapConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/OffHeapConfiguration.java @@ -14,7 +14,6 @@ * limitations under the License. * */ - package org.springframework.data.gemfire.config.annotation; import static java.util.Arrays.stream; @@ -30,6 +29,7 @@ import java.util.Properties; import java.util.Set; import org.apache.geode.cache.Region; + import org.springframework.beans.BeansException; import org.springframework.beans.PropertyValue; import org.springframework.beans.factory.config.BeanDefinition; diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/PdxConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/PdxConfiguration.java index d6752fbe..e76177c2 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/PdxConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/PdxConfiguration.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.gemfire.config.annotation; import java.lang.annotation.Annotation; @@ -21,6 +20,7 @@ import java.util.Optional; import org.apache.geode.cache.GemFireCache; import org.apache.geode.pdx.PdxSerializer; + import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableBeanFactory; @@ -241,7 +241,6 @@ public class PdxConfiguration extends AbstractAnnotationConfigSupport implements * @see #getBeanFactory() */ @NonNull - @SuppressWarnings("unchecked") protected PdxSerializer resolvePdxSerializer() { BeanFactory beanFactory = getBeanFactory(); diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/RedisServerConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/RedisServerConfiguration.java index 81f3959a..94e95fde 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/RedisServerConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/RedisServerConfiguration.java @@ -14,7 +14,6 @@ * limitations under the License. * */ - package org.springframework.data.gemfire.config.annotation; import java.lang.annotation.Annotation; diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/RegionDataAccessTracingConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/RegionDataAccessTracingConfiguration.java index 0997fbc5..1776df8b 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/RegionDataAccessTracingConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/RegionDataAccessTracingConfiguration.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.gemfire.config.annotation; import org.springframework.context.annotation.Bean; @@ -30,7 +29,6 @@ import org.springframework.data.gemfire.config.annotation.support.RegionDataAcce @SuppressWarnings("unused") public class RegionDataAccessTracingConfiguration { - /* (non-Javadoc) */ @Bean public RegionDataAccessTracingAspect regionDataAccessTracingAspect() { return new RegionDataAccessTracingAspect(); diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/StatisticsConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/StatisticsConfiguration.java index b951900a..9dc50c14 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/StatisticsConfiguration.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/StatisticsConfiguration.java @@ -14,7 +14,6 @@ * limitations under the License. * */ - package org.springframework.data.gemfire.config.annotation; import java.lang.annotation.Annotation; diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/support/AbstractAnnotationConfigSupport.java b/src/main/java/org/springframework/data/gemfire/config/annotation/support/AbstractAnnotationConfigSupport.java index 4fe5aa82..14e40551 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/support/AbstractAnnotationConfigSupport.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/support/AbstractAnnotationConfigSupport.java @@ -68,6 +68,7 @@ import org.slf4j.LoggerFactory; * @author John Blum * @author Udo Kohlmeyer * @see java.lang.ClassLoader + * @see java.lang.annotation.Annotation * @see org.springframework.beans.factory.BeanClassLoaderAware * @see org.springframework.beans.factory.BeanFactory * @see org.springframework.beans.factory.BeanFactoryAware @@ -77,7 +78,10 @@ import org.slf4j.LoggerFactory; * @see org.springframework.context.EnvironmentAware * @see org.springframework.context.expression.BeanFactoryAccessor * @see org.springframework.context.expression.EnvironmentAccessor + * @see org.springframework.core.annotation.AnnotationAttributes * @see org.springframework.core.env.Environment + * @see org.springframework.core.type.AnnotationMetadata + * @see org.springframework.core.type.MethodMetadata * @see org.springframework.expression.EvaluationContext * @since 1.9.0 */ @@ -847,6 +851,21 @@ public abstract class AbstractAnnotationConfigSupport return resolveBeanClass(beanDefinition, resolveBeanClassLoader(registry)); } + /** + * Resolves the {@link Class type} of the bean defined by the given {@link BeanDefinition}. + * + * @param beanDefinition {@link BeanDefinition} defining the bean from which the {@link Class type} is resolved. + * @param beanFactory {@link ConfigurableBeanFactory} used to resolve the {@link ClassLoader} used to resolve + * the bean's {@link Class type}. + * @return an {@link Optional} {@link Class} specifying the resolved type of the bean. + * @see org.springframework.beans.factory.config.BeanDefinition + * @see org.springframework.beans.factory.config.ConfigurableBeanFactory + * @see #resolveBeanClass(BeanDefinition, ClassLoader) + */ + protected Optional> resolveBeanClass(BeanDefinition beanDefinition, ConfigurableBeanFactory beanFactory) { + return resolveBeanClass(beanDefinition, beanFactory.getBeanClassLoader()); + } + /** * Resolves the {@link Class type} of the bean defined by the given {@link BeanDefinition}. * diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/support/EmbeddedServiceConfigurationSupport.java b/src/main/java/org/springframework/data/gemfire/config/annotation/support/EmbeddedServiceConfigurationSupport.java index 30187ebb..3f8f76bc 100644 --- a/src/main/java/org/springframework/data/gemfire/config/annotation/support/EmbeddedServiceConfigurationSupport.java +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/support/EmbeddedServiceConfigurationSupport.java @@ -33,6 +33,7 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; +import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.type.AnnotationMetadata; import org.springframework.data.gemfire.CacheFactoryBean; import org.springframework.data.gemfire.client.ClientCacheFactoryBean; @@ -48,13 +49,17 @@ import org.springframework.util.StringUtils; * the configuration of Pivotal GemFire and Apache Geode embedded services. * * @author John Blum + * @see java.util.Map * @see java.util.Properties * @see org.springframework.beans.factory.BeanFactory * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory * @see org.springframework.beans.factory.config.BeanDefinitionHolder + * @see org.springframework.beans.factory.config.BeanPostProcessor + * @see org.springframework.beans.factory.config.NamedBeanHolder * @see org.springframework.beans.factory.support.BeanDefinitionBuilder * @see org.springframework.beans.factory.support.BeanDefinitionRegistry * @see org.springframework.context.annotation.ImportBeanDefinitionRegistrar + * @see org.springframework.core.annotation.AnnotationAttributes * @see org.springframework.core.type.AnnotationMetadata * @see org.springframework.data.gemfire.config.annotation.AbstractCacheConfiguration * @see org.springframework.data.gemfire.config.annotation.support.AbstractAnnotationConfigSupport @@ -91,7 +96,9 @@ public abstract class EmbeddedServiceConfigurationSupport extends AbstractAnnota BeanDefinitionRegistry registry) { if (isAnnotationPresent(importingClassMetadata)) { - Map annotationAttributes = getAnnotationAttributes(importingClassMetadata); + + AnnotationAttributes annotationAttributes = getAnnotationAttributes(importingClassMetadata); + registerBeanDefinitions(importingClassMetadata, annotationAttributes, registry); setGemFireProperties(importingClassMetadata, annotationAttributes, registry); } @@ -104,7 +111,7 @@ public abstract class EmbeddedServiceConfigurationSupport extends AbstractAnnota } protected void setGemFireProperties(AnnotationMetadata importingClassMetadata, - Map annotationAttributes, BeanDefinitionRegistry registry) { + AnnotationAttributes annotationAttributes, BeanDefinitionRegistry registry) { Properties gemfireProperties = toGemFireProperties(annotationAttributes); @@ -319,7 +326,7 @@ public abstract class EmbeddedServiceConfigurationSupport extends AbstractAnnota Properties gemfirePropertiesBean = (Properties) bean; - gemfirePropertiesBean.putAll(gemfireProperties); + gemfirePropertiesBean.putAll(this.gemfireProperties); } return bean; diff --git a/src/test/java/org/springframework/data/gemfire/config/annotation/PeerCacheApplicationWithAddedCacheServerIntegrationTests.java b/src/test/java/org/springframework/data/gemfire/config/annotation/PeerCacheApplicationWithAddedCacheServerIntegrationTests.java new file mode 100644 index 00000000..3cd29466 --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/config/annotation/PeerCacheApplicationWithAddedCacheServerIntegrationTests.java @@ -0,0 +1,139 @@ +/* + * Copyright 2018 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 + * + * https://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.List; + +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.apache.geode.cache.Cache; +import org.apache.geode.cache.server.CacheServer; +import org.apache.geode.distributed.internal.DistributionConfig; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.data.gemfire.process.ProcessWrapper; +import org.springframework.data.gemfire.test.support.ClientServerIntegrationTestsSupport; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Integration Tests to test and assert the property configuration of a {@link PeerCacheApplication} along with + * added {@link CacheServer CacheServers} using the {@link EnableCacheServer} annotation. + * + * @author John Blum + * @see org.junit.Test + * @see org.apache.geode.cache.Cache + * @see org.apache.geode.cache.server.CacheServer + * @see org.apache.geode.distributed.internal.DistributionConfig + * @see org.springframework.test.context.junit4.SpringRunner + * @since 2.2.0 + */ +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = PeerCacheApplicationWithAddedCacheServerIntegrationTests.TestPeerCacheConfiguration.class) +@SuppressWarnings("unused") +// TODO: Convert to Unit Test once STDG is used by SDG. +public class PeerCacheApplicationWithAddedCacheServerIntegrationTests extends ClientServerIntegrationTestsSupport { + + private static int cacheServerPort; + private static int locatorPort; + + private static ProcessWrapper gemfireLocator; + + private static final String GEMFIRE_LOG_LEVEL = "config"; + + @BeforeClass + public static void startGemFireLocator() throws Exception { + + locatorPort = findAvailablePort(); + + gemfireLocator = run(TestLocatorConfiguration.class, + "-Dspring.data.gemfire.locator.port=" + locatorPort, + String.format("-Dgemfire.%s=%s", DistributionConfig.ENABLE_CLUSTER_CONFIGURATION_NAME, false)); + + waitForServerToStart("localhost", locatorPort); + + cacheServerPort = findAvailablePort(); + + System.setProperty("spring.data.gemfire.cache.peer.locators", String.format("localhost[%d]", locatorPort)); + System.setProperty("spring.data.gemfire.cache.server.port", String.valueOf(cacheServerPort)); + } + + @AfterClass + public static void stopGemFireLocator() { + + stop(gemfireLocator); + + System.getProperties().stringPropertyNames().stream() + .filter(propertyName -> propertyName.startsWith("spring.data.gemfire.")) + .forEach(System::clearProperty); + } + + @Autowired + private Cache cache; + + @Before + public void setup() { + + assertThat(this.cache).isNotNull(); + assertThat(this.cache.getName()).isEqualTo("PeerCacheApplicationWithAddedCacheServerIntegrationTests"); + assertThat(this.cache.getDistributedSystem()).isNotNull(); + assertThat(this.cache.getDistributedSystem().getProperties()).isNotNull(); + assertThat(this.cache.getDistributedSystem().getProperties().getProperty(DistributionConfig.LOCATORS_NAME)) + .isEqualTo(String.format("localhost[%d]", locatorPort)); + assertThat(this.cache.getDistributedSystem().getProperties().getProperty(DistributionConfig.NAME_NAME)) + .isEqualTo("PeerCacheApplicationWithAddedCacheServerIntegrationTests"); + } + + @Test + public void cacheServerWasConfiguredCorrectly() { + + List cacheServers = this.cache.getCacheServers(); + + assertThat(cacheServers).isNotNull(); + assertThat(cacheServers).hasSize(1); + + CacheServer cacheServer = cacheServers.get(0); + + assertThat(cacheServer).isNotNull(); + assertThat(cacheServer.getPort()).isEqualTo(cacheServerPort); + } + + @LocatorApplication(logLevel = GEMFIRE_LOG_LEVEL) + static class TestLocatorConfiguration { + + public static void main(String[] args) { + + AnnotationConfigApplicationContext applicationContext = + new AnnotationConfigApplicationContext(TestLocatorConfiguration.class); + + applicationContext.registerShutdownHook(); + + block(); + } + } + + @EnableCacheServer + @PeerCacheApplication(name = "PeerCacheApplicationWithAddedCacheServerIntegrationTests") + static class TestPeerCacheConfiguration { } + +}