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