1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.config.spring.parsers; |
12 | |
|
13 | |
import org.mule.util.ClassUtils; |
14 | |
import org.mule.util.StringUtils; |
15 | |
|
16 | |
import org.springframework.beans.MutablePropertyValues; |
17 | |
import org.springframework.beans.factory.config.RuntimeBeanReference; |
18 | |
import org.springframework.beans.factory.support.AbstractBeanDefinition; |
19 | |
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; |
20 | |
import org.springframework.beans.factory.xml.ParserContext; |
21 | |
import org.w3c.dom.Element; |
22 | |
|
23 | |
public class ClassOrRefDefinitionParser extends AbstractBeanDefinitionParser |
24 | |
{ |
25 | |
private String propertyName; |
26 | |
|
27 | |
public ClassOrRefDefinitionParser(String propertyName) |
28 | |
{ |
29 | 0 | super(); |
30 | |
|
31 | 0 | if (StringUtils.isEmpty(propertyName)) |
32 | |
{ |
33 | 0 | throw new IllegalArgumentException("propertyName cannot be null"); |
34 | |
} |
35 | 0 | this.propertyName = propertyName; |
36 | 0 | } |
37 | |
|
38 | |
@Override |
39 | |
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) |
40 | |
{ |
41 | 0 | MutablePropertyValues parentProps = parserContext.getContainingBeanDefinition().getPropertyValues(); |
42 | |
|
43 | 0 | String ref = element.getAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_REF); |
44 | 0 | String className = element.getAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_CLASS); |
45 | |
|
46 | 0 | if (StringUtils.isBlank(ref) && StringUtils.isBlank(className)) |
47 | |
{ |
48 | 0 | String elementName = element.getLocalName(); |
49 | 0 | throw new IllegalArgumentException("Neither ref nor class attribute specified for the " |
50 | |
+ elementName + " element"); |
51 | |
} |
52 | |
|
53 | 0 | if (StringUtils.isNotBlank(ref)) |
54 | |
{ |
55 | |
|
56 | 0 | parentProps.addPropertyValue(propertyName, new RuntimeBeanReference(ref)); |
57 | |
} |
58 | |
else |
59 | |
{ |
60 | |
|
61 | |
Object instance; |
62 | |
try |
63 | |
{ |
64 | 0 | instance = ClassUtils.instanciateClass(className, ClassUtils.NO_ARGS, getClass()); |
65 | |
} |
66 | 0 | catch (Exception e) |
67 | |
{ |
68 | 0 | throw new RuntimeException(e); |
69 | 0 | } |
70 | |
|
71 | 0 | parentProps.addPropertyValue(propertyName, instance); |
72 | |
} |
73 | |
|
74 | 0 | return null; |
75 | |
} |
76 | |
} |
77 | |
|
78 | |
|