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 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 {
56 this.componentAttributeName = componentAttributeName;
57 setBeanAssemblerFactory(new LocalBeanAssemblerFactory());
58 }
59
60
61
62
63 protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext)
64 {
65 name = null;
66 componentAttributeValue = null;
67 AbstractBeanDefinition bd = super.parseInternal(element, parserContext);
68 element.removeAttribute(ATTRIBUTE_NAME);
69 return bd;
70 }
71
72
73
74 private void setName()
75 {
76 BeanDefinition beanDef = getParserContext().getRegistry().getBeanDefinition(componentAttributeValue);
77 MutablePropertyValues propertyValues = beanDef.getPropertyValues();
78 if (!propertyValues.contains(ATTRIBUTE_NAME))
79 {
80 logger.debug("Setting " + ATTRIBUTE_NAME + " on " + componentAttributeValue + " to " + name);
81 propertyValues.addPropertyValue(ATTRIBUTE_NAME, name);
82 }
83 else
84 {
85 logger.debug("Not setting " + ATTRIBUTE_NAME + " on " + componentAttributeValue +
86 " as already " + propertyValues.getPropertyValue(ATTRIBUTE_NAME));
87 }
88 }
89
90 private class LocalBeanAssembler extends DefaultBeanAssembler
91 {
92
93 public LocalBeanAssembler(PropertyConfiguration beanConfig, BeanDefinitionBuilder bean,
94 PropertyConfiguration targetConfig, BeanDefinition target)
95 {
96 super(beanConfig, bean, targetConfig, target);
97 }
98
99 protected void addPropertyWithReference(MutablePropertyValues properties, SingleProperty config, String name, Object value)
100 {
101
102 if (ATTRIBUTE_NAME.equals(name) && value instanceof String)
103 {
104 NameTransferDefinitionParser.this.name = (String) value;
105
106 if (null != componentAttributeValue)
107 {
108 setName();
109 }
110 }
111 else
112 {
113 super.addPropertyWithReference(properties, config, name, value);
114
115
116 if (componentAttributeName.equals(name) && value instanceof String)
117 {
118 componentAttributeValue = (String) value;
119
120 if (null != NameTransferDefinitionParser.this.name)
121 {
122 setName();
123 }
124 }
125 }
126 }
127 }
128
129 private class LocalBeanAssemblerFactory implements BeanAssemblerFactory
130 {
131
132 public BeanAssembler newBeanAssembler(PropertyConfiguration beanConfig, BeanDefinitionBuilder bean,
133 PropertyConfiguration targetConfig, BeanDefinition target)
134 {
135 return new LocalBeanAssembler(beanConfig, bean, targetConfig, target);
136 }
137
138 }
139
140 }