View Javadoc

1   /*
2    * $Id: MuleBeanDefinitionDocumentReader.java 7963 2007-08-21 08:53:15Z 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  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          String location = ele.getAttribute(RESOURCE_ATTRIBUTE);
41          if (!StringUtils.hasText(location))
42          {
43              getReaderContext().error("Resource location must not be empty", ele);
44              return;
45          }
46  
47          // Resolve system properties: e.g. "${user.dir}"
48          location = SystemPropertyUtils.resolvePlaceholders(location);
49  
50          if (ResourcePatternUtils.isUrl(location))
51          {
52              try
53              {
54                  int importCount = getReaderContext().getReader().loadBeanDefinitions(location);
55                  if (logger.isDebugEnabled())
56                  {
57                      logger.debug("Imported " + importCount + " bean definitions from URL location [" + location + "]");
58                  }
59              }
60              catch (BeanDefinitionStoreException ex)
61              {
62                  getReaderContext().error(
63                          "Failed to import bean definitions from URL location [" + location + "]", ele, ex);
64              }
65          }
66          else
67          {
68              // No URL -> considering resource location as relative to the current file.
69              try
70              {
71                  Resource relativeResource = getReaderContext().getResource().createRelative(location);
72                  int importCount = getReaderContext().getReader().loadBeanDefinitions(relativeResource);
73                  if (logger.isDebugEnabled())
74                  {
75                      logger.debug("Imported " + importCount + " bean definitions from relative location [" + location + "]");
76                  }
77              }
78              ////////////////////////////////////////////////////////////////////////////////////////////
79              // Customized for Mule
80              ////////////////////////////////////////////////////////////////////////////////////////////
81              catch (IOException ex)
82              {
83                  if (logger.isDebugEnabled())
84                  {
85                      logger.debug("Invalid relative resource location [" + location + "] to import bean definitions from, will try loading from classpath");
86                  }
87                  Resource classpathResource = new ClassPathResource(location);
88                  int importCount = getReaderContext().getReader().loadBeanDefinitions(classpathResource);
89                  if (logger.isDebugEnabled())
90                  {
91                      logger.debug("Imported " + importCount + " bean definitions from classpath resource [" + location + "]");
92                  }
93              }
94              catch (BeanDefinitionStoreException ex)
95              {
96                  if (logger.isDebugEnabled())
97                  {
98                      logger.debug("Failed to import bean definitions from relative location [" + location + "], will try loading from classpath");
99                  }
100                 Resource classpathResource = new ClassPathResource(location);
101                 int importCount = getReaderContext().getReader().loadBeanDefinitions(classpathResource);
102                 if (logger.isDebugEnabled())
103                 {
104                     logger.debug("Imported " + importCount + " bean definitions from classpath resource [" + location + "]");
105                 }
106             }
107             ////////////////////////////////////////////////////////////////////////////////////////////
108             ////////////////////////////////////////////////////////////////////////////////////////////
109         }
110 
111         getReaderContext().fireImportProcessed(location, extractSource(ele));
112     }
113 }
114 
115