Coverage Report - org.mule.config.builders.MuleXmlBuilderContextListener
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleXmlBuilderContextListener
0%
0/30
0%
0/8
2
 
 1  
 /*
 2  
  * $Id: MuleXmlBuilderContextListener.java 10789 2008-02-12 20:04:43Z dfeist $
 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.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.context.MuleContextFactory;
 18  
 import org.mule.api.lifecycle.InitialisationException;
 19  
 import org.mule.config.spring.SpringXmlConfigurationBuilder;
 20  
 import org.mule.context.DefaultMuleContextFactory;
 21  
 
 22  
 import javax.servlet.ServletContext;
 23  
 import javax.servlet.ServletContextEvent;
 24  
 import javax.servlet.ServletContextListener;
 25  
 
 26  
 import org.apache.commons.logging.Log;
 27  
 import org.apache.commons.logging.LogFactory;
 28  
 
 29  
 /**
 30  
  * <code>MuleXmlBuilderContextListener</code> is a bootstrap listener used to
 31  
  * construct a MuleManager instance. This listener delegates to the
 32  
  * <i>MuleXmlConfigurationBuilder</i>.
 33  
  * <p>
 34  
  * The location of the configuration file can be specified in a init parameter called
 35  
  * <i>org.mule.config</i>, the value can be a path on the local file system or on
 36  
  * the classpath. If a config parameter is not specified a default <i>mule-config.xml</i>
 37  
  * will be used.
 38  
  * </p>
 39  
  * 
 40  
  * @see SpringXmlConfigurationBuilder
 41  
  */
 42  
 
 43  0
 public class MuleXmlBuilderContextListener implements ServletContextListener
 44  
 {
 45  
     /**
 46  
      * One or more Mule config files.
 47  
      */
 48  
     public static final String INIT_PARAMETER_MULE_CONFIG = "org.mule.config";
 49  
 
 50  
     private MuleContext muleContext;
 51  
 
 52  0
     protected transient final Log logger = LogFactory.getLog(MuleXmlBuilderContextListener.class);
 53  
 
 54  
     public void contextInitialized(ServletContextEvent event)
 55  
     {
 56  0
         initialize(event.getServletContext());
 57  0
     }
 58  
 
 59  
     public void initialize(ServletContext context)
 60  
     {
 61  0
         String config = context.getInitParameter(INIT_PARAMETER_MULE_CONFIG);
 62  0
         if (config == null)
 63  
         {
 64  0
             config = getDefaultConfigResource();
 65  0
             System.out.println("No Mule config file(s) specified, using default: " + config);
 66  
         }
 67  
         else
 68  
         {
 69  0
             System.out.println("Mule config file(s): " + config);
 70  
         }
 71  
 
 72  
         try
 73  
         {
 74  0
             muleContext = createManager(config, context);
 75  0
             muleContext.start();
 76  
         }
 77  0
         catch (MuleException ex)
 78  
         {
 79  0
             context.log(ex.getMessage(), ex);
 80  
             // Logging is not configured OOTB for Tomcat, so we'd better make a
 81  
             // start-up failure plain to see.
 82  0
             ex.printStackTrace();
 83  
         }
 84  0
         catch (Error error)
 85  
         {
 86  
             // WSAD doesn't always report the java.lang.Error, log it
 87  0
             context.log(error.getMessage(), error);
 88  
             // Logging is not configured OOTB for Tomcat, so we'd better make a
 89  
             // start-up failure plain to see.
 90  0
             error.printStackTrace();
 91  0
             throw error;
 92  0
         }
 93  0
     }
 94  
 
 95  
     /**
 96  
      * Used to actually construct the UMOManager instance
 97  
      * 
 98  
      * @param configResource the location of the config resource, this can be on the
 99  
      *            local file system or on the classpath.
 100  
      * @return A configured UMOManager instance
 101  
      * @throws InitialisationException 
 102  
      */
 103  
     protected MuleContext createManager(String configResource, ServletContext context)
 104  
         throws ConfigurationException, InitialisationException
 105  
     {
 106  0
         WebappMuleXmlConfigurationBuilder builder = new WebappMuleXmlConfigurationBuilder(context, configResource);
 107  0
         MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
 108  0
         return muleContextFactory.createMuleContext(builder);
 109  
     }
 110  
 
 111  
     /**
 112  
      * If no config location resource is configured on the servlet context, the value
 113  
      * returned from this method will be used to initialise the MuleManager.
 114  
      * 
 115  
      * @return the default config resource location
 116  
      */
 117  
     protected String getDefaultConfigResource()
 118  
     {
 119  0
         return MuleServer.DEFAULT_CONFIGURATION;
 120  
     }
 121  
 
 122  
     public void contextDestroyed(ServletContextEvent event)
 123  
     {
 124  0
         destroy();
 125  0
     }
 126  
 
 127  
     public void destroy()
 128  
     {
 129  0
         if (muleContext != null)
 130  
         {
 131  0
             if (!muleContext.isDisposing() || !muleContext.isDisposed())
 132  
             {
 133  0
                 muleContext.dispose();
 134  
             }
 135  
         }
 136  0
     }
 137  
 }