17
17
18
18
package org .springframework .data .gemfire .config .support ;
19
19
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
-
24
20
import java .lang .annotation .Annotation ;
25
21
import java .lang .reflect .Method ;
26
- import java .util .ArrayList ;
27
22
import java .util .List ;
28
- import java .util .Optional ;
29
23
30
24
import org .apache .geode .cache .CacheEvent ;
31
25
import org .apache .geode .cache .CacheListener ;
32
26
import org .apache .geode .cache .EntryEvent ;
33
27
import org .apache .geode .cache .Region ;
34
28
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 ;
38
29
import org .springframework .beans .factory .config .BeanPostProcessor ;
39
- import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
40
30
import org .springframework .context .annotation .Configuration ;
41
31
import org .springframework .core .annotation .AnnotationAttributes ;
42
32
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 ;
44
34
import org .springframework .data .gemfire .eventing .EventProcessorUtils ;
45
35
import org .springframework .data .gemfire .eventing .config .CacheListenerEventType ;
46
36
import org .springframework .data .gemfire .eventing .config .PojoCacheListenerWrapper ;
47
37
import org .springframework .data .gemfire .eventing .config .PojoRegionEventCacheListenerWrapper ;
48
38
import org .springframework .data .gemfire .eventing .config .RegionCacheListenerEventType ;
49
- import org .springframework .util .Assert ;
50
- import org .springframework .util .ObjectUtils ;
51
39
52
40
/**
53
41
* 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
55
43
*/
56
44
@ Configuration
57
- public class CacheListenerPostProcessor extends CallbackPostProcessor
58
- implements BeanPostProcessor , BeanFactoryAware {
59
-
60
- private ConfigurableListableBeanFactory beanFactory ;
45
+ public class CacheListenerPostProcessor extends CallbackPostProcessor {
61
46
62
47
@ Override
63
- protected Class getRegionCallbackClass () {
64
- return AsRegionEventListener .class ;
48
+ protected Class getRegionEventHandlerClass () {
49
+ return AsRegionEventHandler .class ;
65
50
}
66
51
67
52
@ Override
68
- protected Class getCallbackClass () {
53
+ protected Class getEventHandlerClass () {
69
54
return AsCacheListener .class ;
70
55
}
71
56
72
57
@ Override
73
58
protected <T extends Annotation > void registerEventHandlers (Object bean , Class <T > listenerAnnotationClazz ,
74
59
Method method , AnnotationAttributes cacheListenerAttributes ) {
75
- if (listenerAnnotationClazz .isAssignableFrom (getCallbackClass ())) {
60
+ if (listenerAnnotationClazz .isAssignableFrom (getEventHandlerClass ())) {
76
61
registerCacheListenerEventHandler (bean , method , cacheListenerAttributes );
77
62
}
78
- else if (listenerAnnotationClazz .isAssignableFrom (getRegionCallbackClass ())) {
63
+ else if (listenerAnnotationClazz .isAssignableFrom (getRegionEventHandlerClass ())) {
79
64
registerRegionEventHandler (bean , method , cacheListenerAttributes );
80
65
}
81
66
}
@@ -93,7 +78,7 @@ private void registerCacheListenerEventHandler(Object bean, Method method,
93
78
}
94
79
95
80
/**
96
- * Lookup {@link RegionCacheListenerEventType} from the {@link AsRegionEventListener } annotation and
81
+ * Lookup {@link RegionCacheListenerEventType} from the {@link AsRegionEventHandler } annotation and
97
82
* create a {@link PojoRegionEventCacheListenerWrapper}
98
83
* of type {@link CacheListener} that would register itself onto a {@link Region} for the configured
99
84
* {@link Region} specific events
@@ -118,70 +103,11 @@ private void registerRegionEventHandler(Object bean, Method method,
118
103
*/
119
104
private <T extends CacheEvent > void registerEventHandlerToRegion (Method method ,
120
105
AnnotationAttributes cacheListenerAttributes , CacheListener cacheListener , Class <T > eventClass ) {
121
- String [] regions = getRegionsForEventRegistration (cacheListenerAttributes .getStringArray ("regions" ),
106
+ List < String > regions = getRegionsForEventRegistration (cacheListenerAttributes .getStringArray ("regions" ),
122
107
getBeanFactory ());
123
108
124
109
EventProcessorUtils .validateEventHandlerMethodParameters (method , eventClass );
125
110
EventProcessorUtils .registerCacheListenerToRegions (regions , beanFactory , cacheListener );
126
111
}
127
112
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
- }
187
113
}
0 commit comments