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