23
23
24
24
import java .lang .annotation .Annotation ;
25
25
import java .lang .reflect .Method ;
26
- import java .lang .reflect .Modifier ;
27
26
import java .util .ArrayList ;
28
27
import java .util .List ;
29
28
import java .util .Optional ;
40
39
import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
41
40
import org .springframework .context .annotation .Configuration ;
42
41
import org .springframework .core .annotation .AnnotationAttributes ;
43
- import org .springframework .core .annotation .AnnotationUtils ;
44
42
import org .springframework .data .gemfire .config .annotation .AsCacheListener ;
45
43
import org .springframework .data .gemfire .config .annotation .AsRegionEventListener ;
46
44
import org .springframework .data .gemfire .eventing .EventProcessorUtils ;
47
45
import org .springframework .data .gemfire .eventing .config .CacheListenerEventType ;
48
46
import org .springframework .data .gemfire .eventing .config .PojoCacheListenerWrapper ;
49
47
import org .springframework .data .gemfire .eventing .config .PojoRegionEventCacheListenerWrapper ;
50
48
import org .springframework .data .gemfire .eventing .config .RegionCacheListenerEventType ;
51
- import org .springframework .data .gemfire .util .ArrayUtils ;
52
49
import org .springframework .util .Assert ;
53
50
import org .springframework .util .ObjectUtils ;
54
- import org .springframework .util .ReflectionUtils ;
55
51
56
52
/**
57
53
* A {@link BeanPostProcessor} to create and register {@link CacheListener}, annotated with {@link AsCacheListener}
58
54
* and {@link AsRegionEventListener} onto the configured {@link Region}s
59
55
*/
60
- @ Configuration public class CacheListenerPostProcessor implements BeanPostProcessor , BeanFactoryAware {
56
+ @ Configuration
57
+ public class CacheListenerPostProcessor extends CallbackPostProcessor
58
+ implements BeanPostProcessor , BeanFactoryAware {
61
59
62
60
private ConfigurableListableBeanFactory beanFactory ;
63
61
64
- @ Override public Object postProcessAfterInitialization (Object bean , String beanName ) throws BeansException {
65
-
66
- registerAnyDeclaredCacheListenerAnnotatedMethods (bean , AsCacheListener .class );
67
- registerAnyDeclaredCacheListenerAnnotatedMethods (bean , AsRegionEventListener .class );
68
-
69
- return bean ;
62
+ @ Override
63
+ protected Class getRegionCallbackClass () {
64
+ return AsRegionEventListener .class ;
70
65
}
71
66
72
- private <T extends Annotation > void registerAnyDeclaredCacheListenerAnnotatedMethods (Object bean ,
73
- Class <T > listenerAnnotationClazz ) {
74
-
75
- stream (nullSafeArray (ReflectionUtils .getAllDeclaredMethods (bean .getClass ()), Method .class )).forEach (method -> {
76
-
77
- Optional <T > cacheListenerAnnotation = Optional .ofNullable (AnnotationUtils
78
- .getAnnotation (method , listenerAnnotationClazz ));
79
-
80
- cacheListenerAnnotation .ifPresent (asCacheListener -> {
81
-
82
- Assert .isTrue (Modifier .isPublic (method .getModifiers ()), String
83
- .format ("The bean [%s] method [%s] annotated with [%s] must be public" , bean .getClass ().getName (),
84
- method .getName (), listenerAnnotationClazz .getName ()));
85
-
86
- AnnotationAttributes cacheListenerAttributes = resolveAnnotationAttributes (asCacheListener );
87
-
88
- registerEventHandlers (bean , listenerAnnotationClazz , method , cacheListenerAttributes );
89
-
90
- });
91
- });
67
+ @ Override
68
+ protected Class getCallbackClass () {
69
+ return AsCacheListener .class ;
92
70
}
93
71
94
- private <T extends Annotation > void registerEventHandlers (Object bean , Class <T > listenerAnnotationClazz ,
72
+ @ Override
73
+ protected <T extends Annotation > void registerEventHandlers (Object bean , Class <T > listenerAnnotationClazz ,
95
74
Method method , AnnotationAttributes cacheListenerAttributes ) {
96
- if (listenerAnnotationClazz .isAssignableFrom (AsCacheListener . class )) {
75
+ if (listenerAnnotationClazz .isAssignableFrom (getCallbackClass () )) {
97
76
registerCacheListenerEventHandler (bean , method , cacheListenerAttributes );
98
77
}
99
- else if (listenerAnnotationClazz .isAssignableFrom (AsRegionEventListener . class )) {
78
+ else if (listenerAnnotationClazz .isAssignableFrom (getRegionCallbackClass () )) {
100
79
registerRegionEventHandler (bean , method , cacheListenerAttributes );
101
80
}
102
81
}
@@ -122,7 +101,7 @@ private void registerCacheListenerEventHandler(Object bean, Method method,
122
101
private void registerRegionEventHandler (Object bean , Method method ,
123
102
AnnotationAttributes cacheListenerAttributes ) {
124
103
RegionCacheListenerEventType [] eventTypes = (RegionCacheListenerEventType []) cacheListenerAttributes
125
- .get ("regionEventTypes " );
104
+ .get ("regionListenerEventTypes " );
126
105
registerEventHandlerToRegion (method , cacheListenerAttributes ,
127
106
new PojoRegionEventCacheListenerWrapper (method , bean , eventTypes ), RegionEvent .class );
128
107
}
@@ -142,7 +121,7 @@ private <T extends CacheEvent> void registerEventHandlerToRegion(Method method,
142
121
String [] regions = getRegionsForEventRegistration (cacheListenerAttributes .getStringArray ("regions" ),
143
122
getBeanFactory ());
144
123
145
- EventProcessorUtils .validateCacheListenerMethodParameters (method , eventClass );
124
+ EventProcessorUtils .validateEventHandlerMethodParameters (method , eventClass );
146
125
EventProcessorUtils .registerCacheListenerToRegions (regions , beanFactory , cacheListener );
147
126
}
148
127
@@ -174,11 +153,6 @@ private String[] getRegionsForEventRegistration(String[] regions, ConfigurableLi
174
153
}
175
154
}
176
155
177
- private AnnotationAttributes resolveAnnotationAttributes (Annotation annotation ) {
178
-
179
- return AnnotationAttributes .fromMap (AnnotationUtils .getAnnotationAttributes (annotation , false , true ));
180
- }
181
-
182
156
/**
183
157
* Returns a reference to the containing Spring {@link BeanFactory}.
184
158
*
@@ -200,7 +174,9 @@ protected ConfigurableListableBeanFactory getBeanFactory() {
200
174
* @see org.springframework.beans.factory.BeanFactoryAware
201
175
* @see org.springframework.beans.factory.BeanFactory
202
176
*/
203
- @ Override @ SuppressWarnings ("all" ) public final void setBeanFactory (BeanFactory beanFactory ) throws BeansException {
177
+ @ Override
178
+ @ SuppressWarnings ("all" )
179
+ public final void setBeanFactory (BeanFactory beanFactory ) throws BeansException {
204
180
205
181
Assert .isInstanceOf (ConfigurableListableBeanFactory .class , beanFactory , String
206
182
.format ("BeanFactory [%1$s] must be an instance of %2$s" , ObjectUtils .nullSafeClassName (beanFactory ),
0 commit comments