1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.lifecycle; |
11 | |
|
12 | |
import org.mule.api.MuleContext; |
13 | |
import org.mule.api.MuleException; |
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.LifecycleException; |
18 | |
import org.mule.api.lifecycle.LifecyclePhase; |
19 | |
import org.mule.api.lifecycle.RegistryLifecycleHelpers; |
20 | |
import org.mule.api.lifecycle.Startable; |
21 | |
import org.mule.api.lifecycle.Stoppable; |
22 | |
import org.mule.api.registry.Registry; |
23 | |
import org.mule.config.i18n.CoreMessages; |
24 | |
import org.mule.lifecycle.phases.ContainerManagedLifecyclePhase; |
25 | |
import org.mule.lifecycle.phases.MuleContextDisposePhase; |
26 | |
import org.mule.lifecycle.phases.MuleContextInitialisePhase; |
27 | |
import org.mule.lifecycle.phases.MuleContextStartPhase; |
28 | |
import org.mule.lifecycle.phases.MuleContextStopPhase; |
29 | |
import org.mule.lifecycle.phases.NotInLifecyclePhase; |
30 | |
|
31 | |
import java.util.Collection; |
32 | |
import java.util.HashMap; |
33 | |
import java.util.HashSet; |
34 | |
import java.util.Iterator; |
35 | |
import java.util.LinkedList; |
36 | |
import java.util.List; |
37 | |
import java.util.Map; |
38 | |
import java.util.Set; |
39 | |
import java.util.TreeMap; |
40 | |
|
41 | |
import org.apache.commons.logging.Log; |
42 | |
import org.apache.commons.logging.LogFactory; |
43 | |
|
44 | |
|
45 | 0 | public class RegistryLifecycleManager extends AbstractLifecycleManager<Registry> implements RegistryLifecycleHelpers |
46 | |
{ |
47 | 0 | protected Map<String, LifecyclePhase> phases = new HashMap<String, LifecyclePhase>(); |
48 | 0 | private TreeMap<String, LifecycleCallback> callbacks = new TreeMap<String, LifecycleCallback>(); |
49 | |
private MuleContext muleContext; |
50 | |
|
51 | |
public RegistryLifecycleManager(String id, Registry object, MuleContext muleContext) |
52 | |
{ |
53 | 0 | super(id, object); |
54 | 0 | this.muleContext = muleContext; |
55 | |
|
56 | 0 | RegistryLifecycleCallback callback = new RegistryLifecycleCallback(); |
57 | |
|
58 | 0 | registerPhase(NotInLifecyclePhase.PHASE_NAME, NOT_IN_LIFECYCLE_PHASE, new EmptyLifecycleCallback()); |
59 | 0 | registerPhase(Initialisable.PHASE_NAME, new MuleContextInitialisePhase(), callback); |
60 | 0 | registerPhase(Startable.PHASE_NAME, new MuleContextStartPhase(), callback); |
61 | 0 | registerPhase(Stoppable.PHASE_NAME, new MuleContextStopPhase(), callback); |
62 | 0 | registerPhase(Disposable.PHASE_NAME, new MuleContextDisposePhase(), callback); |
63 | 0 | } |
64 | |
|
65 | |
public RegistryLifecycleManager(String id, Registry object, Map<String, LifecyclePhase> phases ) |
66 | |
{ |
67 | 0 | super(id, object); |
68 | 0 | RegistryLifecycleCallback callback = new RegistryLifecycleCallback(); |
69 | |
|
70 | 0 | registerPhase(NotInLifecyclePhase.PHASE_NAME, NOT_IN_LIFECYCLE_PHASE, new LifecycleCallback(){ |
71 | |
public void onTransition(String phaseName, Object object) throws MuleException |
72 | 0 | { }}); |
73 | |
|
74 | 0 | for (Map.Entry<String, LifecyclePhase> entry : phases.entrySet()) |
75 | |
{ |
76 | 0 | registerPhase(entry.getKey(), entry.getValue(), callback); |
77 | |
} |
78 | 0 | } |
79 | |
|
80 | |
@Override |
81 | |
protected void registerTransitions() |
82 | |
{ |
83 | 0 | addDirectTransition(NotInLifecyclePhase.PHASE_NAME, Initialisable.PHASE_NAME); |
84 | 0 | addDirectTransition(Initialisable.PHASE_NAME, Startable.PHASE_NAME); |
85 | |
|
86 | |
|
87 | 0 | addDirectTransition(Startable.PHASE_NAME, Stoppable.PHASE_NAME); |
88 | 0 | addDirectTransition(Stoppable.PHASE_NAME, Startable.PHASE_NAME); |
89 | |
|
90 | 0 | addDirectTransition(NotInLifecyclePhase.PHASE_NAME, Disposable.PHASE_NAME); |
91 | 0 | addDirectTransition(Initialisable.PHASE_NAME, Disposable.PHASE_NAME); |
92 | 0 | addDirectTransition(Stoppable.PHASE_NAME, Disposable.PHASE_NAME); |
93 | |
|
94 | 0 | } |
95 | |
|
96 | |
protected void registerPhase(String phaseName, LifecyclePhase phase, LifecycleCallback callback) |
97 | |
{ |
98 | 0 | phaseNames.add(phaseName); |
99 | 0 | callbacks.put(phaseName, callback); |
100 | 0 | phases.put(phaseName, phase); |
101 | 0 | } |
102 | |
|
103 | |
public void fireLifecycle(String destinationPhase) throws LifecycleException |
104 | |
{ |
105 | 0 | checkPhase(destinationPhase); |
106 | 0 | if (isDirectTransition(destinationPhase)) |
107 | |
{ |
108 | |
|
109 | |
|
110 | 0 | invokePhase(destinationPhase, object, callbacks.get(destinationPhase)); |
111 | |
} |
112 | |
else |
113 | |
{ |
114 | |
|
115 | 0 | boolean start = false; |
116 | 0 | for (String phase : phaseNames) |
117 | |
{ |
118 | 0 | if (start) |
119 | |
{ |
120 | 0 | invokePhase(phase, object, callbacks.get(phase)); |
121 | 0 | if (phase.equals(destinationPhase)) |
122 | |
{ |
123 | 0 | break; |
124 | |
} |
125 | |
} |
126 | 0 | if (phase.equals(getCurrentPhase())) |
127 | |
{ |
128 | 0 | start = true; |
129 | |
} |
130 | |
} |
131 | |
} |
132 | 0 | } |
133 | |
|
134 | |
protected void invokePhase(String phase, Object object, LifecycleCallback callback) throws LifecycleException |
135 | |
{ |
136 | |
try |
137 | |
{ |
138 | 0 | setExecutingPhase(phase); |
139 | 0 | callback.onTransition(phase, object); |
140 | 0 | setCurrentPhase(phase); |
141 | |
} |
142 | 0 | catch (LifecycleException e) |
143 | |
{ |
144 | 0 | throw e; |
145 | |
} |
146 | 0 | catch (MuleException e) |
147 | |
{ |
148 | 0 | throw new LifecycleException(CoreMessages.failedToInvokeLifecycle(phase, object), e); |
149 | |
} |
150 | |
finally |
151 | |
{ |
152 | 0 | setExecutingPhase(null); |
153 | 0 | } |
154 | 0 | } |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public void applyPhase(Object object, String fromPhase, String toPhase) throws LifecycleException |
163 | |
{ |
164 | |
|
165 | 0 | if(fromPhase == null || toPhase==null) |
166 | |
{ |
167 | 0 | throw new IllegalArgumentException("toPhase and fromPhase must be null"); |
168 | |
} |
169 | 0 | if(!phaseNames.contains(fromPhase)) |
170 | |
{ |
171 | 0 | throw new IllegalArgumentException("fromPhase '" + fromPhase + "' not a valid phase."); |
172 | |
} |
173 | 0 | if(!phaseNames.contains(toPhase)) |
174 | |
{ |
175 | 0 | throw new IllegalArgumentException("toPhase '" + fromPhase + "' not a valid phase."); |
176 | |
} |
177 | 0 | boolean start = false; |
178 | 0 | for (String phaseName : phaseNames) |
179 | |
{ |
180 | 0 | if(start) |
181 | |
{ |
182 | 0 | phases.get(phaseName).applyLifecycle(object); |
183 | |
} |
184 | 0 | if(toPhase.equals(phaseName)) |
185 | |
{ |
186 | 0 | break; |
187 | |
} |
188 | 0 | if(phaseName.equals(fromPhase)) |
189 | |
{ |
190 | 0 | start = true; |
191 | |
} |
192 | |
|
193 | |
} |
194 | 0 | } |
195 | |
|
196 | |
public void applyCompletedPhases(Object object) throws LifecycleException |
197 | |
{ |
198 | 0 | String lastPhase = NotInLifecyclePhase.PHASE_NAME; |
199 | 0 | for (String phase : completedPhases) |
200 | |
{ |
201 | 0 | if(isDirectTransition(lastPhase, phase)) |
202 | |
{ |
203 | 0 | LifecyclePhase lp = phases.get(phase); |
204 | 0 | lp.applyLifecycle(object); |
205 | 0 | lastPhase = phase; |
206 | 0 | } |
207 | |
} |
208 | 0 | } |
209 | |
|
210 | 0 | class RegistryLifecycleCallback implements LifecycleCallback<Object> |
211 | |
{ |
212 | |
|
213 | |
|
214 | |
|
215 | 0 | protected transient final Log logger = LogFactory.getLog(RegistryLifecycleCallback.class); |
216 | |
|
217 | |
public void onTransition(String phaseName, Object object) throws MuleException |
218 | |
{ |
219 | 0 | LifecyclePhase phase = phases.get(phaseName); |
220 | |
|
221 | 0 | if (logger.isDebugEnabled()) |
222 | |
{ |
223 | 0 | logger.debug("Applying lifecycle phase: " + phase + " for registry: " + object.getClass().getSimpleName()); |
224 | |
} |
225 | |
|
226 | 0 | if (phase instanceof ContainerManagedLifecyclePhase) |
227 | |
{ |
228 | 0 | phase.applyLifecycle(object); |
229 | 0 | return; |
230 | |
} |
231 | |
|
232 | |
|
233 | 0 | Set<Object> duplicates = new HashSet<Object>(); |
234 | |
|
235 | 0 | for (LifecycleObject lo : phase.getOrderedLifecycleObjects()) |
236 | |
{ |
237 | |
|
238 | 0 | Collection<?> targetsObj = getLifecycleObject().lookupObjects(lo.getType()); |
239 | 0 | List<Object> targets = new LinkedList<Object>(targetsObj); |
240 | 0 | if (targets.size() == 0) |
241 | |
{ |
242 | 0 | continue; |
243 | |
} |
244 | |
|
245 | 0 | lo.firePreNotification(muleContext); |
246 | |
|
247 | 0 | for (Iterator<Object> target = targets.iterator(); target.hasNext();) |
248 | |
{ |
249 | 0 | Object o = target.next(); |
250 | 0 | if (duplicates.contains(o)) |
251 | |
{ |
252 | 0 | target.remove(); |
253 | |
} |
254 | |
else |
255 | |
{ |
256 | 0 | if (logger.isDebugEnabled()) |
257 | |
{ |
258 | 0 | logger.debug("lifecycle phase: " + phase.getName() + " for object: " + o); |
259 | |
} |
260 | 0 | phase.applyLifecycle(o); |
261 | 0 | target.remove(); |
262 | 0 | duplicates.add(o); |
263 | |
} |
264 | 0 | } |
265 | |
|
266 | 0 | lo.firePostNotification(muleContext); |
267 | 0 | } |
268 | 0 | } |
269 | |
} |
270 | |
} |