cleanup
[cdm-vaadin.git] / src / main / java / org / springframework / context / event / DefaultPojoEventListenerManager.java
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.logging.log4j.LogManager;
19 import org.apache.logging.log4j.Logger;
20 import org.springframework.beans.BeansException;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.context.ApplicationContext;
23 import org.springframework.context.ApplicationContextAware;
24 import org.springframework.context.ApplicationListener;
25 import org.springframework.core.annotation.AnnotationUtils;
26 import org.springframework.util.ReflectionUtils;
27
28 import com.vaadin.spring.annotation.SpringComponent;
29 import com.vaadin.spring.annotation.UIScope;
30
31 /**
32 * The DefaultPojoEventListenerManager helps sending events to event listener methods defined in beans
33 * which are not manages by the Spring {@link ApplicationEventMulticaster}. The {@link ApplicationEventMulticaster}
34 * for example misses sending events to spring beans with scope "Prototype".
35 *
36 * @author a.kohlbecker
37 * @since May 29, 2017
38 */
39 @SpringComponent
40 @UIScope
41 public class DefaultPojoEventListenerManager implements PojoEventListenerManager, ApplicationContextAware, Serializable {
42
43 private static final long serialVersionUID = -6814417168274166953L;
44
45 private static final Logger logger = LogManager.getLogger();
46
47 @Autowired
48 private ApplicationEventMulticaster applicationEventMulticaster;
49
50 private final EventExpressionEvaluator evaluator = new EventExpressionEvaluator();
51
52 private Map<Object, Collection<ApplicationListener<?>>> listenerMap = new HashMap<>();
53
54 private ApplicationContext applicationContext;
55
56 @Override
57 public void addEventListeners(Object o){
58
59 Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(o.getClass());
60 for (Method method : methods) {
61 EventListener eventListener = AnnotationUtils.findAnnotation(method, EventListener.class);
62 if (eventListener == null) {
63 continue;
64 }
65 ApplicationListenerPojoMethodAdapter applicationListener = new ApplicationListenerPojoMethodAdapter(o.toString(), o.getClass(), method, o);
66 applicationListener.init(this.applicationContext, this.evaluator);
67
68 if(logger.isTraceEnabled()){
69 logger.trace(String.format("Adding ApplicationListener for %s@%s#%s", o.getClass().getSimpleName(), o.hashCode(), method.toGenericString()));
70 }
71 applicationEventMulticaster.addApplicationListener(applicationListener);
72 addToMap(o, applicationListener);
73
74 }
75 }
76
77 @Override
78 public void removeEventListeners(Object o){
79 if(listenerMap.containsKey(o)){
80 listenerMap.get(o).forEach(l -> applicationEventMulticaster.removeApplicationListener(l));
81 listenerMap.remove(o);
82 }
83 }
84
85 private void addToMap(Object o, ApplicationListener<?> applicationListener) {
86 if(!listenerMap.containsKey(o)){
87 listenerMap.put(o, new ArrayList<>());
88 }
89 listenerMap.get(o).add(applicationListener);
90 }
91
92 /**
93 * {@inheritDoc}
94 */
95 @Override
96 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
97 this.applicationContext = applicationContext;
98 }
99
100 }