View Javadoc

1   /*
2    * $Id: MuleXmlBuilderContextListener.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.MuleManager;
14  import org.mule.MuleServer;
15  import org.mule.config.ConfigurationException;
16  import org.mule.umo.UMOException;
17  import org.mule.umo.manager.UMOManager;
18  import org.mule.util.StringUtils;
19  
20  import javax.servlet.ServletContext;
21  import javax.servlet.ServletContextEvent;
22  import javax.servlet.ServletContextListener;
23  
24  /**
25   * <code>MuleXmlBuilderContextListener</code> is a bootstrap listener used to
26   * construct a MuleManager instance. This listener delegates to the
27   * <i>MuleXmlConfigurationBuilder</i>.
28   * <p>
29   * The location of the configuration file can be specified in a init parameter called
30   * <i>org.mule.config</i>, the value can be a path on the local file system or on
31   * the classpath. If a config parameter is not specified a default
32   * <i>mule-config.xml</i> will be used.
33   * </p>
34   */
35  
36  public class MuleXmlBuilderContextListener implements ServletContextListener
37  {
38      /**
39       * One or more Mule config files.
40       */
41      public static final String INIT_PARAMETER_MULE_CONFIG = "org.mule.config";
42  
43      /**
44       * Classpath within the servlet context (e.g., "WEB-INF/classes").  Mule will attempt to load config 
45       * files from here first, and then from the remaining classpath.
46       */
47      public static final String INIT_PARAMETER_WEBAPP_CLASSPATH = "org.mule.webapp.classpath";
48  
49      public void contextInitialized(ServletContextEvent event)
50      {
51          initialize(event.getServletContext());
52      }
53  
54      public void initialize(ServletContext context)
55      {
56          String config = context.getInitParameter(INIT_PARAMETER_MULE_CONFIG);
57          if (config == null)
58          {
59              config = getDefaultConfigResource();
60          }
61  
62          String webappClasspath = context.getInitParameter(INIT_PARAMETER_WEBAPP_CLASSPATH);
63          if (StringUtils.isBlank(webappClasspath))
64          {
65              webappClasspath = null;
66          }
67          
68          try
69          {
70              createManager(config, webappClasspath, context);
71          }
72          catch (UMOException ex)
73          {
74              context.log(ex.getMessage(), ex);
75              // Logging is not configured OOTB for Tomcat, so we'd better make a start-up failure plain to see.
76              ex.printStackTrace();
77          }
78          catch (Error error)
79          {
80              // WSAD doesn't always report the java.lang.Error, log it
81              context.log(error.getMessage(), error);
82              // Logging is not configured OOTB for Tomcat, so we'd better make a start-up failure plain to see.
83              error.printStackTrace();
84              throw error;
85          }
86      }
87  
88      /**
89       * Used to actually construct the UMOManager instance
90       * 
91       * @param configResource the location of the config resource, this can be on the
92       *            local file system or on the classpath.
93       * @return A configured UMOManager instance
94       */
95      protected UMOManager createManager(String configResource, String webappClasspath, ServletContext context)
96          throws ConfigurationException
97      {
98          WebappMuleXmlConfigurationBuilder builder = new WebappMuleXmlConfigurationBuilder(context, webappClasspath);
99          return builder.configure(configResource, null);
100     }
101 
102     /**
103      * If no config location resource is configured on the servlet context, the value
104      * returned from this method will be used to initialise the MuleManager.
105      * 
106      * @return the default config resource location
107      */
108     protected String getDefaultConfigResource()
109     {
110         return MuleServer.DEFAULT_CONFIGURATION;
111     }
112 
113     public void contextDestroyed(ServletContextEvent event)
114     {
115         destroy();
116     }
117 
118     public void destroy()
119     {
120         MuleManager.getInstance().dispose();
121     }
122 }