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.config.spring.parsers.specific;
8   
9   import org.mule.component.DefaultJavaComponent;
10  import org.mule.component.simple.StaticComponent;
11  import org.mule.util.IOUtils;
12  import org.mule.util.StringUtils;
13  
14  import java.io.IOException;
15  import java.util.HashMap;
16  import java.util.Map;
17  
18  import org.springframework.beans.factory.BeanCreationException;
19  import org.springframework.beans.factory.support.AbstractBeanDefinition;
20  import org.w3c.dom.Element;
21  import org.w3c.dom.NodeList;
22  
23  /**
24   * BDP which parses the <return-data> element of the <static-component>.
25   */
26  public class StaticComponentDefinitionParser extends SimpleComponentDefinitionParser
27  {
28      public StaticComponentDefinitionParser()
29      {
30          super(DefaultJavaComponent.class, StaticComponent.class);
31      }
32  
33      @Override
34      protected AbstractBeanDefinition getObjectFactoryDefinition(Element element)
35      {
36          AbstractBeanDefinition objectFactoryBeanDefinition = super.getObjectFactoryDefinition(element);
37          
38          String returnData = null;
39  
40          NodeList list = element.getChildNodes();
41          for (int i = 0; i < list.getLength(); i++)
42          {
43              if ("return-data".equals(list.item(i).getLocalName()))
44              {
45                  Element rData = (Element) list.item(i);
46                  if (StringUtils.isNotEmpty(rData.getAttribute("file")))
47                  {
48                      String file = rData.getAttribute("file");
49                      try
50                      {
51                          returnData = IOUtils.getResourceAsString(file, getClass());
52                      }
53                      catch (IOException e)
54                      {
55                          throw new BeanCreationException("Failed to load test-data resource: " + file, e);
56                      }
57                  }
58                  else
59                  {
60                      returnData = rData.getTextContent();
61                  }
62              }
63          }
64  
65          if (returnData != null)
66          {
67              Map props = new HashMap();
68              props.put("data", returnData);
69              objectFactoryBeanDefinition.getPropertyValues().addPropertyValue("properties", props);
70          }
71          
72          return objectFactoryBeanDefinition;
73      }
74  }