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