01: /*
02: * $Id: MuleResourceLoader.java 10256 2008-01-08 15:20:25Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
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: * <code>MuleResourceLoader</code> is a custom Spring resource loader that calls
27: * standard Mule methods for loading resource files.
28: */
29: public class MuleResourceLoader extends DefaultResourceLoader implements
30: ResourcePatternResolver {
31: protected transient Log logger = LogFactory
32: .getLog(MuleResourceLoader.class);
33:
34: public Resource getResource(String rsc) {
35: return getResourceByPath(rsc);
36: }
37:
38: protected Resource getResourceByPath(String path) {
39: InputStream is = null;
40: try {
41: is = IOUtils.getResourceAsStream(path, getClass());
42: } catch (IOException e) {
43: logger.error("Unable to load Spring resource " + path
44: + " : " + e.getMessage());
45: return null;
46: }
47:
48: if (is != null) {
49: return new InputStreamResource(is);
50: } else {
51: logger.error("Unable to locate Spring resource " + path);
52: return null;
53: }
54: }
55:
56: public Resource[] getResources(String rsc) throws IOException {
57: if (rsc == null) {
58: throw new IOException("No resources to read");
59: }
60: String[] resourcesNames = org.springframework.util.StringUtils
61: .tokenizeToStringArray(rsc, ",;", true, true);
62: Resource[] resources = new Resource[resourcesNames.length];
63: for (int i = 0; i < resourcesNames.length; ++i) {
64: resources[i] = getResourceByPath(resourcesNames[i]);
65: }
66: return resources;
67: }
68: }
|