KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > extras > spring > config > MuleResourceLoader


1 /*
2  * $Id: MuleResourceLoader.java 3316 2006-09-29 18:12:42 +0000 (Fri, 29 Sep 2006) holger $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.extras.spring.config;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.mule.util.IOUtils;
19 import org.springframework.core.io.DefaultResourceLoader;
20 import org.springframework.core.io.InputStreamResource;
21 import org.springframework.core.io.Resource;
22 import org.springframework.core.io.support.ResourcePatternResolver;
23
24 /**
25  * <code>MuleResourceLoader</code> is a custom Spring resource loader that calls
26  * standard Mule methods for loading resource files.
27  */

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