View Javadoc

1   /*
2    * $Id: WebappMuleXmlConfigurationBuilder.java 9066 2007-10-11 10:35:58Z akuzmin $
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 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.builders;
12  
13  import org.mule.config.ConfigurationException;
14  import org.mule.util.FileUtils;
15  
16  import java.io.File;
17  import java.io.InputStream;
18  
19  import javax.servlet.ServletContext;
20  
21  /**
22   * <code>WebappMuleXmlConfigurationBuilder</code> will first try and load config
23   * resources from the Servlet context. If this fails it fails back to the methods
24   * used by the MuleXmlConfigurationBuilder.
25   */
26  public class WebappMuleXmlConfigurationBuilder extends MuleXmlConfigurationBuilder
27  {
28      private ServletContext context;
29  
30      /**
31       * Classpath within the servlet context (e.g., "WEB-INF/classes").  Mule will attempt to load config
32       * files from here first, and then from the remaining classpath.
33       */
34      private String webappClasspath;
35  
36      public WebappMuleXmlConfigurationBuilder(ServletContext context, String webappClasspath)
37              throws ConfigurationException
38      {
39          super();
40          this.context = context;
41          this.webappClasspath = webappClasspath;
42      }
43  
44      /**
45       * Attempt to load any resource from the Servlet Context first, then from the classpath.
46       */
47      protected InputStream loadResource(String resource) throws ConfigurationException
48      {
49          String resourcePath = resource;
50          InputStream is = null;
51          if (webappClasspath != null)
52          {
53              resourcePath = new File(webappClasspath, resource).getPath();
54              is = context.getResourceAsStream(resourcePath);
55          }
56          if (is == null)
57          {
58              is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
59          }
60          if (logger.isDebugEnabled())
61          {
62              if (is != null)
63              {
64                  logger.debug("Resource " + resource + " is found in Servlet Context.");
65              }
66              else
67              {
68                  logger.debug("Resource " + resourcePath + " is not found in Servlet Context, loading from classpath or as external file");
69              }
70          }
71          if (is == null && webappClasspath != null)
72          {
73              resourcePath = FileUtils.newFile(webappClasspath, resource).getPath();
74              try
75              {
76                  is = super.loadResource(resourcePath);
77              }
78              catch (ConfigurationException ex)
79              {
80                  logger.debug("Resource " + resourcePath + " is not found in filesystem");
81              }
82          }
83          if (is == null)
84          {
85              is = super.loadResource(resource);
86  
87          }
88          return is;
89      }
90  }