|
|
|
@ -81,6 +81,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe |
|
|
|
|
|
|
|
|
|
|
|
private EventListener eventListener; |
|
|
|
private EventListener eventListener; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) { |
|
|
|
public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) { |
|
|
|
this.beanName = beanName; |
|
|
|
this.beanName = beanName; |
|
|
|
this.method = method; |
|
|
|
this.method = method; |
|
|
|
@ -90,6 +91,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe |
|
|
|
this.methodKey = new AnnotatedElementKey(this.method, this.targetClass); |
|
|
|
this.methodKey = new AnnotatedElementKey(this.method, this.targetClass); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Initialize this instance. |
|
|
|
* Initialize this instance. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@ -98,11 +100,40 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe |
|
|
|
this.evaluator = evaluator; |
|
|
|
this.evaluator = evaluator; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onApplicationEvent(ApplicationEvent event) { |
|
|
|
public void onApplicationEvent(ApplicationEvent event) { |
|
|
|
processEvent(event); |
|
|
|
processEvent(event); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean supportsEventType(ResolvableType eventType) { |
|
|
|
|
|
|
|
for (ResolvableType declaredEventType : this.declaredEventTypes) { |
|
|
|
|
|
|
|
if (declaredEventType.isAssignableFrom(eventType)) { |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (PayloadApplicationEvent.class.isAssignableFrom(eventType.getRawClass())) { |
|
|
|
|
|
|
|
ResolvableType payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric(); |
|
|
|
|
|
|
|
if (declaredEventType.isAssignableFrom(payloadType)) { |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return eventType.hasUnresolvableGenerics(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean supportsSourceType(Class<?> sourceType) { |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public int getOrder() { |
|
|
|
|
|
|
|
Order order = getMethodAnnotation(Order.class); |
|
|
|
|
|
|
|
return (order != null ? order.value() : 0); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Process the specified {@link ApplicationEvent}, checking if the condition |
|
|
|
* Process the specified {@link ApplicationEvent}, checking if the condition |
|
|
|
* match and handling non-null result, if any. |
|
|
|
* match and handling non-null result, if any. |
|
|
|
@ -144,7 +175,6 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected void handleResult(Object result) { |
|
|
|
protected void handleResult(Object result) { |
|
|
|
Assert.notNull(this.applicationContext, "ApplicationContext must no be null."); |
|
|
|
|
|
|
|
if (result.getClass().isArray()) { |
|
|
|
if (result.getClass().isArray()) { |
|
|
|
Object[] events = ObjectUtils.toObjectArray(result); |
|
|
|
Object[] events = ObjectUtils.toObjectArray(result); |
|
|
|
for (Object event : events) { |
|
|
|
for (Object event : events) { |
|
|
|
@ -164,6 +194,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe |
|
|
|
|
|
|
|
|
|
|
|
private void publishEvent(Object event) { |
|
|
|
private void publishEvent(Object event) { |
|
|
|
if (event != null) { |
|
|
|
if (event != null) { |
|
|
|
|
|
|
|
Assert.notNull(this.applicationContext, "ApplicationContext must no be null"); |
|
|
|
this.applicationContext.publishEvent(event); |
|
|
|
this.applicationContext.publishEvent(event); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -174,41 +205,14 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe |
|
|
|
} |
|
|
|
} |
|
|
|
String condition = getCondition(); |
|
|
|
String condition = getCondition(); |
|
|
|
if (StringUtils.hasText(condition)) { |
|
|
|
if (StringUtils.hasText(condition)) { |
|
|
|
Assert.notNull(this.evaluator, "Evaluator must no be null."); |
|
|
|
Assert.notNull(this.evaluator, "EventExpressionEvaluator must no be null"); |
|
|
|
EvaluationContext evaluationContext = this.evaluator.createEvaluationContext(event, |
|
|
|
EvaluationContext evaluationContext = this.evaluator.createEvaluationContext( |
|
|
|
this.targetClass, this.method, args); |
|
|
|
event, this.targetClass, this.method, args); |
|
|
|
return this.evaluator.condition(condition, this.methodKey, evaluationContext); |
|
|
|
return this.evaluator.condition(condition, this.methodKey, evaluationContext); |
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean supportsEventType(ResolvableType eventType) { |
|
|
|
|
|
|
|
for (ResolvableType declaredEventType : this.declaredEventTypes) { |
|
|
|
|
|
|
|
if (declaredEventType.isAssignableFrom(eventType)) { |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (PayloadApplicationEvent.class.isAssignableFrom(eventType.getRawClass())) { |
|
|
|
|
|
|
|
ResolvableType payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric(); |
|
|
|
|
|
|
|
if (declaredEventType.isAssignableFrom(payloadType)) { |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return eventType.hasUnresolvableGenerics(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean supportsSourceType(Class<?> sourceType) { |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public int getOrder() { |
|
|
|
|
|
|
|
Order order = getMethodAnnotation(Order.class); |
|
|
|
|
|
|
|
return (order != null ? order.value() : 0); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected <A extends Annotation> A getMethodAnnotation(Class<A> annotationType) { |
|
|
|
protected <A extends Annotation> A getMethodAnnotation(Class<A> annotationType) { |
|
|
|
return AnnotationUtils.findAnnotation(this.method, annotationType); |
|
|
|
return AnnotationUtils.findAnnotation(this.method, annotationType); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -246,7 +250,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe |
|
|
|
* Return the target bean instance to use. |
|
|
|
* Return the target bean instance to use. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected Object getTargetBean() { |
|
|
|
protected Object getTargetBean() { |
|
|
|
Assert.notNull(this.applicationContext, "ApplicationContext must no be null."); |
|
|
|
Assert.notNull(this.applicationContext, "ApplicationContext must no be null"); |
|
|
|
return this.applicationContext.getBean(this.beanName); |
|
|
|
return this.applicationContext.getBean(this.beanName); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -346,8 +350,8 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe |
|
|
|
private List<ResolvableType> resolveDeclaredEventTypes() { |
|
|
|
private List<ResolvableType> resolveDeclaredEventTypes() { |
|
|
|
int count = this.method.getParameterTypes().length; |
|
|
|
int count = this.method.getParameterTypes().length; |
|
|
|
if (count > 1) { |
|
|
|
if (count > 1) { |
|
|
|
throw new IllegalStateException("Maximum one parameter is allowed " + |
|
|
|
throw new IllegalStateException( |
|
|
|
"for event listener method: " + method); |
|
|
|
"Maximum one parameter is allowed for event listener method: " + this.method); |
|
|
|
} |
|
|
|
} |
|
|
|
EventListener ann = getEventListener(); |
|
|
|
EventListener ann = getEventListener(); |
|
|
|
if (ann != null && ann.classes().length > 0) { |
|
|
|
if (ann != null && ann.classes().length > 0) { |
|
|
|
@ -359,13 +363,14 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
if (count == 0) { |
|
|
|
if (count == 0) { |
|
|
|
throw new IllegalStateException("Event parameter is mandatory " + |
|
|
|
throw new IllegalStateException( |
|
|
|
"for event listener method: " + method); |
|
|
|
"Event parameter is mandatory for event listener method: " + this.method); |
|
|
|
} |
|
|
|
} |
|
|
|
return Collections.singletonList(ResolvableType.forMethodParameter(this.method, 0)); |
|
|
|
return Collections.singletonList(ResolvableType.forMethodParameter(this.method, 0)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String toString() { |
|
|
|
public String toString() { |
|
|
|
return this.method.toGenericString(); |
|
|
|
return this.method.toGenericString(); |
|
|
|
|