Coverage Report - org.mule.config.spring.parsers.ClassOrRefDefinitionParser
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassOrRefDefinitionParser
0%
0/19
0%
0/8
0
 
 1  
 /*
 2  
  * $Id: ClassOrRefDefinitionParser.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;
 12  
 
 13  
 import org.mule.util.ClassUtils;
 14  
 import org.mule.util.StringUtils;
 15  
 
 16  
 import org.springframework.beans.MutablePropertyValues;
 17  
 import org.springframework.beans.factory.config.RuntimeBeanReference;
 18  
 import org.springframework.beans.factory.support.AbstractBeanDefinition;
 19  
 import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
 20  
 import org.springframework.beans.factory.xml.ParserContext;
 21  
 import org.w3c.dom.Element;
 22  
 
 23  
 public class ClassOrRefDefinitionParser extends AbstractBeanDefinitionParser
 24  
 {
 25  
     private String propertyName;
 26  
 
 27  
     public ClassOrRefDefinitionParser(String propertyName)
 28  
     {
 29  0
         super();
 30  
         
 31  0
         if (StringUtils.isEmpty(propertyName))
 32  
         {
 33  0
             throw new IllegalArgumentException("propertyName cannot be null");
 34  
         }
 35  0
         this.propertyName = propertyName;
 36  0
     }
 37  
     
 38  
     @Override
 39  
     protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext)
 40  
     {
 41  0
         MutablePropertyValues parentProps = parserContext.getContainingBeanDefinition().getPropertyValues();
 42  
 
 43  0
         String ref = element.getAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_REF);
 44  0
         String className = element.getAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_CLASS);
 45  
 
 46  0
         if (StringUtils.isBlank(ref) && StringUtils.isBlank(className))
 47  
         {
 48  0
             String elementName = element.getLocalName();
 49  0
             throw new IllegalArgumentException("Neither ref nor class attribute specified for the "
 50  
                 + elementName + " element");
 51  
         }
 52  
         
 53  0
         if (StringUtils.isNotBlank(ref))
 54  
         {
 55  
             // add a ref to other bean
 56  0
             parentProps.addPropertyValue(propertyName, new RuntimeBeanReference(ref));
 57  
         }
 58  
         else
 59  
         {
 60  
             // class attributed specified, instantiate and set directly
 61  
             Object instance;
 62  
             try
 63  
             {
 64  0
                 instance = ClassUtils.instanciateClass(className, ClassUtils.NO_ARGS, getClass());
 65  
             }
 66  0
             catch (Exception e)
 67  
             {
 68  0
                 throw new RuntimeException(e);
 69  0
             }
 70  
 
 71  0
             parentProps.addPropertyValue(propertyName, instance);
 72  
         }
 73  
 
 74  0
         return null;
 75  
     }
 76  
 }
 77  
 
 78