1
2
3
4
5
6
7
8
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.config.MuleProperties;
18 import org.mule.api.context.MuleContextBuilder;
19 import org.mule.api.context.MuleContextFactory;
20 import org.mule.api.lifecycle.InitialisationException;
21 import org.mule.config.DefaultMuleConfiguration;
22 import org.mule.config.spring.SpringXmlConfigurationBuilder;
23 import org.mule.context.DefaultMuleContextBuilder;
24 import org.mule.context.DefaultMuleContextFactory;
25 import org.mule.util.StringUtils;
26
27 import javax.servlet.ServletContext;
28 import javax.servlet.ServletContextEvent;
29 import javax.servlet.ServletContextListener;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.springframework.context.ApplicationContext;
34 import org.springframework.web.context.WebApplicationContext;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class MuleXmlBuilderContextListener implements ServletContextListener
51 {
52
53
54
55 public static final String INIT_PARAMETER_MULE_CONFIG = "org.mule.config";
56
57 protected MuleContext muleContext;
58
59 protected transient final Log logger = LogFactory.getLog(MuleXmlBuilderContextListener.class);
60
61 public void contextInitialized(ServletContextEvent event)
62 {
63 initialize(event.getServletContext());
64 }
65
66 public void initialize(ServletContext context)
67 {
68 String config = context.getInitParameter(INIT_PARAMETER_MULE_CONFIG);
69 if (config == null)
70 {
71 config = getDefaultConfigResource();
72 if (logger.isDebugEnabled())
73 {
74 logger.debug("No Mule config file(s) specified, using default: " + config);
75 }
76 }
77 else
78 {
79 if (logger.isDebugEnabled())
80 {
81 logger.debug("Mule config file(s): " + config);
82 }
83 }
84
85 try
86 {
87 muleContext = createMuleContext(config, context);
88 context.setAttribute(MuleProperties.MULE_CONTEXT_PROPERTY, muleContext);
89 muleContext.start();
90 }
91 catch (MuleException ex)
92 {
93 context.log(ex.getMessage(), ex);
94
95
96 ex.printStackTrace();
97 }
98 catch (Error error)
99 {
100
101 context.log(error.getMessage(), error);
102
103
104 error.printStackTrace();
105 throw error;
106 }
107 }
108
109
110
111
112
113 protected MuleContext createMuleContext(String configResource, ServletContext context)
114 throws ConfigurationException, InitialisationException
115 {
116 final String serverId = StringUtils.defaultIfEmpty(context.getInitParameter("mule.serverId"), null);
117 WebappMuleXmlConfigurationBuilder builder = new WebappMuleXmlConfigurationBuilder(context, configResource);
118 MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
119
120 DefaultMuleConfiguration muleConfiguration = new DefaultMuleConfiguration();
121 if (serverId != null)
122 {
123 muleConfiguration.setId(serverId);
124 }
125 MuleContextBuilder muleContextBuilder = new DefaultMuleContextBuilder();
126 muleContextBuilder.setMuleConfiguration(muleConfiguration);
127
128
129 final ApplicationContext parentContext = (ApplicationContext) context.getAttribute(
130 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
131 if (parentContext != null)
132 {
133 builder.setParentContext(parentContext);
134 }
135 return muleContextFactory.createMuleContext(builder, muleContextBuilder);
136 }
137
138
139
140
141
142
143
144 protected String getDefaultConfigResource()
145 {
146 return MuleServer.DEFAULT_CONFIGURATION;
147 }
148
149 public void contextDestroyed(ServletContextEvent event)
150 {
151 destroy();
152 }
153
154 public void destroy()
155 {
156 if (muleContext != null)
157 {
158 if (!muleContext.isDisposing() || !muleContext.isDisposed())
159 {
160 muleContext.dispose();
161 }
162 }
163 }
164 }