View Javadoc

1   /*
2    * $Id: WebappMuleXmlConfigurationBuilder.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.InputStream;
17  
18  import javax.servlet.ServletContext;
19  
20  /**
21   * <code>WebappMuleXmlConfigurationBuilder</code> will first try and load config
22   * resources from the Servlet context. If this fails it fails back to the methods
23   * used by the MuleXmlConfigurationBuilder.
24   */
25  public class WebappMuleXmlConfigurationBuilder extends MuleXmlConfigurationBuilder
26  {
27      private ServletContext context;
28      
29      /**
30       * Classpath within the servlet context (e.g., "WEB-INF/classes").  Mule will attempt to load config 
31       * files from here first, and then from the remaining classpath.
32       */
33      private String webappClasspath;
34  
35      public WebappMuleXmlConfigurationBuilder(ServletContext context, String webappClasspath) 
36          throws ConfigurationException
37      {
38          super();
39          this.context = context;
40          this.webappClasspath = webappClasspath;
41      }
42  
43      /**
44       * Attempt to load any resource from the Servlet Context first, then from the classpath.
45       */
46      protected InputStream loadResource(String resource) throws ConfigurationException
47      {
48          String resourcePath = FileUtils.newFile(webappClasspath, resource).getPath();
49          logger.debug("Searching for resource " + resourcePath + " in Servlet Context.");
50          InputStream is = context.getResourceAsStream(resourcePath);
51          if (is == null)
52          {
53              logger.debug("Resource " + resourcePath + " not found in Servlet Context, loading from classpath");
54              is = super.loadResource(resource);
55          }
56          return is;
57      }
58  }