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.PropertiesMuleConfigurationFactory;
23 import org.mule.config.spring.SpringXmlConfigurationBuilder;
24 import org.mule.context.DefaultMuleContextBuilder;
25 import org.mule.context.DefaultMuleContextFactory;
26 import org.mule.util.FilenameUtils;
27 import org.mule.util.StringUtils;
28
29 import java.io.File;
30
31 import javax.servlet.ServletContext;
32 import javax.servlet.ServletContextEvent;
33 import javax.servlet.ServletContextListener;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.springframework.context.ApplicationContext;
38 import org.springframework.web.context.WebApplicationContext;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class MuleXmlBuilderContextListener implements ServletContextListener
55 {
56
57
58
59 public static final String INIT_PARAMETER_MULE_CONFIG = "org.mule.config";
60
61 public static final String INIT_PARAMETER_MULE_APP_CONFIG = "org.mule.app.config";
62
63
64
65
66 public static final String ATTR_JAVAX_SERVLET_CONTEXT_TEMPDIR = "javax.servlet.context.tempdir";
67
68 protected MuleContext muleContext;
69
70 protected transient final Log logger = LogFactory.getLog(MuleXmlBuilderContextListener.class);
71
72 public void contextInitialized(ServletContextEvent event)
73 {
74 initialize(event.getServletContext());
75 }
76
77 public void initialize(ServletContext context)
78 {
79 String config = context.getInitParameter(INIT_PARAMETER_MULE_CONFIG);
80 if (config == null)
81 {
82 config = getDefaultConfigResource();
83 if (logger.isDebugEnabled())
84 {
85 logger.debug("No Mule config file(s) specified, using default: " + config);
86 }
87 }
88 else
89 {
90 if (logger.isDebugEnabled())
91 {
92 logger.debug("Mule config file(s): " + config);
93 }
94 }
95
96 try
97 {
98 muleContext = createMuleContext(config, context);
99 context.setAttribute(MuleProperties.MULE_CONTEXT_PROPERTY, muleContext);
100 muleContext.start();
101 }
102 catch (MuleException ex)
103 {
104 context.log(ex.getMessage(), ex);
105
106
107 ex.printStackTrace();
108 }
109 catch (Error error)
110 {
111
112 context.log(error.getMessage(), error);
113
114
115 error.printStackTrace();
116 throw error;
117 }
118 }
119
120
121
122
123
124 protected MuleContext createMuleContext(String configResource, ServletContext servletContext)
125 throws ConfigurationException, InitialisationException
126 {
127 String serverId = StringUtils.defaultIfEmpty(servletContext.getInitParameter("mule.serverId"), null);
128
129
130
131 if (serverId == null)
132 {
133
134
135 final File tempDir = (File) servletContext.getAttribute(ATTR_JAVAX_SERVLET_CONTEXT_TEMPDIR);
136 final String contextName = FilenameUtils.getBaseName(tempDir.toString());
137 serverId = contextName;
138 }
139
140 WebappMuleXmlConfigurationBuilder builder = new WebappMuleXmlConfigurationBuilder(servletContext, configResource);
141 MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
142
143 String muleAppConfig = servletContext.getInitParameter(INIT_PARAMETER_MULE_APP_CONFIG) != null
144 ? servletContext.getInitParameter(INIT_PARAMETER_MULE_APP_CONFIG)
145 : PropertiesMuleConfigurationFactory.getMuleAppConfiguration(configResource);
146
147 DefaultMuleConfiguration muleConfiguration = new PropertiesMuleConfigurationFactory(muleAppConfig).createConfiguration();
148
149
150
151
152
153 muleConfiguration.setContainerMode(true);
154
155 if (serverId != null)
156 {
157 muleConfiguration.setId(serverId);
158 }
159 MuleContextBuilder muleContextBuilder = new DefaultMuleContextBuilder();
160 muleContextBuilder.setMuleConfiguration(muleConfiguration);
161
162
163 final ApplicationContext parentContext = (ApplicationContext) servletContext.getAttribute(
164 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
165 if (parentContext != null)
166 {
167 builder.setParentContext(parentContext);
168 }
169 return muleContextFactory.createMuleContext(builder, muleContextBuilder);
170 }
171
172
173
174
175
176
177
178 protected String getDefaultConfigResource()
179 {
180 return MuleServer.DEFAULT_CONFIGURATION;
181 }
182
183 public void contextDestroyed(ServletContextEvent event)
184 {
185 destroy();
186 }
187
188 public void destroy()
189 {
190 if (muleContext != null)
191 {
192 if (!muleContext.isDisposing() || !muleContext.isDisposed())
193 {
194 muleContext.dispose();
195 }
196 }
197 }
198 }