View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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   * Configures a FunctionalTestComponent wrapped as a JavaComponent.  This parser provides a short form way of
36   * configuring a test component in Mule.
37   */
38  //TODO This should really extend StaticComponentDefinitionParser from mule-core as it is quite similar.
39  public class TestComponentDefinitionParser extends ComponentDefinitionParser
40  {
41      private static Class OBJECT_FACTORY_TYPE = SingletonObjectFactory.class;
42      private Class componentInstanceClass = FunctionalTestComponent.class;
43  
44      public TestComponentDefinitionParser()
45      {
46          super(DefaultJavaComponent.class);
47          addIgnored("appendString");
48          addIgnored("enableMessageHistory");
49          addIgnored("enableNotifications");
50          addIgnored("throwException");
51          addIgnored("exceptionToThrow");
52          addIgnored("waitTime");
53          addIgnored("doInboundTransform");
54          addIgnored("logMessageDetails");
55      }
56  
57      public TestComponentDefinitionParser(Class componentInstanceClass)
58      {
59          this();
60          this.componentInstanceClass = componentInstanceClass;
61      }
62  
63      protected void parseChild(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
64      {
65          // Create a BeanDefinition for the nested object factory and set it a
66          // property value for the component
67          AbstractBeanDefinition objectFactoryBeanDefinition = new GenericBeanDefinition();
68          objectFactoryBeanDefinition.setBeanClass(OBJECT_FACTORY_TYPE);
69          objectFactoryBeanDefinition.getPropertyValues().addPropertyValue(AbstractObjectFactory.ATTRIBUTE_OBJECT_CLASS,
70                  componentInstanceClass);
71          objectFactoryBeanDefinition.setInitMethodName(Initialisable.PHASE_NAME);
72          objectFactoryBeanDefinition.setDestroyMethodName(Disposable.PHASE_NAME);
73          Map props = new HashMap();
74          for (int i = 0; i < element.getAttributes().getLength(); i++)
75          {
76              Node n = element.getAttributes().item(i);
77                  props.put(n.getLocalName(), n.getNodeValue());
78          }
79          String returnData = null;
80  
81          NodeList list = element.getChildNodes();
82          for (int i = 0; i < list.getLength(); i++)
83          {
84              if ("return-data".equals(list.item(i).getLocalName()))
85              {
86                  Element rData = (Element) list.item(i);
87                  if (StringUtils.isNotEmpty(rData.getAttribute("file")))
88                  {
89                      String file = rData.getAttribute("file");
90                      try
91                      {
92                          returnData = IOUtils.getResourceAsString(file, getClass());
93                      }
94                      catch (IOException e)
95                      {
96                          throw new BeanCreationException("Failed to load test-data resource: " + file, e);
97                      }
98                  }
99                  else
100                 {
101                     returnData = rData.getTextContent();
102                 }
103             }
104             else if ("callback".equals(list.item(i).getLocalName()))
105             {
106                 Element ele = (Element) list.item(i);
107                 String c = ele.getAttribute("class");
108                 try
109                 {
110                     EventCallback cb = (EventCallback)ClassUtils.instanciateClass(c);
111                     props.put("eventCallback", cb);
112 
113                 }
114                 catch (Exception e)
115                 {
116                     throw new BeanCreationException("Failed to load event-callback: " + c, e);
117                 }
118             }
119 
120         }
121 
122         if (returnData != null)
123         {
124             props.put("returnData", returnData);
125         }
126         objectFactoryBeanDefinition.getPropertyValues().addPropertyValue("properties", props);
127 
128         builder.addPropertyValue("objectFactory", objectFactoryBeanDefinition);
129 
130         super.parseChild(element, parserContext, builder);
131     }
132 
133 }