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.configuration.PropertyConfiguration; |
17 | |
import org.mule.config.spring.parsers.assembly.configuration.SingleProperty; |
18 | |
|
19 | |
import org.springframework.beans.MutablePropertyValues; |
20 | |
import org.springframework.beans.factory.config.BeanDefinition; |
21 | |
import org.springframework.beans.factory.support.AbstractBeanDefinition; |
22 | |
import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
23 | |
import org.springframework.beans.factory.xml.ParserContext; |
24 | |
import org.w3c.dom.Element; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class NameTransferDefinitionParser extends ParentDefinitionParser |
44 | |
{ |
45 | |
|
46 | |
private String name; |
47 | |
private String componentAttributeValue; |
48 | |
private String componentAttributeName; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public NameTransferDefinitionParser(String componentAttributeName) |
55 | 0 | { |
56 | 0 | this.componentAttributeName = componentAttributeName; |
57 | 0 | setBeanAssemblerFactory(new LocalBeanAssemblerFactory()); |
58 | 0 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) |
64 | |
{ |
65 | 0 | name = null; |
66 | 0 | componentAttributeValue = null; |
67 | 0 | AbstractBeanDefinition bd = super.parseInternal(element, parserContext); |
68 | 0 | element.removeAttribute(ATTRIBUTE_NAME); |
69 | 0 | return bd; |
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
private void setName() |
75 | |
{ |
76 | 0 | BeanDefinition beanDef = getParserContext().getRegistry().getBeanDefinition(componentAttributeValue); |
77 | 0 | MutablePropertyValues propertyValues = beanDef.getPropertyValues(); |
78 | 0 | if (!propertyValues.contains(ATTRIBUTE_NAME)) |
79 | |
{ |
80 | 0 | logger.debug("Setting " + ATTRIBUTE_NAME + " on " + componentAttributeValue + " to " + name); |
81 | 0 | propertyValues.addPropertyValue(ATTRIBUTE_NAME, name); |
82 | |
} |
83 | |
else |
84 | |
{ |
85 | 0 | logger.debug("Not setting " + ATTRIBUTE_NAME + " on " + componentAttributeValue + |
86 | |
" as already " + propertyValues.getPropertyValue(ATTRIBUTE_NAME)); |
87 | |
} |
88 | 0 | } |
89 | |
|
90 | |
private class LocalBeanAssembler extends DefaultBeanAssembler |
91 | |
{ |
92 | |
|
93 | |
public LocalBeanAssembler(PropertyConfiguration beanConfig, BeanDefinitionBuilder bean, |
94 | |
PropertyConfiguration targetConfig, BeanDefinition target) |
95 | 0 | { |
96 | 0 | super(beanConfig, bean, targetConfig, target); |
97 | 0 | } |
98 | |
|
99 | |
protected void addPropertyWithReference(MutablePropertyValues properties, SingleProperty config, String name, Object value) |
100 | |
{ |
101 | |
|
102 | 0 | if (ATTRIBUTE_NAME.equals(name) && value instanceof String) |
103 | |
{ |
104 | 0 | NameTransferDefinitionParser.this.name = (String) value; |
105 | |
|
106 | 0 | if (null != componentAttributeValue) |
107 | |
{ |
108 | 0 | setName(); |
109 | |
} |
110 | |
} |
111 | |
else |
112 | |
{ |
113 | 0 | super.addPropertyWithReference(properties, config, name, value); |
114 | |
|
115 | |
|
116 | 0 | if (componentAttributeName.equals(name) && value instanceof String) |
117 | |
{ |
118 | 0 | componentAttributeValue = (String) value; |
119 | |
|
120 | 0 | if (null != NameTransferDefinitionParser.this.name) |
121 | |
{ |
122 | 0 | setName(); |
123 | |
} |
124 | |
} |
125 | |
} |
126 | 0 | } |
127 | |
} |
128 | |
|
129 | 0 | private class LocalBeanAssemblerFactory implements BeanAssemblerFactory |
130 | |
{ |
131 | |
|
132 | |
public BeanAssembler newBeanAssembler(PropertyConfiguration beanConfig, BeanDefinitionBuilder bean, |
133 | |
PropertyConfiguration targetConfig, BeanDefinition target) |
134 | |
{ |
135 | 0 | return new LocalBeanAssembler(beanConfig, bean, targetConfig, target); |
136 | |
} |
137 | |
|
138 | |
} |
139 | |
|
140 | |
} |