1
2
3
4
5
6
7
8
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
26
27
28 public class MuleBeanDefinitionDocumentReader extends DefaultBeanDefinitionDocumentReader
29 {
30
31
32
33
34
35
36
37
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
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
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
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