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