1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.config.spring; |
12 | |
|
13 | |
import org.mule.util.IOUtils; |
14 | |
|
15 | |
import java.io.IOException; |
16 | |
import java.io.InputStream; |
17 | |
|
18 | |
import org.apache.commons.logging.Log; |
19 | |
import org.apache.commons.logging.LogFactory; |
20 | |
import org.springframework.core.io.DefaultResourceLoader; |
21 | |
import org.springframework.core.io.InputStreamResource; |
22 | |
import org.springframework.core.io.Resource; |
23 | |
import org.springframework.core.io.support.ResourcePatternResolver; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | public class MuleResourceLoader extends DefaultResourceLoader implements ResourcePatternResolver |
30 | |
{ |
31 | 0 | protected transient Log logger = LogFactory.getLog(MuleResourceLoader.class); |
32 | |
|
33 | |
public Resource getResource(String rsc) |
34 | |
{ |
35 | 0 | return getResourceByPath(rsc); |
36 | |
} |
37 | |
|
38 | |
protected Resource getResourceByPath(String path) |
39 | |
{ |
40 | 0 | InputStream is = null; |
41 | |
try |
42 | |
{ |
43 | 0 | is = IOUtils.getResourceAsStream(path, getClass()); |
44 | |
} |
45 | 0 | catch (IOException e) |
46 | |
{ |
47 | 0 | logger.error("Unable to load Spring resource " + path + " : " + e.getMessage()); |
48 | 0 | return null; |
49 | 0 | } |
50 | |
|
51 | 0 | if (is != null) |
52 | |
{ |
53 | 0 | return new InputStreamResource(is); |
54 | |
} |
55 | |
else |
56 | |
{ |
57 | 0 | logger.error("Unable to locate Spring resource " + path); |
58 | 0 | return null; |
59 | |
} |
60 | |
} |
61 | |
|
62 | |
public Resource[] getResources(String rsc) throws IOException |
63 | |
{ |
64 | 0 | if (rsc == null) |
65 | |
{ |
66 | 0 | throw new IOException("No resources to read"); |
67 | |
} |
68 | 0 | String[] resourcesNames = org.springframework.util.StringUtils.tokenizeToStringArray(rsc, ",;", true, |
69 | |
true); |
70 | 0 | Resource[] resources = new Resource[resourcesNames.length]; |
71 | 0 | for (int i = 0; i < resourcesNames.length; ++i) |
72 | |
{ |
73 | 0 | resources[i] = getResourceByPath(resourcesNames[i]); |
74 | |
} |
75 | 0 | return resources; |
76 | |
} |
77 | |
} |