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