SGF-689 - Add support for JSR-107 JCache API Annotations in Caching-defined Regions.
This commit is contained in:
@@ -26,8 +26,13 @@ import org.springframework.data.gemfire.cache.GemfireCacheManager;
|
||||
* The {@link GemfireCachingConfiguration} class is a Spring {@link Configuration @Configuration} class
|
||||
* used to configure Pivotal GemFire or Apache Geode as the caching provider in Spring's Cache Abstraction.
|
||||
*
|
||||
* Additionally, this Spring {@link Configuration} class also enables the Spring Cache Abstraction
|
||||
* with {@link EnableCaching}.
|
||||
* This {@link Configuration @Configuration} class is specifically responsible for declaring and registering
|
||||
* Spring Data GemFire/Geode's {@link GemfireCacheManager} implementation to properly enable either Pivotal GemFire
|
||||
* or Apache Geode as the caching provider used with Springs Cache Abstraction.
|
||||
*
|
||||
* Additionally, this Spring {@link Configuration @Configuration} class also enables the Spring Cache Abstraction
|
||||
* by declaring Spring's {@link EnableCaching} annotation for the user extending or importing this class using
|
||||
* the SDG provided {@link EnableGemfireCaching} annotation.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see org.apache.geode.cache.GemFireCache
|
||||
|
||||
@@ -16,14 +16,17 @@
|
||||
|
||||
package org.springframework.data.gemfire.config.annotation;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Arrays.stream;
|
||||
import static org.springframework.data.gemfire.util.ArrayUtils.asArray;
|
||||
import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray;
|
||||
import static org.springframework.data.gemfire.util.StreamUtils.concat;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@@ -32,6 +35,11 @@ import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.cache.annotation.CacheDefaults;
|
||||
import javax.cache.annotation.CacheRemove;
|
||||
import javax.cache.annotation.CacheRemoveAll;
|
||||
import javax.cache.annotation.CacheResult;
|
||||
|
||||
import org.apache.geode.cache.GemFireCache;
|
||||
import org.apache.geode.cache.Region;
|
||||
import org.apache.geode.cache.RegionShortcut;
|
||||
@@ -45,7 +53,6 @@ import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
@@ -105,21 +112,10 @@ import org.springframework.util.StringUtils;
|
||||
@Configuration
|
||||
public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfigSupport implements ImportAware {
|
||||
|
||||
private static final Class[] CLASS_CACHE_ANNOTATION_TYPES;
|
||||
|
||||
private static final Class[] METHOD_CACHE_ANNOTATION_TYPES =
|
||||
asArray(Cacheable.class, CacheEvict.class, CachePut.class);
|
||||
|
||||
static {
|
||||
|
||||
List<Class> annotationTypes = new ArrayList<>();
|
||||
|
||||
Collections.addAll(annotationTypes, METHOD_CACHE_ANNOTATION_TYPES);
|
||||
annotationTypes.add(CacheConfig.class);
|
||||
|
||||
CLASS_CACHE_ANNOTATION_TYPES = annotationTypes.toArray(new Class[annotationTypes.size()]);
|
||||
|
||||
}
|
||||
private final CacheNameResolver composableCacheNameResolver = type ->
|
||||
asList(new Jsr107CacheAnnotationsCacheNameResolver(), new SpringCacheAnnotationsCacheNameResolver()).stream()
|
||||
.flatMap(cacheNameResolver -> cacheNameResolver.resolveCacheNames(type).stream())
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
private ClientRegionShortcut clientRegionShortcut = ClientRegionShortcut.PROXY;
|
||||
|
||||
@@ -142,6 +138,17 @@ public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfig
|
||||
return EnableCachingDefinedRegions.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the configured {@link CacheNameResolver} to resolve all the declared cache name on Spring application
|
||||
* beans/components declared and registered in the Spring container (context).
|
||||
*
|
||||
* @return the configured {@link CacheNameResolver} to resolve all teh caches used by the Spring application.
|
||||
* @see org.springframework.data.gemfire.config.annotation.CachingDefinedRegionsConfiguration.CacheNameResolver
|
||||
*/
|
||||
protected CacheNameResolver getCacheNameResolver() {
|
||||
return this.composableCacheNameResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the {@link ClientRegionShortcut} specifying the data management policy to use
|
||||
* when creating a client {@link Region}.
|
||||
@@ -273,7 +280,7 @@ public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfig
|
||||
|
||||
@Bean
|
||||
@SuppressWarnings("all")
|
||||
public BeanDefinitionRegistryPostProcessor cacheAbstractionAnnotationsRegionBeanDefinitionRegistrar() {
|
||||
public BeanDefinitionRegistryPostProcessor cachingAnnotationsRegionBeanDefinitionRegistrar() {
|
||||
|
||||
return new BeanDefinitionRegistryPostProcessorSupport() {
|
||||
|
||||
@@ -284,24 +291,6 @@ public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfig
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
@SuppressWarnings("all")
|
||||
public BeanPostProcessor cacheAbstractionAnnotationsRegionBeanRegistrar(ConfigurableBeanFactory beanFactory) {
|
||||
|
||||
return new BeanPostProcessor() {
|
||||
|
||||
@Nullable @Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
|
||||
if (isNotInfrastructureBean(bean)) {
|
||||
registerRegionBeans(collectCacheNames(bean.getClass()), beanFactory);
|
||||
}
|
||||
|
||||
return bean;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void registerBeanDefinitions(BeanDefinitionRegistry registry) {
|
||||
|
||||
for (String beanName : registry.getBeanDefinitionNames()) {
|
||||
@@ -310,73 +299,11 @@ public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfig
|
||||
|
||||
if (isNotInfrastructureBean(beanDefinition)) {
|
||||
resolveBeanClass(beanDefinition, registry).ifPresent(beanClass ->
|
||||
registerRegionBeanDefinitions(collectCacheNames(beanClass), registry));
|
||||
registerRegionBeanDefinitions(getCacheNameResolver().resolveCacheNames(beanClass), registry));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Set<String> collectCacheNames(Class<?> type) {
|
||||
|
||||
Set<String> cacheNames = new HashSet<>();
|
||||
|
||||
cacheNames.addAll(collectCachingCacheNames(type));
|
||||
cacheNames.addAll(collectCacheNames(type, CLASS_CACHE_ANNOTATION_TYPES));
|
||||
|
||||
stream(type.getMethods()).forEach(method -> {
|
||||
if (isUserLevelMethod(method)) {
|
||||
cacheNames.addAll(collectCachingCacheNames(method));
|
||||
cacheNames.addAll(collectCacheNames(method, METHOD_CACHE_ANNOTATION_TYPES));
|
||||
}
|
||||
});
|
||||
|
||||
return cacheNames;
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
Set<String> collectCacheNames(AnnotatedElement annotatedElement,
|
||||
Class<? extends Annotation>... annotationTypes) {
|
||||
|
||||
Stream<String> cacheNames = stream(nullSafeArray(annotationTypes, Class.class))
|
||||
.map(annotationType -> resolveAnnotation(annotatedElement, annotationType))
|
||||
.flatMap(annotation -> collectCacheNames((Annotation) annotation).stream());
|
||||
|
||||
return cacheNames.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private Set<String> collectCacheNames(Annotation annotation) {
|
||||
|
||||
return Optional.ofNullable(annotation)
|
||||
.map(this::getAnnotationAttributes)
|
||||
.map(annotationAttributes -> annotationAttributes.getStringArray("cacheNames"))
|
||||
.map(CollectionUtils::asSet)
|
||||
.orElse(Collections.emptySet());
|
||||
}
|
||||
|
||||
private Set<String> collectCachingCacheNames(AnnotatedElement annotatedElement) {
|
||||
|
||||
Set<String> cacheNames = new HashSet<>();
|
||||
|
||||
Optional.ofNullable(resolveAnnotation(annotatedElement, Caching.class))
|
||||
.ifPresent(caching -> {
|
||||
|
||||
cacheNames.addAll(stream(nullSafeArray(caching.cacheable(), Cacheable.class))
|
||||
.flatMap(cacheable -> collectCacheNames(cacheable).stream())
|
||||
.collect(Collectors.toSet()));
|
||||
|
||||
cacheNames.addAll(stream(nullSafeArray(caching.evict(), CacheEvict.class))
|
||||
.flatMap(cacheEvict -> collectCacheNames(cacheEvict).stream())
|
||||
.collect(Collectors.toSet()));
|
||||
|
||||
cacheNames.addAll(stream(nullSafeArray(caching.put(), CachePut.class))
|
||||
.flatMap(cachePut -> collectCacheNames(cachePut).stream())
|
||||
.collect(Collectors.toSet()));
|
||||
|
||||
});
|
||||
|
||||
return cacheNames;
|
||||
}
|
||||
|
||||
private BeanDefinitionRegistry registerRegionBeanDefinitions(Set<String> cacheNames,
|
||||
BeanDefinitionRegistry registry) {
|
||||
|
||||
@@ -400,6 +327,24 @@ public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfig
|
||||
return registry;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@SuppressWarnings("all")
|
||||
public BeanPostProcessor cachingAnnotationsRegionBeanRegistrar(ConfigurableBeanFactory beanFactory) {
|
||||
|
||||
return new BeanPostProcessor() {
|
||||
|
||||
@Nullable @Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
|
||||
if (isNotInfrastructureBean(bean)) {
|
||||
registerRegionBeans(getCacheNameResolver().resolveCacheNames(bean.getClass()), beanFactory);
|
||||
}
|
||||
|
||||
return bean;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private ConfigurableBeanFactory registerRegionBeans(Set<String> cacheNames, ConfigurableBeanFactory beanFactory) {
|
||||
|
||||
cacheNames.forEach(cacheName -> {
|
||||
@@ -432,4 +377,148 @@ public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfig
|
||||
|
||||
return beanFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link CacheNameResolver} is a {@link FunctionalInterface} declaring a contract for all implementations
|
||||
* used to resolve all cache names declared and used by a Spring application. A resolver typically inspects
|
||||
* all the application beans/components declared and registered in the Spring container (context) setup by
|
||||
* the application to determine whether the application components require caching behavior.
|
||||
*
|
||||
* @see org.springframework.data.gemfire.config.annotation.CachingDefinedRegionsConfiguration.Jsr107CacheAnnotationsCacheNameResolver
|
||||
* @see org.springframework.data.gemfire.config.annotation.CachingDefinedRegionsConfiguration.SpringCacheAnnotationsCacheNameResolver
|
||||
*/
|
||||
@FunctionalInterface
|
||||
protected interface CacheNameResolver {
|
||||
Set<String> resolveCacheNames(Class<?> type);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link AbstractCacheNameResolver} is an abstract base class encapsulating reusable functionality common
|
||||
* to all {@link CacheNameResolver} implementations.
|
||||
*
|
||||
* Current implementations support inlude JSR-107, JCache API annotation and Spring's Cache Abstraction annotations.
|
||||
*
|
||||
* @see org.springframework.data.gemfire.config.annotation.CachingDefinedRegionsConfiguration.CacheNameResolver
|
||||
*/
|
||||
protected abstract class AbstractCacheNameResolver implements CacheNameResolver {
|
||||
|
||||
private final String JSR_107_CACHE_NAME_ATTRIBUTE_NAME = "cacheName";
|
||||
private final String SPRING_CACHE_NAMES_ATTRIBUTE_NAME = "cacheNames";
|
||||
|
||||
private final String[] EMPTY_ARRAY = new String[0];
|
||||
|
||||
protected abstract Class<? extends Annotation>[] getClassCacheAnnotationTypes();
|
||||
|
||||
protected abstract Class<? extends Annotation>[] getMethodCacheAnnotationTypes();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Class[] append(Class[] annotationTypes, Class... additionalAnnotationTypes) {
|
||||
|
||||
List<Class> annotationTypeList = new ArrayList<>(Arrays.asList(annotationTypes));
|
||||
|
||||
Collections.addAll(annotationTypeList, additionalAnnotationTypes);
|
||||
|
||||
return annotationTypeList.toArray(new Class[annotationTypeList.size()]);
|
||||
}
|
||||
|
||||
protected Set<String> resolveCacheNames(Annotation annotation) {
|
||||
|
||||
return Optional.ofNullable(annotation)
|
||||
.map(it -> getAnnotationAttributes(it))
|
||||
.map(annotationAttributes -> {
|
||||
|
||||
String attributeName = annotationAttributes.containsKey(SPRING_CACHE_NAMES_ATTRIBUTE_NAME)
|
||||
? SPRING_CACHE_NAMES_ATTRIBUTE_NAME : JSR_107_CACHE_NAME_ATTRIBUTE_NAME;
|
||||
|
||||
return annotationAttributes.containsKey(attributeName)
|
||||
? annotationAttributes.getStringArray(attributeName)
|
||||
: EMPTY_ARRAY;
|
||||
|
||||
})
|
||||
.map(CollectionUtils::asSet)
|
||||
.orElse(Collections.emptySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Set<String> resolveCacheNames(Class<?> type) {
|
||||
|
||||
Set<String> cacheNames = new HashSet<>();
|
||||
|
||||
cacheNames.addAll(resolveCacheNames(type, getClassCacheAnnotationTypes()));
|
||||
|
||||
stream(type.getMethods())
|
||||
.filter(method -> isUserLevelMethod(method))
|
||||
.forEach(method -> cacheNames.addAll(resolveCacheNames(method, getMethodCacheAnnotationTypes())));
|
||||
|
||||
return cacheNames;
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
protected Set<String> resolveCacheNames(AnnotatedElement annotatedElement,
|
||||
Class<? extends Annotation>... annotationTypes) {
|
||||
|
||||
Stream<String> cacheNames = stream(nullSafeArray(annotationTypes, Class.class))
|
||||
.map(annotationType -> resolveAnnotation(annotatedElement, annotationType))
|
||||
.flatMap(annotation -> resolveCacheNames((Annotation) annotation).stream());
|
||||
|
||||
return cacheNames.collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
|
||||
protected class Jsr107CacheAnnotationsCacheNameResolver extends AbstractCacheNameResolver {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Class<? extends Annotation>[] getClassCacheAnnotationTypes() {
|
||||
return append(getMethodCacheAnnotationTypes(), CacheDefaults.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<? extends Annotation>[] getMethodCacheAnnotationTypes() {
|
||||
return asArray(javax.cache.annotation.CachePut.class, CacheRemove.class,
|
||||
CacheRemoveAll.class, CacheResult.class);
|
||||
}
|
||||
}
|
||||
|
||||
protected class SpringCacheAnnotationsCacheNameResolver extends AbstractCacheNameResolver {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Class<? extends Annotation>[] getClassCacheAnnotationTypes() {
|
||||
return append(getMethodCacheAnnotationTypes(), Caching.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<? extends Annotation>[] getMethodCacheAnnotationTypes() {
|
||||
return asArray(Cacheable.class, CacheEvict.class, CachePut.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Set<String> resolveCacheNames(Class<?> type) {
|
||||
|
||||
Set<String> cacheNames = super.resolveCacheNames(type);
|
||||
|
||||
cacheNames.addAll(resolveCachingCacheNames(type));
|
||||
|
||||
stream(type.getMethods()).filter(method -> isUserLevelMethod(method))
|
||||
.forEach(method -> cacheNames.addAll(resolveCachingCacheNames(method)));
|
||||
|
||||
return cacheNames;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Set<String> resolveCachingCacheNames(AnnotatedElement annotatedElement) {
|
||||
|
||||
Set<String> cacheNames = new HashSet<>();
|
||||
|
||||
Optional.ofNullable(resolveAnnotation(annotatedElement, Caching.class)).ifPresent(caching ->
|
||||
concat(stream(caching.cacheable()), stream(caching.evict()), stream(caching.put()))
|
||||
.flatMap(cacheAnnotation -> resolveCacheNames(cacheAnnotation).stream())
|
||||
.collect(Collectors.toCollection(() -> cacheNames)));
|
||||
|
||||
return cacheNames;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ public @interface EnableLocator {
|
||||
*
|
||||
* Defaults to {@literal 10334}.
|
||||
*
|
||||
* Use the {@literal spring.data.gemfire.locator.host} property in {@literal application.properties}.
|
||||
* Use the {@literal spring.data.gemfire.locator.port} property in {@literal application.properties}.
|
||||
*/
|
||||
int port() default LocatorConfiguration.DEFAULT_LOCATOR_PORT;
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ public abstract class ArrayUtils {
|
||||
* @return the given {@code array} if not {@literal null} or empty otherwise return the {@code defaultArray}.
|
||||
*/
|
||||
public static <T> T[] defaultIfEmpty(T[] array, T[] defaultArray) {
|
||||
return (!ObjectUtils.isEmpty(array) ? array : defaultArray);
|
||||
return !ObjectUtils.isEmpty(array) ? array : defaultArray;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,7 +76,7 @@ public abstract class ArrayUtils {
|
||||
* @see #getFirst(Object[], Object)
|
||||
*/
|
||||
public static <T> T getFirst(T[] array, T defaultValue) {
|
||||
return (isEmpty(array) ? defaultValue : array[0]);
|
||||
return isEmpty(array) ? defaultValue : array[0];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,8 +91,9 @@ public abstract class ArrayUtils {
|
||||
* @see java.lang.reflect.Array#newInstance(Class, int)
|
||||
*/
|
||||
public static Object[] insert(Object[] originalArray, int position, Object element) {
|
||||
Object[] newArray = (Object[]) Array.newInstance(originalArray.getClass().getComponentType(),
|
||||
originalArray.length + 1);
|
||||
|
||||
Object[] newArray =
|
||||
(Object[]) Array.newInstance(originalArray.getClass().getComponentType(), originalArray.length + 1);
|
||||
|
||||
|
||||
// copy all elements before the given position (here, position refers to the length, or number of elements
|
||||
@@ -106,7 +107,8 @@ public abstract class ArrayUtils {
|
||||
|
||||
// copy remaining elements from originalArray, starting at position, to new array
|
||||
if (position < originalArray.length) {
|
||||
System.arraycopy(originalArray, position, newArray, position + 1, originalArray.length - position);
|
||||
System.arraycopy(originalArray, position, newArray, position + 1,
|
||||
originalArray.length - position);
|
||||
}
|
||||
|
||||
return newArray;
|
||||
@@ -120,7 +122,7 @@ public abstract class ArrayUtils {
|
||||
* @see #length(Object...)
|
||||
*/
|
||||
public static boolean isEmpty(Object[] array) {
|
||||
return (length(array) == 0);
|
||||
return length(array) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,7 +132,7 @@ public abstract class ArrayUtils {
|
||||
* @return the length of the given array or 0 if the array reference is null.
|
||||
*/
|
||||
public static int length(Object[] array) {
|
||||
return (array != null ? array.length : 0);
|
||||
return array != null ? array.length : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -145,7 +147,7 @@ public abstract class ArrayUtils {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T[] nullSafeArray(T[] array, Class<T> componentType) {
|
||||
return (array != null ? array : (T[]) Array.newInstance(componentType, 0));
|
||||
return array != null ? array : (T[]) Array.newInstance(componentType, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,8 +161,9 @@ public abstract class ArrayUtils {
|
||||
* @see java.lang.reflect.Array#newInstance(Class, int)
|
||||
*/
|
||||
public static Object[] remove(Object[] originalArray, int position) {
|
||||
Object[] newArray = (Object[]) Array.newInstance(originalArray.getClass().getComponentType(),
|
||||
originalArray.length - 1);
|
||||
|
||||
Object[] newArray =
|
||||
(Object[]) Array.newInstance(originalArray.getClass().getComponentType(), originalArray.length - 1);
|
||||
|
||||
// copy all elements before position (here, position refers to the length, or number of elements to be copied
|
||||
if (position > 0) {
|
||||
@@ -169,7 +172,8 @@ public abstract class ArrayUtils {
|
||||
|
||||
// copy remaining elements after position from the originalArray
|
||||
if (position < originalArray.length - 1) {
|
||||
System.arraycopy(originalArray, position + 1, newArray, position, originalArray.length - 1 - position);
|
||||
System.arraycopy(originalArray, position + 1, newArray, position,
|
||||
originalArray.length - 1 - position);
|
||||
}
|
||||
|
||||
return newArray;
|
||||
@@ -184,7 +188,9 @@ public abstract class ArrayUtils {
|
||||
* @see java.util.Arrays#sort(Object[])
|
||||
*/
|
||||
public static <T extends Comparable<T>> T[] sort(T[] array) {
|
||||
|
||||
Arrays.sort(array);
|
||||
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import org.apache.geode.cache.Cache;
|
||||
import org.apache.geode.cache.CacheClosedException;
|
||||
import org.apache.geode.cache.CacheFactory;
|
||||
import org.apache.geode.cache.GemFireCache;
|
||||
import org.apache.geode.cache.Region;
|
||||
import org.apache.geode.cache.client.ClientCache;
|
||||
import org.apache.geode.cache.client.ClientCacheFactory;
|
||||
import org.apache.geode.distributed.DistributedSystem;
|
||||
@@ -67,13 +66,16 @@ public abstract class CacheUtils extends DistributedSystemUtils {
|
||||
/* (non-Javadoc) */
|
||||
public static boolean isDurable(ClientCache clientCache) {
|
||||
|
||||
DistributedSystem distributedSystem = getDistributedSystem(clientCache);
|
||||
|
||||
// NOTE technically the following code snippet would be more useful/valuable but is not "testable"!
|
||||
//((InternalDistributedSystem) distributedSystem).getConfig().getDurableClientId();
|
||||
|
||||
return (isConnected(distributedSystem) && StringUtils.hasText(distributedSystem.getProperties()
|
||||
.getProperty(DURABLE_CLIENT_ID_PROPERTY_NAME, null)));
|
||||
return Optional.ofNullable(clientCache)
|
||||
.<DistributedSystem>map(CacheUtils::getDistributedSystem)
|
||||
.filter(DistributedSystem::isConnected)
|
||||
.map(DistributedSystem::getProperties)
|
||||
.map(properties -> properties.getProperty(DURABLE_CLIENT_ID_PROPERTY_NAME, null))
|
||||
.filter(StringUtils::hasText)
|
||||
.isPresent();
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
@@ -91,6 +93,7 @@ public abstract class CacheUtils extends DistributedSystemUtils {
|
||||
|
||||
/* (non-Javadoc) */
|
||||
public static boolean closeCache() {
|
||||
|
||||
try {
|
||||
CacheFactory.getAnyInstance().close();
|
||||
return true;
|
||||
@@ -102,6 +105,7 @@ public abstract class CacheUtils extends DistributedSystemUtils {
|
||||
|
||||
/* (non-Javadoc) */
|
||||
public static boolean closeClientCache() {
|
||||
|
||||
try {
|
||||
ClientCacheFactory.getAnyInstance().close();
|
||||
return true;
|
||||
@@ -113,6 +117,7 @@ public abstract class CacheUtils extends DistributedSystemUtils {
|
||||
|
||||
/* (non-Javadoc) */
|
||||
public static Cache getCache() {
|
||||
|
||||
try {
|
||||
return CacheFactory.getAnyInstance();
|
||||
}
|
||||
@@ -123,6 +128,7 @@ public abstract class CacheUtils extends DistributedSystemUtils {
|
||||
|
||||
/* (non-Javadoc) */
|
||||
public static ClientCache getClientCache() {
|
||||
|
||||
try {
|
||||
return ClientCacheFactory.getAnyInstance();
|
||||
}
|
||||
@@ -135,9 +141,4 @@ public abstract class CacheUtils extends DistributedSystemUtils {
|
||||
public static GemFireCache resolveGemFireCache() {
|
||||
return Optional.<GemFireCache>ofNullable(getClientCache()).orElseGet(CacheUtils::getCache);
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
public static String toRegionPath(String regionName) {
|
||||
return String.format("%1$s%2$s", Region.SEPARATOR, regionName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.data.gemfire.util;
|
||||
|
||||
import static java.util.stream.StreamSupport.stream;
|
||||
import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -26,6 +27,7 @@ import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
|
||||
@@ -61,11 +63,10 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio
|
||||
* @see java.util.Collection
|
||||
*/
|
||||
public static <E, T extends Collection<E>> T addAll(T collection, Iterable<E> iterable) {
|
||||
Assert.notNull(collection, "Collection must not be null");
|
||||
|
||||
for (E element : nullSafeIterable(iterable)) {
|
||||
collection.add(element);
|
||||
}
|
||||
Assert.notNull(collection, "Collection is required");
|
||||
|
||||
stream(nullSafeIterable(iterable).spliterator(), false).forEach(collection::add);
|
||||
|
||||
return collection;
|
||||
}
|
||||
@@ -79,8 +80,11 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio
|
||||
*/
|
||||
@SafeVarargs
|
||||
public static <T> Set<T> asSet(T... elements) {
|
||||
|
||||
Set<T> set = new HashSet<>(elements.length);
|
||||
|
||||
Collections.addAll(set, elements);
|
||||
|
||||
return Collections.unmodifiableSet(set);
|
||||
}
|
||||
|
||||
@@ -93,6 +97,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio
|
||||
* @see java.util.Collection#contains(Object)
|
||||
*/
|
||||
public static boolean containsAny(Collection<?> collection, Object... elements) {
|
||||
|
||||
if (collection != null) {
|
||||
for (Object element : nullSafeArray(elements, Object.class)) {
|
||||
if (collection.contains(element)) {
|
||||
@@ -104,20 +109,6 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the given {@link Iterable} if not {@literal null} or empty, otherwise returns the {@code defaultIterable}.
|
||||
*
|
||||
* @param <T> concrete {@link Class} type of the {@link Iterable}.
|
||||
* @param <E> {@link Class} type of the elements in the {@link Iterable Iterables}.
|
||||
* @param iterable {@link Iterable} to evaluate.
|
||||
* @param defaultIterable {@link Iterable} to return if the given {@code iterable} is {@literal null} or empty.
|
||||
* @return {@code iterable} if not {@literal null} or empty otherwise return {@code defaultIterable}.
|
||||
* @see java.lang.Iterable
|
||||
*/
|
||||
public static <E, T extends Iterable<E>> T defaultIfEmpty(T iterable, T defaultIterable) {
|
||||
return (iterable != null && iterable.iterator().hasNext() ? iterable : defaultIterable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an empty {@link Iterable} object.
|
||||
*
|
||||
@@ -168,7 +159,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio
|
||||
* @see java.util.Collection
|
||||
*/
|
||||
public static <T> Collection<T> nullSafeCollection(Collection<T> collection) {
|
||||
return (collection != null ? collection : Collections.emptyList());
|
||||
return collection != null ? collection : Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -182,7 +173,21 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio
|
||||
* @see java.util.Iterator
|
||||
*/
|
||||
public static <T> Iterable<T> nullSafeIterable(Iterable<T> iterable) {
|
||||
return (iterable != null ? iterable : Collections::emptyIterator);
|
||||
return iterable != null ? iterable : Collections::emptyIterator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the given {@link Iterable} if not {@literal null} or empty, otherwise returns the {@code defaultIterable}.
|
||||
*
|
||||
* @param <T> concrete {@link Class} type of the {@link Iterable}.
|
||||
* @param <E> {@link Class} type of the elements in the {@link Iterable Iterables}.
|
||||
* @param iterable {@link Iterable} to evaluate.
|
||||
* @param defaultIterable {@link Iterable} to return if the given {@code iterable} is {@literal null} or empty.
|
||||
* @return {@code iterable} if not {@literal null} or empty otherwise return {@code defaultIterable}.
|
||||
* @see java.lang.Iterable
|
||||
*/
|
||||
public static <E, T extends Iterable<E>> T nullSafeIterable(T iterable, T defaultIterable) {
|
||||
return Optional.ofNullable(iterable).filter(it -> it.iterator().hasNext()).orElse(defaultIterable);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -196,7 +201,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio
|
||||
* @see java.util.List
|
||||
*/
|
||||
public static <T> List<T> nullSafeList(List<T> list) {
|
||||
return (list != null ? list : Collections.emptyList());
|
||||
return list != null ? list : Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,7 +217,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public static <K, V> Map<K, V> nullSafeMap(Map<K, V> map) {
|
||||
return (map != null ? map : Collections.emptyMap());
|
||||
return map != null ? map : Collections.<K, V>emptyMap();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -226,7 +231,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio
|
||||
* @see java.util.Set
|
||||
*/
|
||||
public static <T> Set<T> nullSafeSet(Set<T> set) {
|
||||
return (set != null ? set : Collections.emptySet());
|
||||
return set != null ? set : Collections.emptySet();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -239,7 +244,9 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio
|
||||
* @see java.util.List
|
||||
*/
|
||||
public static <T extends Comparable<T>> List<T> sort(List<T> list) {
|
||||
|
||||
Collections.sort(list);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
@@ -257,7 +264,8 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio
|
||||
* @see java.util.List
|
||||
*/
|
||||
public static <T> List<T> subList(List<T> source, int... indices) {
|
||||
Assert.notNull(source, "List must not be null");
|
||||
|
||||
Assert.notNull(source, "List is required");
|
||||
|
||||
List<T> result = new ArrayList<>(indices.length);
|
||||
|
||||
@@ -270,6 +278,7 @@ public abstract class CollectionUtils extends org.springframework.util.Collectio
|
||||
|
||||
/* (non-Javadoc) */
|
||||
public static String toString(Map<?, ?> map) {
|
||||
|
||||
StringBuilder builder = new StringBuilder("{\n");
|
||||
|
||||
int count = 0;
|
||||
|
||||
@@ -57,7 +57,8 @@ public abstract class DistributedSystemUtils extends SpringUtils {
|
||||
String durableClientId, Integer durableClientTimeout) {
|
||||
|
||||
if (StringUtils.hasText(durableClientId)) {
|
||||
Assert.notNull(gemfireProperties, "gemfireProperties must not be null");
|
||||
|
||||
Assert.notNull(gemfireProperties, "gemfireProperties are required");
|
||||
|
||||
gemfireProperties.setProperty(DURABLE_CLIENT_ID_PROPERTY_NAME, durableClientId);
|
||||
|
||||
@@ -71,7 +72,7 @@ public abstract class DistributedSystemUtils extends SpringUtils {
|
||||
|
||||
/* (non-Javadoc) */
|
||||
public static boolean isConnected(DistributedSystem distributedSystem) {
|
||||
return (distributedSystem != null && distributedSystem.isConnected());
|
||||
return Optional.ofNullable(distributedSystem).filter(DistributedSystem::isConnected).isPresent();
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
|
||||
@@ -23,10 +23,12 @@ import org.apache.geode.cache.RegionAttributes;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* The RegionUtils class...
|
||||
* The {@link RegionUtils} class is an abstract utility class for working with {@link Region Regions}.
|
||||
*
|
||||
* @author John Blum
|
||||
* @since 1.0.0
|
||||
* @see org.apache.geode.cache.Region
|
||||
* @see org.apache.geode.cache.RegionAttributes
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public abstract class RegionUtils extends CacheUtils {
|
||||
|
||||
@@ -38,4 +40,9 @@ public abstract class RegionUtils extends CacheUtils {
|
||||
.filter(StringUtils::hasText)
|
||||
.isPresent();
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
public static String toRegionPath(String regionName) {
|
||||
return String.format("%1$s%2$s", Region.SEPARATOR, regionName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.util;
|
||||
|
||||
import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* The {@link StreamUtils} class is a abstract utility class for working with {@link Stream Streams}.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see java.util.stream.Stream
|
||||
* @since 2.0.2
|
||||
*/
|
||||
public abstract class StreamUtils {
|
||||
|
||||
/**
|
||||
* Concatenates an array of {@link Stream Streams} into a single, continuous {@link Stream}.
|
||||
*
|
||||
* @param <T> {@link Class type} of elements in the {@link Stream Streams}.
|
||||
* @param streams array of {@link Stream Streams} to concatenate.
|
||||
* @return the concatenated array of {@link Stream Streams} as a single, continuous {@link Stream}.
|
||||
* @see java.util.stream.Stream
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> Stream<T> concat(Stream<T>... streams) {
|
||||
|
||||
Stream<T> concatenatedStream = Stream.empty();
|
||||
|
||||
for (Stream<T> stream : nullSafeArray(streams, Stream.class)) {
|
||||
concatenatedStream = Stream.concat(concatenatedStream, stream);
|
||||
}
|
||||
|
||||
return concatenatedStream;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user