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