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