View Javadoc

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