View Javadoc

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