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 | 0 | public class MuleBeanDefinitionDocumentReader extends DefaultBeanDefinitionDocumentReader |
29 | |
{ |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
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 | |
|
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 | |
|
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 | |
|
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 | |
|