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 | 0 | 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 | 0 | super(service.getName(), service); |
43 | 0 | this.muleContext = muleContext; |
44 | 0 | } |
45 | |
|
46 | |
@Override |
47 | |
protected void registerTransitions() |
48 | |
{ |
49 | 0 | super.registerTransitions(); |
50 | |
|
51 | |
|
52 | 0 | addDirectTransition(Startable.PHASE_NAME, Pausable.PHASE_NAME); |
53 | |
|
54 | 0 | addDirectTransition(Pausable.PHASE_NAME, Resumable.PHASE_NAME); |
55 | 0 | addDirectTransition(Pausable.PHASE_NAME, Stoppable.PHASE_NAME); |
56 | 0 | } |
57 | |
|
58 | |
@Override |
59 | |
protected void notifyTransition(String destinationPhase) |
60 | |
{ |
61 | 0 | if (destinationPhase.equals(Resumable.PHASE_NAME)) |
62 | |
{ |
63 | |
|
64 | 0 | completedPhases.remove(Resumable.PHASE_NAME); |
65 | 0 | completedPhases.remove(Pausable.PHASE_NAME); |
66 | 0 | setCurrentPhase(Startable.PHASE_NAME); |
67 | |
} |
68 | 0 | } |
69 | |
|
70 | |
@Override |
71 | |
public void fireInitialisePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException |
72 | |
{ |
73 | 0 | checkPhase(Initialisable.PHASE_NAME); |
74 | |
|
75 | 0 | if(logger.isInfoEnabled()) logger.info("Initialising service: " + getLifecycleObject().getName()); |
76 | 0 | invokePhase(Initialisable.PHASE_NAME, getLifecycleObject(), callback); |
77 | 0 | fireNotification(ServiceNotification.SERVICE_INITIALISED); |
78 | 0 | } |
79 | |
|
80 | |
@Override |
81 | |
public void fireStartPhase(LifecycleCallback<FlowConstruct> callback) throws MuleException |
82 | |
{ |
83 | 0 | checkPhase(Startable.PHASE_NAME); |
84 | 0 | if(logger.isInfoEnabled()) logger.info("Starting service: " + getLifecycleObject().getName()); |
85 | |
|
86 | 0 | invokePhase(Startable.PHASE_NAME, getLifecycleObject(), callback); |
87 | 0 | fireNotification(ServiceNotification.SERVICE_STARTED); |
88 | 0 | } |
89 | |
|
90 | |
public void firePausePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException |
91 | |
{ |
92 | 0 | checkPhase(Pausable.PHASE_NAME); |
93 | 0 | if(logger.isInfoEnabled()) logger.info("Pausing service: " + getLifecycleObject().getName()); |
94 | |
|
95 | |
|
96 | 0 | invokePhase(Pausable.PHASE_NAME, getLifecycleObject(), callback); |
97 | 0 | fireNotification(ServiceNotification.SERVICE_PAUSED); |
98 | 0 | } |
99 | |
|
100 | |
public void fireResumePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException |
101 | |
{ |
102 | 0 | checkPhase(Resumable.PHASE_NAME); |
103 | 0 | if(logger.isInfoEnabled()) logger.info("Resuming service: " + getLifecycleObject().getName()); |
104 | |
|
105 | 0 | invokePhase(Resumable.PHASE_NAME, getLifecycleObject(), callback); |
106 | 0 | fireNotification(ServiceNotification.SERVICE_RESUMED); |
107 | 0 | } |
108 | |
|
109 | |
@Override |
110 | |
public void fireStopPhase(LifecycleCallback<FlowConstruct> callback) throws MuleException |
111 | |
{ |
112 | 0 | checkPhase(Stoppable.PHASE_NAME); |
113 | 0 | if(logger.isInfoEnabled()) logger.info("Stopping service: " + getLifecycleObject().getName()); |
114 | |
|
115 | 0 | invokePhase(Stoppable.PHASE_NAME, getLifecycleObject(), callback); |
116 | 0 | fireNotification(ServiceNotification.SERVICE_STOPPED); |
117 | 0 | } |
118 | |
|
119 | |
@Override |
120 | |
public void fireDisposePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException |
121 | |
{ |
122 | 0 | checkPhase(Disposable.PHASE_NAME); |
123 | 0 | if(logger.isInfoEnabled()) logger.info("Disposing service: " + getLifecycleObject().getName()); |
124 | |
|
125 | 0 | invokePhase(Disposable.PHASE_NAME, getLifecycleObject(), callback); |
126 | 0 | fireNotification(ServiceNotification.SERVICE_DISPOSED); |
127 | 0 | } |
128 | |
|
129 | |
protected void fireNotification(int action) |
130 | |
{ |
131 | |
|
132 | 0 | muleContext.fireNotification(new FlowConstructNotification(getLifecycleObject(), action)); |
133 | 0 | if(getLifecycleObject() instanceof Service) |
134 | |
{ |
135 | 0 | muleContext.fireNotification(new ServiceNotification((Service)getLifecycleObject(), action)); |
136 | |
} |
137 | 0 | } |
138 | |
} |