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 | 0 | 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 | 0 | protected transient final Log logger = LogFactory.getLog(MuleXmlBuilderContextListener.class); |
53 | |
|
54 | |
public void contextInitialized(ServletContextEvent event) |
55 | |
{ |
56 | 0 | initialize(event.getServletContext()); |
57 | 0 | } |
58 | |
|
59 | |
public void initialize(ServletContext context) |
60 | |
{ |
61 | 0 | String config = context.getInitParameter(INIT_PARAMETER_MULE_CONFIG); |
62 | 0 | if (config == null) |
63 | |
{ |
64 | 0 | config = getDefaultConfigResource(); |
65 | 0 | System.out.println("No Mule config file(s) specified, using default: " + config); |
66 | |
} |
67 | |
else |
68 | |
{ |
69 | 0 | System.out.println("Mule config file(s): " + config); |
70 | |
} |
71 | |
|
72 | |
try |
73 | |
{ |
74 | 0 | muleContext = createManager(config, context); |
75 | 0 | muleContext.start(); |
76 | |
} |
77 | 0 | catch (MuleException ex) |
78 | |
{ |
79 | 0 | context.log(ex.getMessage(), ex); |
80 | |
|
81 | |
|
82 | 0 | ex.printStackTrace(); |
83 | |
} |
84 | 0 | catch (Error error) |
85 | |
{ |
86 | |
|
87 | 0 | context.log(error.getMessage(), error); |
88 | |
|
89 | |
|
90 | 0 | error.printStackTrace(); |
91 | 0 | throw error; |
92 | 0 | } |
93 | 0 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
protected MuleContext createManager(String configResource, ServletContext context) |
104 | |
throws ConfigurationException, InitialisationException |
105 | |
{ |
106 | 0 | WebappMuleXmlConfigurationBuilder builder = new WebappMuleXmlConfigurationBuilder(context, configResource); |
107 | 0 | MuleContextFactory muleContextFactory = new DefaultMuleContextFactory(); |
108 | 0 | return muleContextFactory.createMuleContext(builder); |
109 | |
} |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
protected String getDefaultConfigResource() |
118 | |
{ |
119 | 0 | return MuleServer.DEFAULT_CONFIGURATION; |
120 | |
} |
121 | |
|
122 | |
public void contextDestroyed(ServletContextEvent event) |
123 | |
{ |
124 | 0 | destroy(); |
125 | 0 | } |
126 | |
|
127 | |
public void destroy() |
128 | |
{ |
129 | 0 | if (muleContext != null) |
130 | |
{ |
131 | 0 | if (!muleContext.isDisposing() || !muleContext.isDisposed()) |
132 | |
{ |
133 | 0 | muleContext.dispose(); |
134 | |
} |
135 | |
} |
136 | 0 | } |
137 | |
} |