1 | |
package org.kuali.spring.util; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.List; |
5 | |
|
6 | |
import org.kuali.spring.util.event.BeanVisitEvent; |
7 | |
import org.kuali.spring.util.event.PropertyValueVisitEvent; |
8 | |
import org.kuali.spring.util.event.ValueResolutionEvent; |
9 | |
import org.kuali.spring.util.event.VisitListener; |
10 | |
import org.slf4j.Logger; |
11 | |
import org.slf4j.LoggerFactory; |
12 | |
import org.springframework.beans.MutablePropertyValues; |
13 | |
import org.springframework.beans.PropertyValue; |
14 | |
import org.springframework.beans.factory.config.BeanDefinition; |
15 | |
import org.springframework.beans.factory.config.BeanDefinitionVisitor; |
16 | |
import org.springframework.util.ObjectUtils; |
17 | |
import org.springframework.util.StringValueResolver; |
18 | |
|
19 | |
public class EnhancedBeanDefinitionVisitor extends BeanDefinitionVisitor { |
20 | 1 | final Logger logger = LoggerFactory.getLogger(EnhancedBeanDefinitionVisitor.class); |
21 | 1 | List<VisitListener> listeners = new ArrayList<VisitListener>(); |
22 | |
StringValueResolver valueResolver; |
23 | |
|
24 | |
public EnhancedBeanDefinitionVisitor() { |
25 | 1 | this(null); |
26 | 1 | } |
27 | |
|
28 | |
public EnhancedBeanDefinitionVisitor(StringValueResolver valueResolver) { |
29 | 1 | super(); |
30 | 1 | this.valueResolver = valueResolver; |
31 | 1 | } |
32 | |
|
33 | |
@Override |
34 | |
protected String resolveStringValue(String strVal) { |
35 | 38 | if (this.valueResolver == null) { |
36 | 0 | throw new IllegalStateException("No StringValueResolver specified"); |
37 | |
} |
38 | 38 | String resolvedValue = this.valueResolver.resolveStringValue(strVal); |
39 | |
|
40 | 38 | return (strVal.equals(resolvedValue) ? strVal : resolvedValue); |
41 | |
} |
42 | |
|
43 | |
public void addListener(VisitListener listener) { |
44 | 1 | listeners.add(listener); |
45 | 1 | } |
46 | |
|
47 | |
@Override |
48 | |
protected Object resolveValue(Object value) { |
49 | 12 | Object newValue = super.resolveValue(value); |
50 | 12 | valueResolved(value, newValue); |
51 | 12 | return newValue; |
52 | |
} |
53 | |
|
54 | |
@Override |
55 | |
public void visitBeanDefinition(BeanDefinition beanDefinition) { |
56 | 13 | beforeVisit(beanDefinition); |
57 | 13 | super.visitBeanDefinition(beanDefinition); |
58 | 13 | afterVisit(beanDefinition); |
59 | 13 | } |
60 | |
|
61 | |
@Override |
62 | |
protected void visitPropertyValues(MutablePropertyValues pvs) { |
63 | 13 | PropertyValue[] pvArray = pvs.getPropertyValues(); |
64 | 25 | for (PropertyValue pv : pvArray) { |
65 | 12 | visitPropertyValue(pvs, pv); |
66 | |
} |
67 | 13 | } |
68 | |
|
69 | |
protected void visitPropertyValue(MutablePropertyValues pvs, PropertyValue pv) { |
70 | 12 | beforeVisit(pvs, pv); |
71 | 12 | Object oldVal = pv.getValue(); |
72 | 12 | Object newVal = resolveValue(pv.getValue()); |
73 | 12 | if (!ObjectUtils.nullSafeEquals(newVal, oldVal)) { |
74 | 0 | pvs.add(pv.getName(), newVal); |
75 | |
} |
76 | 12 | afterVisit(pvs, pv, oldVal, newVal); |
77 | 12 | } |
78 | |
|
79 | |
protected void beforeVisit(MutablePropertyValues pvs, PropertyValue pv) { |
80 | 12 | PropertyValueVisitEvent event = new PropertyValueVisitEvent(pvs, pv); |
81 | 12 | for (VisitListener listener : listeners) { |
82 | 12 | listener.beforeVisit(event); |
83 | |
} |
84 | 12 | } |
85 | |
|
86 | |
protected void afterVisit(MutablePropertyValues pvs, PropertyValue pv, Object oldVal, Object newVal) { |
87 | 12 | PropertyValueVisitEvent event = new PropertyValueVisitEvent(pvs, pv, oldVal, newVal); |
88 | 12 | for (VisitListener listener : listeners) { |
89 | 12 | listener.afterVisit(event); |
90 | |
} |
91 | 12 | } |
92 | |
|
93 | |
protected void beforeVisit(BeanDefinition beanDefinition) { |
94 | 13 | BeanVisitEvent event = new BeanVisitEvent(beanDefinition); |
95 | 13 | for (VisitListener listener : listeners) { |
96 | 13 | listener.beforeVisit(event); |
97 | |
} |
98 | 13 | } |
99 | |
|
100 | |
protected void afterVisit(BeanDefinition beanDefinition) { |
101 | 13 | BeanVisitEvent event = new BeanVisitEvent(beanDefinition); |
102 | 13 | for (VisitListener listener : listeners) { |
103 | 13 | listener.afterVisit(event); |
104 | |
} |
105 | 13 | } |
106 | |
|
107 | |
protected void valueResolved(Object oldValue, Object newValue) { |
108 | 12 | ValueResolutionEvent event = new ValueResolutionEvent(oldValue, newValue); |
109 | 12 | for (VisitListener listener : listeners) { |
110 | 12 | listener.valueResolved(event); |
111 | |
} |
112 | 12 | } |
113 | |
|
114 | |
public List<VisitListener> getListeners() { |
115 | 0 | return listeners; |
116 | |
} |
117 | |
|
118 | |
public void setListeners(List<VisitListener> listeners) { |
119 | 0 | this.listeners = listeners; |
120 | 0 | } |
121 | |
|
122 | |
public StringValueResolver getValueResolver() { |
123 | 0 | return valueResolver; |
124 | |
} |
125 | |
|
126 | |
public void setValueResolver(StringValueResolver valueResolver) { |
127 | 1 | this.valueResolver = valueResolver; |
128 | 1 | } |
129 | |
|
130 | |
} |