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.api.lifecycle.Disposable;
10  import org.mule.api.lifecycle.Initialisable;
11  import org.mule.component.DefaultJavaComponent;
12  import org.mule.object.AbstractObjectFactory;
13  import org.mule.object.SingletonObjectFactory;
14  
15  import java.util.HashMap;
16  import java.util.Map;
17  
18  import org.springframework.beans.factory.support.AbstractBeanDefinition;
19  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
20  import org.springframework.beans.factory.support.GenericBeanDefinition;
21  import org.springframework.beans.factory.xml.ParserContext;
22  import org.w3c.dom.Element;
23  import org.w3c.dom.NamedNodeMap;
24  import org.w3c.dom.Node;
25  
26  /**
27   * Used to parse shortcut elements for simple built-in components such as
28   * {@link org.mule.component.simple.EchoComponent} and
29   * {@link org.mule.component.simple.LogComponent}. This allows shortcuts like
30   * for example <i>&lt;mule:bridge-service/&gt;</i> to be used instead of having to
31   * use the <i>&lt;mule:service/&gt;</i> element and specify the class name (and
32   * scope) for built-in components that don't require configuration. <p/> <b>This
33   * DefinitionParser should only be used for state-less components.</b> <p/> In order
34   * to further customize components and use serviceFactory properties the
35   * &lt;mule:service/&gt; element should be used.
36   */
37  public class SimpleComponentDefinitionParser extends ComponentDefinitionParser
38  {
39      private static Class OBJECT_FACTORY_TYPE = SingletonObjectFactory.class;
40      private Class componentInstanceClass;
41      private Map properties = new HashMap();
42      
43      public SimpleComponentDefinitionParser(Class component, Class componentInstanceClass)
44      {
45          super(DefaultJavaComponent.class);
46          this.componentInstanceClass = componentInstanceClass;
47      }
48  
49      @Override
50      protected void preProcess(Element element)
51      {
52          super.preProcess(element);
53  
54          NamedNodeMap attrs = element.getAttributes();
55          
56          int numAttrs = attrs.getLength();
57          Node attr;
58          for (int i = 0; i < numAttrs; ++i)
59          {
60              attr = attrs.item(0);
61              properties.put(attr.getNodeName(), attr.getNodeValue());
62              attrs.removeNamedItem(attr.getNodeName());
63          }
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          builder.addPropertyValue("objectFactory", getObjectFactoryDefinition(element));
71  
72          super.parseChild(element, parserContext, builder);
73      }
74      
75      protected AbstractBeanDefinition getObjectFactoryDefinition(Element element)
76      {
77          AbstractBeanDefinition objectFactoryBeanDefinition = new GenericBeanDefinition();
78          objectFactoryBeanDefinition.setBeanClass(OBJECT_FACTORY_TYPE);
79          objectFactoryBeanDefinition.getPropertyValues()
80              .addPropertyValue(AbstractObjectFactory.ATTRIBUTE_OBJECT_CLASS, componentInstanceClass);
81          objectFactoryBeanDefinition.getPropertyValues().addPropertyValue("properties", properties);
82  
83          //Marker for MULE-4813
84          objectFactoryBeanDefinition.setInitMethodName(Initialisable.PHASE_NAME);
85          objectFactoryBeanDefinition.setDestroyMethodName(Disposable.PHASE_NAME);
86          return objectFactoryBeanDefinition;
87      }
88  }