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 public class MuleResourceLoader extends DefaultResourceLoader implements ResourcePatternResolver
30 {
31 protected transient Log logger = LogFactory.getLog(MuleResourceLoader.class);
32
33 public Resource getResource(String rsc)
34 {
35 return getResourceByPath(rsc);
36 }
37
38 protected Resource getResourceByPath(String path)
39 {
40 InputStream is = null;
41 try
42 {
43 is = IOUtils.getResourceAsStream(path, getClass());
44 }
45 catch (IOException e)
46 {
47 logger.error("Unable to load Spring resource " + path + " : " + e.getMessage());
48 return null;
49 }
50
51 if (is != null)
52 {
53 return new InputStreamResource(is);
54 }
55 else
56 {
57 logger.error("Unable to locate Spring resource " + path);
58 return null;
59 }
60 }
61
62 public Resource[] getResources(String rsc) throws IOException
63 {
64 if (rsc == null)
65 {
66 throw new IOException("No resources to read");
67 }
68 String[] resourcesNames = org.springframework.util.StringUtils.tokenizeToStringArray(rsc, ",;", true,
69 true);
70 Resource[] resources = new Resource[resourcesNames.length];
71 for (int i = 0; i < resourcesNames.length; ++i)
72 {
73 resources[i] = getResourceByPath(resourcesNames[i]);
74 }
75 return resources;
76 }
77 }