View Javadoc
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  public class DeployableMuleXmlContextListener implements ServletContextListener
29  {
30  
31      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          initialize(event.getServletContext());
39      }
40  
41      public void initialize(ServletContext context)
42      {
43          String config = context.getInitParameter(MuleXmlBuilderContextListener.INIT_PARAMETER_MULE_CONFIG);
44          if (config == null)
45          {
46              config = MuleServer.DEFAULT_CONFIGURATION;
47              if (logger.isDebugEnabled())
48              {
49                  logger.debug("No Mule config file(s) specified, using default: " + config);
50              }
51          }
52          else
53          {
54              if (logger.isDebugEnabled())
55              {
56                  logger.debug("Mule config file(s): " + config);
57              }
58          }
59  
60          if (muleContext == null)
61          {
62              throw new RuntimeException("MuleContext is not available");
63          }
64  
65          try
66          {
67              configurationBuilder = new WebappMuleXmlConfigurationBuilder(context, config);
68              configurationBuilder.setUseDefaultConfigResource(false);
69  
70              // Support Spring-first configuration in webapps
71              final ApplicationContext parentContext = (ApplicationContext) context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
72              if (parentContext != null)
73              {
74                  configurationBuilder.setParentContext(parentContext);
75              }
76              configurationBuilder.configure(muleContext);
77          }
78          catch (MuleException ex)
79          {
80              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              ex.printStackTrace();
84          }
85          catch (Error error)
86          {
87              // WSAD doesn't always report the java.lang.Error, log it
88              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              error.printStackTrace();
92              throw error;
93          }
94      }
95  
96      public void contextDestroyed(ServletContextEvent event)
97      {
98          if (muleContext != null && configurationBuilder != null)
99          {
100             configurationBuilder.unconfigure(muleContext);
101         }
102     }
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         muleContext = context;
114     }
115 
116 }