2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package org
.springframework
.context
.event
;
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
;
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
;
28 import com
.vaadin
.spring
.annotation
.SpringComponent
;
29 import com
.vaadin
.spring
.annotation
.UIScope
;
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".
36 * @author a.kohlbecker
41 public class DefaultPojoEventListenerManager
implements PojoEventListenerManager
, ApplicationContextAware
, Serializable
{
43 private static final long serialVersionUID
= -6814417168274166953L;
45 private final static Logger logger
= LogManager
.getLogger();
48 private ApplicationEventMulticaster applicationEventMulticaster
;
50 private final EventExpressionEvaluator evaluator
= new EventExpressionEvaluator();
52 private Map
<Object
, Collection
<ApplicationListener
<?
>>> listenerMap
= new HashMap
<>();
54 private ApplicationContext applicationContext
;
57 public void addEventListeners(Object o
){
59 Method
[] methods
= ReflectionUtils
.getUniqueDeclaredMethods(o
.getClass());
60 for (Method method
: methods
) {
61 EventListener eventListener
= AnnotationUtils
.findAnnotation(method
, EventListener
.class);
62 if (eventListener
== null) {
65 ApplicationListenerPojoMethodAdapter applicationListener
= new ApplicationListenerPojoMethodAdapter(o
.toString(), o
.getClass(), method
, o
);
66 applicationListener
.init(this.applicationContext
, this.evaluator
);
68 if(logger
.isTraceEnabled()){
69 logger
.trace(String
.format("Adding ApplicationListener for %s@%s#%s", o
.getClass().getSimpleName(), o
.hashCode(), method
.toGenericString()));
71 applicationEventMulticaster
.addApplicationListener(applicationListener
);
72 addToMap(o
, applicationListener
);
78 public void removeEventListeners(Object o
){
79 if(listenerMap
.containsKey(o
)){
80 listenerMap
.get(o
).forEach(l
-> applicationEventMulticaster
.removeApplicationListener(l
));
81 listenerMap
.remove(o
);
85 private void addToMap(Object o
, ApplicationListener
<?
> applicationListener
) {
86 if(!listenerMap
.containsKey(o
)){
87 listenerMap
.put(o
, new ArrayList
<>());
89 listenerMap
.get(o
).add(applicationListener
);
96 public void setApplicationContext(ApplicationContext applicationContext
) throws BeansException
{
97 this.applicationContext
= applicationContext
;