Coverage Report - org.mule.config.builders.WebappMuleXmlConfigurationBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
WebappMuleXmlConfigurationBuilder
0%
0/24
0%
0/14
4.5
 
 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  0
         super();
 40  0
         this.context = context;
 41  0
         this.webappClasspath = webappClasspath;
 42  0
     }
 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  0
         String resourcePath = resource;
 50  0
         InputStream is = null;
 51  0
         if (webappClasspath != null)
 52  
         {
 53  0
             resourcePath = new File(webappClasspath, resource).getPath();
 54  0
             is = context.getResourceAsStream(resourcePath);
 55  
         }
 56  0
         if (is == null)
 57  
         {
 58  0
             is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
 59  
         }
 60  0
         if (logger.isDebugEnabled())
 61  
         {
 62  0
             if (is != null)
 63  
             {
 64  0
                 logger.debug("Resource " + resource + " is found in Servlet Context.");
 65  
             }
 66  
             else
 67  
             {
 68  0
                 logger.debug("Resource " + resourcePath + " is not found in Servlet Context, loading from classpath or as external file");
 69  
             }
 70  
         }
 71  0
         if (is == null && webappClasspath != null)
 72  
         {
 73  0
             resourcePath = FileUtils.newFile(webappClasspath, resource).getPath();
 74  
             try
 75  
             {
 76  0
                 is = super.loadResource(resourcePath);
 77  
             }
 78  0
             catch (ConfigurationException ex)
 79  
             {
 80  0
                 logger.debug("Resource " + resourcePath + " is not found in filesystem");
 81  0
             }
 82  
         }
 83  0
         if (is == null)
 84  
         {
 85  0
             is = super.loadResource(resource);
 86  
 
 87  
         }
 88  0
         return is;
 89  
     }
 90  
 }