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