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 | |
public class TestComponentDefinitionParser extends ComponentDefinitionParser |
42 | |
{ |
43 | 2 | private static Class OBJECT_FACTORY_TYPE = SingletonObjectFactory.class; |
44 | 6 | private Class componentInstanceClass = FunctionalTestComponent.class; |
45 | |
|
46 | |
public TestComponentDefinitionParser() |
47 | |
{ |
48 | 6 | super(DefaultJavaComponent.class); |
49 | 6 | addIgnored("appendString"); |
50 | 6 | addIgnored("enableMessageHistory"); |
51 | 6 | addIgnored("enableNotifications"); |
52 | 6 | addIgnored("throwException"); |
53 | 6 | addIgnored("exceptionToThrow"); |
54 | 6 | addIgnored("waitTime"); |
55 | 6 | addIgnored("doInboundTransform"); |
56 | 6 | } |
57 | |
|
58 | |
protected void parseChild(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) |
59 | |
{ |
60 | 18 | Element parent = (Element) element.getParentNode(); |
61 | 18 | String serviceName = parent.getAttribute(ATTRIBUTE_NAME); |
62 | 18 | builder.addPropertyReference("service", serviceName); |
63 | |
|
64 | |
|
65 | |
|
66 | 18 | AbstractBeanDefinition objectFactoryBeanDefinition = new GenericBeanDefinition(); |
67 | 18 | objectFactoryBeanDefinition.setBeanClass(OBJECT_FACTORY_TYPE); |
68 | 18 | objectFactoryBeanDefinition.getPropertyValues().addPropertyValue(AbstractObjectFactory.ATTRIBUTE_OBJECT_CLASS, |
69 | |
componentInstanceClass); |
70 | 18 | objectFactoryBeanDefinition.setInitMethodName(Initialisable.PHASE_NAME); |
71 | 18 | objectFactoryBeanDefinition.setDestroyMethodName(Disposable.PHASE_NAME); |
72 | 18 | Map props = new HashMap(); |
73 | 54 | for (int i = 0; i < element.getAttributes().getLength(); i++) |
74 | |
{ |
75 | 36 | Node n = element.getAttributes().item(i); |
76 | 36 | props.put(n.getLocalName(), n.getNodeValue()); |
77 | |
} |
78 | 18 | String returnData = null; |
79 | |
|
80 | 18 | NodeList list = element.getChildNodes(); |
81 | 78 | for (int i = 0; i < list.getLength(); i++) |
82 | |
{ |
83 | 60 | if ("return-data".equals(list.item(i).getLocalName())) |
84 | |
{ |
85 | 12 | Element rData = (Element) list.item(i); |
86 | 12 | if (StringUtils.isNotEmpty(rData.getAttribute("file"))) |
87 | |
{ |
88 | 6 | String file = rData.getAttribute("file"); |
89 | |
try |
90 | |
{ |
91 | 6 | returnData = IOUtils.getResourceAsString(file, getClass()); |
92 | |
} |
93 | 0 | catch (IOException e) |
94 | |
{ |
95 | 0 | throw new BeanCreationException("Failed to load test-data resource: " + file, e); |
96 | 6 | } |
97 | 6 | } |
98 | |
else |
99 | |
{ |
100 | 6 | returnData = rData.getTextContent(); |
101 | |
} |
102 | 12 | } |
103 | 48 | else if ("callback".equals(list.item(i).getLocalName())) |
104 | |
{ |
105 | 12 | Element ele = (Element) list.item(i); |
106 | 12 | String c = ele.getAttribute("class"); |
107 | |
try |
108 | |
{ |
109 | 12 | EventCallback cb = (EventCallback)ClassUtils.instanciateClass(c, ClassUtils.NO_ARGS); |
110 | 12 | props.put("eventCallback", cb); |
111 | |
|
112 | |
} |
113 | 0 | catch (Exception e) |
114 | |
{ |
115 | 0 | throw new BeanCreationException("Failed to load event-callback: " + c, e); |
116 | 12 | } |
117 | |
} |
118 | |
|
119 | |
} |
120 | |
|
121 | 18 | if (returnData != null) |
122 | |
{ |
123 | 12 | props.put("returnData", returnData); |
124 | |
} |
125 | 18 | objectFactoryBeanDefinition.getPropertyValues().addPropertyValue("properties", props); |
126 | |
|
127 | 18 | builder.addPropertyValue("objectFactory", objectFactoryBeanDefinition); |
128 | |
|
129 | 18 | super.parseChild(element, parserContext, builder); |
130 | 18 | } |
131 | |
|
132 | |
} |