14
14
* limitations under the License.
15
15
*
16
16
*/
17
+
17
18
package org .springframework .data .gemfire .config .support ;
18
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
+ import java .lang .annotation .Annotation ;
25
+ import java .lang .reflect .Method ;
26
+ import java .lang .reflect .Modifier ;
27
+ import java .util .ArrayList ;
28
+ import java .util .List ;
29
+ import java .util .Optional ;
30
+
31
+ import org .apache .geode .cache .EntryEvent ;
19
32
import org .apache .geode .cache .Region ;
33
+ import org .apache .geode .cache .RegionEvent ;
20
34
import org .springframework .beans .BeansException ;
21
35
import org .springframework .beans .factory .BeanFactory ;
22
36
import org .springframework .beans .factory .BeanFactoryAware ;
34
48
import org .springframework .util .ObjectUtils ;
35
49
import org .springframework .util .ReflectionUtils ;
36
50
37
- import java .lang .annotation .Annotation ;
38
- import java .lang .reflect .Method ;
39
- import java .lang .reflect .Modifier ;
40
- import java .util .ArrayList ;
41
- import java .util .List ;
42
- import java .util .Optional ;
43
-
44
- import static java .util .Arrays .*;
45
- import static org .springframework .data .gemfire .util .ArrayUtils .*;
46
- import static org .springframework .data .gemfire .util .RuntimeExceptionFactory .*;
47
-
51
+ /**
52
+ *
53
+ */
48
54
@ Configuration public class CacheListenerPostProcessor implements BeanPostProcessor , BeanFactoryAware {
49
55
50
56
private ConfigurableListableBeanFactory beanFactory ;
@@ -65,44 +71,52 @@ private void registerAnyDeclaredCacheListenerAnnotatedMethods(Object bean) {
65
71
if (cacheListenerAnnotation != null ) {
66
72
67
73
Assert .isTrue (Modifier .isPublic (method .getModifiers ()), String
68
- .format ("The bean [%s] method [%s] annotated with [%s] must be public" , bean .getClass ().getName (),
69
- method .getName (), AsCacheListener .class .getName ()));
74
+ .format ("The bean [%s] method [%s] annotated with [%s] must be public" , bean .getClass ().getName (),
75
+ method .getName (), AsCacheListener .class .getName ()));
70
76
71
77
AnnotationAttributes cacheListenerAttributes = resolveAnnotationAttributes (cacheListenerAnnotation );
72
78
73
- CacheListenerEventType [] eventTypes = (CacheListenerEventType []) cacheListenerAttributes .get ("eventTypes" );
79
+ CacheListenerEventType [] eventTypes = (CacheListenerEventType []) cacheListenerAttributes
80
+ .get ("eventTypes" );
74
81
RegionCacheListenerEventType [] regionEventTypes = (RegionCacheListenerEventType []) cacheListenerAttributes
75
- .get ("regionEventTypes" );
82
+ .get ("regionEventTypes" );
76
83
77
84
if (regionEventTypes .length > 0 && eventTypes .length > 0 ) {
78
85
throw new IllegalArgumentException (String
79
- .format ("Populating both event and regionEvent types is not allowed for bean [%s] method [%s]" ,
80
- bean .getClass ().getName (), method .getName ()));
86
+ .format ("Populating both event and regionEvent types is not allowed for bean [%s] method [%s]" ,
87
+ bean .getClass ().getName (), method .getName ()));
81
88
}
82
89
83
- String [] regions = getRegionsForEventRegistration (cacheListenerAttributes .getStringArray ("regions" ),
84
- getBeanFactory ());
85
-
86
- if (eventTypes .length > 0 ) {
87
- registerCacheListenerEvents (bean , method , regions , eventTypes );
88
- }
89
- if (regionEventTypes .length > 0 ) {
90
- registerRegionCacheListenerEvents (bean , method , regions , regionEventTypes );
91
- }
90
+ registerEventHandlerToRegion (bean , method , cacheListenerAttributes , eventTypes , regionEventTypes );
92
91
}
93
92
});
94
93
}
95
94
95
+ private void registerEventHandlerToRegion (Object bean , Method method , AnnotationAttributes cacheListenerAttributes ,
96
+ CacheListenerEventType [] eventTypes , RegionCacheListenerEventType [] regionEventTypes ) {
97
+ String [] regions = getRegionsForEventRegistration (cacheListenerAttributes .getStringArray ("regions" ),
98
+ getBeanFactory ());
99
+
100
+ if (eventTypes .length > 0 ) {
101
+ EventProcessorUtils .validateCacheListenerMethodParameters (method ,EntryEvent .class );
102
+ registerCacheListenerEvents (bean , method , regions , eventTypes );
103
+ }
104
+ if (regionEventTypes .length > 0 ) {
105
+ EventProcessorUtils .validateCacheListenerMethodParameters (method , RegionEvent .class );
106
+ registerRegionCacheListenerEvents (bean , method , regions , regionEventTypes );
107
+ }
108
+ }
109
+
96
110
private void registerCacheListenerEvents (Object bean , Method method , String [] regions ,
97
- CacheListenerEventType [] eventTypes ) {
111
+ CacheListenerEventType [] eventTypes ) {
98
112
99
113
EventProcessorUtils .registerCacheListenerOntoRegions (regions , method , bean , eventTypes , getBeanFactory ());
100
114
}
101
115
102
116
private void registerRegionCacheListenerEvents (Object bean , Method method , String [] regions ,
103
- RegionCacheListenerEventType [] regionEventTypes ) {
117
+ RegionCacheListenerEventType [] regionEventTypes ) {
104
118
EventProcessorUtils
105
- .registerCacheListenerForRegionEventsOntoRegions (regions , method , bean , regionEventTypes , getBeanFactory ());
119
+ .registerCacheListenerForRegionEventsOntoRegions (regions , method , bean , regionEventTypes , getBeanFactory ());
106
120
}
107
121
108
122
private String [] getRegionsForEventRegistration (String [] regions , ConfigurableListableBeanFactory beanFactory ) {
@@ -118,7 +132,8 @@ private String[] getRegionsForEventRegistration(String[] regions, ConfigurableLi
118
132
String [] tempRegions = new String [regionNames .size ()];
119
133
regionNames .toArray (tempRegions );
120
134
return tempRegions ;
121
- } else {
135
+ }
136
+ else {
122
137
return regions ;
123
138
}
124
139
}
@@ -128,33 +143,33 @@ private AnnotationAttributes resolveAnnotationAttributes(Annotation annotation)
128
143
return AnnotationAttributes .fromMap (AnnotationUtils .getAnnotationAttributes (annotation , false , true ));
129
144
}
130
145
146
+ /**
147
+ * Returns a reference to the containing Spring {@link BeanFactory}.
148
+ *
149
+ * @return a reference to the containing Spring {@link BeanFactory}.
150
+ * @throws IllegalStateException if the {@link BeanFactory} was not configured.
151
+ * @see org.springframework.beans.factory.BeanFactory
152
+ */
153
+ protected ConfigurableListableBeanFactory getBeanFactory () {
154
+ return Optional .ofNullable (this .beanFactory )
155
+ .orElseThrow (() -> newIllegalStateException ("BeanFactory was not properly configured" ));
156
+ }
157
+
131
158
/**
132
159
* Sets a reference to the configured Spring {@link BeanFactory}.
133
160
*
134
161
* @param beanFactory configured Spring {@link BeanFactory}.
135
162
* @throws IllegalArgumentException if the given {@link BeanFactory} is not an instance of
136
- * {@link ConfigurableListableBeanFactory}.
163
+ * {@link ConfigurableListableBeanFactory}.
137
164
* @see org.springframework.beans.factory.BeanFactoryAware
138
165
* @see org.springframework.beans.factory.BeanFactory
139
166
*/
140
167
@ Override @ SuppressWarnings ("all" ) public final void setBeanFactory (BeanFactory beanFactory ) throws BeansException {
141
168
142
169
Assert .isInstanceOf (ConfigurableListableBeanFactory .class , beanFactory , String
143
- .format ("BeanFactory [%1$s] must be an instance of %2$s" , ObjectUtils .nullSafeClassName (beanFactory ),
144
- ConfigurableListableBeanFactory .class .getSimpleName ()));
170
+ .format ("BeanFactory [%1$s] must be an instance of %2$s" , ObjectUtils .nullSafeClassName (beanFactory ),
171
+ ConfigurableListableBeanFactory .class .getSimpleName ()));
145
172
146
173
this .beanFactory = (ConfigurableListableBeanFactory ) beanFactory ;
147
174
}
148
-
149
- /**
150
- * Returns a reference to the containing Spring {@link BeanFactory}.
151
- *
152
- * @return a reference to the containing Spring {@link BeanFactory}.
153
- * @throws IllegalStateException if the {@link BeanFactory} was not configured.
154
- * @see org.springframework.beans.factory.BeanFactory
155
- */
156
- protected ConfigurableListableBeanFactory getBeanFactory () {
157
- return Optional .ofNullable (this .beanFactory )
158
- .orElseThrow (() -> newIllegalStateException ("BeanFactory was not properly configured" ));
159
- }
160
175
}
0 commit comments