DATAGEODE-225 - Allow the JSR-107, JCache API to be an optional dependency when using @EnableCachingDefinedRegions.
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -86,8 +86,8 @@
|
|||||||
<groupId>javax.enterprise</groupId>
|
<groupId>javax.enterprise</groupId>
|
||||||
<artifactId>cdi-api</artifactId>
|
<artifactId>cdi-api</artifactId>
|
||||||
<version>${cdi}</version>
|
<version>${cdi}</version>
|
||||||
<scope>provided</scope>
|
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Core Spring Framework -->
|
<!-- Core Spring Framework -->
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import static java.util.Arrays.asList;
|
|||||||
import static java.util.Arrays.stream;
|
import static java.util.Arrays.stream;
|
||||||
import static org.springframework.data.gemfire.util.ArrayUtils.asArray;
|
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.ArrayUtils.nullSafeArray;
|
||||||
import static org.springframework.data.gemfire.util.StreamUtils.concat;
|
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.AnnotatedElement;
|
import java.lang.reflect.AnnotatedElement;
|
||||||
@@ -34,11 +33,6 @@ import java.util.Set;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
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.GemFireCache;
|
||||||
import org.apache.geode.cache.Region;
|
import org.apache.geode.cache.Region;
|
||||||
import org.apache.geode.cache.RegionShortcut;
|
import org.apache.geode.cache.RegionShortcut;
|
||||||
@@ -65,6 +59,7 @@ import org.springframework.data.gemfire.config.annotation.support.AbstractAnnota
|
|||||||
import org.springframework.data.gemfire.config.annotation.support.CacheTypeAwareRegionFactoryBean;
|
import org.springframework.data.gemfire.config.annotation.support.CacheTypeAwareRegionFactoryBean;
|
||||||
import org.springframework.data.gemfire.support.CompositeLifecycle;
|
import org.springframework.data.gemfire.support.CompositeLifecycle;
|
||||||
import org.springframework.data.gemfire.util.CollectionUtils;
|
import org.springframework.data.gemfire.util.CollectionUtils;
|
||||||
|
import org.springframework.data.gemfire.util.StreamUtils;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
@@ -109,8 +104,13 @@ import org.springframework.util.StringUtils;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfigSupport implements ImportAware {
|
public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfigSupport implements ImportAware {
|
||||||
|
|
||||||
|
private final CacheNameResolver[] configuredCacheNameResolvers = {
|
||||||
|
new Jsr107CacheAnnotationsCacheNameResolverFactory().create(),
|
||||||
|
new SpringCacheAnnotationsCacheNameResolver()
|
||||||
|
};
|
||||||
|
|
||||||
private final CacheNameResolver composableCacheNameResolver = type ->
|
private final CacheNameResolver composableCacheNameResolver = type ->
|
||||||
asList(new Jsr107CacheAnnotationsCacheNameResolver(), new SpringCacheAnnotationsCacheNameResolver()).stream()
|
asList(this.configuredCacheNameResolvers).stream()
|
||||||
.flatMap(cacheNameResolver -> cacheNameResolver.resolveCacheNames(type).stream())
|
.flatMap(cacheNameResolver -> cacheNameResolver.resolveCacheNames(type).stream())
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
@@ -346,6 +346,7 @@ public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfig
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public Lifecycle cachingDefinedRegionsCompositeLifecycleBean() {
|
public Lifecycle cachingDefinedRegionsCompositeLifecycleBean() {
|
||||||
return this.compositeLifecycle;
|
return this.compositeLifecycle;
|
||||||
}
|
}
|
||||||
@@ -356,11 +357,11 @@ public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfig
|
|||||||
* all the application beans/components declared and registered in the Spring container (context) setup by
|
* 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.
|
* the application to determine whether the application components require caching behavior.
|
||||||
*
|
*
|
||||||
* @see org.springframework.data.gemfire.config.annotation.CachingDefinedRegionsConfiguration.Jsr107CacheAnnotationsCacheNameResolver
|
* @see Jsr107CacheAnnotationsCacheNameResolver
|
||||||
* @see org.springframework.data.gemfire.config.annotation.CachingDefinedRegionsConfiguration.SpringCacheAnnotationsCacheNameResolver
|
* @see org.springframework.data.gemfire.config.annotation.CachingDefinedRegionsConfiguration.SpringCacheAnnotationsCacheNameResolver
|
||||||
*/
|
*/
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
protected interface CacheNameResolver {
|
public interface CacheNameResolver {
|
||||||
Set<String> resolveCacheNames(Class<?> type);
|
Set<String> resolveCacheNames(Class<?> type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -372,13 +373,19 @@ public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfig
|
|||||||
*
|
*
|
||||||
* @see org.springframework.data.gemfire.config.annotation.CachingDefinedRegionsConfiguration.CacheNameResolver
|
* @see org.springframework.data.gemfire.config.annotation.CachingDefinedRegionsConfiguration.CacheNameResolver
|
||||||
*/
|
*/
|
||||||
protected abstract class AbstractCacheNameResolver implements CacheNameResolver {
|
public abstract static class AbstractCacheNameResolver extends AbstractAnnotationConfigSupport
|
||||||
|
implements CacheNameResolver {
|
||||||
|
|
||||||
private final String JSR_107_CACHE_NAME_ATTRIBUTE_NAME = "cacheName";
|
private final String JSR_107_CACHE_NAME_ATTRIBUTE_NAME = "cacheName";
|
||||||
private final String SPRING_CACHE_NAMES_ATTRIBUTE_NAME = "cacheNames";
|
private final String SPRING_CACHE_NAMES_ATTRIBUTE_NAME = "cacheNames";
|
||||||
|
|
||||||
private final String[] EMPTY_ARRAY = new String[0];
|
private final String[] EMPTY_ARRAY = new String[0];
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected final Class<? extends Annotation> getAnnotationType() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract Class<? extends Annotation>[] getClassCacheAnnotationTypes();
|
protected abstract Class<? extends Annotation>[] getClassCacheAnnotationTypes();
|
||||||
|
|
||||||
protected abstract Class<? extends Annotation>[] getMethodCacheAnnotationTypes();
|
protected abstract Class<? extends Annotation>[] getMethodCacheAnnotationTypes();
|
||||||
@@ -395,11 +402,12 @@ public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfig
|
|||||||
protected Set<String> resolveCacheNames(Annotation annotation) {
|
protected Set<String> resolveCacheNames(Annotation annotation) {
|
||||||
|
|
||||||
return Optional.ofNullable(annotation)
|
return Optional.ofNullable(annotation)
|
||||||
.map(it -> getAnnotationAttributes(it))
|
.map(this::getAnnotationAttributes)
|
||||||
.map(annotationAttributes -> {
|
.map(annotationAttributes -> {
|
||||||
|
|
||||||
String attributeName = annotationAttributes.containsKey(SPRING_CACHE_NAMES_ATTRIBUTE_NAME)
|
String attributeName = annotationAttributes.containsKey(SPRING_CACHE_NAMES_ATTRIBUTE_NAME)
|
||||||
? SPRING_CACHE_NAMES_ATTRIBUTE_NAME : JSR_107_CACHE_NAME_ATTRIBUTE_NAME;
|
? SPRING_CACHE_NAMES_ATTRIBUTE_NAME
|
||||||
|
: JSR_107_CACHE_NAME_ATTRIBUTE_NAME;
|
||||||
|
|
||||||
return annotationAttributes.containsKey(attributeName)
|
return annotationAttributes.containsKey(attributeName)
|
||||||
? annotationAttributes.getStringArray(attributeName)
|
? annotationAttributes.getStringArray(attributeName)
|
||||||
@@ -407,7 +415,7 @@ public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfig
|
|||||||
|
|
||||||
})
|
})
|
||||||
.map(CollectionUtils::asSet)
|
.map(CollectionUtils::asSet)
|
||||||
.orElse(Collections.emptySet());
|
.orElseGet(Collections::emptySet);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -416,7 +424,7 @@ public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfig
|
|||||||
Set<String> cacheNames = new HashSet<>(resolveCacheNames(type, getClassCacheAnnotationTypes()));
|
Set<String> cacheNames = new HashSet<>(resolveCacheNames(type, getClassCacheAnnotationTypes()));
|
||||||
|
|
||||||
stream(type.getMethods())
|
stream(type.getMethods())
|
||||||
.filter(method -> isUserLevelMethod(method))
|
.filter(this::isUserLevelMethod)
|
||||||
.forEach(method -> cacheNames.addAll(resolveCacheNames(method, getMethodCacheAnnotationTypes())));
|
.forEach(method -> cacheNames.addAll(resolveCacheNames(method, getMethodCacheAnnotationTypes())));
|
||||||
|
|
||||||
return cacheNames;
|
return cacheNames;
|
||||||
@@ -434,22 +442,7 @@ public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfig
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected class Jsr107CacheAnnotationsCacheNameResolver extends AbstractCacheNameResolver {
|
protected static class SpringCacheAnnotationsCacheNameResolver 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
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@@ -469,7 +462,8 @@ public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfig
|
|||||||
|
|
||||||
cacheNames.addAll(resolveCachingCacheNames(type));
|
cacheNames.addAll(resolveCachingCacheNames(type));
|
||||||
|
|
||||||
stream(type.getMethods()).filter(method -> isUserLevelMethod(method))
|
stream(type.getMethods())
|
||||||
|
.filter(this::isUserLevelMethod)
|
||||||
.forEach(method -> cacheNames.addAll(resolveCachingCacheNames(method)));
|
.forEach(method -> cacheNames.addAll(resolveCachingCacheNames(method)));
|
||||||
|
|
||||||
return cacheNames;
|
return cacheNames;
|
||||||
@@ -481,7 +475,7 @@ public class CachingDefinedRegionsConfiguration extends AbstractAnnotationConfig
|
|||||||
Set<String> cacheNames = new HashSet<>();
|
Set<String> cacheNames = new HashSet<>();
|
||||||
|
|
||||||
Optional.ofNullable(resolveAnnotation(annotatedElement, Caching.class)).ifPresent(caching ->
|
Optional.ofNullable(resolveAnnotation(annotatedElement, Caching.class)).ifPresent(caching ->
|
||||||
concat(stream(caching.cacheable()), stream(caching.evict()), stream(caching.put()))
|
StreamUtils.concat(stream(caching.cacheable()), stream(caching.evict()), stream(caching.put()))
|
||||||
.flatMap(cacheAnnotation -> resolveCacheNames(cacheAnnotation).stream())
|
.flatMap(cacheAnnotation -> resolveCacheNames(cacheAnnotation).stream())
|
||||||
.collect(Collectors.toCollection(() -> cacheNames)));
|
.collect(Collectors.toCollection(() -> cacheNames)));
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2019 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.springframework.data.gemfire.util.ArrayUtils.asArray;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
|
||||||
|
import javax.cache.annotation.CacheDefaults;
|
||||||
|
import javax.cache.annotation.CachePut;
|
||||||
|
import javax.cache.annotation.CacheRemove;
|
||||||
|
import javax.cache.annotation.CacheRemoveAll;
|
||||||
|
import javax.cache.annotation.CacheResult;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link Jsr107CacheAnnotationsCacheNameResolver} class is a {@link CachingDefinedRegionsConfiguration.CacheNameResolver}
|
||||||
|
* implementation that can resolve JSR-107, JCache API cache annotations from a given {@link Class class type}.
|
||||||
|
*
|
||||||
|
* @author John Blum
|
||||||
|
* @see java.lang.annotation.Annotation
|
||||||
|
* @see org.springframework.data.gemfire.config.annotation.CachingDefinedRegionsConfiguration.AbstractCacheNameResolver
|
||||||
|
* @since 2.2.0
|
||||||
|
*/
|
||||||
|
class Jsr107CacheAnnotationsCacheNameResolver extends CachingDefinedRegionsConfiguration.AbstractCacheNameResolver {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
protected Class<? extends Annotation>[] getClassCacheAnnotationTypes() {
|
||||||
|
return append(getMethodCacheAnnotationTypes(), CacheDefaults.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Class<? extends Annotation>[] getMethodCacheAnnotationTypes() {
|
||||||
|
|
||||||
|
return asArray(
|
||||||
|
CachePut.class,
|
||||||
|
CacheRemove.class,
|
||||||
|
CacheRemoveAll.class,
|
||||||
|
CacheResult.class
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2019 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 java.util.Collections;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory class used to construct an instance of the {@link Jsr107CacheAnnotationsCacheNameResolver} if and only if
|
||||||
|
* the JSR-107, JCache API lib is on the application classpath.
|
||||||
|
*
|
||||||
|
* @author John Blum
|
||||||
|
* @see org.springframework.data.gemfire.config.annotation.Jsr107CacheAnnotationsCacheNameResolver
|
||||||
|
* @since 2.2.0
|
||||||
|
*/
|
||||||
|
class Jsr107CacheAnnotationsCacheNameResolverFactory {
|
||||||
|
|
||||||
|
private final boolean jcacheApiPresent;
|
||||||
|
|
||||||
|
Jsr107CacheAnnotationsCacheNameResolverFactory() {
|
||||||
|
|
||||||
|
this.jcacheApiPresent =
|
||||||
|
ClassUtils.isPresent("javax.cache.annotation.CacheResult", getClass().getClassLoader());
|
||||||
|
}
|
||||||
|
|
||||||
|
CachingDefinedRegionsConfiguration.CacheNameResolver create() {
|
||||||
|
|
||||||
|
return this.jcacheApiPresent
|
||||||
|
? new Jsr107CacheAnnotationsCacheNameResolver()
|
||||||
|
: new NoOpCacheNameResolver();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class NoOpCacheNameResolver implements CachingDefinedRegionsConfiguration.CacheNameResolver {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<String> resolveCacheNames(Class<?> type) {
|
||||||
|
return Collections.emptySet();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user