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 | |
|
13 | |
import javax.servlet.ServletContext; |
14 | |
import javax.servlet.ServletContextEvent; |
15 | |
import javax.servlet.ServletContextListener; |
16 | |
|
17 | |
import org.apache.commons.logging.Log; |
18 | |
import org.apache.commons.logging.LogFactory; |
19 | |
import org.springframework.context.ApplicationContext; |
20 | |
import org.springframework.web.context.WebApplicationContext; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 0 | public class DeployableMuleXmlContextListener implements ServletContextListener |
29 | |
{ |
30 | |
|
31 | 0 | protected transient final Log logger = LogFactory.getLog(DeployableMuleXmlContextListener.class); |
32 | |
|
33 | |
private WebappMuleXmlConfigurationBuilder configurationBuilder; |
34 | |
private static MuleContext muleContext; |
35 | |
|
36 | |
public void contextInitialized(ServletContextEvent event) |
37 | |
{ |
38 | 0 | initialize(event.getServletContext()); |
39 | 0 | } |
40 | |
|
41 | |
public void initialize(ServletContext context) |
42 | |
{ |
43 | 0 | String config = context.getInitParameter(MuleXmlBuilderContextListener.INIT_PARAMETER_MULE_CONFIG); |
44 | 0 | if (config == null) |
45 | |
{ |
46 | 0 | config = MuleServer.DEFAULT_CONFIGURATION; |
47 | 0 | if (logger.isDebugEnabled()) |
48 | |
{ |
49 | 0 | logger.debug("No Mule config file(s) specified, using default: " + config); |
50 | |
} |
51 | |
} |
52 | |
else |
53 | |
{ |
54 | 0 | if (logger.isDebugEnabled()) |
55 | |
{ |
56 | 0 | logger.debug("Mule config file(s): " + config); |
57 | |
} |
58 | |
} |
59 | |
|
60 | 0 | if (muleContext == null) |
61 | |
{ |
62 | 0 | throw new RuntimeException("MuleContext is not available"); |
63 | |
} |
64 | |
|
65 | |
try |
66 | |
{ |
67 | 0 | configurationBuilder = new WebappMuleXmlConfigurationBuilder(context, config); |
68 | 0 | configurationBuilder.setUseDefaultConfigResource(false); |
69 | |
|
70 | |
|
71 | 0 | final ApplicationContext parentContext = (ApplicationContext) context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); |
72 | 0 | if (parentContext != null) |
73 | |
{ |
74 | 0 | configurationBuilder.setParentContext(parentContext); |
75 | |
} |
76 | 0 | configurationBuilder.configure(muleContext); |
77 | |
} |
78 | 0 | catch (MuleException ex) |
79 | |
{ |
80 | 0 | context.log(ex.getMessage(), ex); |
81 | |
|
82 | |
|
83 | 0 | ex.printStackTrace(); |
84 | |
} |
85 | 0 | catch (Error error) |
86 | |
{ |
87 | |
|
88 | 0 | context.log(error.getMessage(), error); |
89 | |
|
90 | |
|
91 | 0 | error.printStackTrace(); |
92 | 0 | throw error; |
93 | 0 | } |
94 | 0 | } |
95 | |
|
96 | |
public void contextDestroyed(ServletContextEvent event) |
97 | |
{ |
98 | 0 | if (muleContext != null && configurationBuilder != null) |
99 | |
{ |
100 | 0 | configurationBuilder.unconfigure(muleContext); |
101 | |
} |
102 | 0 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public static void setMuleContext(MuleContext context) |
112 | |
{ |
113 | 0 | muleContext = context; |
114 | 0 | } |
115 | |
|
116 | |
} |