View Javadoc

1   /*
2    * $Id: ImmutableMuleDescriptor.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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 CPAL v1.0
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;
12  
13  import org.mule.MuleManager;
14  import org.mule.config.MuleConfiguration;
15  import org.mule.config.PoolingProfile;
16  import org.mule.config.QueueProfile;
17  import org.mule.config.ThreadingProfile;
18  import org.mule.impl.container.ContainerKeyPair;
19  import org.mule.impl.container.DescriptorContainerContext;
20  import org.mule.impl.container.DescriptorContainerKeyPair;
21  import org.mule.impl.container.MuleContainerContext;
22  import org.mule.impl.endpoint.MuleEndpoint;
23  import org.mule.routing.inbound.InboundPassThroughRouter;
24  import org.mule.routing.inbound.InboundRouterCollection;
25  import org.mule.routing.outbound.OutboundPassThroughRouter;
26  import org.mule.routing.outbound.OutboundRouterCollection;
27  import org.mule.umo.UMOException;
28  import org.mule.umo.UMOImmutableDescriptor;
29  import org.mule.umo.endpoint.UMOEndpoint;
30  import org.mule.umo.lifecycle.Initialisable;
31  import org.mule.umo.lifecycle.InitialisationException;
32  import org.mule.umo.manager.ContainerException;
33  import org.mule.umo.routing.UMOInboundRouterCollection;
34  import org.mule.umo.routing.UMONestedRouter;
35  import org.mule.umo.routing.UMONestedRouterCollection;
36  import org.mule.umo.routing.UMOOutboundRouter;
37  import org.mule.umo.routing.UMOOutboundRouterCollection;
38  import org.mule.umo.routing.UMOResponseRouterCollection;
39  import org.mule.umo.transformer.UMOTransformer;
40  
41  import java.beans.ExceptionListener;
42  import java.util.ArrayList;
43  import java.util.HashMap;
44  import java.util.Iterator;
45  import java.util.List;
46  import java.util.Map;
47  
48  import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
49  
50  /**
51   * <code>MuleDescriptor</code> describes all the properties for a Mule UMO. New
52   * Mule UMOs can be initialised as needed from their descriptor.
53   */
54  
55  public class ImmutableMuleDescriptor implements UMOImmutableDescriptor
56  {
57      /**
58       * The initial states that the component can be started in
59       */
60      public static final String INITIAL_STATE_STOPPED = "stopped";
61      public static final String INITIAL_STATE_STARTED = "started";
62      public static final String INITIAL_STATE_PAUSED = "paused";
63      /**
64       * Property that allows for a property file to be used to load properties instead
65       * of listing them directly in the mule-configuration file
66       */
67      protected static final String MULE_PROPERTY_DOT_PROPERTIES = "org.mule.dotProperties";
68  
69      /**
70       * holds the exception stategy for this UMO
71       */
72      protected ExceptionListener exceptionListener;
73  
74      /**
75       * The implementationReference used to create the Object UMO instance. Can either
76       * be a string such as a container reference or classname or can be an instance
77       * of the implementation.
78       */
79      protected Object implementationReference;
80  
81      /**
82       * The descriptor name
83       */
84      protected String name;
85  
86      /**
87       * The properties for the Mule UMO.
88       */
89      protected Map properties = new HashMap();
90  
91      /**
92       * The descriptors version
93       */
94      protected String version = "1.0";
95  
96      /**
97       * A list of UMOinteceptors that will be executed when the Mule UMO executed
98       */
99      protected List intecerptorList = new CopyOnWriteArrayList();
100 
101     protected UMOInboundRouterCollection inboundRouter;
102 
103     protected UMOOutboundRouterCollection outboundRouter;
104 
105     protected UMONestedRouterCollection nestedRouter;
106 
107     protected UMOResponseRouterCollection responseRouter;
108 
109     protected String modelName;
110 
111     /**
112      * The default receive endpoint.
113      *
114      * @deprecated Please use <code>inboundRouter</code> instead.
115      * @see MULE-506
116      */
117     protected UMOEndpoint inboundEndpoint;
118 
119     /**
120      * The transformer for the default receive endpoint.
121      *
122      * @deprecated Please use <code>inboundRouter</code> instead.
123      * @see MULE-506
124      */
125     protected UMOTransformer inboundTransformer = null;
126 
127     /**
128      * The default send endpoint.
129      *
130      * @deprecated Please use <code>outboundRouter</code> instead.
131      * @see MULE-506
132      */
133     protected UMOEndpoint outboundEndpoint;
134 
135     /**
136      * The transformer for the default send Endpoint
137      *
138      * @deprecated Please use <code>outboundRouter</code> instead.
139      * @see MULE-506
140      */
141     protected UMOTransformer outboundTransformer = null;
142 
143     /**
144      * The transformer for the response
145      *
146      * @deprecated Please use <code>responseRouter</code> instead.
147      * @see MULE-506
148      */
149     protected UMOTransformer responseTransformer = null;
150 
151     /**
152      * The threading profile to use for this component. If this is not set a default
153      * will be provided by the server
154      */
155     protected ThreadingProfile threadingProfile;
156 
157     /**
158      * the pooling configuration used when initialising the component described by
159      * this descriptor.
160      */
161     protected PoolingProfile poolingProfile;
162 
163     /**
164      * The queuing profile for events received for this component
165      */
166     protected QueueProfile queueProfile;
167 
168     /**
169      * Determines whether the component described by this descriptor is hosted in a
170      * container. If the value is false the component will not be pooled by Mule.
171      *
172      * @deprecated Use <code>container</code> instead.
173      * @see MULE-812
174      */
175     protected boolean containerManaged = true;
176 
177     /**
178      * Determines the initial state of this component when the model starts. Can be
179      * 'stopped' or 'started' (default)
180      */
181     protected String initialState = INITIAL_STATE_STARTED;
182 
183     /**
184      * Determines if this component is a singleton
185      */
186     protected boolean singleton = false;
187 
188     protected List initialisationCallbacks = new ArrayList();
189 
190     protected String encoding = null;
191 
192     /**
193      * The name of the container that the component implementation resides in If
194      * null, the container is not known, if 'none' the component is instanciated from
195      * its implementation class name.
196      */
197     protected String container = null;
198 
199     /**
200      * Default constructor. Initalises common properties for the MuleConfiguration
201      * object
202      *
203      * @see org.mule.config.MuleConfiguration
204      */
205     public ImmutableMuleDescriptor(ImmutableMuleDescriptor descriptor)
206     {
207         inboundRouter = descriptor.getInboundRouter();
208         outboundRouter = descriptor.getOutboundRouter();
209         responseRouter = descriptor.getResponseRouter();
210         inboundTransformer = descriptor.getInboundTransformer();
211         outboundTransformer = descriptor.getOutboundTransformer();
212         responseTransformer = descriptor.getResponseTransformer();
213         implementationReference = descriptor.getImplementation();
214         version = descriptor.getVersion();
215         intecerptorList = descriptor.getInterceptors();
216         properties = descriptor.getProperties();
217         name = descriptor.getName();
218         encoding = descriptor.getEncoding();
219 
220         threadingProfile = descriptor.getThreadingProfile();
221         poolingProfile = descriptor.getPoolingProfile();
222         queueProfile = descriptor.getQueueProfile();
223         exceptionListener = descriptor.getExceptionListener();
224         initialState = descriptor.getInitialState();
225         singleton = descriptor.isSingleton();
226         containerManaged = descriptor.isContainerManaged();
227     }
228 
229     /**
230      * Default constructor used by mutable versions of this class to provide defaults
231      * for certain properties
232      */
233     protected ImmutableMuleDescriptor()
234     {
235         inboundRouter = new InboundRouterCollection();
236         inboundRouter.addRouter(new InboundPassThroughRouter());
237     }
238 
239     public void initialise() throws InitialisationException
240     {
241         MuleConfiguration config = MuleManager.getConfiguration();
242         if (threadingProfile == null)
243         {
244             threadingProfile = config.getComponentThreadingProfile();
245         }
246         if (poolingProfile == null)
247         {
248             poolingProfile = config.getPoolingProfile();
249         }
250         if (queueProfile == null)
251         {
252             queueProfile = config.getQueueProfile();
253         }
254 
255         else if (exceptionListener instanceof Initialisable)
256         {
257             ((Initialisable) exceptionListener).initialise();
258         }
259 
260         if (inboundEndpoint != null)
261         {
262             if (inboundTransformer != null)
263             {
264                 inboundEndpoint.setTransformer(inboundTransformer);
265             }
266             ((MuleEndpoint) inboundEndpoint).initialise();
267             // If the transformer was set on the endpoint uri, it will only
268             // be initialised when the endpoint is initialised, hence we make
269             // this call here to ensure a consistent state
270             if (inboundTransformer == null)
271             {
272                 inboundTransformer = inboundEndpoint.getTransformer();
273             }
274         }
275 
276         if (outboundEndpoint != null)
277         {
278             if (outboundTransformer != null)
279             {
280                 outboundEndpoint.setTransformer(outboundTransformer);
281             }
282             ((MuleEndpoint) outboundEndpoint).initialise();
283             // If the transformer was set on the endpoint uri, it will only
284             // be initialised when the endpoint is initialised, hence we make
285             // this call here to ensure a consistent state
286             if (outboundTransformer == null)
287             {
288                 outboundTransformer = outboundEndpoint.getTransformer();
289             }
290         }
291 
292         if (exceptionListener instanceof Initialisable)
293         {
294             ((Initialisable) exceptionListener).initialise();
295         }
296 
297         MuleEndpoint endpoint;
298         if (inboundRouter == null)
299         {
300             // Create Default routes that route to the default inbound and
301             // outbound endpoints
302             inboundRouter = new InboundRouterCollection();
303             inboundRouter.addRouter(new InboundPassThroughRouter());
304         }
305         else
306         {
307             if (inboundRouter.getCatchAllStrategy() != null
308                 && inboundRouter.getCatchAllStrategy().getEndpoint() != null)
309             {
310                 ((MuleEndpoint) inboundRouter.getCatchAllStrategy().getEndpoint()).initialise();
311             }
312             for (Iterator iterator = inboundRouter.getEndpoints().iterator(); iterator.hasNext();)
313             {
314                 endpoint = (MuleEndpoint) iterator.next();
315                 endpoint.initialise();
316             }
317         }
318 
319         if (responseRouter != null)
320         {
321             for (Iterator iterator = responseRouter.getEndpoints().iterator(); iterator.hasNext();)
322             {
323                 endpoint = (MuleEndpoint) iterator.next();
324                 endpoint.initialise();
325             }
326         }
327 
328         if (nestedRouter != null)
329         {
330             for (Iterator it = nestedRouter.getRouters().iterator(); it.hasNext();)
331             {
332                 UMONestedRouter nestedRouter = (UMONestedRouter) it.next();
333                 endpoint = (MuleEndpoint) nestedRouter.getEndpoint();
334                 endpoint.initialise();
335             }
336         }
337 
338         if (outboundRouter == null)
339         {
340             outboundRouter = new OutboundRouterCollection();
341             outboundRouter.addRouter(new OutboundPassThroughRouter(this));
342         }
343         else
344         {
345             if (outboundRouter.getCatchAllStrategy() != null
346                 && outboundRouter.getCatchAllStrategy().getEndpoint() != null)
347             {
348                 outboundRouter.getCatchAllStrategy().getEndpoint().initialise();
349             }
350             UMOOutboundRouter router;
351             for (Iterator iterator = outboundRouter.getRouters().iterator(); iterator.hasNext();)
352             {
353                 router = (UMOOutboundRouter) iterator.next();
354                 for (Iterator iterator1 = router.getEndpoints().iterator(); iterator1.hasNext();)
355                 {
356                     endpoint = (MuleEndpoint) iterator1.next();
357                     endpoint.initialise();
358                 }
359             }
360         }
361         // Is a reference of an implementation object?
362         if (implementationReference instanceof String)
363         {
364             if (DescriptorContainerContext.DESCRIPTOR_CONTAINER_NAME.equals(container))
365             {
366                 implementationReference = new DescriptorContainerKeyPair(name, implementationReference);
367             }
368             else
369             {
370                 implementationReference = new ContainerKeyPair(container, implementationReference);
371             }
372         }
373     }
374 
375     /*
376      * (non-Javadoc)
377      *
378      * @see org.mule.umo.UMODescriptor#getExceptionListener()
379      */
380     public ExceptionListener getExceptionListener()
381     {
382         return exceptionListener;
383     }
384 
385     /*
386      * (non-Javadoc)
387      *
388      * @see org.mule.transformers.HasTransformer#getInboundTransformer()
389      */
390     public UMOTransformer getInboundTransformer()
391     {
392         return inboundTransformer;
393     }
394 
395     /*
396      * (non-Javadoc)
397      *
398      * @see org.mule.umo.UMODescriptor#getName()
399      */
400     public String getName()
401     {
402         return name;
403     }
404 
405     /*
406      * (non-Javadoc)
407      *
408      * @see org.mule.impl.MuleDescriptor#getOutboundTransformer()
409      */
410     public UMOTransformer getOutboundTransformer()
411     {
412         return outboundTransformer;
413     }
414 
415     /*
416      * (non-Javadoc)
417      *
418      * @see org.mule.impl.MuleDescriptor#getResponseTransformer()
419      */
420     public UMOTransformer getResponseTransformer()
421     {
422         return responseTransformer;
423     }
424 
425     /*
426      * (non-Javadoc)
427      *
428      * @see org.mule.umo.UMODescriptor#getParams() Not HashMap is used instead of Map
429      *      due to a Spring quirk where the property is not found if specified as a
430      *      map
431      */
432     public Map getProperties()
433     {
434         return properties;
435     }
436 
437     /*
438      * (non-Javadoc)
439      *
440      * @see org.mule.umo.UMODescriptor#getVersion()
441      */
442     public String getVersion()
443     {
444         return version;
445     }
446 
447     /*
448      * (non-Javadoc)
449      *
450      * @see org.mule.umo.UMODescriptor#getinteceptorList()
451      */
452     public List getInterceptors()
453     {
454         return intecerptorList;
455     }
456 
457     public String getEncoding()
458     {
459         return encoding;
460     }
461 
462     /*
463      * (non-Javadoc)
464      *
465      * @see java.lang.Object#toString()
466      */
467     public String toString()
468     {
469         StringBuffer buffer = new StringBuffer();
470         buffer.append("name=").append(name);
471         buffer.append(", outbound endpoint=").append(outboundEndpoint);
472         buffer.append(", send transformer=").append(outboundTransformer);
473         buffer.append(", inbound endpointUri=").append(inboundEndpoint);
474         buffer.append(", receive transformer=").append(inboundTransformer);
475         buffer.append(", encoding=").append(encoding);
476         return buffer.toString();
477     }
478 
479     /*
480      * (non-Javadoc)
481      *
482      * @see org.mule.umo.UMODescriptor#getImplementation()
483      */
484     public Object getImplementation()
485     {
486         return implementationReference;
487     }
488 
489     public UMOInboundRouterCollection getInboundRouter()
490     {
491         return inboundRouter;
492     }
493 
494     public UMOOutboundRouterCollection getOutboundRouter()
495     {
496         return outboundRouter;
497     }
498 
499     public UMONestedRouterCollection getNestedRouter()
500     {
501         return nestedRouter;
502     }
503 
504     /**
505      * The threading profile used by the UMO when managing a component. Can be used
506      * to allocate more or less resources to this particular umo component.
507      */
508     public ThreadingProfile getThreadingProfile()
509     {
510         return threadingProfile;
511     }
512 
513     public PoolingProfile getPoolingProfile()
514     {
515         return poolingProfile;
516     }
517 
518     public QueueProfile getQueueProfile()
519     {
520         return queueProfile;
521     }
522 
523     public boolean isContainerManaged()
524     {
525         return !MuleContainerContext.MULE_CONTAINER_NAME.equalsIgnoreCase(container);
526     }
527 
528     public Class getImplementationClass() throws UMOException
529     {
530         // check for other types of references
531         Class implClass;
532         if (implementationReference instanceof String || implementationReference instanceof ContainerKeyPair)
533         {
534             Object object = MuleManager.getInstance().getContainerContext().getComponent(
535                 implementationReference);
536             implClass = object.getClass();
537         }
538         else
539         {
540             implClass = implementationReference.getClass();
541         }
542 
543         return implClass;
544     }
545 
546     /**
547      * A helper method that will resolved a component for a given reference id. For
548      * example, for a component declared in a Spring Application context the id would
549      * be the bean id, in Pico the id would be a fully qualified class name.
550      *
551      * @param reference the reference to use when resolving the component
552      * @return the Implementation of the component
553      */
554     protected Class getImplementationForReference(String reference) throws ContainerException
555     {
556         Object object = MuleManager.getInstance().getContainerContext().getComponent(reference);
557         return object.getClass();
558     }
559 
560     public void fireInitialisationCallbacks(Object component) throws InitialisationException
561     {
562         InitialisationCallback callback;
563         for (Iterator iterator = initialisationCallbacks.iterator(); iterator.hasNext();)
564         {
565             callback = (InitialisationCallback) iterator.next();
566             callback.initialise(component);
567         }
568     }
569 
570     /**
571      * The inbound Provider to use when receiveing an event. This may get overidden
572      * by the configured behaviour of the inbound router on this component
573      *
574      * @return the inbound endpoint or null if one is not set
575      * @see org.mule.umo.endpoint.UMOEndpoint
576      */
577     public UMOEndpoint getInboundEndpoint()
578     {
579         return inboundEndpoint;
580     }
581 
582     /**
583      * The outbound Provider to use when sending an event. This may get overidden by
584      * the configured behaviour of the outbound router on this component
585      *
586      * @return the outbound endpoint or null if one is not set
587      * @see org.mule.umo.endpoint.UMOEndpoint
588      */
589     public UMOEndpoint getOutboundEndpoint()
590     {
591         return outboundEndpoint;
592     }
593 
594     public UMOResponseRouterCollection getResponseRouter()
595     {
596         return responseRouter;
597     }
598 
599     public boolean isSingleton()
600     {
601         return singleton;
602     }
603 
604     public String getInitialState()
605     {
606         return initialState;
607     }
608 
609     /**
610      * Returns the name of the contaier where the object for this descriptor resides.
611      * If this value is 'none' the 'implementaiton' attributed is expected to be a
612      * fully qualified class name that will be instanciated.
613      *
614      * @return the container name, or null if it is not known - in which case each
615      *         container will be queried for the component implementation.
616      */
617     public String getContainer()
618     {
619         return container;
620     }
621 
622 
623     public String getModelName()
624     {
625         return modelName;
626     }
627 }