1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.config.spring.parsers.generic; |
12 | |
|
13 | |
import org.mule.config.spring.parsers.assembly.BeanAssembler; |
14 | |
import org.mule.config.spring.parsers.assembly.BeanAssemblerFactory; |
15 | |
import org.mule.config.spring.parsers.assembly.DefaultBeanAssembler; |
16 | |
import org.mule.config.spring.parsers.assembly.DefaultBeanAssemblerFactory; |
17 | |
import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration; |
18 | |
import org.springframework.beans.MutablePropertyValues; |
19 | |
import org.springframework.beans.PropertyValue; |
20 | |
import org.springframework.beans.factory.config.BeanDefinition; |
21 | |
import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
22 | |
import org.springframework.beans.factory.support.ManagedList; |
23 | |
import org.w3c.dom.Element; |
24 | |
|
25 | |
import java.util.List; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class WrappingChildDefinitionParser extends ChildDefinitionParser |
31 | |
{ |
32 | |
private final Class wrapperClass; |
33 | |
private final String propertyNameInWrapper; |
34 | |
private final String unwrappedPropertyName; |
35 | |
private final WrappingController wrappingController; |
36 | |
|
37 | |
public WrappingChildDefinitionParser(String setterMethod, |
38 | |
Class clazz, |
39 | |
Class constraint, |
40 | |
boolean allowClassAttribute, |
41 | |
Class wrapperClass, |
42 | |
String propertyNameInWrapper, |
43 | |
String unwrappedPropertyName, |
44 | |
WrappingController wrappingController) |
45 | |
{ |
46 | 0 | super(setterMethod, clazz, constraint, allowClassAttribute); |
47 | 0 | this.wrapperClass = wrapperClass; |
48 | 0 | this.propertyNameInWrapper = propertyNameInWrapper; |
49 | 0 | this.unwrappedPropertyName = unwrappedPropertyName; |
50 | 0 | this.wrappingController = wrappingController; |
51 | 0 | } |
52 | |
|
53 | |
@Override |
54 | |
public String getPropertyName(Element |
55 | |
e) |
56 | |
{ |
57 | 0 | if (!wrappingController.shouldWrap(e)) |
58 | |
{ |
59 | 0 | return unwrappedPropertyName; |
60 | |
} |
61 | |
else |
62 | |
{ |
63 | 0 | return super.getPropertyName(e); |
64 | |
} |
65 | |
} |
66 | |
|
67 | |
@Override |
68 | |
protected void preProcess(Element |
69 | |
element) |
70 | |
{ |
71 | 0 | super.preProcess(element); |
72 | 0 | if (wrappingController.shouldWrap(element)) |
73 | |
{ |
74 | 0 | setBeanAssemblerFactory(new MessageProcessorWrappingBeanAssemblerFactory(wrapperClass, propertyNameInWrapper)); |
75 | |
} |
76 | |
else |
77 | |
{ |
78 | 0 | setBeanAssemblerFactory(new DefaultBeanAssemblerFactory()); |
79 | |
} |
80 | 0 | } |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public interface WrappingController |
86 | |
{ |
87 | |
boolean shouldWrap(Element elm); |
88 | |
} |
89 | |
|
90 | |
private static class MessageProcessorWrappingBeanAssemblerFactory implements BeanAssemblerFactory |
91 | |
{ |
92 | |
private final Class wrapperClass; |
93 | |
private final String propertyNameInWrapper; |
94 | |
|
95 | |
public MessageProcessorWrappingBeanAssemblerFactory(Class wrapperClass, String propertyNameInWrapper) |
96 | 0 | { |
97 | 0 | this.wrapperClass = wrapperClass; |
98 | 0 | this.propertyNameInWrapper = propertyNameInWrapper; |
99 | 0 | } |
100 | |
|
101 | |
public BeanAssembler newBeanAssembler(PropertyConfiguration beanConfig, |
102 | |
BeanDefinitionBuilder bean, |
103 | |
PropertyConfiguration targetConfig, |
104 | |
BeanDefinition target) |
105 | |
{ |
106 | 0 | return new MessageProcessorWrappingBeanAssembler(beanConfig, bean, targetConfig, target, wrapperClass, propertyNameInWrapper); |
107 | |
} |
108 | |
} |
109 | |
|
110 | |
private static class MessageProcessorWrappingBeanAssembler extends DefaultBeanAssembler |
111 | |
{ |
112 | |
private final Class wrapperClass; |
113 | |
private final String propertyNameInWrapper; |
114 | |
|
115 | |
public MessageProcessorWrappingBeanAssembler(PropertyConfiguration beanConfig, |
116 | |
BeanDefinitionBuilder bean, |
117 | |
PropertyConfiguration targetConfig, |
118 | |
BeanDefinition target, |
119 | |
Class wrapperClass, |
120 | |
String propertyNameInWrapper) |
121 | |
{ |
122 | 0 | super(beanConfig, bean, targetConfig, target); |
123 | 0 | this.wrapperClass = wrapperClass; |
124 | 0 | this.propertyNameInWrapper = propertyNameInWrapper; |
125 | 0 | } |
126 | |
|
127 | |
public void insertBeanInTarget(String oldName) |
128 | |
{ |
129 | 0 | assertTargetPresent(); |
130 | 0 | String newName = bestGuessName(targetConfig, oldName, target.getBeanClassName()); |
131 | 0 | MutablePropertyValues targetProperties = target.getPropertyValues(); |
132 | 0 | PropertyValue pv = targetProperties.getPropertyValue(newName); |
133 | 0 | Object oldValue = null == pv ? null : pv.getValue(); |
134 | |
|
135 | 0 | BeanDefinitionBuilder wrapper = BeanDefinitionBuilder.genericBeanDefinition(wrapperClass); |
136 | 0 | wrapper.addPropertyValue(propertyNameInWrapper, bean.getBeanDefinition()); |
137 | |
|
138 | 0 | if (oldValue == null) |
139 | |
{ |
140 | 0 | oldValue = new ManagedList(); |
141 | 0 | pv = new PropertyValue(newName, oldValue); |
142 | 0 | targetProperties.addPropertyValue(pv); |
143 | |
} |
144 | 0 | if (targetConfig.isCollection(oldName)) |
145 | |
{ |
146 | 0 | List list = retrieveList(oldValue); |
147 | 0 | list.add(wrapper.getBeanDefinition()); |
148 | 0 | } |
149 | |
else |
150 | |
{ |
151 | 0 | targetProperties.addPropertyValue(newName, wrapper.getBeanDefinition()); |
152 | |
} |
153 | 0 | } |
154 | |
} |
155 | |
} |