View Javadoc

1   /*
2    * $Id: SimpleComponentDefinitionParser.java 22576 2011-07-31 18:24:01Z mike.schilling $
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 = numAttrs-1; i >= 0; --i)
63          {
64              attr = attrs.item(i);
65              if (attr.getNamespaceURI() == null)
66              {
67                  properties.put(attr.getNodeName(), attr.getNodeValue());
68                  attrs.removeNamedItem(attr.getNodeName());
69              }
70          }
71      }
72  
73      protected void parseChild(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
74      {
75          // Create a BeanDefinition for the nested object factory and set it a
76          // property value for the component
77          builder.addPropertyValue("objectFactory", getObjectFactoryDefinition(element));
78  
79          super.parseChild(element, parserContext, builder);
80      }
81      
82      protected AbstractBeanDefinition getObjectFactoryDefinition(Element element)
83      {
84          AbstractBeanDefinition objectFactoryBeanDefinition = new GenericBeanDefinition();
85          objectFactoryBeanDefinition.setBeanClass(OBJECT_FACTORY_TYPE);
86          objectFactoryBeanDefinition.getPropertyValues()
87              .addPropertyValue(AbstractObjectFactory.ATTRIBUTE_OBJECT_CLASS, componentInstanceClass);
88          objectFactoryBeanDefinition.getPropertyValues().addPropertyValue("properties", properties);
89  
90          //Marker for MULE-4813
91          objectFactoryBeanDefinition.setInitMethodName(Initialisable.PHASE_NAME);
92          objectFactoryBeanDefinition.setDestroyMethodName(Disposable.PHASE_NAME);
93          return objectFactoryBeanDefinition;
94      }
95  }