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