Coverage Report - org.mule.config.builders.DeployableMuleXmlContextListener
 
Classes in this File Line Coverage Branch Coverage Complexity
DeployableMuleXmlContextListener
0%
0/33
0%
0/14
3.5
 
 1  
 /*
 2  
  * $Id: DeployableMuleXmlContextListener.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  
 
 17  
 import javax.servlet.ServletContext;
 18  
 import javax.servlet.ServletContextEvent;
 19  
 import javax.servlet.ServletContextListener;
 20  
 
 21  
 import org.apache.commons.logging.Log;
 22  
 import org.apache.commons.logging.LogFactory;
 23  
 import org.springframework.context.ApplicationContext;
 24  
 import org.springframework.web.context.WebApplicationContext;
 25  
 
 26  
 /**
 27  
  * This ServletContextListener should be used instead of
 28  
  * {@link MuleXmlBuilderContextListener} when the webapp is contributing a
 29  
  * configuration to an existing Mule instance configured and started at the server
 30  
  * level rather than embedding a Mule instance in the webapp itself.
 31  
  */
 32  0
 public class DeployableMuleXmlContextListener implements ServletContextListener
 33  
 {
 34  
 
 35  0
     protected transient final Log logger = LogFactory.getLog(DeployableMuleXmlContextListener.class);
 36  
 
 37  
     private WebappMuleXmlConfigurationBuilder configurationBuilder;
 38  
     private static MuleContext muleContext;
 39  
 
 40  
     public void contextInitialized(ServletContextEvent event)
 41  
     {
 42  0
         initialize(event.getServletContext());
 43  0
     }
 44  
 
 45  
     public void initialize(ServletContext context)
 46  
     {
 47  0
         String config = context.getInitParameter(MuleXmlBuilderContextListener.INIT_PARAMETER_MULE_CONFIG);
 48  0
         if (config == null)
 49  
         {
 50  0
             config = MuleServer.DEFAULT_CONFIGURATION;
 51  0
             if (logger.isDebugEnabled())
 52  
             {
 53  0
                 logger.debug("No Mule config file(s) specified, using default: " + config);
 54  
             }
 55  
         }
 56  
         else
 57  
         {
 58  0
             if (logger.isDebugEnabled())
 59  
             {
 60  0
                 logger.debug("Mule config file(s): " + config);
 61  
             }
 62  
         }
 63  
 
 64  0
         if (muleContext == null)
 65  
         {
 66  0
             throw new RuntimeException("MuleContext is not available");
 67  
         }
 68  
 
 69  
         try
 70  
         {
 71  0
             configurationBuilder = new WebappMuleXmlConfigurationBuilder(context, config);
 72  0
             configurationBuilder.setUseDefaultConfigResource(false);
 73  
 
 74  
             // Support Spring-first configuration in webapps
 75  0
             final ApplicationContext parentContext = (ApplicationContext) context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
 76  0
             if (parentContext != null)
 77  
             {
 78  0
                 configurationBuilder.setParentContext(parentContext);
 79  
             }
 80  0
             configurationBuilder.configure(muleContext);
 81  
         }
 82  0
         catch (MuleException ex)
 83  
         {
 84  0
             context.log(ex.getMessage(), ex);
 85  
             // Logging is not configured OOTB for Tomcat, so we'd better make a
 86  
             // start-up failure plain to see.
 87  0
             ex.printStackTrace();
 88  
         }
 89  0
         catch (Error error)
 90  
         {
 91  
             // WSAD doesn't always report the java.lang.Error, log it
 92  0
             context.log(error.getMessage(), error);
 93  
             // Logging is not configured OOTB for Tomcat, so we'd better make a
 94  
             // start-up failure plain to see.
 95  0
             error.printStackTrace();
 96  0
             throw error;
 97  0
         }
 98  0
     }
 99  
 
 100  
     public void contextDestroyed(ServletContextEvent event)
 101  
     {
 102  0
         if (muleContext != null && configurationBuilder != null)
 103  
         {
 104  0
             configurationBuilder.unconfigure(muleContext);
 105  
         }
 106  0
     }
 107  
 
 108  
     /**
 109  
      * This method is to be used only by application server or web container
 110  
      * integrations that allow web applications to be hot-deployed.
 111  
      * 
 112  
      * @param context the single shared muleContext instance that will be used to
 113  
      *            configure mule configurations hot-deployed as web application.
 114  
      */
 115  
     public static void setMuleContext(MuleContext context)
 116  
     {
 117  0
         muleContext = context;
 118  0
     }
 119  
 
 120  
 }