1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.config.builders; |
12 | |
|
13 | |
import org.mule.config.ConfigurationException; |
14 | |
import org.mule.util.FileUtils; |
15 | |
|
16 | |
import java.io.File; |
17 | |
import java.io.InputStream; |
18 | |
|
19 | |
import javax.servlet.ServletContext; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
public class WebappMuleXmlConfigurationBuilder extends MuleXmlConfigurationBuilder |
27 | |
{ |
28 | |
private ServletContext context; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
private String webappClasspath; |
35 | |
|
36 | |
public WebappMuleXmlConfigurationBuilder(ServletContext context, String webappClasspath) |
37 | |
throws ConfigurationException |
38 | |
{ |
39 | 0 | super(); |
40 | 0 | this.context = context; |
41 | 0 | this.webappClasspath = webappClasspath; |
42 | 0 | } |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
protected InputStream loadResource(String resource) throws ConfigurationException |
48 | |
{ |
49 | 0 | String resourcePath = resource; |
50 | 0 | InputStream is = null; |
51 | 0 | if (webappClasspath != null) |
52 | |
{ |
53 | 0 | resourcePath = new File(webappClasspath, resource).getPath(); |
54 | 0 | is = context.getResourceAsStream(resourcePath); |
55 | |
} |
56 | 0 | if (is == null) |
57 | |
{ |
58 | 0 | is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource); |
59 | |
} |
60 | 0 | if (logger.isDebugEnabled()) |
61 | |
{ |
62 | 0 | if (is != null) |
63 | |
{ |
64 | 0 | logger.debug("Resource " + resource + " is found in Servlet Context."); |
65 | |
} |
66 | |
else |
67 | |
{ |
68 | 0 | logger.debug("Resource " + resourcePath + " is not found in Servlet Context, loading from classpath or as external file"); |
69 | |
} |
70 | |
} |
71 | 0 | if (is == null && webappClasspath != null) |
72 | |
{ |
73 | 0 | resourcePath = FileUtils.newFile(webappClasspath, resource).getPath(); |
74 | |
try |
75 | |
{ |
76 | 0 | is = super.loadResource(resourcePath); |
77 | |
} |
78 | 0 | catch (ConfigurationException ex) |
79 | |
{ |
80 | 0 | logger.debug("Resource " + resourcePath + " is not found in filesystem"); |
81 | 0 | } |
82 | |
} |
83 | 0 | if (is == null) |
84 | |
{ |
85 | 0 | is = super.loadResource(resource); |
86 | |
|
87 | |
} |
88 | 0 | return is; |
89 | |
} |
90 | |
} |