Coverage Report - org.mule.config.spring.parsers.specific.SimpleComponentDefinitionParser
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleComponentDefinitionParser
0%
0/23
0%
0/2
0
 
 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  0
     private static Class OBJECT_FACTORY_TYPE = SingletonObjectFactory.class;
 40  
     private Class componentInstanceClass;
 41  0
     private Map properties = new HashMap();
 42  
     
 43  
     public SimpleComponentDefinitionParser(Class component, Class componentInstanceClass)
 44  
     {
 45  0
         super(DefaultJavaComponent.class);
 46  0
         this.componentInstanceClass = componentInstanceClass;
 47  0
     }
 48  
 
 49  
     @Override
 50  
     protected void preProcess(Element element)
 51  
     {
 52  0
         super.preProcess(element);
 53  
 
 54  0
         NamedNodeMap attrs = element.getAttributes();
 55  
         
 56  0
         int numAttrs = attrs.getLength();
 57  
         Node attr;
 58  0
         for (int i = 0; i < numAttrs; ++i)
 59  
         {
 60  0
             attr = attrs.item(0);
 61  0
             properties.put(attr.getNodeName(), attr.getNodeValue());
 62  0
             attrs.removeNamedItem(attr.getNodeName());
 63  
         }
 64  0
     }
 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  0
         builder.addPropertyValue("objectFactory", getObjectFactoryDefinition(element));
 71  
 
 72  0
         super.parseChild(element, parserContext, builder);
 73  0
     }
 74  
     
 75  
     protected AbstractBeanDefinition getObjectFactoryDefinition(Element element)
 76  
     {
 77  0
         AbstractBeanDefinition objectFactoryBeanDefinition = new GenericBeanDefinition();
 78  0
         objectFactoryBeanDefinition.setBeanClass(OBJECT_FACTORY_TYPE);
 79  0
         objectFactoryBeanDefinition.getPropertyValues()
 80  
             .addPropertyValue(AbstractObjectFactory.ATTRIBUTE_OBJECT_CLASS, componentInstanceClass);
 81  0
         objectFactoryBeanDefinition.getPropertyValues().addPropertyValue("properties", properties);
 82  
 
 83  
         //Marker for MULE-4813
 84  0
         objectFactoryBeanDefinition.setInitMethodName(Initialisable.PHASE_NAME);
 85  0
         objectFactoryBeanDefinition.setDestroyMethodName(Disposable.PHASE_NAME);
 86  0
         return objectFactoryBeanDefinition;
 87  
     }
 88  
 }