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