1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package org.springframework.context.event;
|
10
|
|
11
|
import java.io.Serializable;
|
12
|
import java.lang.reflect.Method;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.Collection;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.Map;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.springframework.beans.BeansException;
|
20
|
import org.springframework.beans.factory.annotation.Autowired;
|
21
|
import org.springframework.context.ApplicationContext;
|
22
|
import org.springframework.context.ApplicationContextAware;
|
23
|
import org.springframework.context.ApplicationListener;
|
24
|
import org.springframework.core.annotation.AnnotationUtils;
|
25
|
import org.springframework.util.ReflectionUtils;
|
26
|
|
27
|
import com.vaadin.spring.annotation.SpringComponent;
|
28
|
import com.vaadin.spring.annotation.UIScope;
|
29
|
|
30
|
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
|
31
|
|
32
|
/**
|
33
|
* The DefaultPojoEventListenerManager helps sending events to event listener methods defined in beans
|
34
|
* which are not manages by the Spring {@link ApplicationEventMulticaster}. The {@link ApplicationEventMulticaster}
|
35
|
* for example misses sending events to spring beans with scope "Prototype".
|
36
|
*
|
37
|
* @author a.kohlbecker
|
38
|
* @since May 29, 2017
|
39
|
*
|
40
|
*/
|
41
|
@SpringComponent
|
42
|
@UIScope
|
43
|
public class DefaultPojoEventListenerManager implements PojoEventListenerManager, ApplicationContextAware, Serializable {
|
44
|
|
45
|
private static final long serialVersionUID = -6814417168274166953L;
|
46
|
|
47
|
public static final Logger logger = Logger.getLogger(AbstractPresenter.class);
|
48
|
|
49
|
@Autowired
|
50
|
private ApplicationEventMulticaster applicationEventMulticaster;
|
51
|
|
52
|
private final EventExpressionEvaluator evaluator = new EventExpressionEvaluator();
|
53
|
|
54
|
private Map<Object, Collection<ApplicationListener<?>>> listenerMap = new HashMap<>();
|
55
|
|
56
|
private ApplicationContext applicationContext;
|
57
|
|
58
|
@Override
|
59
|
public void addEventListeners(Object o){
|
60
|
|
61
|
Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(o.getClass());
|
62
|
for (Method method : methods) {
|
63
|
EventListener eventListener = AnnotationUtils.findAnnotation(method, EventListener.class);
|
64
|
if (eventListener == null) {
|
65
|
continue;
|
66
|
}
|
67
|
ApplicationListenerPojoMethodAdapter applicationListener = new ApplicationListenerPojoMethodAdapter(o.toString(), o.getClass(), method, o);
|
68
|
applicationListener.init(this.applicationContext, this.evaluator);
|
69
|
|
70
|
if(logger.isTraceEnabled()){
|
71
|
logger.trace(String.format("Adding ApplicationListener for %s@%s#%s", o.getClass().getSimpleName(), o.hashCode(), method.toGenericString()));
|
72
|
}
|
73
|
applicationEventMulticaster.addApplicationListener(applicationListener);
|
74
|
addToMap(o, applicationListener);
|
75
|
|
76
|
}
|
77
|
}
|
78
|
|
79
|
@Override
|
80
|
public void removeEventListeners(Object o){
|
81
|
if(listenerMap.containsKey(o)){
|
82
|
listenerMap.get(o).forEach(l -> applicationEventMulticaster.removeApplicationListener(l));
|
83
|
listenerMap.remove(o);
|
84
|
}
|
85
|
}
|
86
|
|
87
|
private void addToMap(Object o, ApplicationListener<?> applicationListener) {
|
88
|
if(!listenerMap.containsKey(o)){
|
89
|
listenerMap.put(o, new ArrayList<>());
|
90
|
}
|
91
|
listenerMap.get(o).add(applicationListener);
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* {@inheritDoc}
|
96
|
*/
|
97
|
@Override
|
98
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
99
|
this.applicationContext = applicationContext;
|
100
|
}
|
101
|
|
102
|
}
|