View Javadoc

1   /*
2    * $Id: MuleResourceLoader.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * <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 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  }