Skip to content

Commit f23a46a

Browse files
author
Udo Kohlmeyer
committed
DATAGEODE-136 - Adding AsCacheWriter Tests
1 parent 09239b7 commit f23a46a

16 files changed

+981
-396
lines changed

spring-data-geode/src/main/java/org/springframework/data/gemfire/config/annotation/AsCacheWriter.java

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.lang.annotation.Target;
2424

2525
import org.springframework.data.gemfire.eventing.config.CacheWriterEventType;
26+
import org.springframework.data.gemfire.eventing.config.RegionCacheWriterEventType;
2627

2728
/**
2829
* Used to declare a concrete method as a CacheWriter event handler
+3-3
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@
2323
import java.lang.annotation.Target;
2424

2525
import org.springframework.data.gemfire.eventing.config.CacheListenerEventType;
26-
import org.springframework.data.gemfire.eventing.config.RegionCacheListenerEventType;
2726
import org.springframework.data.gemfire.eventing.config.CacheWriterEventType;
27+
import org.springframework.data.gemfire.eventing.config.RegionCacheListenerEventType;
2828
import org.springframework.data.gemfire.eventing.config.RegionCacheWriterEventType;
2929

3030
/**
31-
* Used to declare a concrete method as a RegionEventListener event handler,
31+
* Used to declare a concrete method as a {@link AsRegionEventHandler} event handler,
3232
* which handles {@link org.apache.geode.cache.Region}
3333
*
3434
* @author Udo Kohlmeyer
3535
* @since 2.3.0
3636
*/
3737
@Retention(RetentionPolicy.RUNTIME)
3838
@Target({ ElementType.METHOD })
39-
public @interface AsRegionEventListener {
39+
public @interface AsRegionEventHandler {
4040

4141
/**
4242
* An array of {@link RegionCacheListenerEventType} that control what region events need to be observed

spring-data-geode/src/main/java/org/springframework/data/gemfire/config/annotation/EnableEventProcessing.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import org.springframework.context.annotation.Import;
2828
import org.springframework.data.gemfire.eventing.config.AsCacheListenerBeanPostProcessorRegistrar;
2929
import org.springframework.data.gemfire.eventing.config.AsCacheWriterBeanPostProcessorRegistrar;
30+
import org.springframework.data.gemfire.eventing.config.ComposableCacheWriterWrapper;
3031
import org.springframework.data.gemfire.eventing.config.PojoCacheListenerWrapper;
31-
import org.springframework.data.gemfire.eventing.config.PojoCacheWriterWrapper;
3232

3333
/**
3434
* Enables GemFire annotated EventHandler implementations. These implementation will include {@link org.apache.geode.cache.CacheListener},
@@ -37,9 +37,10 @@
3737
* This annotation results in the container discovering any beans that are annotated with:
3838
* <ul><
3939
* <li> {code}@AsCacheListener{code},wraps them in a {@link PojoCacheListenerWrapper}</li>
40-
* <li> {code}@AsCacheWriter{code},wraps them in a {@link PojoCacheWriterWrapper}</li>
40+
* <li> {code}@AsCacheWriter{code},wraps them in a {@link ComposableCacheWriterWrapper}</li>
4141
* <li></li>and register them with the corresponding {@link org.apache.geode.cache.Region}.
42-
*</ul>
42+
* </ul>
43+
*
4344
* @author Udo Kohlmeyer
4445
* @since 2.3.0
4546
*/

spring-data-geode/src/main/java/org/springframework/data/gemfire/config/support/CacheListenerPostProcessor.java

+10-84
Original file line numberDiff line numberDiff line change
@@ -17,65 +17,50 @@
1717

1818
package org.springframework.data.gemfire.config.support;
1919

20-
import static java.util.Arrays.*;
21-
import static org.springframework.data.gemfire.util.ArrayUtils.*;
22-
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.*;
23-
2420
import java.lang.annotation.Annotation;
2521
import java.lang.reflect.Method;
26-
import java.util.ArrayList;
2722
import java.util.List;
28-
import java.util.Optional;
2923

3024
import org.apache.geode.cache.CacheEvent;
3125
import org.apache.geode.cache.CacheListener;
3226
import org.apache.geode.cache.EntryEvent;
3327
import org.apache.geode.cache.Region;
3428
import org.apache.geode.cache.RegionEvent;
35-
import org.springframework.beans.BeansException;
36-
import org.springframework.beans.factory.BeanFactory;
37-
import org.springframework.beans.factory.BeanFactoryAware;
3829
import org.springframework.beans.factory.config.BeanPostProcessor;
39-
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
4030
import org.springframework.context.annotation.Configuration;
4131
import org.springframework.core.annotation.AnnotationAttributes;
4232
import org.springframework.data.gemfire.config.annotation.AsCacheListener;
43-
import org.springframework.data.gemfire.config.annotation.AsRegionEventListener;
33+
import org.springframework.data.gemfire.config.annotation.AsRegionEventHandler;
4434
import org.springframework.data.gemfire.eventing.EventProcessorUtils;
4535
import org.springframework.data.gemfire.eventing.config.CacheListenerEventType;
4636
import org.springframework.data.gemfire.eventing.config.PojoCacheListenerWrapper;
4737
import org.springframework.data.gemfire.eventing.config.PojoRegionEventCacheListenerWrapper;
4838
import org.springframework.data.gemfire.eventing.config.RegionCacheListenerEventType;
49-
import org.springframework.util.Assert;
50-
import org.springframework.util.ObjectUtils;
5139

5240
/**
5341
* A {@link BeanPostProcessor} to create and register {@link CacheListener}, annotated with {@link AsCacheListener}
54-
* and {@link AsRegionEventListener} onto the configured {@link Region}s
42+
* and {@link AsRegionEventHandler} onto the configured {@link Region}s
5543
*/
5644
@Configuration
57-
public class CacheListenerPostProcessor extends CallbackPostProcessor
58-
implements BeanPostProcessor, BeanFactoryAware {
59-
60-
private ConfigurableListableBeanFactory beanFactory;
45+
public class CacheListenerPostProcessor extends CallbackPostProcessor {
6146

6247
@Override
63-
protected Class getRegionCallbackClass() {
64-
return AsRegionEventListener.class;
48+
protected Class getRegionEventHandlerClass() {
49+
return AsRegionEventHandler.class;
6550
}
6651

6752
@Override
68-
protected Class getCallbackClass() {
53+
protected Class getEventHandlerClass() {
6954
return AsCacheListener.class;
7055
}
7156

7257
@Override
7358
protected <T extends Annotation> void registerEventHandlers(Object bean, Class<T> listenerAnnotationClazz,
7459
Method method, AnnotationAttributes cacheListenerAttributes) {
75-
if (listenerAnnotationClazz.isAssignableFrom(getCallbackClass())) {
60+
if (listenerAnnotationClazz.isAssignableFrom(getEventHandlerClass())) {
7661
registerCacheListenerEventHandler(bean, method, cacheListenerAttributes);
7762
}
78-
else if (listenerAnnotationClazz.isAssignableFrom(getRegionCallbackClass())) {
63+
else if (listenerAnnotationClazz.isAssignableFrom(getRegionEventHandlerClass())) {
7964
registerRegionEventHandler(bean, method, cacheListenerAttributes);
8065
}
8166
}
@@ -93,7 +78,7 @@ private void registerCacheListenerEventHandler(Object bean, Method method,
9378
}
9479

9580
/**
96-
* Lookup {@link RegionCacheListenerEventType} from the {@link AsRegionEventListener} annotation and
81+
* Lookup {@link RegionCacheListenerEventType} from the {@link AsRegionEventHandler} annotation and
9782
* create a {@link PojoRegionEventCacheListenerWrapper}
9883
* of type {@link CacheListener} that would register itself onto a {@link Region} for the configured
9984
* {@link Region} specific events
@@ -118,70 +103,11 @@ private void registerRegionEventHandler(Object bean, Method method,
118103
*/
119104
private <T extends CacheEvent> void registerEventHandlerToRegion(Method method,
120105
AnnotationAttributes cacheListenerAttributes, CacheListener cacheListener, Class<T> eventClass) {
121-
String[] regions = getRegionsForEventRegistration(cacheListenerAttributes.getStringArray("regions"),
106+
List<String> regions = getRegionsForEventRegistration(cacheListenerAttributes.getStringArray("regions"),
122107
getBeanFactory());
123108

124109
EventProcessorUtils.validateEventHandlerMethodParameters(method, eventClass);
125110
EventProcessorUtils.registerCacheListenerToRegions(regions, beanFactory, cacheListener);
126111
}
127112

128-
/**
129-
* Takes an array of Region names. If empty, returns all configured {@link Region} names, otherwise returns the input
130-
* region name array
131-
*
132-
* @param regions - An Array of {@link Region} names. This can be empty and thus defaults to all configured {@link Region}
133-
* @param beanFactory - A {@link org.springframework.data.gemfire.ConfigurableRegionFactoryBean}
134-
* @return An array of {@link Region} names. If the input regions array is empty, the result will be an array with all
135-
* configured {@link Region} names
136-
*/
137-
private String[] getRegionsForEventRegistration(String[] regions, ConfigurableListableBeanFactory beanFactory) {
138-
if (isEmpty(regions)) {
139-
List<String> regionNames = new ArrayList<>();
140-
stream(beanFactory.getBeanDefinitionNames()).forEach(beanName -> {
141-
Object bean = beanFactory.getBean(beanName);
142-
if (bean instanceof Region) {
143-
Region region = (Region) bean;
144-
regionNames.add(region.getName());
145-
}
146-
});
147-
String[] tempRegions = new String[regionNames.size()];
148-
regionNames.toArray(tempRegions);
149-
return tempRegions;
150-
}
151-
else {
152-
return regions;
153-
}
154-
}
155-
156-
/**
157-
* Returns a reference to the containing Spring {@link BeanFactory}.
158-
*
159-
* @return a reference to the containing Spring {@link BeanFactory}.
160-
* @throws IllegalStateException if the {@link BeanFactory} was not configured.
161-
* @see org.springframework.beans.factory.BeanFactory
162-
*/
163-
protected ConfigurableListableBeanFactory getBeanFactory() {
164-
return Optional.ofNullable(this.beanFactory)
165-
.orElseThrow(() -> newIllegalStateException("BeanFactory was not properly configured"));
166-
}
167-
168-
/**
169-
* Sets a reference to the configured Spring {@link BeanFactory}.
170-
*
171-
* @param beanFactory configured Spring {@link BeanFactory}.
172-
* @throws IllegalArgumentException if the given {@link BeanFactory} is not an instance of
173-
* {@link ConfigurableListableBeanFactory}.
174-
* @see org.springframework.beans.factory.BeanFactoryAware
175-
* @see org.springframework.beans.factory.BeanFactory
176-
*/
177-
@Override
178-
@SuppressWarnings("all")
179-
public final void setBeanFactory(BeanFactory beanFactory) throws BeansException {
180-
181-
Assert.isInstanceOf(ConfigurableListableBeanFactory.class, beanFactory, String
182-
.format("BeanFactory [%1$s] must be an instance of %2$s", ObjectUtils.nullSafeClassName(beanFactory),
183-
ConfigurableListableBeanFactory.class.getSimpleName()));
184-
185-
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
186-
}
187113
}

0 commit comments

Comments
 (0)