1
2
3
4
5
6
7 package org.mule.service;
8
9 import org.mule.api.MuleContext;
10 import org.mule.api.MuleException;
11 import org.mule.api.construct.FlowConstruct;
12 import org.mule.api.lifecycle.Disposable;
13 import org.mule.api.lifecycle.Initialisable;
14 import org.mule.api.lifecycle.LifecycleCallback;
15 import org.mule.api.lifecycle.Startable;
16 import org.mule.api.lifecycle.Stoppable;
17 import org.mule.api.service.Service;
18 import org.mule.context.notification.FlowConstructNotification;
19 import org.mule.context.notification.ServiceNotification;
20 import org.mule.lifecycle.SimpleLifecycleManager;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25
26
27
28
29 public class ServiceLifecycleManager extends SimpleLifecycleManager<FlowConstruct>
30 {
31
32
33
34 protected transient final Log logger = LogFactory.getLog(ServiceLifecycleManager.class);
35 protected MuleContext muleContext;
36
37 public ServiceLifecycleManager(FlowConstruct service, MuleContext muleContext) throws MuleException
38 {
39 super(service.getName(), service);
40 this.muleContext = muleContext;
41 }
42
43 @Override
44 protected void registerTransitions()
45 {
46 super.registerTransitions();
47
48
49 addDirectTransition(Startable.PHASE_NAME, Pausable.PHASE_NAME);
50
51 addDirectTransition(Pausable.PHASE_NAME, Resumable.PHASE_NAME);
52 addDirectTransition(Pausable.PHASE_NAME, Stoppable.PHASE_NAME);
53 }
54
55 @Override
56 protected void notifyTransition(String destinationPhase)
57 {
58 if (destinationPhase.equals(Resumable.PHASE_NAME))
59 {
60
61 completedPhases.remove(Resumable.PHASE_NAME);
62 completedPhases.remove(Pausable.PHASE_NAME);
63 setCurrentPhase(Startable.PHASE_NAME);
64 }
65 }
66
67 @Override
68 public void fireInitialisePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
69 {
70 checkPhase(Initialisable.PHASE_NAME);
71
72 if(logger.isInfoEnabled()) logger.info("Initialising service: " + getLifecycleObject().getName());
73 invokePhase(Initialisable.PHASE_NAME, getLifecycleObject(), callback);
74 fireNotification(ServiceNotification.SERVICE_INITIALISED);
75 }
76
77 @Override
78 public void fireStartPhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
79 {
80 checkPhase(Startable.PHASE_NAME);
81 if(logger.isInfoEnabled()) logger.info("Starting service: " + getLifecycleObject().getName());
82
83 invokePhase(Startable.PHASE_NAME, getLifecycleObject(), callback);
84 fireNotification(ServiceNotification.SERVICE_STARTED);
85 }
86
87 public void firePausePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
88 {
89 checkPhase(Pausable.PHASE_NAME);
90 if(logger.isInfoEnabled()) logger.info("Pausing service: " + getLifecycleObject().getName());
91
92
93 invokePhase(Pausable.PHASE_NAME, getLifecycleObject(), callback);
94 fireNotification(ServiceNotification.SERVICE_PAUSED);
95 }
96
97 public void fireResumePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
98 {
99 checkPhase(Resumable.PHASE_NAME);
100 if(logger.isInfoEnabled()) logger.info("Resuming service: " + getLifecycleObject().getName());
101
102 invokePhase(Resumable.PHASE_NAME, getLifecycleObject(), callback);
103 fireNotification(ServiceNotification.SERVICE_RESUMED);
104 }
105
106 @Override
107 public void fireStopPhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
108 {
109 checkPhase(Stoppable.PHASE_NAME);
110 if(logger.isInfoEnabled()) logger.info("Stopping service: " + getLifecycleObject().getName());
111
112 invokePhase(Stoppable.PHASE_NAME, getLifecycleObject(), callback);
113 fireNotification(ServiceNotification.SERVICE_STOPPED);
114 }
115
116 @Override
117 public void fireDisposePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
118 {
119 checkPhase(Disposable.PHASE_NAME);
120 if(logger.isInfoEnabled()) logger.info("Disposing service: " + getLifecycleObject().getName());
121
122 invokePhase(Disposable.PHASE_NAME, getLifecycleObject(), callback);
123 fireNotification(ServiceNotification.SERVICE_DISPOSED);
124 }
125
126 protected void fireNotification(int action)
127 {
128
129 muleContext.fireNotification(new FlowConstructNotification(getLifecycleObject(), action));
130 if(getLifecycleObject() instanceof Service)
131 {
132 muleContext.fireNotification(new ServiceNotification((Service)getLifecycleObject(), action));
133 }
134 }
135 }