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