View Javadoc

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