Coverage Report - org.mule.config.builders.MuleXmlBuilderContextListener
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleXmlBuilderContextListener
0%
0/42
0%
0/16
2.667
 
 1  
 /*
 2  
  * $Id: MuleXmlBuilderContextListener.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.builders;
 12  
 
 13  
 import org.mule.MuleServer;
 14  
 import org.mule.api.MuleContext;
 15  
 import org.mule.api.MuleException;
 16  
 import org.mule.api.config.ConfigurationException;
 17  
 import org.mule.api.config.MuleProperties;
 18  
 import org.mule.api.context.MuleContextBuilder;
 19  
 import org.mule.api.context.MuleContextFactory;
 20  
 import org.mule.api.lifecycle.InitialisationException;
 21  
 import org.mule.config.DefaultMuleConfiguration;
 22  
 import org.mule.config.spring.SpringXmlConfigurationBuilder;
 23  
 import org.mule.context.DefaultMuleContextBuilder;
 24  
 import org.mule.context.DefaultMuleContextFactory;
 25  
 import org.mule.util.StringUtils;
 26  
 
 27  
 import javax.servlet.ServletContext;
 28  
 import javax.servlet.ServletContextEvent;
 29  
 import javax.servlet.ServletContextListener;
 30  
 
 31  
 import org.apache.commons.logging.Log;
 32  
 import org.apache.commons.logging.LogFactory;
 33  
 import org.springframework.context.ApplicationContext;
 34  
 import org.springframework.web.context.WebApplicationContext;
 35  
 
 36  
 /**
 37  
  * <code>MuleXmlBuilderContextListener</code> is a bootstrap listener used to
 38  
  * construct a {@link org.mule.api.MuleContext} instance. This listener delegates to the
 39  
  * <i>MuleXmlConfigurationBuilder</i>.
 40  
  * <p>
 41  
  * The location of the configuration file can be specified in a init parameter called
 42  
  * <i>org.mule.config</i>, the value can be a path on the local file system or on
 43  
  * the classpath. If a config parameter is not specified a default <i>mule-config.xml</i>
 44  
  * will be used.
 45  
  * </p>
 46  
  * 
 47  
  * @see SpringXmlConfigurationBuilder
 48  
  */
 49  
 
 50  0
 public class MuleXmlBuilderContextListener implements ServletContextListener
 51  
 {
 52  
     /**
 53  
      * One or more Mule config files.
 54  
      */
 55  
     public static final String INIT_PARAMETER_MULE_CONFIG = "org.mule.config";
 56  
 
 57  
     protected MuleContext muleContext;
 58  
 
 59  0
     protected transient final Log logger = LogFactory.getLog(MuleXmlBuilderContextListener.class);
 60  
 
 61  
     public void contextInitialized(ServletContextEvent event)
 62  
     {
 63  0
         initialize(event.getServletContext());
 64  0
     }
 65  
 
 66  
     public void initialize(ServletContext context)
 67  
     {
 68  0
         String config = context.getInitParameter(INIT_PARAMETER_MULE_CONFIG);
 69  0
         if (config == null)
 70  
         {
 71  0
             config = getDefaultConfigResource();
 72  0
             if (logger.isDebugEnabled())
 73  
             {
 74  0
                 logger.debug("No Mule config file(s) specified, using default: " + config);
 75  
             }
 76  
         }
 77  
         else
 78  
         {
 79  0
             if (logger.isDebugEnabled())
 80  
             {
 81  0
                 logger.debug("Mule config file(s): " + config);
 82  
             }
 83  
         }
 84  
 
 85  
         try
 86  
         {
 87  0
             muleContext = createMuleContext(config, context);
 88  0
             context.setAttribute(MuleProperties.MULE_CONTEXT_PROPERTY, muleContext);
 89  0
             muleContext.start();
 90  
         }
 91  0
         catch (MuleException ex)
 92  
         {
 93  0
             context.log(ex.getMessage(), ex);
 94  
             // Logging is not configured OOTB for Tomcat, so we'd better make a
 95  
             // start-up failure plain to see.
 96  0
             ex.printStackTrace();
 97  
         }
 98  0
         catch (Error error)
 99  
         {
 100  
             // WSAD doesn't always report the java.lang.Error, log it
 101  0
             context.log(error.getMessage(), error);
 102  
             // Logging is not configured OOTB for Tomcat, so we'd better make a
 103  
             // start-up failure plain to see.
 104  0
             error.printStackTrace();
 105  0
             throw error;
 106  0
         }
 107  0
     }
 108  
 
 109  
     /**
 110  
      * Creates the MuleContext based on the configuration resource(s) and possibly 
 111  
      * init parameters for the Servlet.
 112  
      */
 113  
     protected MuleContext createMuleContext(String configResource, ServletContext context)
 114  
         throws ConfigurationException, InitialisationException
 115  
     {
 116  0
         final String serverId = StringUtils.defaultIfEmpty(context.getInitParameter("mule.serverId"), null);
 117  0
         WebappMuleXmlConfigurationBuilder builder = new WebappMuleXmlConfigurationBuilder(context, configResource);
 118  0
         MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
 119  
 
 120  0
         DefaultMuleConfiguration muleConfiguration = new DefaultMuleConfiguration();
 121  0
         if (serverId != null)
 122  
         {
 123  0
             muleConfiguration.setId(serverId);
 124  
         }
 125  0
         MuleContextBuilder muleContextBuilder = new DefaultMuleContextBuilder();
 126  0
         muleContextBuilder.setMuleConfiguration(muleConfiguration);
 127  
 
 128  
         // Support Spring-first configuration in webapps
 129  0
         final ApplicationContext parentContext = (ApplicationContext) context.getAttribute(
 130  
                                                         WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
 131  0
         if (parentContext != null)
 132  
         {
 133  0
             builder.setParentContext(parentContext);
 134  
         }
 135  0
         return muleContextFactory.createMuleContext(builder, muleContextBuilder);
 136  
     }
 137  
 
 138  
     /**
 139  
      * If no config location resource is configured on the servlet context, the value
 140  
      * returned from this method will be used to initialise the MuleManager.
 141  
      * 
 142  
      * @return the default config resource location
 143  
      */
 144  
     protected String getDefaultConfigResource()
 145  
     {
 146  0
         return MuleServer.DEFAULT_CONFIGURATION;
 147  
     }
 148  
 
 149  
     public void contextDestroyed(ServletContextEvent event)
 150  
     {
 151  0
         destroy();
 152  0
     }
 153  
 
 154  
     public void destroy()
 155  
     {
 156  0
         if (muleContext != null)
 157  
         {
 158  0
             if (!muleContext.isDisposing() || !muleContext.isDisposed())
 159  
             {
 160  0
                 muleContext.dispose();
 161  
             }
 162  
         }
 163  0
     }
 164  
 }