View Javadoc

1   /*
2    * $Id: TestComponentDefinitionParser.java 20321 2010-11-24 15:21:24Z dfeist $
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("waitTime");
56          addIgnored("doInboundTransform");
57          addIgnored("logMessageDetails");
58      }
59  
60      public TestComponentDefinitionParser(Class componentInstanceClass)
61      {
62          this();
63          this.componentInstanceClass = componentInstanceClass;
64      }
65  
66      protected void parseChild(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
67      {
68          // Create a BeanDefinition for the nested object factory and set it a
69          // property value for the component
70          AbstractBeanDefinition objectFactoryBeanDefinition = new GenericBeanDefinition();
71          objectFactoryBeanDefinition.setBeanClass(OBJECT_FACTORY_TYPE);
72          objectFactoryBeanDefinition.getPropertyValues().addPropertyValue(AbstractObjectFactory.ATTRIBUTE_OBJECT_CLASS,
73                  componentInstanceClass);
74          objectFactoryBeanDefinition.setInitMethodName(Initialisable.PHASE_NAME);
75          objectFactoryBeanDefinition.setDestroyMethodName(Disposable.PHASE_NAME);
76          Map props = new HashMap();
77          for (int i = 0; i < element.getAttributes().getLength(); i++)
78          {
79              Node n = element.getAttributes().item(i);
80                  props.put(n.getLocalName(), n.getNodeValue());
81          }
82          String returnData = null;
83  
84          NodeList list = element.getChildNodes();
85          for (int i = 0; i < list.getLength(); i++)
86          {
87              if ("return-data".equals(list.item(i).getLocalName()))
88              {
89                  Element rData = (Element) list.item(i);
90                  if (StringUtils.isNotEmpty(rData.getAttribute("file")))
91                  {
92                      String file = rData.getAttribute("file");
93                      try
94                      {
95                          returnData = IOUtils.getResourceAsString(file, getClass());
96                      }
97                      catch (IOException e)
98                      {
99                          throw new BeanCreationException("Failed to load test-data resource: " + file, e);
100                     }
101                 }
102                 else
103                 {
104                     returnData = rData.getTextContent();
105                 }
106             }
107             else if ("callback".equals(list.item(i).getLocalName()))
108             {
109                 Element ele = (Element) list.item(i);
110                 String c = ele.getAttribute("class");
111                 try
112                 {
113                     EventCallback cb = (EventCallback)ClassUtils.instanciateClass(c);
114                     props.put("eventCallback", cb);
115 
116                 }
117                 catch (Exception e)
118                 {
119                     throw new BeanCreationException("Failed to load event-callback: " + c, e);
120                 }
121             }
122 
123         }
124 
125         if (returnData != null)
126         {
127             props.put("returnData", returnData);
128         }
129         objectFactoryBeanDefinition.getPropertyValues().addPropertyValue("properties", props);
130 
131         builder.addPropertyValue("objectFactory", objectFactoryBeanDefinition);
132 
133         super.parseChild(element, parserContext, builder);
134     }
135 
136 }