View Javadoc

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