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