KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > model > AbstractModel


1 /*
2  * $Id: AbstractModel.java 4219 2006-12-09 10:15:14Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.impl.model;
12
13 import java.beans.ExceptionListener JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.mule.MuleManager;
21 import org.mule.config.i18n.Message;
22 import org.mule.config.i18n.Messages;
23 import org.mule.impl.DefaultComponentExceptionStrategy;
24 import org.mule.impl.DefaultLifecycleAdapterFactory;
25 import org.mule.impl.ImmutableMuleDescriptor;
26 import org.mule.impl.MuleSession;
27 import org.mule.impl.internal.notifications.ModelNotification;
28 import org.mule.model.DynamicEntryPointResolver;
29 import org.mule.umo.UMOComponent;
30 import org.mule.umo.UMODescriptor;
31 import org.mule.umo.UMOException;
32 import org.mule.umo.UMOSession;
33 import org.mule.umo.lifecycle.Initialisable;
34 import org.mule.umo.lifecycle.InitialisationException;
35 import org.mule.umo.lifecycle.UMOLifecycleAdapterFactory;
36 import org.mule.umo.manager.UMOServerNotification;
37 import org.mule.umo.model.ModelException;
38 import org.mule.umo.model.UMOEntryPointResolver;
39 import org.mule.umo.model.UMOModel;
40
41 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
42 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentSkipListMap;
43 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
44
45 /**
46  * <code>MuleModel</code> is the default implementation of the UMOModel. The model
47  * encapsulates and manages the runtime behaviour of a Mule Server instance. It is
48  * responsible for maintaining the UMOs instances and their configuration.
49  */

50 public abstract class AbstractModel implements UMOModel
51 {
52     /**
53      * logger used by this class
54      */

55     protected transient Log logger = LogFactory.getLog(getClass());
56
57     private String JavaDoc name;
58     private UMOEntryPointResolver entryPointResolver;
59     private UMOLifecycleAdapterFactory lifecycleAdapterFactory;
60
61     private Map JavaDoc components;
62
63     /**
64      * Collection for mule descriptors registered in this Manager
65      */

66     protected Map JavaDoc descriptors;
67
68     private AtomicBoolean initialised = new AtomicBoolean(false);
69
70     private AtomicBoolean started = new AtomicBoolean(false);
71
72     private ExceptionListener JavaDoc exceptionListener;
73
74     /**
75      * Default constructor
76      */

77     public AbstractModel()
78     {
79         // Always set default entrypoint resolver, lifecycle and compoenent
80
// resolver and exceptionstrategy.
81
entryPointResolver = new DynamicEntryPointResolver();
82         lifecycleAdapterFactory = new DefaultLifecycleAdapterFactory();
83         components = new ConcurrentSkipListMap();
84         descriptors = new ConcurrentHashMap();
85         exceptionListener = new DefaultComponentExceptionStrategy();
86         name = "mule";
87     }
88
89     /*
90      * (non-Javadoc)
91      *
92      * @see org.mule.umo.UMOModel#getName()
93      */

94     public String JavaDoc getName()
95     {
96         return name;
97     }
98
99     /*
100      * (non-Javadoc)
101      *
102      * @see org.mule.umo.UMOModel#setName(java.lang.String)
103      */

104     public void setName(String JavaDoc name)
105     {
106         this.name = name;
107     }
108
109     /*
110      * (non-Javadoc)
111      *
112      * @see org.mule.umo.model.UMOModel#getEntryPointResolver()
113      */

114     public UMOEntryPointResolver getEntryPointResolver()
115     {
116         return entryPointResolver;
117     }
118
119     /*
120      * (non-Javadoc)
121      *
122      * @see org.mule.umo.model.UMOModel#setEntryPointResolver(org.mule.umo.model.UMOEntryPointResolver)
123      */

124     public void setEntryPointResolver(UMOEntryPointResolver entryPointResolver)
125     {
126         this.entryPointResolver = entryPointResolver;
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      * @see org.mule.umo.UMOModel#isUMORegistered(java.lang.String)
133      */

134     public boolean isComponentRegistered(String JavaDoc name)
135     {
136         return (components.get(name) != null);
137     }
138
139     /*
140      * (non-Javadoc)
141      *
142      * @see org.mule.umo.UMOModel#registerUMO(org.mule.umo.UMODescriptor)
143      */

144     public UMOComponent registerComponent(UMODescriptor descriptor) throws UMOException
145     {
146         if (descriptor == null)
147         {
148             throw new ModelException(new Message(Messages.X_IS_NULL, "UMO Descriptor"));
149         }
150
151         if (initialised.get())
152         {
153             descriptor.initialise();
154         }
155         // Set the es if one wasn't set in the configuration
156
if (descriptor.getExceptionListener() == null)
157         {
158             descriptor.setExceptionListener(exceptionListener);
159         }
160
161         // detect duplicate descriptor declarations
162
if (descriptors.get(descriptor.getName()) != null)
163         {
164             throw new ModelException(new Message(Messages.DESCRIPTOR_X_ALREADY_EXISTS, descriptor.getName()));
165         }
166
167         UMOComponent component = (UMOComponent)components.get(descriptor.getName());
168
169         if (component == null)
170         {
171             component = createComponent(descriptor);
172             descriptors.put(descriptor.getName(), descriptor);
173             components.put(descriptor.getName(), component);
174         }
175
176         logger.debug("Added Mule UMO: " + descriptor.getName());
177
178         if (initialised.get())
179         {
180             logger.info("Initialising component: " + descriptor.getName());
181             component.initialise();
182         }
183         if (started.get())
184         {
185             logger.info("Starting component: " + descriptor.getName());
186             component.start();
187         }
188         return component;
189     }
190
191     public void unregisterComponent(UMODescriptor descriptor) throws UMOException
192     {
193         if (descriptor == null)
194         {
195             throw new ModelException(new Message(Messages.X_IS_NULL, "UMO Descriptor"));
196         }
197
198         if (!isComponentRegistered(descriptor.getName()))
199         {
200             throw new ModelException(new Message(Messages.COMPONENT_X_NOT_REGISTERED, descriptor.getName()));
201         }
202         UMOComponent component = (UMOComponent)components.remove(descriptor.getName());
203
204         if (component != null)
205         {
206             component.stop();
207             descriptors.remove(descriptor.getName());
208             component.dispose();
209             logger.info("The component: " + descriptor.getName() + " has been unregistered and disposing");
210         }
211     }
212
213     /*
214      * (non-Javadoc)
215      *
216      * @see org.mule.umo.model.UMOModel#getLifecycleAdapterFactory()
217      */

218     public UMOLifecycleAdapterFactory getLifecycleAdapterFactory()
219     {
220         return lifecycleAdapterFactory;
221     }
222
223     /*
224      * (non-Javadoc)
225      *
226      * @see org.mule.umo.model.UMOModel#setLifecycleAdapterFactory(org.mule.umo.lifecycle.UMOLifecycleAdapterFactory)
227      */

228     public void setLifecycleAdapterFactory(UMOLifecycleAdapterFactory lifecycleAdapterFactory)
229     {
230         this.lifecycleAdapterFactory = lifecycleAdapterFactory;
231     }
232
233     /**
234      * Destroys any current components
235      */

236     public void dispose()
237     {
238         fireNotification(new ModelNotification(this, ModelNotification.MODEL_DISPOSING));
239
240         for (Iterator JavaDoc i = components.values().iterator(); i.hasNext();)
241         {
242             UMOComponent component = (UMOComponent)i.next();
243             try
244             {
245                 component.dispose();
246                 logger.info(component + " has been destroyed successfully");
247             }
248             catch (Exception JavaDoc e1)
249             {
250                 logger.warn("Failed to dispose component: " + e1.getMessage());
251             }
252         }
253
254         components.clear();
255         descriptors.clear();
256         components = null;
257         descriptors = null;
258
259         fireNotification(new ModelNotification(this, ModelNotification.MODEL_DISPOSED));
260     }
261
262     /**
263      * Returns a valid component for the given Mule name
264      *
265      * @param muleName the Name of the Mule for which the component is required
266      * @return a component for the specified name
267      */

268     public UMOSession getComponentSession(String JavaDoc muleName)
269     {
270         UMOComponent component = (UMOComponent)components.get(muleName);
271         if (component == null)
272         {
273             logger.warn("Component: " + muleName + " not found returning null session");
274             return null;
275         }
276         else
277         {
278             return new MuleSession(component);
279         }
280     }
281
282     /**
283      * Stops any registered components
284      *
285      * @throws UMOException if a Component fails tcomponent
286      */

287     public void stop() throws UMOException
288     {
289         fireNotification(new ModelNotification(this, ModelNotification.MODEL_STOPPING));
290         for (Iterator JavaDoc i = components.values().iterator(); i.hasNext();)
291         {
292             UMOComponent component = (UMOComponent)i.next();
293             component.stop();
294             logger.info("Component " + component + " has been stopped successfully");
295         }
296         fireNotification(new ModelNotification(this, ModelNotification.MODEL_STOPPED));
297     }
298
299     /**
300      * Starts all registered components
301      *
302      * @throws UMOException if any of the components fail to start
303      */

304     public void start() throws UMOException
305     {
306         if (!initialised.get())
307         {
308             initialise();
309         }
310
311         if (!started.get())
312         {
313             fireNotification(new ModelNotification(this, ModelNotification.MODEL_STARTING));
314
315             for (Iterator JavaDoc i = components.values().iterator(); i.hasNext();)
316             {
317                 AbstractComponent component = (AbstractComponent)i.next();
318
319                 if (component.getDescriptor().getInitialState().equals(
320                     ImmutableMuleDescriptor.INITIAL_STATE_STARTED))
321                 {
322                     component.start();
323                     logger.info("Component " + component + " has been started successfully");
324                 }
325                 else if (component.getDescriptor().getInitialState().equals(
326                     ImmutableMuleDescriptor.INITIAL_STATE_PAUSED))
327                 {
328                     component.start(true);
329                     logger.info("Component " + component
330                                 + " has been started and paused (initial state = 'paused')");
331                 }
332                 else
333                 {
334                     logger.info("Component " + component
335                                 + " has not been started (initial state = 'stopped')");
336                 }
337             }
338             started.set(true);
339             fireNotification(new ModelNotification(this, ModelNotification.MODEL_STARTED));
340         }
341         else
342         {
343             logger.debug("Model already started");
344         }
345     }
346
347     /**
348      * Starts a single Mule Component. This can be useful when stopping and starting
349      * some Mule UMOs while letting others continue
350      *
351      * @param name the name of the Mule UMO to start
352      * @throws UMOException if the MuleUMO is not registered or the component failed
353      * to start
354      */

355     public void startComponent(String JavaDoc name) throws UMOException
356     {
357         UMOComponent component = (UMOComponent)components.get(name);
358         if (component == null)
359         {
360             throw new ModelException(new Message(Messages.COMPONENT_X_NOT_REGISTERED, name));
361         }
362         else
363         {
364             component.start();
365             logger.info("Mule " + component.toString() + " has been started successfully");
366         }
367     }
368
369     /**
370      * Stops a single Mule Component. This can be useful when stopping and starting
371      * some Mule UMOs while letting others continue.
372      *
373      * @param name the name of the Mule UMO to stop
374      * @throws UMOException if the MuleUMO is not registered
375      */

376     public void stopComponent(String JavaDoc name) throws UMOException
377     {
378         UMOComponent component = (UMOComponent)components.get(name);
379         if (component == null)
380         {
381             throw new ModelException(new Message(Messages.COMPONENT_X_NOT_REGISTERED, name));
382         }
383         else
384         {
385             component.stop();
386             logger.info("mule " + name + " has been stopped successfully");
387         }
388     }
389
390     /**
391      * Pauses event processing for a single Mule Component. Unlike stopComponent(), a
392      * paused component will still consume messages from the underlying transport,
393      * but those messages will be queued until the component is resumed. <p/> In
394      * order to persist these queued messages you can set the 'recoverableMode'
395      * property on the Muleconfiguration to true. this causes all internal queues to
396      * store their state.
397      *
398      * @param name the name of the Mule UMO to stop
399      * @throws org.mule.umo.UMOException if the MuleUMO is not registered or the
400      * component failed to pause.
401      * @see org.mule.config.MuleConfiguration
402      */

403     public void pauseComponent(String JavaDoc name) throws UMOException
404     {
405         UMOComponent component = (UMOComponent)components.get(name);
406
407         if (component != null)
408         {
409             component.pause();
410             logger.info("Mule Component " + name + " has been paused successfully");
411         }
412         else
413         {
414             throw new ModelException(new Message(Messages.COMPONENT_X_NOT_REGISTERED, name));
415         }
416     }
417
418     /**
419      * Resumes a single Mule Component that has been paused. If the component is not
420      * paused nothing is executed.
421      *
422      * @param name the name of the Mule UMO to resume
423      * @throws org.mule.umo.UMOException if the MuleUMO is not registered or the
424      * component failed to resume
425      */

426     public void resumeComponent(String JavaDoc name) throws UMOException
427     {
428         UMOComponent component = (UMOComponent)components.get(name);
429
430         if (component != null)
431         {
432             component.resume();
433             logger.info("Mule Component " + name + " has been resumed successfully");
434         }
435         else
436         {
437             throw new ModelException(new Message(Messages.COMPONENT_X_NOT_REGISTERED, name));
438         }
439     }
440
441     public void setComponents(List JavaDoc descriptors) throws UMOException
442     {
443         for (Iterator JavaDoc iterator = descriptors.iterator(); iterator.hasNext();)
444         {
445             registerComponent((UMODescriptor)iterator.next());
446         }
447     }
448
449     public void initialise() throws InitialisationException
450     {
451         if (!initialised.get())
452         {
453             fireNotification(new ModelNotification(this, ModelNotification.MODEL_INITIALISING));
454
455             if (exceptionListener instanceof Initialisable)
456             {
457                 ((Initialisable)exceptionListener).initialise();
458             }
459             UMOComponent component = null;
460             for (Iterator JavaDoc i = components.values().iterator(); i.hasNext();)
461             {
462                 component = (UMOComponent)i.next();
463                 component.initialise();
464
465                 logger.info("Component " + component.getDescriptor().getName()
466                             + " has been started successfully");
467             }
468             initialised.set(true);
469             fireNotification(new ModelNotification(this, ModelNotification.MODEL_INITIALISED));
470         }
471         else
472         {
473             logger.debug("Model already initialised");
474         }
475     }
476
477     public ExceptionListener JavaDoc getExceptionListener()
478     {
479         return exceptionListener;
480     }
481
482     public void setExceptionListener(ExceptionListener JavaDoc exceptionListener)
483     {
484         this.exceptionListener = exceptionListener;
485     }
486
487     public UMODescriptor getDescriptor(String JavaDoc name)
488     {
489         return (UMODescriptor)descriptors.get(name);
490     }
491
492     public UMOComponent getComponent(String JavaDoc name)
493     {
494         return (UMOComponent)components.get(name);
495     }
496
497     /**
498      * Gets an iterator of all component names registered in the model
499      *
500      * @return an iterator of all component names
501      */

502     public Iterator JavaDoc getComponentNames()
503     {
504         return components.keySet().iterator();
505     }
506
507     void fireNotification(UMOServerNotification notification)
508     {
509         MuleManager.getInstance().fireNotification(notification);
510     }
511
512     protected abstract UMOComponent createComponent(UMODescriptor descriptor);
513 }
514
Popular Tags