1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.config.spring.parsers.specific; |
12 | |
|
13 | |
import org.mule.api.lifecycle.Disposable; |
14 | |
import org.mule.api.lifecycle.Initialisable; |
15 | |
import org.mule.component.DefaultJavaComponent; |
16 | |
import org.mule.object.AbstractObjectFactory; |
17 | |
import org.mule.object.SingletonObjectFactory; |
18 | |
|
19 | |
import java.util.HashMap; |
20 | |
import java.util.Map; |
21 | |
|
22 | |
import org.springframework.beans.factory.support.AbstractBeanDefinition; |
23 | |
import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
24 | |
import org.springframework.beans.factory.support.GenericBeanDefinition; |
25 | |
import org.springframework.beans.factory.xml.ParserContext; |
26 | |
import org.w3c.dom.Element; |
27 | |
import org.w3c.dom.NamedNodeMap; |
28 | |
import org.w3c.dom.Node; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class SimpleComponentDefinitionParser extends ComponentDefinitionParser |
42 | |
{ |
43 | 0 | private static Class OBJECT_FACTORY_TYPE = SingletonObjectFactory.class; |
44 | |
private Class componentInstanceClass; |
45 | 0 | private Map properties = new HashMap(); |
46 | |
|
47 | |
public SimpleComponentDefinitionParser(Class component, Class componentInstanceClass) |
48 | |
{ |
49 | 0 | super(DefaultJavaComponent.class); |
50 | 0 | this.componentInstanceClass = componentInstanceClass; |
51 | 0 | } |
52 | |
|
53 | |
@Override |
54 | |
protected void preProcess(Element element) |
55 | |
{ |
56 | 0 | super.preProcess(element); |
57 | |
|
58 | 0 | NamedNodeMap attrs = element.getAttributes(); |
59 | |
|
60 | 0 | int numAttrs = attrs.getLength(); |
61 | |
Node attr; |
62 | 0 | for (int i = 0; i < numAttrs; ++i) |
63 | |
{ |
64 | 0 | attr = attrs.item(i); |
65 | 0 | properties.put(attr.getNodeName(), attr.getNodeValue()); |
66 | 0 | attrs.removeNamedItem(attr.getNodeName()); |
67 | |
} |
68 | 0 | } |
69 | |
|
70 | |
protected void parseChild(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) |
71 | |
{ |
72 | |
|
73 | |
|
74 | 0 | builder.addPropertyValue("objectFactory", getObjectFactoryDefinition(element)); |
75 | |
|
76 | 0 | super.parseChild(element, parserContext, builder); |
77 | 0 | } |
78 | |
|
79 | |
protected AbstractBeanDefinition getObjectFactoryDefinition(Element element) |
80 | |
{ |
81 | 0 | AbstractBeanDefinition objectFactoryBeanDefinition = new GenericBeanDefinition(); |
82 | 0 | objectFactoryBeanDefinition.setBeanClass(OBJECT_FACTORY_TYPE); |
83 | 0 | objectFactoryBeanDefinition.getPropertyValues() |
84 | |
.addPropertyValue(AbstractObjectFactory.ATTRIBUTE_OBJECT_CLASS, componentInstanceClass); |
85 | 0 | objectFactoryBeanDefinition.getPropertyValues().addPropertyValue("properties", properties); |
86 | |
|
87 | |
|
88 | 0 | objectFactoryBeanDefinition.setInitMethodName(Initialisable.PHASE_NAME); |
89 | 0 | objectFactoryBeanDefinition.setDestroyMethodName(Disposable.PHASE_NAME); |
90 | 0 | return objectFactoryBeanDefinition; |
91 | |
} |
92 | |
} |