Coverage Report - org.mule.extras.spring.config.MuleBeanDefinitionDocumentReader
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleBeanDefinitionDocumentReader
0%
0/34
0%
0/8
13
 
 1  
 /*
 2  
  * $Id: MuleBeanDefinitionDocumentReader.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.extras.spring.config;
 12  
 
 13  
 import java.io.IOException;
 14  
 
 15  
 import org.springframework.beans.factory.BeanDefinitionStoreException;
 16  
 import org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader;
 17  
 import org.springframework.core.io.ClassPathResource;
 18  
 import org.springframework.core.io.Resource;
 19  
 import org.springframework.core.io.support.ResourcePatternUtils;
 20  
 import org.springframework.util.StringUtils;
 21  
 import org.springframework.util.SystemPropertyUtils;
 22  
 import org.w3c.dom.Element;
 23  
 
 24  
 /**
 25  
  * By default, an <import resource="file.xml"/> statement will assume the imported resource's location is relative
 26  
  * to the current file.  Override to try loading the resource from the classpath as well.
 27  
  */
 28  0
 public class MuleBeanDefinitionDocumentReader extends DefaultBeanDefinitionDocumentReader
 29  
 {
 30  
     /**
 31  
      * By default, an <import resource="file.xml"/> statement will assume the imported resource's location is relative
 32  
      * to the current file.  Override this method to try loading the resource from the classpath as well.
 33  
      * <p/>
 34  
      * Note: only the section labelled "Customized for Mule" below has been changed, the rest is copy-pasted from the
 35  
      * parent (Spring) class.
 36  
      */
 37  
     //@Override
 38  
     protected void importBeanDefinitionResource(Element ele)
 39  
     {
 40  0
         String location = ele.getAttribute(RESOURCE_ATTRIBUTE);
 41  0
         if (!StringUtils.hasText(location))
 42  
         {
 43  0
             getReaderContext().error("Resource location must not be empty", ele);
 44  0
             return;
 45  
         }
 46  
 
 47  
         // Resolve system properties: e.g. "${user.dir}"
 48  0
         location = SystemPropertyUtils.resolvePlaceholders(location);
 49  
 
 50  0
         if (ResourcePatternUtils.isUrl(location))
 51  
         {
 52  
             try
 53  
             {
 54  0
                 int importCount = getReaderContext().getReader().loadBeanDefinitions(location);
 55  0
                 if (logger.isDebugEnabled())
 56  
                 {
 57  0
                     logger.debug("Imported " + importCount + " bean definitions from URL location [" + location + "]");
 58  
                 }
 59  
             }
 60  0
             catch (BeanDefinitionStoreException ex)
 61  
             {
 62  0
                 getReaderContext().error(
 63  
                         "Failed to import bean definitions from URL location [" + location + "]", ele, ex);
 64  0
             }
 65  
         }
 66  
         else
 67  
         {
 68  
             // No URL -> considering resource location as relative to the current file.
 69  
             try
 70  
             {
 71  0
                 Resource relativeResource = getReaderContext().getResource().createRelative(location);
 72  0
                 int importCount = getReaderContext().getReader().loadBeanDefinitions(relativeResource);
 73  0
                 if (logger.isDebugEnabled())
 74  
                 {
 75  0
                     logger.debug("Imported " + importCount + " bean definitions from relative location [" + location + "]");
 76  
                 }
 77  
             }
 78  
             ////////////////////////////////////////////////////////////////////////////////////////////
 79  
             // Customized for Mule
 80  
             ////////////////////////////////////////////////////////////////////////////////////////////
 81  0
             catch (IOException ex)
 82  
             {
 83  0
                 if (logger.isDebugEnabled())
 84  
                 {
 85  0
                     logger.debug("Invalid relative resource location [" + location + "] to import bean definitions from, will try loading from classpath");
 86  
                 }
 87  0
                 Resource classpathResource = new ClassPathResource(location);
 88  0
                 int importCount = getReaderContext().getReader().loadBeanDefinitions(classpathResource);
 89  0
                 if (logger.isDebugEnabled())
 90  
                 {
 91  0
                     logger.debug("Imported " + importCount + " bean definitions from classpath resource [" + location + "]");
 92  
                 }
 93  
             }
 94  0
             catch (BeanDefinitionStoreException ex)
 95  
             {
 96  0
                 if (logger.isDebugEnabled())
 97  
                 {
 98  0
                     logger.debug("Failed to import bean definitions from relative location [" + location + "], will try loading from classpath");
 99  
                 }
 100  0
                 Resource classpathResource = new ClassPathResource(location);
 101  0
                 int importCount = getReaderContext().getReader().loadBeanDefinitions(classpathResource);
 102  0
                 if (logger.isDebugEnabled())
 103  
                 {
 104  0
                     logger.debug("Imported " + importCount + " bean definitions from classpath resource [" + location + "]");
 105  
                 }
 106  0
             }
 107  
             ////////////////////////////////////////////////////////////////////////////////////////////
 108  
             ////////////////////////////////////////////////////////////////////////////////////////////
 109  
         }
 110  
 
 111  0
         getReaderContext().fireImportProcessed(location, extractSource(ele));
 112  0
     }
 113  
 }
 114  
 
 115