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