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