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.context.MuleContextFactory;
18 import org.mule.api.lifecycle.InitialisationException;
19 import org.mule.config.spring.SpringXmlConfigurationBuilder;
20 import org.mule.context.DefaultMuleContextFactory;
21
22 import javax.servlet.ServletContext;
23 import javax.servlet.ServletContextEvent;
24 import javax.servlet.ServletContextListener;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class MuleXmlBuilderContextListener implements ServletContextListener
44 {
45
46
47
48 public static final String INIT_PARAMETER_MULE_CONFIG = "org.mule.config";
49
50 private MuleContext muleContext;
51
52 protected transient final Log logger = LogFactory.getLog(MuleXmlBuilderContextListener.class);
53
54 public void contextInitialized(ServletContextEvent event)
55 {
56 initialize(event.getServletContext());
57 }
58
59 public void initialize(ServletContext context)
60 {
61 String config = context.getInitParameter(INIT_PARAMETER_MULE_CONFIG);
62 if (config == null)
63 {
64 config = getDefaultConfigResource();
65 System.out.println("No Mule config file(s) specified, using default: " + config);
66 }
67 else
68 {
69 System.out.println("Mule config file(s): " + config);
70 }
71
72 try
73 {
74 muleContext = createManager(config, context);
75 muleContext.start();
76 }
77 catch (MuleException ex)
78 {
79 context.log(ex.getMessage(), ex);
80
81
82 ex.printStackTrace();
83 }
84 catch (Error error)
85 {
86
87 context.log(error.getMessage(), error);
88
89
90 error.printStackTrace();
91 throw error;
92 }
93 }
94
95
96
97
98
99
100
101
102
103 protected MuleContext createManager(String configResource, ServletContext context)
104 throws ConfigurationException, InitialisationException
105 {
106 WebappMuleXmlConfigurationBuilder builder = new WebappMuleXmlConfigurationBuilder(context, configResource);
107 MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
108 return muleContextFactory.createMuleContext(builder);
109 }
110
111
112
113
114
115
116
117 protected String getDefaultConfigResource()
118 {
119 return MuleServer.DEFAULT_CONFIGURATION;
120 }
121
122 public void contextDestroyed(ServletContextEvent event)
123 {
124 destroy();
125 }
126
127 public void destroy()
128 {
129 if (muleContext != null)
130 {
131 if (!muleContext.isDisposing() || !muleContext.isDisposed())
132 {
133 muleContext.dispose();
134 }
135 }
136 }
137 }