View Javadoc

1   /*
2    * $Id: MuleXmlBuilderContextListener.java 10789 2008-02-12 20:04:43Z dfeist $
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.MuleServer;
14  import org.mule.api.MuleContext;
15  import org.mule.api.MuleException;
16  import org.mule.api.config.ConfigurationException;
17  import org.mule.api.context.MuleContextFactory;
18  import org.mule.api.lifecycle.InitialisationException;
19  import org.mule.config.spring.SpringXmlConfigurationBuilder;
20  import org.mule.context.DefaultMuleContextFactory;
21  
22  import javax.servlet.ServletContext;
23  import javax.servlet.ServletContextEvent;
24  import javax.servlet.ServletContextListener;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   * <code>MuleXmlBuilderContextListener</code> is a bootstrap listener used to
31   * construct a MuleManager instance. This listener delegates to the
32   * <i>MuleXmlConfigurationBuilder</i>.
33   * <p>
34   * The location of the configuration file can be specified in a init parameter called
35   * <i>org.mule.config</i>, the value can be a path on the local file system or on
36   * the classpath. If a config parameter is not specified a default <i>mule-config.xml</i>
37   * will be used.
38   * </p>
39   * 
40   * @see SpringXmlConfigurationBuilder
41   */
42  
43  public class MuleXmlBuilderContextListener implements ServletContextListener
44  {
45      /**
46       * One or more Mule config files.
47       */
48      public static final String INIT_PARAMETER_MULE_CONFIG = "org.mule.config";
49  
50      private MuleContext muleContext;
51  
52      protected transient final Log logger = LogFactory.getLog(MuleXmlBuilderContextListener.class);
53  
54      public void contextInitialized(ServletContextEvent event)
55      {
56          initialize(event.getServletContext());
57      }
58  
59      public void initialize(ServletContext context)
60      {
61          String config = context.getInitParameter(INIT_PARAMETER_MULE_CONFIG);
62          if (config == null)
63          {
64              config = getDefaultConfigResource();
65              System.out.println("No Mule config file(s) specified, using default: " + config);
66          }
67          else
68          {
69              System.out.println("Mule config file(s): " + config);
70          }
71  
72          try
73          {
74              muleContext = createManager(config, context);
75              muleContext.start();
76          }
77          catch (MuleException ex)
78          {
79              context.log(ex.getMessage(), ex);
80              // Logging is not configured OOTB for Tomcat, so we'd better make a
81              // start-up failure plain to see.
82              ex.printStackTrace();
83          }
84          catch (Error error)
85          {
86              // WSAD doesn't always report the java.lang.Error, log it
87              context.log(error.getMessage(), error);
88              // Logging is not configured OOTB for Tomcat, so we'd better make a
89              // start-up failure plain to see.
90              error.printStackTrace();
91              throw error;
92          }
93      }
94  
95      /**
96       * Used to actually construct the UMOManager instance
97       * 
98       * @param configResource the location of the config resource, this can be on the
99       *            local file system or on the classpath.
100      * @return A configured UMOManager instance
101      * @throws InitialisationException 
102      */
103     protected MuleContext createManager(String configResource, ServletContext context)
104         throws ConfigurationException, InitialisationException
105     {
106         WebappMuleXmlConfigurationBuilder builder = new WebappMuleXmlConfigurationBuilder(context, configResource);
107         MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
108         return muleContextFactory.createMuleContext(builder);
109     }
110 
111     /**
112      * If no config location resource is configured on the servlet context, the value
113      * returned from this method will be used to initialise the MuleManager.
114      * 
115      * @return the default config resource location
116      */
117     protected String getDefaultConfigResource()
118     {
119         return MuleServer.DEFAULT_CONFIGURATION;
120     }
121 
122     public void contextDestroyed(ServletContextEvent event)
123     {
124         destroy();
125     }
126 
127     public void destroy()
128     {
129         if (muleContext != null)
130         {
131             if (!muleContext.isDisposing() || !muleContext.isDisposed())
132             {
133                 muleContext.dispose();
134             }
135         }
136     }
137 }