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