SGF-692 - Add Annotation support for Region Compression.
This commit is contained in:
14
pom.xml
14
pom.xml
@@ -16,6 +16,9 @@
|
|||||||
<name>Spring Data GemFire</name>
|
<name>Spring Data GemFire</name>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
<dist.key>SGF</dist.key>
|
||||||
|
<java-module-name>spring.data.gemfire</java-module-name>
|
||||||
|
<source.level>1.8</source.level>
|
||||||
<antlr.version>2.7.7</antlr.version>
|
<antlr.version>2.7.7</antlr.version>
|
||||||
<apache-shiro.version>1.3.2</apache-shiro.version>
|
<apache-shiro.version>1.3.2</apache-shiro.version>
|
||||||
<assertj.version>3.6.2</assertj.version>
|
<assertj.version>3.6.2</assertj.version>
|
||||||
@@ -23,9 +26,9 @@
|
|||||||
<gemfire.version>9.1.1</gemfire.version>
|
<gemfire.version>9.1.1</gemfire.version>
|
||||||
<google-code-findbugs.version>2.0.2</google-code-findbugs.version>
|
<google-code-findbugs.version>2.0.2</google-code-findbugs.version>
|
||||||
<multithreadedtc.version>1.01</multithreadedtc.version>
|
<multithreadedtc.version>1.01</multithreadedtc.version>
|
||||||
|
<snappy.version>0.4</snappy.version>
|
||||||
<springdata.commons>2.1.0.BUILD-SNAPSHOT</springdata.commons>
|
<springdata.commons>2.1.0.BUILD-SNAPSHOT</springdata.commons>
|
||||||
<spring-shell.version>1.2.0.RELEASE</spring-shell.version>
|
<spring-shell.version>1.2.0.RELEASE</spring-shell.version>
|
||||||
<java-module-name>spring.data.gemfire</java-module-name>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
@@ -149,7 +152,14 @@
|
|||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- 3rd Party Depenendencies -->
|
<dependency>
|
||||||
|
<groupId>org.iq80.snappy</groupId>
|
||||||
|
<artifactId>snappy</artifactId>
|
||||||
|
<version>${snappy.version}</version>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 3rd Party Dependencies -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.aspectj</groupId>
|
<groupId>org.aspectj</groupId>
|
||||||
<artifactId>aspectjweaver</artifactId>
|
<artifactId>aspectjweaver</artifactId>
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ import org.apache.geode.cache.RegionShortcut;
|
|||||||
import org.apache.geode.cache.Scope;
|
import org.apache.geode.cache.Scope;
|
||||||
import org.apache.geode.cache.asyncqueue.AsyncEventQueue;
|
import org.apache.geode.cache.asyncqueue.AsyncEventQueue;
|
||||||
import org.apache.geode.cache.wan.GatewaySender;
|
import org.apache.geode.cache.wan.GatewaySender;
|
||||||
|
import org.apache.geode.compression.Compressor;
|
||||||
import org.apache.geode.internal.cache.UserSpecifiedRegionAttributes;
|
import org.apache.geode.internal.cache.UserSpecifiedRegionAttributes;
|
||||||
import org.springframework.beans.factory.DisposableBean;
|
import org.springframework.beans.factory.DisposableBean;
|
||||||
import org.springframework.beans.factory.FactoryBean;
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
@@ -125,6 +126,8 @@ public abstract class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K,
|
|||||||
private Class<K> keyConstraint;
|
private Class<K> keyConstraint;
|
||||||
private Class<V> valueConstraint;
|
private Class<V> valueConstraint;
|
||||||
|
|
||||||
|
private Compressor compressor;
|
||||||
|
|
||||||
private DataPolicy dataPolicy;
|
private DataPolicy dataPolicy;
|
||||||
|
|
||||||
private EvictionAttributes evictionAttributes;
|
private EvictionAttributes evictionAttributes;
|
||||||
@@ -310,6 +313,8 @@ public abstract class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K,
|
|||||||
|
|
||||||
Optional.ofNullable(this.cacheWriter).ifPresent(regionFactory::setCacheWriter);
|
Optional.ofNullable(this.cacheWriter).ifPresent(regionFactory::setCacheWriter);
|
||||||
|
|
||||||
|
Optional.ofNullable(this.compressor).ifPresent(regionFactory::setCompressor);
|
||||||
|
|
||||||
resolveDataPolicy(regionFactory, this.persistent, this.dataPolicy);
|
resolveDataPolicy(regionFactory, this.persistent, this.dataPolicy);
|
||||||
|
|
||||||
Optional.ofNullable(this.diskStoreName)
|
Optional.ofNullable(this.diskStoreName)
|
||||||
@@ -767,6 +772,16 @@ public abstract class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K,
|
|||||||
this.close = close;
|
this.close = close;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configures the {@link Compressor} used to compress the this {@link Region Region's} data.
|
||||||
|
*
|
||||||
|
* @param compressor {@link Compressor} used to compress the this {@link Region Region's} data.
|
||||||
|
* @see org.apache.geode.compression.Compressor
|
||||||
|
*/
|
||||||
|
public void setCompressor(Compressor compressor) {
|
||||||
|
this.compressor = compressor;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates whether the Region referred to by this factory bean will be destroyed on shutdown (default false).
|
* Indicates whether the Region referred to by this factory bean will be destroyed on shutdown (default false).
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ import org.apache.geode.cache.client.ClientRegionFactory;
|
|||||||
import org.apache.geode.cache.client.ClientRegionShortcut;
|
import org.apache.geode.cache.client.ClientRegionShortcut;
|
||||||
import org.apache.geode.cache.client.Pool;
|
import org.apache.geode.cache.client.Pool;
|
||||||
import org.apache.geode.cache.client.PoolManager;
|
import org.apache.geode.cache.client.PoolManager;
|
||||||
|
import org.apache.geode.compression.Compressor;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.DisposableBean;
|
import org.springframework.beans.factory.DisposableBean;
|
||||||
import org.springframework.beans.factory.FactoryBean;
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
@@ -95,6 +96,8 @@ public class ClientRegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V>
|
|||||||
|
|
||||||
private ClientRegionShortcut shortcut;
|
private ClientRegionShortcut shortcut;
|
||||||
|
|
||||||
|
private Compressor compressor;
|
||||||
|
|
||||||
private DataPolicy dataPolicy;
|
private DataPolicy dataPolicy;
|
||||||
|
|
||||||
private EvictionAttributes evictionAttributes;
|
private EvictionAttributes evictionAttributes;
|
||||||
@@ -391,6 +394,8 @@ public class ClientRegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V>
|
|||||||
|
|
||||||
stream(nullSafeArray(this.cacheListeners, CacheListener.class)).forEach(clientRegionFactory::addCacheListener);
|
stream(nullSafeArray(this.cacheListeners, CacheListener.class)).forEach(clientRegionFactory::addCacheListener);
|
||||||
|
|
||||||
|
Optional.ofNullable(this.compressor).ifPresent(clientRegionFactory::setCompressor);
|
||||||
|
|
||||||
Optional.ofNullable(this.diskStoreName).filter(StringUtils::hasText)
|
Optional.ofNullable(this.diskStoreName).filter(StringUtils::hasText)
|
||||||
.ifPresent(clientRegionFactory::setDiskStoreName);
|
.ifPresent(clientRegionFactory::setDiskStoreName);
|
||||||
|
|
||||||
@@ -560,6 +565,16 @@ public class ClientRegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V>
|
|||||||
this.destroy = (this.destroy && !close); // retain previous value iff close is false.
|
this.destroy = (this.destroy && !close); // retain previous value iff close is false.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configures the {@link Compressor} used to compress the this {@link Region Region's} data.
|
||||||
|
*
|
||||||
|
* @param compressor {@link Compressor} used to compress the this {@link Region Region's} data.
|
||||||
|
* @see org.apache.geode.compression.Compressor
|
||||||
|
*/
|
||||||
|
public void setCompressor(Compressor compressor) {
|
||||||
|
this.compressor = compressor;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the Data Policy. Used only when a new Region is created.
|
* Sets the Data Policy. Used only when a new Region is created.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -0,0 +1,184 @@
|
|||||||
|
/*
|
||||||
|
* 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 java.util.Arrays.stream;
|
||||||
|
import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray;
|
||||||
|
import static org.springframework.data.gemfire.util.CollectionUtils.asSet;
|
||||||
|
import static org.springframework.data.gemfire.util.CollectionUtils.nullSafeIterable;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.apache.geode.cache.Region;
|
||||||
|
import org.apache.geode.compression.Compressor;
|
||||||
|
import org.apache.geode.compression.SnappyCompressor;
|
||||||
|
import org.springframework.beans.PropertyValue;
|
||||||
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
|
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||||
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.ImportAware;
|
||||||
|
import org.springframework.core.annotation.AnnotationAttributes;
|
||||||
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
|
import org.springframework.data.gemfire.RegionLookupFactoryBean;
|
||||||
|
import org.springframework.data.gemfire.config.annotation.support.AbstractAnnotationConfigSupport;
|
||||||
|
import org.springframework.data.gemfire.util.CollectionUtils;
|
||||||
|
import org.springframework.data.gemfire.util.SpringUtils;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link CompressionConfiguration} class is a Spring {@link ImportAware} implementation capable of
|
||||||
|
* enabling Pivotal GemFire/Apache Geode cache {@link Region Regions} data compression.
|
||||||
|
*
|
||||||
|
* @author John Blum
|
||||||
|
* @see org.apache.geode.cache.Region
|
||||||
|
* @see org.springframework.beans.factory.config.BeanFactoryPostProcessor
|
||||||
|
* @see org.springframework.context.annotation.Bean
|
||||||
|
* @see org.springframework.context.annotation.Configuration
|
||||||
|
* @see org.springframework.context.annotation.ImportAware
|
||||||
|
* @see org.springframework.data.gemfire.config.annotation.EnableCompression
|
||||||
|
* @see org.springframework.data.gemfire.config.annotation.support.AbstractAnnotationConfigSupport
|
||||||
|
* @since 2.0.2
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class CompressionConfiguration extends AbstractAnnotationConfigSupport implements ImportAware {
|
||||||
|
|
||||||
|
protected static final String SNAPPY_COMPRESSOR_BEAN_NAME = "SnappyCompressor";
|
||||||
|
|
||||||
|
private String compressorBeanName = SNAPPY_COMPRESSOR_BEAN_NAME;
|
||||||
|
|
||||||
|
private Set<String> regionNames = new HashSet<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the {@link EnableCompression} {@link Annotation} {@link Class} type.
|
||||||
|
*
|
||||||
|
* @return the {@link EnableCompression} {@link Annotation} {@link Class} type.
|
||||||
|
* @see org.springframework.data.gemfire.config.annotation.EnableOffHeap
|
||||||
|
* @see java.lang.annotation.Annotation
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected Class<? extends Annotation> getAnnotationType() {
|
||||||
|
return EnableCompression.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCompressorBeanName(String compressorBeanName) {
|
||||||
|
this.compressorBeanName = compressorBeanName;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String resolveCompressorBeanName() {
|
||||||
|
return Optional.ofNullable(this.compressorBeanName).filter(StringUtils::hasText)
|
||||||
|
.orElse(SNAPPY_COMPRESSOR_BEAN_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRegionNames(String[] regionNames) {
|
||||||
|
setRegionNames(asSet(nullSafeArray(regionNames, String.class)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRegionNames(Iterable<String> regionNames) {
|
||||||
|
this.regionNames = CollectionUtils.addAll(this.regionNames, nullSafeIterable(regionNames));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Set<String> resolveRegionNames() {
|
||||||
|
return Collections.unmodifiableSet(this.regionNames);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setImportMetadata(AnnotationMetadata importingClassMetadata) {
|
||||||
|
|
||||||
|
if (isAnnotationPresent(importingClassMetadata)) {
|
||||||
|
|
||||||
|
AnnotationAttributes enableCompressionAttributes = getAnnotationAttributes(importingClassMetadata);
|
||||||
|
|
||||||
|
setCompressorBeanName(resolveProperty(cacheCompressionProperty("compressor-bean-name"),
|
||||||
|
enableCompressionAttributes.getString("compressorBeanName")));
|
||||||
|
|
||||||
|
setRegionNames(resolveProperty(cacheCompressionProperty("region-names"),
|
||||||
|
String[].class, enableCompressionAttributes.getStringArray("regionNames")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(SNAPPY_COMPRESSOR_BEAN_NAME)
|
||||||
|
Compressor snappyCompressor() {
|
||||||
|
return new SnappyCompressor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
BeanFactoryPostProcessor regionCompressionBeanFactoryPostProcessor() {
|
||||||
|
|
||||||
|
String resolvedCompressorBeanName = resolveCompressorBeanName();
|
||||||
|
|
||||||
|
return beanFactory ->
|
||||||
|
stream(nullSafeArray(beanFactory.getBeanDefinitionNames(), String.class)).forEach(beanName ->
|
||||||
|
Optional.of(beanFactory.getBeanDefinition(beanName))
|
||||||
|
.filter(beanDefinition -> isTargetedRegionBean(beanName, beanDefinition, beanFactory))
|
||||||
|
.ifPresent(beanDefinition -> SpringUtils.setPropertyReference(
|
||||||
|
beanDefinition, "compressor", resolvedCompressorBeanName)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isTargetedRegionBean(String beanName, BeanDefinition beanDefinition,
|
||||||
|
ConfigurableListableBeanFactory beanFactory) {
|
||||||
|
|
||||||
|
return isNamedRegion(beanName, beanDefinition, beanFactory) && isRegionBean(beanDefinition, beanFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isRegionBean(BeanDefinition beanDefinition, ConfigurableListableBeanFactory beanFactory) {
|
||||||
|
|
||||||
|
return Optional.ofNullable(beanDefinition)
|
||||||
|
.flatMap(it -> resolveBeanClass(it, beanFactory.getBeanClassLoader()))
|
||||||
|
.filter(beanClass -> RegionLookupFactoryBean.class.isAssignableFrom(beanClass))
|
||||||
|
.isPresent();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isNamedRegion(String beanName, BeanDefinition beanDefinition,
|
||||||
|
ConfigurableListableBeanFactory beanFactory) {
|
||||||
|
|
||||||
|
Set<String> resolvedRegionNames = resolveRegionNames();
|
||||||
|
|
||||||
|
return CollectionUtils.isEmpty(resolvedRegionNames)
|
||||||
|
|| CollectionUtils.containsAny(resolvedRegionNames, resolveBeanNames(beanName, beanDefinition, beanFactory));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Collection<String> resolveBeanNames(String beanName, BeanDefinition beanDefinition,
|
||||||
|
ConfigurableListableBeanFactory beanFactory) {
|
||||||
|
|
||||||
|
Collection<String> beanNames = new HashSet<>();
|
||||||
|
|
||||||
|
beanNames.add(beanName);
|
||||||
|
|
||||||
|
Collections.addAll(beanNames, beanFactory.getAliases(beanName));
|
||||||
|
|
||||||
|
PropertyValue regionName = beanDefinition.getPropertyValues().getPropertyValue("regionName");
|
||||||
|
|
||||||
|
if (regionName != null) {
|
||||||
|
|
||||||
|
Object regionNameValue = regionName.getValue();
|
||||||
|
|
||||||
|
if (regionNameValue != null) {
|
||||||
|
beanNames.add(regionNameValue.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return beanNames;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* 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.springframework.data.gemfire.config.annotation.CompressionConfiguration.SNAPPY_COMPRESSOR_BEAN_NAME;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Inherited;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import org.apache.geode.cache.Region;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link EnableCompression} annotation marks a Spring {@link Configuration @Configuration} annotated application
|
||||||
|
* {@link Class} to configure and enable Pivotal GemFire/Apache Geode {@link Region} data compression.
|
||||||
|
*
|
||||||
|
* @author John Blum
|
||||||
|
* @see java.lang.annotation.Annotation
|
||||||
|
* @see org.springframework.context.annotation.Import
|
||||||
|
* @see org.springframework.data.gemfire.config.annotation.CompressionConfiguration
|
||||||
|
*/
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Inherited
|
||||||
|
@Documented
|
||||||
|
@Import(CompressionConfiguration.class)
|
||||||
|
public @interface EnableCompression {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reference to the {@link String name} of a bean having type {@link org.apache.geode.compression.Compressor}
|
||||||
|
* registered in the Spring container to handle {@link Region} compression.
|
||||||
|
*
|
||||||
|
* Defaults to {@literal snappyCompressor}.
|
||||||
|
*
|
||||||
|
* Set the {@literal spring.data.gemfire.cache.compression.compressor-bean-name}
|
||||||
|
* in {@literal application.properties}.
|
||||||
|
*/
|
||||||
|
String compressorBeanName() default SNAPPY_COMPRESSOR_BEAN_NAME;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identifies all the {@link Region Regions} by name in which the data compression will be enabled.
|
||||||
|
*
|
||||||
|
* Defaults to all {@link Region Regions}.
|
||||||
|
*
|
||||||
|
* Set the {@literal spring.data.gemfire.cache.compression.region-names} property
|
||||||
|
* in {@literal application.properties}.
|
||||||
|
*/
|
||||||
|
String[] regionNames() default {};
|
||||||
|
|
||||||
|
}
|
||||||
@@ -32,7 +32,6 @@ import java.util.Set;
|
|||||||
import org.apache.geode.cache.Region;
|
import org.apache.geode.cache.Region;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.PropertyValue;
|
import org.springframework.beans.PropertyValue;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
|
||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
@@ -48,11 +47,15 @@ import org.springframework.data.gemfire.util.PropertiesBuilder;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link OffHeapConfiguration} class is a Spring {@link ImportBeanDefinitionRegistrar} capable of
|
* The {@link OffHeapConfiguration} class is a Spring {@link ImportBeanDefinitionRegistrar} capable of
|
||||||
* enabling Pivotal GemFire/Apache Geode cache {@link Region Regions} to use Off-Heap memory for data storage.
|
* enabling Pivotal GemFire/Apache Geode cache {@link Region Regions} to use Off-Heap Memory for data storage.
|
||||||
*
|
*
|
||||||
* @author John Blum
|
* @author John Blum
|
||||||
|
* @see java.util.Properties
|
||||||
|
* @see org.apache.geode.cache.Region
|
||||||
|
* @see org.springframework.beans.factory.config.BeanFactoryPostProcessor
|
||||||
* @see org.springframework.context.annotation.ImportBeanDefinitionRegistrar
|
* @see org.springframework.context.annotation.ImportBeanDefinitionRegistrar
|
||||||
* @see org.springframework.data.gemfire.config.annotation.EnableOffHeap
|
* @see org.springframework.data.gemfire.config.annotation.EnableOffHeap
|
||||||
|
* @see org.springframework.data.gemfire.config.annotation.support.AbstractAnnotationConfigSupport
|
||||||
* @see org.springframework.data.gemfire.config.annotation.support.EmbeddedServiceConfigurationSupport
|
* @see org.springframework.data.gemfire.config.annotation.support.EmbeddedServiceConfigurationSupport
|
||||||
* @since 1.9.0
|
* @since 1.9.0
|
||||||
*/
|
*/
|
||||||
@@ -121,13 +124,13 @@ public class OffHeapConfiguration extends EmbeddedServiceConfigurationSupport {
|
|||||||
.addPropertyValue("offHeap", true)));
|
.addPropertyValue("offHeap", true)));
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isTargetedRegionBean(String beanName, BeanDefinition beanDefinition,
|
private boolean isTargetedRegionBean(String beanName, BeanDefinition beanDefinition,
|
||||||
ConfigurableListableBeanFactory beanFactory) {
|
ConfigurableListableBeanFactory beanFactory) {
|
||||||
|
|
||||||
return isNamedRegion(beanName, beanDefinition, beanFactory) && isRegionBean(beanDefinition, beanFactory);
|
return isNamedRegion(beanName, beanDefinition, beanFactory) && isRegionBean(beanDefinition, beanFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isRegionBean(BeanDefinition beanDefinition, ConfigurableListableBeanFactory beanFactory) {
|
private boolean isRegionBean(BeanDefinition beanDefinition, ConfigurableListableBeanFactory beanFactory) {
|
||||||
|
|
||||||
return Optional.ofNullable(beanDefinition)
|
return Optional.ofNullable(beanDefinition)
|
||||||
.flatMap(it -> resolveBeanClass(it, beanFactory.getBeanClassLoader()))
|
.flatMap(it -> resolveBeanClass(it, beanFactory.getBeanClassLoader()))
|
||||||
@@ -135,13 +138,15 @@ public class OffHeapConfiguration extends EmbeddedServiceConfigurationSupport {
|
|||||||
.isPresent();
|
.isPresent();
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isNamedRegion(String beanName, BeanDefinition beanDefinition, BeanFactory beanFactory) {
|
private boolean isNamedRegion(String beanName, BeanDefinition beanDefinition,
|
||||||
|
ConfigurableListableBeanFactory beanFactory) {
|
||||||
|
|
||||||
return CollectionUtils.isEmpty(regionNames)
|
return CollectionUtils.isEmpty(regionNames)
|
||||||
|| CollectionUtils.containsAny(regionNames, getBeanNames(beanName, beanDefinition, beanFactory));
|
|| CollectionUtils.containsAny(regionNames, resolveBeanNames(beanName, beanDefinition, beanFactory));
|
||||||
}
|
}
|
||||||
|
|
||||||
Collection<String> getBeanNames(String beanName, BeanDefinition beanDefinition, BeanFactory beanFactory) {
|
private Collection<String> resolveBeanNames(String beanName, BeanDefinition beanDefinition,
|
||||||
|
ConfigurableListableBeanFactory beanFactory) {
|
||||||
|
|
||||||
Collection<String> beanNames = new HashSet<>();
|
Collection<String> beanNames = new HashSet<>();
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import org.apache.geode.cache.RegionShortcut;
|
|||||||
import org.apache.geode.cache.Scope;
|
import org.apache.geode.cache.Scope;
|
||||||
import org.apache.geode.cache.client.ClientCache;
|
import org.apache.geode.cache.client.ClientCache;
|
||||||
import org.apache.geode.cache.client.ClientRegionShortcut;
|
import org.apache.geode.cache.client.ClientRegionShortcut;
|
||||||
|
import org.apache.geode.compression.Compressor;
|
||||||
import org.springframework.beans.factory.FactoryBean;
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
import org.springframework.data.gemfire.GemfireUtils;
|
import org.springframework.data.gemfire.GemfireUtils;
|
||||||
import org.springframework.data.gemfire.GenericRegionFactoryBean;
|
import org.springframework.data.gemfire.GenericRegionFactoryBean;
|
||||||
@@ -61,6 +62,7 @@ import org.springframework.util.StringUtils;
|
|||||||
* @see org.apache.geode.cache.Scope
|
* @see org.apache.geode.cache.Scope
|
||||||
* @see org.apache.geode.cache.client.ClientCache
|
* @see org.apache.geode.cache.client.ClientCache
|
||||||
* @see org.apache.geode.cache.client.ClientRegionShortcut
|
* @see org.apache.geode.cache.client.ClientRegionShortcut
|
||||||
|
* @see org.apache.geode.compression.Compressor
|
||||||
* @see org.springframework.data.gemfire.GenericRegionFactoryBean
|
* @see org.springframework.data.gemfire.GenericRegionFactoryBean
|
||||||
* @see org.springframework.data.gemfire.LocalRegionFactoryBean
|
* @see org.springframework.data.gemfire.LocalRegionFactoryBean
|
||||||
* @see org.springframework.data.gemfire.PartitionedRegionFactoryBean
|
* @see org.springframework.data.gemfire.PartitionedRegionFactoryBean
|
||||||
@@ -84,6 +86,8 @@ public class CacheTypeAwareRegionFactoryBean<K, V> extends RegionLookupFactoryBe
|
|||||||
|
|
||||||
private ClientRegionShortcut clientRegionShortcut = ClientRegionShortcut.PROXY;
|
private ClientRegionShortcut clientRegionShortcut = ClientRegionShortcut.PROXY;
|
||||||
|
|
||||||
|
private Compressor compressor;
|
||||||
|
|
||||||
private DataPolicy dataPolicy = DataPolicy.DEFAULT;
|
private DataPolicy dataPolicy = DataPolicy.DEFAULT;
|
||||||
|
|
||||||
private List<RegionConfigurer> regionConfigurers = Collections.emptyList();
|
private List<RegionConfigurer> regionConfigurers = Collections.emptyList();
|
||||||
@@ -129,6 +133,7 @@ public class CacheTypeAwareRegionFactoryBean<K, V> extends RegionLookupFactoryBe
|
|||||||
clientRegionFactory.setBeanFactory(getBeanFactory());
|
clientRegionFactory.setBeanFactory(getBeanFactory());
|
||||||
clientRegionFactory.setCache(gemfireCache);
|
clientRegionFactory.setCache(gemfireCache);
|
||||||
clientRegionFactory.setClose(isClose());
|
clientRegionFactory.setClose(isClose());
|
||||||
|
clientRegionFactory.setCompressor(getCompressor());
|
||||||
clientRegionFactory.setDiskStoreName(getDiskStoreName());
|
clientRegionFactory.setDiskStoreName(getDiskStoreName());
|
||||||
clientRegionFactory.setKeyConstraint(getKeyConstraint());
|
clientRegionFactory.setKeyConstraint(getKeyConstraint());
|
||||||
clientRegionFactory.setLookupEnabled(getLookupEnabled());
|
clientRegionFactory.setLookupEnabled(getLookupEnabled());
|
||||||
@@ -178,6 +183,7 @@ public class CacheTypeAwareRegionFactoryBean<K, V> extends RegionLookupFactoryBe
|
|||||||
serverRegionFactory.setBeanFactory(getBeanFactory());
|
serverRegionFactory.setBeanFactory(getBeanFactory());
|
||||||
serverRegionFactory.setCache(gemfireCache);
|
serverRegionFactory.setCache(gemfireCache);
|
||||||
serverRegionFactory.setClose(isClose());
|
serverRegionFactory.setClose(isClose());
|
||||||
|
serverRegionFactory.setCompressor(getCompressor());
|
||||||
serverRegionFactory.setDataPolicy(getDataPolicy());
|
serverRegionFactory.setDataPolicy(getDataPolicy());
|
||||||
serverRegionFactory.setDiskStoreName(getDiskStoreName());
|
serverRegionFactory.setDiskStoreName(getDiskStoreName());
|
||||||
serverRegionFactory.setKeyConstraint(getKeyConstraint());
|
serverRegionFactory.setKeyConstraint(getKeyConstraint());
|
||||||
@@ -254,6 +260,26 @@ public class CacheTypeAwareRegionFactoryBean<K, V> extends RegionLookupFactoryBe
|
|||||||
return Boolean.TRUE.equals(getClose());
|
return Boolean.TRUE.equals(getClose());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configures the {@link Compressor} used to compress the this {@link Region Region's} data.
|
||||||
|
*
|
||||||
|
* @param compressor {@link Compressor} used to compress the this {@link Region Region's} data.
|
||||||
|
* @see org.apache.geode.compression.Compressor
|
||||||
|
*/
|
||||||
|
public void setCompressor(Compressor compressor) {
|
||||||
|
this.compressor = compressor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the configured {@link Compressor} used to compress the this {@link Region Region's} data.
|
||||||
|
*
|
||||||
|
* @return the configured {@link Compressor} used to compress the this {@link Region Region's} data.
|
||||||
|
* @see org.apache.geode.compression.Compressor
|
||||||
|
*/
|
||||||
|
protected Compressor getCompressor() {
|
||||||
|
return this.compressor;
|
||||||
|
}
|
||||||
|
|
||||||
public void setDataPolicy(DataPolicy dataPolicy) {
|
public void setDataPolicy(DataPolicy dataPolicy) {
|
||||||
this.dataPolicy = dataPolicy;
|
this.dataPolicy = dataPolicy;
|
||||||
}
|
}
|
||||||
@@ -278,10 +304,21 @@ public class CacheTypeAwareRegionFactoryBean<K, V> extends RegionLookupFactoryBe
|
|||||||
return this.keyConstraint;
|
return this.keyConstraint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure the {@link Region} to manage data in Off-Heap Memory.
|
||||||
|
*
|
||||||
|
* @param offHeap boolean value indicating whether the {@link Region Region's} data
|
||||||
|
* will be managed in Off-Heap Memory.
|
||||||
|
*/
|
||||||
public void setOffHeap(Boolean offHeap) {
|
public void setOffHeap(Boolean offHeap) {
|
||||||
this.offHeap = offHeap;
|
this.offHeap = offHeap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the configuration setting for whether the {@link Region Region's} data will be managed in Off-Heap Memory.
|
||||||
|
*
|
||||||
|
* @return a boolean value indicating whether the {@link Region Region's} data will be managed in Off-Heap Memory.
|
||||||
|
*/
|
||||||
protected Boolean getOffHeap() {
|
protected Boolean getOffHeap() {
|
||||||
return this.offHeap;
|
return this.offHeap;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import java.util.function.Supplier;
|
|||||||
|
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
|
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -53,6 +54,24 @@ public abstract class SpringUtils {
|
|||||||
return bean;
|
return bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc) */
|
||||||
|
public static BeanDefinition setPropertyReference(BeanDefinition beanDefinition,
|
||||||
|
String propertyName, String beanName) {
|
||||||
|
|
||||||
|
beanDefinition.getPropertyValues().addPropertyValue(propertyName, new RuntimeBeanReference(beanName));
|
||||||
|
|
||||||
|
return beanDefinition;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc) */
|
||||||
|
public static BeanDefinition setPropertyValue(BeanDefinition beanDefinition,
|
||||||
|
String propertyName, Object propertyValue) {
|
||||||
|
|
||||||
|
beanDefinition.getPropertyValues().addPropertyValue(propertyName, propertyValue);
|
||||||
|
|
||||||
|
return beanDefinition;
|
||||||
|
}
|
||||||
|
|
||||||
/* (non-Javadoc) */
|
/* (non-Javadoc) */
|
||||||
public static String defaultIfEmpty(String value, String defaultValue) {
|
public static String defaultIfEmpty(String value, String defaultValue) {
|
||||||
return (StringUtils.hasText(value) ? value : defaultValue);
|
return (StringUtils.hasText(value) ? value : defaultValue);
|
||||||
|
|||||||
@@ -0,0 +1,178 @@
|
|||||||
|
/*
|
||||||
|
* 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 static org.mockito.Mockito.mock;
|
||||||
|
import static org.springframework.data.gemfire.config.annotation.CompressionConfiguration.SNAPPY_COMPRESSOR_BEAN_NAME;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.apache.geode.cache.GemFireCache;
|
||||||
|
import org.apache.geode.cache.Region;
|
||||||
|
import org.apache.geode.cache.client.ClientRegionShortcut;
|
||||||
|
import org.apache.geode.compression.Compressor;
|
||||||
|
import org.apache.geode.compression.SnappyCompressor;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.data.gemfire.GemfireUtils;
|
||||||
|
import org.springframework.data.gemfire.LocalRegionFactoryBean;
|
||||||
|
import org.springframework.data.gemfire.PartitionedRegionFactoryBean;
|
||||||
|
import org.springframework.data.gemfire.ReplicatedRegionFactoryBean;
|
||||||
|
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
|
||||||
|
import org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects;
|
||||||
|
import org.springframework.data.gemfire.test.model.Person;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The EnableCompressionConfigurationUnitTests class...
|
||||||
|
*
|
||||||
|
* @author John Blum
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public class EnableCompressionConfigurationUnitTests {
|
||||||
|
|
||||||
|
private ConfigurableApplicationContext applicationContext;
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
Optional.ofNullable(this.applicationContext).ifPresent(ConfigurableApplicationContext::close);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ConfigurableApplicationContext newApplicationContext(Class<?>... annotatedClasses) {
|
||||||
|
return new AnnotationConfigApplicationContext(annotatedClasses);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertRegionCompressor(Region<?, ?> region, String regionName, Compressor compressor) {
|
||||||
|
|
||||||
|
assertThat(region).isNotNull();
|
||||||
|
assertThat(region.getName()).isEqualTo(regionName);
|
||||||
|
assertThat(region.getFullPath()).isEqualTo(GemfireUtils.toRegionPath(regionName));
|
||||||
|
assertThat(region.getAttributes()).isNotNull();
|
||||||
|
assertThat(region.getAttributes().getCompressor()).isEqualTo(compressor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void enableCompressionForAllRegions() {
|
||||||
|
|
||||||
|
this.applicationContext = newApplicationContext(EnableCompressionForAllRegionsConfiguration.class);
|
||||||
|
|
||||||
|
assertThat(this.applicationContext).isNotNull();
|
||||||
|
|
||||||
|
Compressor compressor = this.applicationContext.getBean(Compressor.class);
|
||||||
|
|
||||||
|
assertThat(compressor).isInstanceOf(SnappyCompressor.class);
|
||||||
|
|
||||||
|
Arrays.asList("People", "ExampleLocalRegion", "ExamplePartitionRegion", "ExampleReplicateRegion")
|
||||||
|
.forEach(regionName -> {
|
||||||
|
assertThat(this.applicationContext.containsBean(regionName)).isTrue();
|
||||||
|
assertRegionCompressor(this.applicationContext.getBean(regionName, Region.class),
|
||||||
|
regionName, compressor);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void enableCompressionForSelectRegions() {
|
||||||
|
|
||||||
|
this.applicationContext = newApplicationContext(EnableCompressionForSelectRegionsConfiguration.class);
|
||||||
|
|
||||||
|
assertThat(this.applicationContext).isNotNull();
|
||||||
|
|
||||||
|
Compressor compressor = this.applicationContext.getBean("MockCompressor", Compressor.class);
|
||||||
|
|
||||||
|
assertThat(compressor).isNotNull();
|
||||||
|
assertThat(this.applicationContext.containsBean(SNAPPY_COMPRESSOR_BEAN_NAME)).isTrue();
|
||||||
|
|
||||||
|
Arrays.asList("People", "ExampleClientRegion").forEach(regionName -> {
|
||||||
|
assertThat(this.applicationContext.containsBean(regionName)).isTrue();
|
||||||
|
assertRegionCompressor(this.applicationContext.getBean(regionName, Region.class),
|
||||||
|
regionName, "People".equals(regionName) ? compressor : null);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@PeerCacheApplication
|
||||||
|
@EnableGemFireMockObjects
|
||||||
|
@EnableEntityDefinedRegions(basePackageClasses = Person.class)
|
||||||
|
@EnableCompression
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
static class EnableCompressionForAllRegionsConfiguration {
|
||||||
|
|
||||||
|
@Bean("ExampleLocalRegion")
|
||||||
|
public LocalRegionFactoryBean<Object, Object> localRegion(GemFireCache gemfireCache) {
|
||||||
|
|
||||||
|
LocalRegionFactoryBean<Object, Object> localRegion = new LocalRegionFactoryBean<>();
|
||||||
|
|
||||||
|
localRegion.setCache(gemfireCache);
|
||||||
|
localRegion.setClose(false);
|
||||||
|
localRegion.setPersistent(false);
|
||||||
|
|
||||||
|
return localRegion;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("ExamplePartitionRegion")
|
||||||
|
public PartitionedRegionFactoryBean<Object, Object> partitionRegion(GemFireCache gemfireCache) {
|
||||||
|
|
||||||
|
PartitionedRegionFactoryBean<Object, Object> partitionRegion = new PartitionedRegionFactoryBean<>();
|
||||||
|
|
||||||
|
partitionRegion.setCache(gemfireCache);
|
||||||
|
partitionRegion.setClose(false);
|
||||||
|
partitionRegion.setPersistent(false);
|
||||||
|
|
||||||
|
return partitionRegion;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("ExampleReplicateRegion")
|
||||||
|
public ReplicatedRegionFactoryBean<Object, Object> replicateRegion(GemFireCache gemfireCache) {
|
||||||
|
|
||||||
|
ReplicatedRegionFactoryBean<Object, Object> replicateRegion = new ReplicatedRegionFactoryBean<>();
|
||||||
|
|
||||||
|
replicateRegion.setCache(gemfireCache);
|
||||||
|
replicateRegion.setClose(false);
|
||||||
|
replicateRegion.setPersistent(false);
|
||||||
|
|
||||||
|
return replicateRegion;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ClientCacheApplication
|
||||||
|
@EnableGemFireMockObjects
|
||||||
|
@EnableEntityDefinedRegions(basePackageClasses = Person.class)
|
||||||
|
@EnableCompression(compressorBeanName = "MockCompressor", regionNames = "People")
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
static class EnableCompressionForSelectRegionsConfiguration {
|
||||||
|
|
||||||
|
@Bean("ExampleClientRegion")
|
||||||
|
public ClientRegionFactoryBean<Object, Object> clientRegion(GemFireCache gemfireCache) {
|
||||||
|
|
||||||
|
ClientRegionFactoryBean<Object, Object> clientRegion = new ClientRegionFactoryBean<>();
|
||||||
|
|
||||||
|
clientRegion.setCache(gemfireCache);
|
||||||
|
clientRegion.setClose(false);
|
||||||
|
clientRegion.setShortcut(ClientRegionShortcut.LOCAL);
|
||||||
|
|
||||||
|
return clientRegion;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("MockCompressor")
|
||||||
|
Compressor mockCompressor() {
|
||||||
|
return mock(Compressor.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -256,7 +256,7 @@ public class EnableGemFirePropertiesIntegrationTests {
|
|||||||
public void offHeapGemFirePropertiesConfiguration() {
|
public void offHeapGemFirePropertiesConfiguration() {
|
||||||
|
|
||||||
MockPropertySource testPropertySource = new MockPropertySource()
|
MockPropertySource testPropertySource = new MockPropertySource()
|
||||||
.withProperty("spring.data.gemfire.cache.off-heap-memory-size", "1024g");
|
.withProperty("spring.data.gemfire.cache.off-heap.memory-size", "1024g");
|
||||||
|
|
||||||
this.applicationContext = newApplicationContext(testPropertySource, TestOffHeapGemFirePropertiesConfiguration.class);
|
this.applicationContext = newApplicationContext(testPropertySource, TestOffHeapGemFirePropertiesConfiguration.class);
|
||||||
|
|
||||||
|
|||||||
@@ -370,7 +370,8 @@ public class AbstractAnnotationConfigSupportTests {
|
|||||||
when(mockBeanDefinition.resolveBeanClass(any(ClassLoader.class)))
|
when(mockBeanDefinition.resolveBeanClass(any(ClassLoader.class)))
|
||||||
.thenThrow(new ClassNotFoundException("Class [non.existing.bean.Class] not found"));
|
.thenThrow(new ClassNotFoundException("Class [non.existing.bean.Class] not found"));
|
||||||
|
|
||||||
assertThat(this.support.resolveBeanClass(mockBeanDefinition, null).orElse(null)).isNull();
|
assertThat(this.support.resolveBeanClass(mockBeanDefinition, (BeanDefinitionRegistry) null)
|
||||||
|
.orElse(null)).isNull();
|
||||||
|
|
||||||
verify(mockBeanDefinition, times(1)).getBeanClassName();
|
verify(mockBeanDefinition, times(1)).getBeanClassName();
|
||||||
verify(mockBeanDefinition, never()).getFactoryMethodName();
|
verify(mockBeanDefinition, never()).getFactoryMethodName();
|
||||||
|
|||||||
@@ -34,7 +34,9 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.junit.MockitoJUnitRunner;
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
import org.springframework.beans.MutablePropertyValues;
|
||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
|
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for {@link SpringUtils}.
|
* Unit tests for {@link SpringUtils}.
|
||||||
@@ -56,6 +58,7 @@ public class SpringUtilsUnitTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void addDependsOnToExistingDependencies() {
|
public void addDependsOnToExistingDependencies() {
|
||||||
|
|
||||||
when(mockBeanDefinition.getDependsOn()).thenReturn(asArray("testBeanNameOne", "testBeanNameTwo"));
|
when(mockBeanDefinition.getDependsOn()).thenReturn(asArray("testBeanNameOne", "testBeanNameTwo"));
|
||||||
|
|
||||||
assertThat(SpringUtils.addDependsOn(mockBeanDefinition, "testBeanNameThree")).isSameAs(mockBeanDefinition);
|
assertThat(SpringUtils.addDependsOn(mockBeanDefinition, "testBeanNameThree")).isSameAs(mockBeanDefinition);
|
||||||
@@ -67,6 +70,7 @@ public class SpringUtilsUnitTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void addDependsOnToNonExistingDependencies() {
|
public void addDependsOnToNonExistingDependencies() {
|
||||||
|
|
||||||
when(mockBeanDefinition.getDependsOn()).thenReturn(null);
|
when(mockBeanDefinition.getDependsOn()).thenReturn(null);
|
||||||
|
|
||||||
assertThat(SpringUtils.addDependsOn(mockBeanDefinition, "testBeanName")).isSameAs(mockBeanDefinition);
|
assertThat(SpringUtils.addDependsOn(mockBeanDefinition, "testBeanName")).isSameAs(mockBeanDefinition);
|
||||||
@@ -77,6 +81,7 @@ public class SpringUtilsUnitTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void addDependsOnWithMultipleDependenciesWithExistingDependencies() {
|
public void addDependsOnWithMultipleDependenciesWithExistingDependencies() {
|
||||||
|
|
||||||
when(mockBeanDefinition.getDependsOn()).thenReturn(asArray("testBeanNameOne", "testBeanNameTwo"));
|
when(mockBeanDefinition.getDependsOn()).thenReturn(asArray("testBeanNameOne", "testBeanNameTwo"));
|
||||||
|
|
||||||
assertThat(SpringUtils.addDependsOn(mockBeanDefinition, "testBeanNameThree", "testBeanNameFour"))
|
assertThat(SpringUtils.addDependsOn(mockBeanDefinition, "testBeanNameThree", "testBeanNameFour"))
|
||||||
@@ -87,6 +92,48 @@ public class SpringUtilsUnitTests {
|
|||||||
.setDependsOn("testBeanNameOne", "testBeanNameTwo", "testBeanNameThree", "testBeanNameFour");
|
.setDependsOn("testBeanNameOne", "testBeanNameTwo", "testBeanNameThree", "testBeanNameFour");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
public void setBeanDefinitionPropertyReference() {
|
||||||
|
|
||||||
|
BeanDefinition mockBeanDefinition = mock(BeanDefinition.class);
|
||||||
|
|
||||||
|
MutablePropertyValues mutablePropertyValues = new MutablePropertyValues();
|
||||||
|
|
||||||
|
when(mockBeanDefinition.getPropertyValues()).thenReturn(mutablePropertyValues);
|
||||||
|
|
||||||
|
assertThat(mutablePropertyValues.size()).isEqualTo(0);
|
||||||
|
|
||||||
|
SpringUtils.setPropertyReference(mockBeanDefinition, "testProperty", "testBean");
|
||||||
|
|
||||||
|
assertThat(mutablePropertyValues.size()).isEqualTo(1);
|
||||||
|
assertThat(mutablePropertyValues.getPropertyValue("testProperty")).isNotNull();
|
||||||
|
assertThat(mutablePropertyValues.getPropertyValue("testProperty").getValue())
|
||||||
|
.isInstanceOf(RuntimeBeanReference.class);
|
||||||
|
assertThat(((RuntimeBeanReference) mutablePropertyValues.getPropertyValue("testProperty").getValue()).getBeanName())
|
||||||
|
.isEqualTo("testBean");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
public void setBeanDefinitionPropertyValue() {
|
||||||
|
|
||||||
|
BeanDefinition mockBeanDefinition = mock(BeanDefinition.class);
|
||||||
|
|
||||||
|
MutablePropertyValues mutablePropertyValues = new MutablePropertyValues();
|
||||||
|
|
||||||
|
when(mockBeanDefinition.getPropertyValues()).thenReturn(mutablePropertyValues);
|
||||||
|
|
||||||
|
assertThat(mutablePropertyValues.size()).isEqualTo(0);
|
||||||
|
|
||||||
|
SpringUtils.setPropertyValue(mockBeanDefinition, "testProperty", "testValue");
|
||||||
|
|
||||||
|
assertThat(mutablePropertyValues.size()).isEqualTo(1);
|
||||||
|
assertThat(mutablePropertyValues.getPropertyValue("testProperty")).isNotNull();
|
||||||
|
assertThat(mutablePropertyValues.getPropertyValue("testProperty").getValue())
|
||||||
|
.isEqualTo("testValue");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void defaultIfEmptyReturnsValue() {
|
public void defaultIfEmptyReturnsValue() {
|
||||||
assertThat(SpringUtils.defaultIfEmpty("test", "DEFAULT")).isEqualTo("test");
|
assertThat(SpringUtils.defaultIfEmpty("test", "DEFAULT")).isEqualTo("test");
|
||||||
|
|||||||
Reference in New Issue
Block a user