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 super();
26
27 if (StringUtils.isEmpty(propertyName))
28 {
29 throw new IllegalArgumentException("propertyName cannot be null");
30 }
31 this.propertyName = propertyName;
32 }
33
34 @Override
35 protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext)
36 {
37 MutablePropertyValues parentProps = parserContext.getContainingBeanDefinition().getPropertyValues();
38
39 String ref = element.getAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_REF);
40 String className = element.getAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_CLASS);
41
42 if (StringUtils.isBlank(ref) && StringUtils.isBlank(className))
43 {
44 String elementName = element.getLocalName();
45 throw new IllegalArgumentException("Neither ref nor class attribute specified for the "
46 + elementName + " element");
47 }
48
49 if (StringUtils.isNotBlank(ref))
50 {
51
52 parentProps.addPropertyValue(propertyName, new RuntimeBeanReference(ref));
53 }
54 else
55 {
56
57 Object instance;
58 try
59 {
60 instance = ClassUtils.instanciateClass(className, ClassUtils.NO_ARGS, getClass());
61 }
62 catch (Exception e)
63 {
64 throw new RuntimeException(e);
65 }
66
67 parentProps.addPropertyValue(propertyName, instance);
68 }
69
70 return null;
71 }
72 }
73
74