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