001: /*
002: * $Id: MuleXmlBuilderContextListener.java 10789 2008-02-12 20:04:43Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.config.builders;
012:
013: import org.mule.MuleServer;
014: import org.mule.api.MuleContext;
015: import org.mule.api.MuleException;
016: import org.mule.api.config.ConfigurationException;
017: import org.mule.api.context.MuleContextFactory;
018: import org.mule.api.lifecycle.InitialisationException;
019: import org.mule.config.spring.SpringXmlConfigurationBuilder;
020: import org.mule.context.DefaultMuleContextFactory;
021:
022: import javax.servlet.ServletContext;
023: import javax.servlet.ServletContextEvent;
024: import javax.servlet.ServletContextListener;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: /**
030: * <code>MuleXmlBuilderContextListener</code> is a bootstrap listener used to
031: * construct a MuleManager instance. This listener delegates to the
032: * <i>MuleXmlConfigurationBuilder</i>.
033: * <p>
034: * The location of the configuration file can be specified in a init parameter called
035: * <i>org.mule.config</i>, the value can be a path on the local file system or on
036: * the classpath. If a config parameter is not specified a default <i>mule-config.xml</i>
037: * will be used.
038: * </p>
039: *
040: * @see SpringXmlConfigurationBuilder
041: */
042:
043: public class MuleXmlBuilderContextListener implements
044: ServletContextListener {
045: /**
046: * One or more Mule config files.
047: */
048: public static final String INIT_PARAMETER_MULE_CONFIG = "org.mule.config";
049:
050: private MuleContext muleContext;
051:
052: protected transient final Log logger = LogFactory
053: .getLog(MuleXmlBuilderContextListener.class);
054:
055: public void contextInitialized(ServletContextEvent event) {
056: initialize(event.getServletContext());
057: }
058:
059: public void initialize(ServletContext context) {
060: String config = context
061: .getInitParameter(INIT_PARAMETER_MULE_CONFIG);
062: if (config == null) {
063: config = getDefaultConfigResource();
064: System.out
065: .println("No Mule config file(s) specified, using default: "
066: + config);
067: } else {
068: System.out.println("Mule config file(s): " + config);
069: }
070:
071: try {
072: muleContext = createManager(config, context);
073: muleContext.start();
074: } catch (MuleException ex) {
075: context.log(ex.getMessage(), ex);
076: // Logging is not configured OOTB for Tomcat, so we'd better make a
077: // start-up failure plain to see.
078: ex.printStackTrace();
079: } catch (Error error) {
080: // WSAD doesn't always report the java.lang.Error, log it
081: context.log(error.getMessage(), error);
082: // Logging is not configured OOTB for Tomcat, so we'd better make a
083: // start-up failure plain to see.
084: error.printStackTrace();
085: throw error;
086: }
087: }
088:
089: /**
090: * Used to actually construct the UMOManager instance
091: *
092: * @param configResource the location of the config resource, this can be on the
093: * local file system or on the classpath.
094: * @return A configured UMOManager instance
095: * @throws InitialisationException
096: */
097: protected MuleContext createManager(String configResource,
098: ServletContext context) throws ConfigurationException,
099: InitialisationException {
100: WebappMuleXmlConfigurationBuilder builder = new WebappMuleXmlConfigurationBuilder(
101: context, configResource);
102: MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
103: return muleContextFactory.createMuleContext(builder);
104: }
105:
106: /**
107: * If no config location resource is configured on the servlet context, the value
108: * returned from this method will be used to initialise the MuleManager.
109: *
110: * @return the default config resource location
111: */
112: protected String getDefaultConfigResource() {
113: return MuleServer.DEFAULT_CONFIGURATION;
114: }
115:
116: public void contextDestroyed(ServletContextEvent event) {
117: destroy();
118: }
119:
120: public void destroy() {
121: if (muleContext != null) {
122: if (!muleContext.isDisposing() || !muleContext.isDisposed()) {
123: muleContext.dispose();
124: }
125: }
126: }
127: }
|