1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.context; |
8 | |
|
9 | |
import org.mule.DefaultMuleContext; |
10 | |
import org.mule.api.MuleContext; |
11 | |
import org.mule.api.MuleRuntimeException; |
12 | |
import org.mule.api.config.MuleConfiguration; |
13 | |
import org.mule.api.config.ThreadingProfile; |
14 | |
import org.mule.api.context.MuleContextBuilder; |
15 | |
import org.mule.api.context.WorkManager; |
16 | |
import org.mule.api.context.notification.ConnectionNotificationListener; |
17 | |
import org.mule.api.context.notification.CustomNotificationListener; |
18 | |
import org.mule.api.context.notification.ExceptionNotificationListener; |
19 | |
import org.mule.api.context.notification.ManagementNotificationListener; |
20 | |
import org.mule.api.context.notification.ModelNotificationListener; |
21 | |
import org.mule.api.context.notification.MuleContextNotificationListener; |
22 | |
import org.mule.api.context.notification.RegistryNotificationListener; |
23 | |
import org.mule.api.context.notification.RoutingNotificationListener; |
24 | |
import org.mule.api.context.notification.SecurityNotificationListener; |
25 | |
import org.mule.api.context.notification.ServiceNotificationListener; |
26 | |
import org.mule.api.context.notification.TransactionNotificationListener; |
27 | |
import org.mule.api.lifecycle.LifecycleManager; |
28 | |
import org.mule.config.DefaultMuleConfiguration; |
29 | |
import org.mule.config.i18n.Message; |
30 | |
import org.mule.config.i18n.MessageFactory; |
31 | |
import org.mule.context.notification.ConnectionNotification; |
32 | |
import org.mule.context.notification.CustomNotification; |
33 | |
import org.mule.context.notification.ExceptionNotification; |
34 | |
import org.mule.context.notification.ManagementNotification; |
35 | |
import org.mule.context.notification.ModelNotification; |
36 | |
import org.mule.context.notification.MuleContextNotification; |
37 | |
import org.mule.context.notification.RegistryNotification; |
38 | |
import org.mule.context.notification.RoutingNotification; |
39 | |
import org.mule.context.notification.SecurityNotification; |
40 | |
import org.mule.context.notification.ServerNotificationManager; |
41 | |
import org.mule.context.notification.ServiceNotification; |
42 | |
import org.mule.context.notification.TransactionNotification; |
43 | |
import org.mule.lifecycle.MuleContextLifecycleManager; |
44 | |
import org.mule.util.ClassUtils; |
45 | |
import org.mule.util.SplashScreen; |
46 | |
import org.mule.work.DefaultWorkListener; |
47 | |
import org.mule.work.MuleWorkManager; |
48 | |
|
49 | |
import javax.resource.spi.work.WorkListener; |
50 | |
|
51 | |
import org.apache.commons.logging.Log; |
52 | |
import org.apache.commons.logging.LogFactory; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | 0 | public class DefaultMuleContextBuilder implements MuleContextBuilder |
61 | |
{ |
62 | 0 | protected static final Log logger = LogFactory.getLog(DefaultMuleContextBuilder.class); |
63 | |
|
64 | |
protected MuleConfiguration config; |
65 | |
|
66 | |
protected MuleContextLifecycleManager lifecycleManager; |
67 | |
|
68 | |
protected WorkManager workManager; |
69 | |
|
70 | |
protected WorkListener workListener; |
71 | |
|
72 | |
protected ServerNotificationManager notificationManager; |
73 | |
|
74 | |
protected SplashScreen startupScreen; |
75 | |
|
76 | |
protected SplashScreen shutdownScreen; |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public MuleContext buildMuleContext() |
82 | |
{ |
83 | 0 | logger.debug("Building new DefaultMuleContext instance with MuleContextBuilder: " + this); |
84 | 0 | MuleContextLifecycleManager manager = getLifecycleManager(); |
85 | 0 | DefaultMuleContext muleContext = new DefaultMuleContext(getMuleConfiguration(), |
86 | |
getWorkManager(), |
87 | |
getWorkListener(), |
88 | |
manager, |
89 | |
getNotificationManager()); |
90 | 0 | manager.setMuleContext(muleContext); |
91 | 0 | muleContext.setExecutionClassLoader(Thread.currentThread().getContextClassLoader()); |
92 | 0 | return muleContext; |
93 | |
} |
94 | |
|
95 | |
public void setMuleConfiguration(MuleConfiguration config) |
96 | |
{ |
97 | 0 | this.config = config; |
98 | 0 | } |
99 | |
|
100 | |
public void setWorkManager(WorkManager workManager) |
101 | |
{ |
102 | 0 | this.workManager = workManager; |
103 | 0 | } |
104 | |
|
105 | |
public void setWorkListener(WorkListener workListener) |
106 | |
{ |
107 | 0 | this.workListener = workListener; |
108 | 0 | } |
109 | |
|
110 | |
public void setNotificationManager(ServerNotificationManager notificationManager) |
111 | |
{ |
112 | 0 | this.notificationManager = notificationManager; |
113 | 0 | } |
114 | |
|
115 | |
protected MuleConfiguration getMuleConfiguration() |
116 | |
{ |
117 | 0 | if (config != null) |
118 | |
{ |
119 | 0 | return config; |
120 | |
} |
121 | |
else |
122 | |
{ |
123 | 0 | return createMuleConfiguration(); |
124 | |
} |
125 | |
} |
126 | |
|
127 | |
protected MuleContextLifecycleManager getLifecycleManager() |
128 | |
{ |
129 | 0 | if (lifecycleManager != null) |
130 | |
{ |
131 | 0 | return lifecycleManager; |
132 | |
} |
133 | |
else |
134 | |
{ |
135 | 0 | return createLifecycleManager(); |
136 | |
} |
137 | |
} |
138 | |
|
139 | |
public void setLifecycleManager(LifecycleManager manager) |
140 | |
{ |
141 | 0 | if (!(manager instanceof MuleContextLifecycleManager)) |
142 | |
{ |
143 | 0 | Message msg = MessageFactory.createStaticMessage( |
144 | |
"lifecycle manager for MuleContext must be a MuleContextLifecycleManager"); |
145 | 0 | throw new MuleRuntimeException(msg); |
146 | |
} |
147 | |
|
148 | 0 | lifecycleManager = (MuleContextLifecycleManager) manager; |
149 | 0 | } |
150 | |
|
151 | |
protected WorkManager getWorkManager() |
152 | |
{ |
153 | 0 | if (workManager != null) |
154 | |
{ |
155 | 0 | return workManager; |
156 | |
} |
157 | |
else |
158 | |
{ |
159 | 0 | return createWorkManager(); |
160 | |
} |
161 | |
} |
162 | |
|
163 | |
protected WorkListener getWorkListener() |
164 | |
{ |
165 | 0 | if (workListener != null) |
166 | |
{ |
167 | 0 | return workListener; |
168 | |
} |
169 | |
else |
170 | |
{ |
171 | 0 | return createWorkListener(); |
172 | |
} |
173 | |
} |
174 | |
|
175 | |
protected ServerNotificationManager getNotificationManager() |
176 | |
{ |
177 | 0 | if (notificationManager != null) |
178 | |
{ |
179 | 0 | return notificationManager; |
180 | |
} |
181 | |
else |
182 | |
{ |
183 | 0 | return createNotificationManager(); |
184 | |
} |
185 | |
} |
186 | |
|
187 | |
public SplashScreen getStartupScreen() |
188 | |
{ |
189 | 0 | return startupScreen; |
190 | |
} |
191 | |
|
192 | |
public void setStartupScreen(SplashScreen startupScreen) |
193 | |
{ |
194 | 0 | this.startupScreen = startupScreen; |
195 | 0 | } |
196 | |
|
197 | |
public SplashScreen getShutdownScreen() |
198 | |
{ |
199 | 0 | return shutdownScreen; |
200 | |
} |
201 | |
|
202 | |
public void setShutdownScreen(SplashScreen shutdownScreen) |
203 | |
{ |
204 | 0 | this.shutdownScreen = shutdownScreen; |
205 | 0 | } |
206 | |
|
207 | |
protected DefaultMuleConfiguration createMuleConfiguration() |
208 | |
{ |
209 | 0 | return new DefaultMuleConfiguration(); |
210 | |
} |
211 | |
|
212 | |
protected MuleContextLifecycleManager createLifecycleManager() |
213 | |
{ |
214 | 0 | return new MuleContextLifecycleManager(); |
215 | |
} |
216 | |
|
217 | |
protected MuleWorkManager createWorkManager() |
218 | |
{ |
219 | 0 | final MuleConfiguration config = getMuleConfiguration(); |
220 | |
|
221 | 0 | final String threadPrefix = config.isContainerMode() |
222 | |
? String.format("[%s].Mule", config.getId()) |
223 | |
: "MuleServer"; |
224 | 0 | return new MuleWorkManager(ThreadingProfile.DEFAULT_THREADING_PROFILE, threadPrefix, config.getShutdownTimeout()); |
225 | |
} |
226 | |
|
227 | |
protected DefaultWorkListener createWorkListener() |
228 | |
{ |
229 | 0 | return new DefaultWorkListener(); |
230 | |
} |
231 | |
|
232 | |
protected ServerNotificationManager createNotificationManager() |
233 | |
{ |
234 | 0 | ServerNotificationManager manager = new ServerNotificationManager(); |
235 | 0 | manager.addInterfaceToType(MuleContextNotificationListener.class, |
236 | |
MuleContextNotification.class); |
237 | 0 | manager.addInterfaceToType(ModelNotificationListener.class, ModelNotification.class); |
238 | 0 | manager.addInterfaceToType(RoutingNotificationListener.class, RoutingNotification.class); |
239 | 0 | manager.addInterfaceToType(ServiceNotificationListener.class, |
240 | |
ServiceNotification.class); |
241 | 0 | manager.addInterfaceToType(SecurityNotificationListener.class, |
242 | |
SecurityNotification.class); |
243 | 0 | manager.addInterfaceToType(ManagementNotificationListener.class, |
244 | |
ManagementNotification.class); |
245 | 0 | manager.addInterfaceToType(CustomNotificationListener.class, CustomNotification.class); |
246 | 0 | manager.addInterfaceToType(ConnectionNotificationListener.class, |
247 | |
ConnectionNotification.class); |
248 | 0 | manager.addInterfaceToType(RegistryNotificationListener.class, |
249 | |
RegistryNotification.class); |
250 | 0 | manager.addInterfaceToType(ExceptionNotificationListener.class, |
251 | |
ExceptionNotification.class); |
252 | 0 | manager.addInterfaceToType(TransactionNotificationListener.class, |
253 | |
TransactionNotification.class); |
254 | 0 | return manager; |
255 | |
} |
256 | |
|
257 | |
@Override |
258 | |
public String toString() |
259 | |
{ |
260 | 0 | return ClassUtils.getClassName(getClass()) + |
261 | |
"{muleConfiguration=" + config + |
262 | |
", lifecycleManager=" + lifecycleManager + |
263 | |
", workManager=" + workManager + |
264 | |
", workListener=" + workListener + |
265 | |
", notificationManager=" + notificationManager + "}"; |
266 | |
} |
267 | |
} |