1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.impl.model; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.config.i18n.CoreMessages; |
15 | |
import org.mule.impl.DefaultComponentExceptionStrategy; |
16 | |
import org.mule.impl.DefaultLifecycleAdapterFactory; |
17 | |
import org.mule.impl.ImmutableMuleDescriptor; |
18 | |
import org.mule.impl.MuleSession; |
19 | |
import org.mule.impl.internal.notifications.ModelNotification; |
20 | |
import org.mule.impl.model.resolvers.DynamicEntryPointResolver; |
21 | |
import org.mule.umo.UMOComponent; |
22 | |
import org.mule.umo.UMODescriptor; |
23 | |
import org.mule.umo.UMOException; |
24 | |
import org.mule.umo.UMOSession; |
25 | |
import org.mule.umo.lifecycle.Initialisable; |
26 | |
import org.mule.umo.lifecycle.InitialisationException; |
27 | |
import org.mule.umo.lifecycle.UMOLifecycleAdapterFactory; |
28 | |
import org.mule.umo.manager.UMOServerNotification; |
29 | |
import org.mule.umo.model.ModelException; |
30 | |
import org.mule.umo.model.UMOEntryPointResolver; |
31 | |
import org.mule.umo.model.UMOModel; |
32 | |
|
33 | |
import java.beans.ExceptionListener; |
34 | |
import java.util.Iterator; |
35 | |
import java.util.List; |
36 | |
import java.util.Map; |
37 | |
|
38 | |
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; |
39 | |
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentSkipListMap; |
40 | |
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean; |
41 | |
|
42 | |
import org.apache.commons.logging.Log; |
43 | |
import org.apache.commons.logging.LogFactory; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public abstract class AbstractModel implements UMOModel |
51 | |
{ |
52 | |
|
53 | |
|
54 | |
|
55 | 818 | protected transient Log logger = LogFactory.getLog(getClass()); |
56 | |
|
57 | |
private String name; |
58 | 818 | private UMOEntryPointResolver entryPointResolver = new DynamicEntryPointResolver(); |
59 | 818 | private UMOLifecycleAdapterFactory lifecycleAdapterFactory = new DefaultLifecycleAdapterFactory(); |
60 | |
|
61 | 818 | private Map components = new ConcurrentSkipListMap(); |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 818 | protected Map descriptors = new ConcurrentHashMap(); |
67 | |
|
68 | 818 | private AtomicBoolean initialised = new AtomicBoolean(false); |
69 | |
|
70 | 818 | private AtomicBoolean started = new AtomicBoolean(false); |
71 | |
|
72 | 818 | private ExceptionListener exceptionListener = new DefaultComponentExceptionStrategy(); |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public AbstractModel() |
78 | 818 | { |
79 | |
|
80 | |
|
81 | 818 | entryPointResolver = new DynamicEntryPointResolver(); |
82 | 818 | lifecycleAdapterFactory = new DefaultLifecycleAdapterFactory(); |
83 | 818 | components = new ConcurrentSkipListMap(); |
84 | 818 | descriptors = new ConcurrentHashMap(); |
85 | 818 | exceptionListener = new DefaultComponentExceptionStrategy(); |
86 | 818 | name = "mule"; |
87 | 818 | } |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public String getName() |
95 | |
{ |
96 | 2462 | return name; |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public void setName(String name) |
105 | |
{ |
106 | 460 | this.name = name; |
107 | 460 | } |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public UMOEntryPointResolver getEntryPointResolver() |
115 | |
{ |
116 | 80 | return entryPointResolver; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public void setEntryPointResolver(UMOEntryPointResolver entryPointResolver) |
125 | |
{ |
126 | 0 | this.entryPointResolver = entryPointResolver; |
127 | 0 | } |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
public boolean isComponentRegistered(String name) |
135 | |
{ |
136 | 4 | return (components.get(name) != null); |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public UMOComponent registerComponent(UMODescriptor descriptor) throws UMOException |
145 | |
{ |
146 | 38 | if (descriptor == null) |
147 | |
{ |
148 | 0 | throw new ModelException(CoreMessages.objectIsNull("UMO Descriptor")); |
149 | |
} |
150 | |
|
151 | |
|
152 | 38 | if (descriptor.getExceptionListener() == null) |
153 | |
{ |
154 | 24 | descriptor.setExceptionListener(exceptionListener); |
155 | |
} |
156 | |
|
157 | 38 | if (initialised.get()) |
158 | |
{ |
159 | 10 | descriptor.initialise(); |
160 | |
} |
161 | |
|
162 | |
|
163 | 38 | if (descriptors.get(descriptor.getName()) != null) |
164 | |
{ |
165 | 2 | throw new ModelException(CoreMessages.descriptorAlreadyExists(descriptor.getName())); |
166 | |
} |
167 | |
|
168 | 36 | UMOComponent component = (UMOComponent) components.get(descriptor.getName()); |
169 | |
|
170 | 36 | if (component == null) |
171 | |
{ |
172 | 36 | component = createComponent(descriptor); |
173 | 36 | descriptors.put(descriptor.getName(), descriptor); |
174 | 36 | components.put(descriptor.getName(), component); |
175 | |
} |
176 | |
|
177 | 36 | logger.debug("Added Mule UMO: " + descriptor.getName()); |
178 | |
|
179 | 36 | if (initialised.get()) |
180 | |
{ |
181 | 10 | logger.info("Initialising component: " + descriptor.getName()); |
182 | 10 | component.initialise(); |
183 | |
} |
184 | 36 | if (started.get()) |
185 | |
{ |
186 | 10 | logger.info("Starting component: " + descriptor.getName()); |
187 | 10 | component.start(); |
188 | |
} |
189 | 36 | return component; |
190 | |
} |
191 | |
|
192 | |
public void unregisterComponent(UMODescriptor descriptor) throws UMOException |
193 | |
{ |
194 | 0 | if (descriptor == null) |
195 | |
{ |
196 | 0 | throw new ModelException(CoreMessages.objectIsNull("UMO Descriptor")); |
197 | |
} |
198 | |
|
199 | 0 | if (!isComponentRegistered(descriptor.getName())) |
200 | |
{ |
201 | 0 | throw new ModelException(CoreMessages.componentNotRegistered(descriptor.getName())); |
202 | |
} |
203 | 0 | UMOComponent component = (UMOComponent) components.remove(descriptor.getName()); |
204 | |
|
205 | 0 | if (component != null) |
206 | |
{ |
207 | 0 | component.stop(); |
208 | 0 | descriptors.remove(descriptor.getName()); |
209 | 0 | component.dispose(); |
210 | 0 | logger.info("The component: " + descriptor.getName() + " has been unregistered and disposing"); |
211 | |
} |
212 | 0 | } |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
public UMOLifecycleAdapterFactory getLifecycleAdapterFactory() |
220 | |
{ |
221 | 80 | return lifecycleAdapterFactory; |
222 | |
} |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
public void setLifecycleAdapterFactory(UMOLifecycleAdapterFactory lifecycleAdapterFactory) |
230 | |
{ |
231 | 0 | this.lifecycleAdapterFactory = lifecycleAdapterFactory; |
232 | 0 | } |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
public void dispose() |
238 | |
{ |
239 | 482 | fireNotification(new ModelNotification(this, ModelNotification.MODEL_DISPOSING)); |
240 | |
|
241 | 482 | for (Iterator i = components.values().iterator(); i.hasNext();) |
242 | |
{ |
243 | 36 | UMOComponent component = (UMOComponent) i.next(); |
244 | |
try |
245 | |
{ |
246 | 36 | component.dispose(); |
247 | 36 | logger.info(component + " has been destroyed successfully"); |
248 | |
} |
249 | 0 | catch (Exception e1) |
250 | |
{ |
251 | |
|
252 | |
|
253 | 0 | logger.warn("Failed to dispose component: " + e1.getMessage()); |
254 | 36 | } |
255 | 36 | } |
256 | |
|
257 | 482 | components.clear(); |
258 | 482 | descriptors.clear(); |
259 | |
|
260 | 482 | fireNotification(new ModelNotification(this, ModelNotification.MODEL_DISPOSED)); |
261 | 482 | } |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
public UMOSession getComponentSession(String muleName) |
270 | |
{ |
271 | 0 | UMOComponent component = (UMOComponent) components.get(muleName); |
272 | 0 | if (component == null) |
273 | |
{ |
274 | 0 | logger.warn("Component: " + muleName + " not found returning null session"); |
275 | 0 | return null; |
276 | |
} |
277 | |
else |
278 | |
{ |
279 | 0 | return new MuleSession(component); |
280 | |
} |
281 | |
} |
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
public void stop() throws UMOException |
289 | |
{ |
290 | 96 | fireNotification(new ModelNotification(this, ModelNotification.MODEL_STOPPING)); |
291 | 96 | for (Iterator i = components.values().iterator(); i.hasNext();) |
292 | |
{ |
293 | 22 | UMOComponent component = (UMOComponent) i.next(); |
294 | 22 | component.stop(); |
295 | 22 | logger.info("Component " + component + " has been stopped successfully"); |
296 | 22 | } |
297 | 96 | started.set(false); |
298 | 96 | initialised.set(false); |
299 | 96 | fireNotification(new ModelNotification(this, ModelNotification.MODEL_STOPPED)); |
300 | 96 | } |
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
public void start() throws UMOException |
308 | |
{ |
309 | 100 | if (!initialised.get()) |
310 | |
{ |
311 | 0 | initialise(); |
312 | |
} |
313 | |
|
314 | 100 | if (!started.get()) |
315 | |
{ |
316 | 100 | fireNotification(new ModelNotification(this, ModelNotification.MODEL_STARTING)); |
317 | |
|
318 | 100 | for (Iterator i = components.values().iterator(); i.hasNext();) |
319 | |
{ |
320 | 22 | AbstractComponent component = (AbstractComponent) i.next(); |
321 | |
|
322 | 22 | if (component.getDescriptor().getInitialState().equals( |
323 | |
ImmutableMuleDescriptor.INITIAL_STATE_STARTED)) |
324 | |
{ |
325 | 20 | component.start(); |
326 | 10 | logger.info("Component " + component + " has been started successfully"); |
327 | |
} |
328 | 2 | else if (component.getDescriptor().getInitialState().equals( |
329 | |
ImmutableMuleDescriptor.INITIAL_STATE_PAUSED)) |
330 | |
{ |
331 | 0 | component.start(true); |
332 | 0 | logger.info("Component " + component |
333 | |
+ " has been started and paused (initial state = 'paused')"); |
334 | |
} |
335 | |
else |
336 | |
{ |
337 | 2 | logger.info("Component " + component |
338 | |
+ " has not been started (initial state = 'stopped')"); |
339 | |
} |
340 | 12 | } |
341 | 90 | started.set(true); |
342 | 90 | fireNotification(new ModelNotification(this, ModelNotification.MODEL_STARTED)); |
343 | |
} |
344 | |
else |
345 | |
{ |
346 | 0 | logger.debug("Model already started"); |
347 | |
} |
348 | 90 | } |
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
public void startComponent(String name) throws UMOException |
359 | |
{ |
360 | 0 | UMOComponent component = (UMOComponent) components.get(name); |
361 | 0 | if (component == null) |
362 | |
{ |
363 | 0 | throw new ModelException(CoreMessages.componentNotRegistered(name)); |
364 | |
} |
365 | |
else |
366 | |
{ |
367 | 0 | component.start(); |
368 | 0 | logger.info("Mule " + component.toString() + " has been started successfully"); |
369 | |
} |
370 | 0 | } |
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
public void stopComponent(String name) throws UMOException |
380 | |
{ |
381 | 0 | UMOComponent component = (UMOComponent) components.get(name); |
382 | 0 | if (component == null) |
383 | |
{ |
384 | 0 | throw new ModelException(CoreMessages.componentNotRegistered(name)); |
385 | |
} |
386 | |
else |
387 | |
{ |
388 | 0 | component.stop(); |
389 | 0 | logger.info("mule " + name + " has been stopped successfully"); |
390 | |
} |
391 | 0 | } |
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
public void pauseComponent(String name) throws UMOException |
407 | |
{ |
408 | 0 | UMOComponent component = (UMOComponent) components.get(name); |
409 | |
|
410 | 0 | if (component != null) |
411 | |
{ |
412 | 0 | component.pause(); |
413 | 0 | logger.info("Mule Component " + name + " has been paused successfully"); |
414 | |
} |
415 | |
else |
416 | |
{ |
417 | 0 | throw new ModelException(CoreMessages.componentNotRegistered(name)); |
418 | |
} |
419 | 0 | } |
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
public void resumeComponent(String name) throws UMOException |
430 | |
{ |
431 | 0 | UMOComponent component = (UMOComponent) components.get(name); |
432 | |
|
433 | 0 | if (component != null) |
434 | |
{ |
435 | 0 | component.resume(); |
436 | 0 | logger.info("Mule Component " + name + " has been resumed successfully"); |
437 | |
} |
438 | |
else |
439 | |
{ |
440 | 0 | throw new ModelException(CoreMessages.componentNotRegistered(name)); |
441 | |
} |
442 | 0 | } |
443 | |
|
444 | |
public void setComponents(List descriptors) throws UMOException |
445 | |
{ |
446 | 0 | for (Iterator iterator = descriptors.iterator(); iterator.hasNext();) |
447 | |
{ |
448 | 0 | registerComponent((UMODescriptor) iterator.next()); |
449 | |
} |
450 | 0 | } |
451 | |
|
452 | |
public void initialise() throws InitialisationException |
453 | |
{ |
454 | 110 | if (!initialised.get()) |
455 | |
{ |
456 | 110 | fireNotification(new ModelNotification(this, ModelNotification.MODEL_INITIALISING)); |
457 | |
|
458 | 110 | if (exceptionListener instanceof Initialisable) |
459 | |
{ |
460 | 110 | ((Initialisable) exceptionListener).initialise(); |
461 | |
} |
462 | 110 | UMOComponent component = null; |
463 | 110 | for (Iterator i = components.values().iterator(); i.hasNext();) |
464 | |
{ |
465 | 22 | component = (UMOComponent) i.next(); |
466 | 22 | component.initialise(); |
467 | |
|
468 | 22 | logger.info("Component " + component.getDescriptor().getName() |
469 | |
+ " has been started successfully"); |
470 | |
} |
471 | 110 | initialised.set(true); |
472 | 110 | fireNotification(new ModelNotification(this, ModelNotification.MODEL_INITIALISED)); |
473 | 110 | } |
474 | |
else |
475 | |
{ |
476 | 0 | logger.debug("Model already initialised"); |
477 | |
} |
478 | 110 | } |
479 | |
|
480 | |
public ExceptionListener getExceptionListener() |
481 | |
{ |
482 | 0 | return exceptionListener; |
483 | |
} |
484 | |
|
485 | |
public void setExceptionListener(ExceptionListener exceptionListener) |
486 | |
{ |
487 | 0 | this.exceptionListener = exceptionListener; |
488 | 0 | } |
489 | |
|
490 | |
public UMODescriptor getDescriptor(String name) |
491 | |
{ |
492 | 0 | return (UMODescriptor) descriptors.get(name); |
493 | |
} |
494 | |
|
495 | |
public UMOComponent getComponent(String name) |
496 | |
{ |
497 | 6 | return (UMOComponent) components.get(name); |
498 | |
} |
499 | |
|
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
public Iterator getComponentNames() |
506 | |
{ |
507 | 2 | return components.keySet().iterator(); |
508 | |
} |
509 | |
|
510 | |
void fireNotification(UMOServerNotification notification) |
511 | |
{ |
512 | 1566 | MuleManager.getInstance().fireNotification(notification); |
513 | 1566 | } |
514 | |
|
515 | |
protected abstract UMOComponent createComponent(UMODescriptor descriptor); |
516 | |
} |