1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.tck.config; |
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.config.spring.parsers.specific.ComponentDefinitionParser; |
13 | |
import org.mule.object.AbstractObjectFactory; |
14 | |
import org.mule.object.SingletonObjectFactory; |
15 | |
import org.mule.tck.functional.EventCallback; |
16 | |
import org.mule.tck.functional.FunctionalTestComponent; |
17 | |
import org.mule.util.ClassUtils; |
18 | |
import org.mule.util.IOUtils; |
19 | |
import org.mule.util.StringUtils; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.util.HashMap; |
23 | |
import java.util.Map; |
24 | |
|
25 | |
import org.springframework.beans.factory.BeanCreationException; |
26 | |
import org.springframework.beans.factory.support.AbstractBeanDefinition; |
27 | |
import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
28 | |
import org.springframework.beans.factory.support.GenericBeanDefinition; |
29 | |
import org.springframework.beans.factory.xml.ParserContext; |
30 | |
import org.w3c.dom.Element; |
31 | |
import org.w3c.dom.Node; |
32 | |
import org.w3c.dom.NodeList; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class TestComponentDefinitionParser extends ComponentDefinitionParser |
40 | |
{ |
41 | 0 | private static Class OBJECT_FACTORY_TYPE = SingletonObjectFactory.class; |
42 | 0 | private Class componentInstanceClass = FunctionalTestComponent.class; |
43 | |
|
44 | |
public TestComponentDefinitionParser() |
45 | |
{ |
46 | 0 | super(DefaultJavaComponent.class); |
47 | 0 | addIgnored("appendString"); |
48 | 0 | addIgnored("enableMessageHistory"); |
49 | 0 | addIgnored("enableNotifications"); |
50 | 0 | addIgnored("throwException"); |
51 | 0 | addIgnored("exceptionToThrow"); |
52 | 0 | addIgnored("waitTime"); |
53 | 0 | addIgnored("doInboundTransform"); |
54 | 0 | addIgnored("logMessageDetails"); |
55 | 0 | } |
56 | |
|
57 | |
public TestComponentDefinitionParser(Class componentInstanceClass) |
58 | |
{ |
59 | 0 | this(); |
60 | 0 | this.componentInstanceClass = componentInstanceClass; |
61 | 0 | } |
62 | |
|
63 | |
protected void parseChild(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) |
64 | |
{ |
65 | |
|
66 | |
|
67 | 0 | AbstractBeanDefinition objectFactoryBeanDefinition = new GenericBeanDefinition(); |
68 | 0 | objectFactoryBeanDefinition.setBeanClass(OBJECT_FACTORY_TYPE); |
69 | 0 | objectFactoryBeanDefinition.getPropertyValues().addPropertyValue(AbstractObjectFactory.ATTRIBUTE_OBJECT_CLASS, |
70 | |
componentInstanceClass); |
71 | 0 | objectFactoryBeanDefinition.setInitMethodName(Initialisable.PHASE_NAME); |
72 | 0 | objectFactoryBeanDefinition.setDestroyMethodName(Disposable.PHASE_NAME); |
73 | 0 | Map props = new HashMap(); |
74 | 0 | for (int i = 0; i < element.getAttributes().getLength(); i++) |
75 | |
{ |
76 | 0 | Node n = element.getAttributes().item(i); |
77 | 0 | props.put(n.getLocalName(), n.getNodeValue()); |
78 | |
} |
79 | 0 | String returnData = null; |
80 | |
|
81 | 0 | NodeList list = element.getChildNodes(); |
82 | 0 | for (int i = 0; i < list.getLength(); i++) |
83 | |
{ |
84 | 0 | if ("return-data".equals(list.item(i).getLocalName())) |
85 | |
{ |
86 | 0 | Element rData = (Element) list.item(i); |
87 | 0 | if (StringUtils.isNotEmpty(rData.getAttribute("file"))) |
88 | |
{ |
89 | 0 | String file = rData.getAttribute("file"); |
90 | |
try |
91 | |
{ |
92 | 0 | returnData = IOUtils.getResourceAsString(file, getClass()); |
93 | |
} |
94 | 0 | catch (IOException e) |
95 | |
{ |
96 | 0 | throw new BeanCreationException("Failed to load test-data resource: " + file, e); |
97 | 0 | } |
98 | 0 | } |
99 | |
else |
100 | |
{ |
101 | 0 | returnData = rData.getTextContent(); |
102 | |
} |
103 | 0 | } |
104 | 0 | else if ("callback".equals(list.item(i).getLocalName())) |
105 | |
{ |
106 | 0 | Element ele = (Element) list.item(i); |
107 | 0 | String c = ele.getAttribute("class"); |
108 | |
try |
109 | |
{ |
110 | 0 | EventCallback cb = (EventCallback)ClassUtils.instanciateClass(c); |
111 | 0 | props.put("eventCallback", cb); |
112 | |
|
113 | |
} |
114 | 0 | catch (Exception e) |
115 | |
{ |
116 | 0 | throw new BeanCreationException("Failed to load event-callback: " + c, e); |
117 | 0 | } |
118 | |
} |
119 | |
|
120 | |
} |
121 | |
|
122 | 0 | if (returnData != null) |
123 | |
{ |
124 | 0 | props.put("returnData", returnData); |
125 | |
} |
126 | 0 | objectFactoryBeanDefinition.getPropertyValues().addPropertyValue("properties", props); |
127 | |
|
128 | 0 | builder.addPropertyValue("objectFactory", objectFactoryBeanDefinition); |
129 | |
|
130 | 0 | super.parseChild(element, parserContext, builder); |
131 | 0 | } |
132 | |
|
133 | |
} |