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