1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule; |
11 | |
|
12 | |
import org.mule.api.MuleContext; |
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.MuleRuntimeException; |
15 | |
import org.mule.api.config.MuleConfiguration; |
16 | |
import org.mule.api.config.MuleProperties; |
17 | |
import org.mule.api.config.ThreadingProfile; |
18 | |
import org.mule.api.context.WorkManager; |
19 | |
import org.mule.api.context.notification.ServerNotification; |
20 | |
import org.mule.api.context.notification.ServerNotificationListener; |
21 | |
import org.mule.api.lifecycle.Disposable; |
22 | |
import org.mule.api.lifecycle.Initialisable; |
23 | |
import org.mule.api.lifecycle.InitialisationException; |
24 | |
import org.mule.api.lifecycle.LifecycleManager; |
25 | |
import org.mule.api.lifecycle.Startable; |
26 | |
import org.mule.api.lifecycle.Stoppable; |
27 | |
import org.mule.api.registry.RegistrationException; |
28 | |
import org.mule.api.registry.Registry; |
29 | |
import org.mule.api.security.SecurityManager; |
30 | |
import org.mule.api.transaction.TransactionManagerFactory; |
31 | |
import org.mule.api.transport.ConnectionStrategy; |
32 | |
import org.mule.config.DefaultMuleConfiguration; |
33 | |
import org.mule.config.i18n.CoreMessages; |
34 | |
import org.mule.context.notification.MuleContextNotification; |
35 | |
import org.mule.context.notification.NotificationException; |
36 | |
import org.mule.context.notification.ServerNotificationManager; |
37 | |
import org.mule.management.stats.AllStatistics; |
38 | |
import org.mule.util.ServerShutdownSplashScreen; |
39 | |
import org.mule.util.ServerStartupSplashScreen; |
40 | |
import org.mule.util.SplashScreen; |
41 | |
import org.mule.util.queue.QueueManager; |
42 | |
|
43 | |
import java.util.Collection; |
44 | |
|
45 | |
import javax.resource.spi.work.WorkListener; |
46 | |
import javax.transaction.TransactionManager; |
47 | |
|
48 | |
import org.apache.commons.beanutils.BeanUtils; |
49 | |
import org.apache.commons.logging.Log; |
50 | |
import org.apache.commons.logging.LogFactory; |
51 | |
|
52 | |
public class DefaultMuleContext implements MuleContext |
53 | |
{ |
54 | |
|
55 | 2 | private static transient Log logger = LogFactory.getLog(DefaultMuleContext.class); |
56 | |
|
57 | |
|
58 | 1164 | private AllStatistics stats = new AllStatistics(); |
59 | |
|
60 | |
private WorkManager workManager; |
61 | |
|
62 | |
private WorkListener workListener; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
protected LifecycleManager lifecycleManager; |
69 | |
|
70 | |
protected ServerNotificationManager notificationManager; |
71 | |
|
72 | |
private MuleConfiguration config; |
73 | |
|
74 | |
|
75 | |
private long startDate; |
76 | |
|
77 | |
public DefaultMuleContext(MuleConfiguration config, |
78 | |
WorkManager workManager, |
79 | |
WorkListener workListener, |
80 | |
LifecycleManager lifecycleManager, |
81 | |
ServerNotificationManager notificationManager) |
82 | 1164 | { |
83 | 1164 | this.config = config; |
84 | 1164 | this.workManager = workManager; |
85 | 1164 | this.workListener = workListener; |
86 | 1164 | this.lifecycleManager = lifecycleManager; |
87 | 1164 | this.notificationManager = notificationManager; |
88 | 1164 | } |
89 | |
|
90 | |
public void initialise() throws InitialisationException |
91 | |
{ |
92 | 1160 | lifecycleManager.checkPhase(Initialisable.PHASE_NAME); |
93 | |
|
94 | 1160 | if (getNotificationManager() == null) |
95 | |
{ |
96 | 0 | throw new MuleRuntimeException( |
97 | |
CoreMessages.objectIsNull(MuleProperties.OBJECT_NOTIFICATION_MANAGER)); |
98 | |
} |
99 | 1160 | if (workManager == null) |
100 | |
{ |
101 | 0 | throw new MuleRuntimeException(CoreMessages.objectIsNull("workManager")); |
102 | |
} |
103 | |
|
104 | |
try |
105 | |
{ |
106 | |
|
107 | 1160 | workManager.start(); |
108 | 1160 | getNotificationManager().start(workManager, workListener); |
109 | |
|
110 | 1160 | fireNotification(new MuleContextNotification(this, MuleContextNotification.CONTEXT_INITIALISING)); |
111 | |
|
112 | 1160 | lifecycleManager.firePhase(this, Initialisable.PHASE_NAME); |
113 | |
|
114 | 1160 | fireNotification(new MuleContextNotification(this, MuleContextNotification.CONTEXT_INITIALISED)); |
115 | |
} |
116 | 0 | catch (Exception e) |
117 | |
{ |
118 | 0 | throw new InitialisationException(e, this); |
119 | 1160 | } |
120 | 1160 | } |
121 | |
|
122 | |
public synchronized void start() throws MuleException |
123 | |
{ |
124 | 26 | lifecycleManager.checkPhase(Startable.PHASE_NAME); |
125 | 26 | if (!isStarted()) |
126 | |
{ |
127 | 26 | if (getSecurityManager() == null) |
128 | |
{ |
129 | 0 | throw new MuleRuntimeException(CoreMessages.objectIsNull("securityManager")); |
130 | |
} |
131 | 26 | if (getQueueManager() == null) |
132 | |
{ |
133 | 0 | throw new MuleRuntimeException(CoreMessages.objectIsNull("queueManager")); |
134 | |
} |
135 | |
|
136 | 26 | startDate = System.currentTimeMillis(); |
137 | |
|
138 | 26 | fireNotification(new MuleContextNotification(this, MuleContextNotification.CONTEXT_STARTING)); |
139 | |
|
140 | 26 | lifecycleManager.firePhase(this, Startable.PHASE_NAME); |
141 | |
|
142 | 26 | if (logger.isInfoEnabled()) |
143 | |
{ |
144 | 0 | SplashScreen splashScreen = SplashScreen.getInstance(ServerStartupSplashScreen.class); |
145 | 0 | splashScreen.setHeader(this); |
146 | 0 | splashScreen.setFooter(this); |
147 | 0 | logger.info(splashScreen.toString()); |
148 | |
} |
149 | 26 | fireNotification(new MuleContextNotification(this, MuleContextNotification.CONTEXT_STARTED)); |
150 | |
} |
151 | 26 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public synchronized void stop() throws MuleException |
161 | |
{ |
162 | 26 | lifecycleManager.checkPhase(Stoppable.PHASE_NAME); |
163 | 26 | fireNotification(new MuleContextNotification(this, MuleContextNotification.CONTEXT_STOPPING)); |
164 | 26 | lifecycleManager.firePhase(this, Stoppable.PHASE_NAME); |
165 | 26 | fireNotification(new MuleContextNotification(this, MuleContextNotification.CONTEXT_STOPPED)); |
166 | 26 | } |
167 | |
|
168 | |
public void dispose() |
169 | |
{ |
170 | 1144 | if (isDisposing()) |
171 | |
{ |
172 | 0 | return; |
173 | |
} |
174 | |
|
175 | 1144 | ServerNotificationManager notificationManager = getNotificationManager(); |
176 | 1144 | lifecycleManager.checkPhase(Disposable.PHASE_NAME); |
177 | 1144 | fireNotification(new MuleContextNotification(this, MuleContextNotification.CONTEXT_DISPOSING)); |
178 | |
|
179 | |
try |
180 | |
{ |
181 | 1144 | if (isStarted()) |
182 | |
{ |
183 | 12 | stop(); |
184 | |
} |
185 | |
} |
186 | 0 | catch (MuleException e) |
187 | |
{ |
188 | 0 | logger.error("Failed to stop manager: " + e.getMessage(), e); |
189 | 1144 | } |
190 | |
|
191 | |
try |
192 | |
{ |
193 | 1144 | lifecycleManager.firePhase(this, Disposable.PHASE_NAME); |
194 | |
} |
195 | 0 | catch (MuleException e) |
196 | |
{ |
197 | 0 | logger.debug("Failed to cleanly dispose Mule: " + e.getMessage(), e); |
198 | 1144 | } |
199 | |
|
200 | 1144 | notificationManager.fireNotification(new MuleContextNotification(this, MuleContextNotification.CONTEXT_DISPOSED)); |
201 | |
|
202 | 1144 | notificationManager.dispose(); |
203 | 1144 | workManager.dispose(); |
204 | |
|
205 | 1144 | if ((getStartDate() > 0) && logger.isInfoEnabled()) |
206 | |
{ |
207 | 0 | SplashScreen splashScreen = SplashScreen.getInstance(ServerShutdownSplashScreen.class); |
208 | 0 | splashScreen.setHeader(this); |
209 | 0 | logger.info(splashScreen.toString()); |
210 | |
} |
211 | |
|
212 | 1144 | } |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
public boolean isInitialised() |
221 | |
{ |
222 | 16 | return lifecycleManager.isPhaseComplete(Initialisable.PHASE_NAME); |
223 | |
} |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
public boolean isInitialising() |
231 | |
{ |
232 | 0 | return Disposable.PHASE_NAME.equals(lifecycleManager.getExecutingPhase()); |
233 | |
} |
234 | |
|
235 | |
protected boolean isStopped() |
236 | |
{ |
237 | 0 | return lifecycleManager.isPhaseComplete(Stoppable.PHASE_NAME); |
238 | |
} |
239 | |
|
240 | |
protected boolean isStopping() |
241 | |
{ |
242 | 0 | return Stoppable.PHASE_NAME.equals(lifecycleManager.getExecutingPhase()); |
243 | |
} |
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
public boolean isStarted() |
251 | |
{ |
252 | 1188 | return lifecycleManager.isPhaseComplete(Startable.PHASE_NAME); |
253 | |
} |
254 | |
|
255 | |
protected boolean isStarting() |
256 | |
{ |
257 | 0 | return Startable.PHASE_NAME.equals(lifecycleManager.getExecutingPhase()); |
258 | |
} |
259 | |
|
260 | |
public boolean isDisposed() |
261 | |
{ |
262 | 1144 | return lifecycleManager.isPhaseComplete(Disposable.PHASE_NAME); |
263 | |
} |
264 | |
|
265 | |
public boolean isDisposing() |
266 | |
{ |
267 | 2286 | return Disposable.PHASE_NAME.equals(lifecycleManager.getExecutingPhase()); |
268 | |
} |
269 | |
|
270 | |
public LifecycleManager getLifecycleManager() |
271 | |
{ |
272 | 72 | return lifecycleManager; |
273 | |
} |
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
public AllStatistics getStatistics() |
281 | |
{ |
282 | 810 | return stats; |
283 | |
} |
284 | |
|
285 | |
public void registerListener(ServerNotificationListener l) throws NotificationException |
286 | |
{ |
287 | 20 | registerListener(l, null); |
288 | 20 | } |
289 | |
|
290 | |
public void registerListener(ServerNotificationListener l, String resourceIdentifier) throws NotificationException |
291 | |
{ |
292 | 26 | ServerNotificationManager notificationManager = getNotificationManager(); |
293 | 26 | if (notificationManager == null) |
294 | |
{ |
295 | 0 | throw new MuleRuntimeException(CoreMessages.serverNotificationManagerNotEnabled()); |
296 | |
} |
297 | 26 | notificationManager.addListenerSubscription(l, resourceIdentifier); |
298 | 26 | } |
299 | |
|
300 | |
public void unregisterListener(ServerNotificationListener l) |
301 | |
{ |
302 | 4 | ServerNotificationManager notificationManager = getNotificationManager(); |
303 | 4 | if (notificationManager != null) |
304 | |
{ |
305 | 4 | notificationManager.removeListener(l); |
306 | |
} |
307 | 4 | } |
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
public void fireNotification(ServerNotification notification) |
320 | |
{ |
321 | 9744 | ServerNotificationManager notificationManager = getNotificationManager(); |
322 | 9744 | if (notificationManager != null) |
323 | |
{ |
324 | 9744 | notificationManager.fireNotification(notification); |
325 | |
} |
326 | 0 | else if (logger.isDebugEnabled()) |
327 | |
{ |
328 | 0 | logger.debug("MuleEvent Manager is not enabled, ignoring notification: " + notification); |
329 | |
} |
330 | 9744 | } |
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
public void setSecurityManager(SecurityManager securityManager) throws RegistrationException |
341 | |
{ |
342 | 0 | checkLifecycleForPropertySet(MuleProperties.OBJECT_SECURITY_MANAGER, Initialisable.PHASE_NAME); |
343 | 0 | getRegistry().registerObject(MuleProperties.OBJECT_SECURITY_MANAGER, securityManager); |
344 | 0 | } |
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
public SecurityManager getSecurityManager() |
355 | |
{ |
356 | 26 | SecurityManager securityManager = (SecurityManager) getRegistry().lookupObject( |
357 | |
MuleProperties.OBJECT_SECURITY_MANAGER); |
358 | 26 | if (securityManager == null) |
359 | |
{ |
360 | 0 | Collection temp = getRegistry().lookupObjects(SecurityManager.class); |
361 | 0 | if (temp.size() > 0) |
362 | |
{ |
363 | 0 | securityManager = ((SecurityManager) temp.iterator().next()); |
364 | |
} |
365 | |
} |
366 | 26 | return securityManager; |
367 | |
} |
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
public WorkManager getWorkManager() |
387 | |
{ |
388 | 16 | return workManager; |
389 | |
} |
390 | |
|
391 | |
public WorkListener getWorkListener() |
392 | |
{ |
393 | 0 | return workListener; |
394 | |
} |
395 | |
|
396 | |
public QueueManager getQueueManager() |
397 | |
{ |
398 | 820 | QueueManager queueManager = (QueueManager) getRegistry().lookupObject(MuleProperties.OBJECT_QUEUE_MANAGER); |
399 | 820 | if (queueManager == null) |
400 | |
{ |
401 | 0 | Collection temp = getRegistry().lookupObjects(QueueManager.class); |
402 | 0 | if (temp.size() > 0) |
403 | |
{ |
404 | 0 | queueManager = ((QueueManager) temp.iterator().next()); |
405 | |
} |
406 | |
} |
407 | 820 | return queueManager; |
408 | |
} |
409 | |
|
410 | |
public void setQueueManager(QueueManager queueManager) throws RegistrationException |
411 | |
{ |
412 | 0 | checkLifecycleForPropertySet(MuleProperties.OBJECT_QUEUE_MANAGER, Initialisable.PHASE_NAME); |
413 | 0 | getRegistry().registerObject(MuleProperties.OBJECT_QUEUE_MANAGER, queueManager); |
414 | 0 | } |
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
public MuleConfiguration getConfiguration() |
421 | |
{ |
422 | 44070 | return config; |
423 | |
} |
424 | |
|
425 | |
public ServerNotificationManager getNotificationManager() |
426 | |
{ |
427 | 13780 | return notificationManager; |
428 | |
} |
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
public void setTransactionManager(TransactionManager manager) throws RegistrationException |
437 | |
{ |
438 | |
|
439 | 0 | getRegistry().registerObject(MuleProperties.OBJECT_TRANSACTION_MANAGER, manager); |
440 | 0 | } |
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
public TransactionManager getTransactionManager() |
450 | |
{ |
451 | 0 | TransactionManager transactionManager = (TransactionManager) getRegistry().lookupObject( |
452 | |
MuleProperties.OBJECT_TRANSACTION_MANAGER); |
453 | 0 | if (transactionManager == null) |
454 | |
{ |
455 | 0 | Collection temp = getRegistry().lookupObjects(TransactionManagerFactory.class); |
456 | 0 | if (temp.size() > 0) |
457 | |
{ |
458 | |
try |
459 | |
{ |
460 | 0 | transactionManager = (((TransactionManagerFactory) temp.iterator().next()).create()); |
461 | |
} |
462 | 0 | catch (Exception e) |
463 | |
{ |
464 | 0 | throw new MuleRuntimeException(CoreMessages.failedToCreate("transaction manager"), e); |
465 | 0 | } |
466 | |
} |
467 | |
else |
468 | |
{ |
469 | 0 | temp = getRegistry().lookupObjects(TransactionManager.class); |
470 | 0 | if (temp.size() > 0) |
471 | |
{ |
472 | 0 | transactionManager = (((TransactionManager) temp.iterator().next())); |
473 | |
} |
474 | |
} |
475 | |
} |
476 | 0 | return transactionManager; |
477 | |
} |
478 | |
|
479 | |
protected void checkLifecycleForPropertySet(String propertyName, String phase) throws IllegalStateException |
480 | |
{ |
481 | 0 | if (lifecycleManager.isPhaseComplete(phase)) |
482 | |
{ |
483 | 0 | throw new IllegalStateException("Cannot set property: '" + propertyName + "' once the server has been gone through the " + phase + " phase."); |
484 | |
} |
485 | 0 | } |
486 | |
|
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
|
492 | |
public Registry getRegistry() |
493 | |
{ |
494 | 22476 | return RegistryContext.getRegistry(); |
495 | |
} |
496 | |
|
497 | |
|
498 | |
|
499 | |
|
500 | |
|
501 | |
public void applyLifecycle(Object object) throws MuleException |
502 | |
{ |
503 | 34250 | lifecycleManager.applyLifecycle(this, object); |
504 | 34250 | } |
505 | |
|
506 | |
public ThreadingProfile getDefaultMessageDispatcherThreadingProfile() |
507 | |
{ |
508 | 472 | return (ThreadingProfile) getRegistry().lookupObject(MuleProperties.OBJECT_DEFAULT_MESSAGE_DISPATCHER_THREADING_PROFILE); |
509 | |
} |
510 | |
|
511 | |
public ThreadingProfile getDefaultMessageRequesterThreadingProfile() |
512 | |
{ |
513 | 0 | return (ThreadingProfile) getRegistry().lookupObject(MuleProperties.OBJECT_DEFAULT_MESSAGE_REQUESTER_THREADING_PROFILE); |
514 | |
} |
515 | |
|
516 | |
public ThreadingProfile getDefaultMessageReceiverThreadingProfile() |
517 | |
{ |
518 | 472 | return (ThreadingProfile) getRegistry().lookupObject(MuleProperties.OBJECT_DEFAULT_MESSAGE_RECEIVER_THREADING_PROFILE); |
519 | |
} |
520 | |
|
521 | |
public ThreadingProfile getDefaultComponentThreadingProfile() |
522 | |
{ |
523 | 390 | return (ThreadingProfile) getRegistry().lookupObject(MuleProperties.OBJECT_DEFAULT_COMPONENT_THREADING_PROFILE); |
524 | |
} |
525 | |
|
526 | |
public ThreadingProfile getDefaultThreadingProfile() |
527 | |
{ |
528 | 0 | return (ThreadingProfile) getRegistry().lookupObject(MuleProperties.OBJECT_DEFAULT_THREADING_PROFILE); |
529 | |
} |
530 | |
|
531 | |
|
532 | |
|
533 | |
|
534 | |
|
535 | |
|
536 | |
|
537 | |
|
538 | |
public ConnectionStrategy getDefaultConnectionStrategy() |
539 | |
{ |
540 | 486 | ConnectionStrategy defaultConnectionStrategy = |
541 | |
(ConnectionStrategy) getRegistry().lookupObject(MuleProperties.OBJECT_DEFAULT_CONNECTION_STRATEGY); |
542 | |
try |
543 | |
{ |
544 | 486 | return (ConnectionStrategy) BeanUtils.cloneBean(defaultConnectionStrategy); |
545 | |
} |
546 | 0 | catch (Exception e) |
547 | |
{ |
548 | 0 | throw new MuleRuntimeException(CoreMessages.failedToClone("Connection Strategy"), e); |
549 | |
} |
550 | |
} |
551 | |
|
552 | |
|
553 | |
|
554 | |
|
555 | |
|
556 | |
|
557 | |
public long getStartDate() |
558 | |
{ |
559 | 1144 | return startDate; |
560 | |
} |
561 | |
} |