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 | 0 | 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 | 0 | protected transient final Log logger = LogFactory.getLog(MuleXmlBuilderContextListener.class); |
60 | |
|
61 | |
public void contextInitialized(ServletContextEvent event) |
62 | |
{ |
63 | 0 | initialize(event.getServletContext()); |
64 | 0 | } |
65 | |
|
66 | |
public void initialize(ServletContext context) |
67 | |
{ |
68 | 0 | String config = context.getInitParameter(INIT_PARAMETER_MULE_CONFIG); |
69 | 0 | if (config == null) |
70 | |
{ |
71 | 0 | config = getDefaultConfigResource(); |
72 | 0 | if (logger.isDebugEnabled()) |
73 | |
{ |
74 | 0 | logger.debug("No Mule config file(s) specified, using default: " + config); |
75 | |
} |
76 | |
} |
77 | |
else |
78 | |
{ |
79 | 0 | if (logger.isDebugEnabled()) |
80 | |
{ |
81 | 0 | logger.debug("Mule config file(s): " + config); |
82 | |
} |
83 | |
} |
84 | |
|
85 | |
try |
86 | |
{ |
87 | 0 | muleContext = createMuleContext(config, context); |
88 | 0 | context.setAttribute(MuleProperties.MULE_CONTEXT_PROPERTY, muleContext); |
89 | 0 | muleContext.start(); |
90 | |
} |
91 | 0 | catch (MuleException ex) |
92 | |
{ |
93 | 0 | context.log(ex.getMessage(), ex); |
94 | |
|
95 | |
|
96 | 0 | ex.printStackTrace(); |
97 | |
} |
98 | 0 | catch (Error error) |
99 | |
{ |
100 | |
|
101 | 0 | context.log(error.getMessage(), error); |
102 | |
|
103 | |
|
104 | 0 | error.printStackTrace(); |
105 | 0 | throw error; |
106 | 0 | } |
107 | 0 | } |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
protected MuleContext createMuleContext(String configResource, ServletContext context) |
114 | |
throws ConfigurationException, InitialisationException |
115 | |
{ |
116 | 0 | final String serverId = StringUtils.defaultIfEmpty(context.getInitParameter("mule.serverId"), null); |
117 | 0 | WebappMuleXmlConfigurationBuilder builder = new WebappMuleXmlConfigurationBuilder(context, configResource); |
118 | 0 | MuleContextFactory muleContextFactory = new DefaultMuleContextFactory(); |
119 | |
|
120 | 0 | DefaultMuleConfiguration muleConfiguration = new DefaultMuleConfiguration(); |
121 | 0 | if (serverId != null) |
122 | |
{ |
123 | 0 | muleConfiguration.setId(serverId); |
124 | |
} |
125 | 0 | MuleContextBuilder muleContextBuilder = new DefaultMuleContextBuilder(); |
126 | 0 | muleContextBuilder.setMuleConfiguration(muleConfiguration); |
127 | |
|
128 | |
|
129 | 0 | final ApplicationContext parentContext = (ApplicationContext) context.getAttribute( |
130 | |
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); |
131 | 0 | if (parentContext != null) |
132 | |
{ |
133 | 0 | builder.setParentContext(parentContext); |
134 | |
} |
135 | 0 | return muleContextFactory.createMuleContext(builder, muleContextBuilder); |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
protected String getDefaultConfigResource() |
145 | |
{ |
146 | 0 | return MuleServer.DEFAULT_CONFIGURATION; |
147 | |
} |
148 | |
|
149 | |
public void contextDestroyed(ServletContextEvent event) |
150 | |
{ |
151 | 0 | destroy(); |
152 | 0 | } |
153 | |
|
154 | |
public void destroy() |
155 | |
{ |
156 | 0 | if (muleContext != null) |
157 | |
{ |
158 | 0 | if (!muleContext.isDisposing() || !muleContext.isDisposed()) |
159 | |
{ |
160 | 0 | muleContext.dispose(); |
161 | |
} |
162 | |
} |
163 | 0 | } |
164 | |
} |