KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > ImmutableMuleDescriptor


1 /*
2  * $Id: ImmutableMuleDescriptor.java 3798 2006-11-04 04:07:14Z aperepel $
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;
12
13 import java.beans.ExceptionListener JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.mule.MuleManager;
21 import org.mule.config.MuleConfiguration;
22 import org.mule.config.PoolingProfile;
23 import org.mule.config.QueueProfile;
24 import org.mule.config.ThreadingProfile;
25 import org.mule.impl.container.ContainerKeyPair;
26 import org.mule.impl.container.DescriptorContainerContext;
27 import org.mule.impl.container.DescriptorContainerKeyPair;
28 import org.mule.impl.container.MuleContainerContext;
29 import org.mule.impl.endpoint.MuleEndpoint;
30 import org.mule.routing.inbound.InboundMessageRouter;
31 import org.mule.routing.inbound.InboundPassThroughRouter;
32 import org.mule.routing.outbound.OutboundMessageRouter;
33 import org.mule.routing.outbound.OutboundPassThroughRouter;
34 import org.mule.umo.UMOException;
35 import org.mule.umo.UMOImmutableDescriptor;
36 import org.mule.umo.endpoint.UMOEndpoint;
37 import org.mule.umo.lifecycle.Initialisable;
38 import org.mule.umo.lifecycle.InitialisationException;
39 import org.mule.umo.manager.ContainerException;
40 import org.mule.umo.routing.UMOInboundMessageRouter;
41 import org.mule.umo.routing.UMOOutboundMessageRouter;
42 import org.mule.umo.routing.UMOOutboundRouter;
43 import org.mule.umo.routing.UMOResponseMessageRouter;
44 import org.mule.umo.transformer.UMOTransformer;
45
46 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
47
48 /**
49  * <code>MuleDescriptor</code> describes all the properties for a Mule UMO. New
50  * Mule UMOs can be initialised as needed from their descriptor.
51  *
52  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
53  * @version $Revision: 3798 $
54  */

55
56 public class ImmutableMuleDescriptor implements UMOImmutableDescriptor
57 {
58     /**
59      * The initial states that the component can be started in
60      */

61     public static final String JavaDoc INITIAL_STATE_STOPPED = "stopped";
62     public static final String JavaDoc INITIAL_STATE_STARTED = "started";
63     public static final String JavaDoc INITIAL_STATE_PAUSED = "paused";
64     /**
65      * Property that allows for a property file to be used to load properties instead
66      * of listing them directly in the mule-configuration file
67      */

68     protected static final String JavaDoc MULE_PROPERTY_DOT_PROPERTIES = "org.mule.dotProperties";
69
70     /**
71      * holds the exception stategy for this UMO
72      */

73     protected ExceptionListener JavaDoc exceptionListener = null;
74
75     /**
76      * The implementationReference used to create the Object UMO instance. Can either
77      * be a string such as a container reference or classname or can be an instance
78      * of the implementation.
79      */

80     protected Object JavaDoc implementationReference = null;
81
82     /**
83      * The descriptor name
84      */

85     protected String JavaDoc name;
86
87     /**
88      * The properties for the Mule UMO.
89      */

90     protected Map JavaDoc properties = new HashMap JavaDoc();
91
92     /**
93      * The descriptors version
94      */

95     protected String JavaDoc version = "1.0";
96
97     /**
98      * A list of UMOinteceptors that will be executed when the Mule UMO executed
99      */

100     protected List JavaDoc intecerptorList = new CopyOnWriteArrayList();
101
102     protected UMOInboundMessageRouter inboundRouter = null;
103
104     protected UMOOutboundMessageRouter outboundRouter = null;
105
106     protected UMOResponseMessageRouter responseRouter = null;
107
108     /**
109      * The default receive endpoint.
110      *
111      * @deprecated Please use <code>inboundRouter</code> instead.
112      * @see MULE-506
113      */

114     protected UMOEndpoint inboundEndpoint;
115
116     /**
117      * The transformer for the default receive endpoint.
118      *
119      * @deprecated Please use <code>inboundRouter</code> instead.
120      * @see MULE-506
121      */

122     protected UMOTransformer inboundTransformer = null;
123
124     /**
125      * The default send endpoint.
126      *
127      * @deprecated Please use <code>outboundRouter</code> instead.
128      * @see MULE-506
129      */

130     protected UMOEndpoint outboundEndpoint;
131
132     /**
133      * The transformer for the default send Endpoint
134      *
135      * @deprecated Please use <code>outboundRouter</code> instead.
136      * @see MULE-506
137      */

138     protected UMOTransformer outboundTransformer = null;
139
140     /**
141      * The transformer for the response
142      *
143      * @deprecated Please use <code>responseRouter</code> instead.
144      * @see MULE-506
145      */

146     protected UMOTransformer responseTransformer = null;
147
148     /**
149      * The threading profile to use for this component. If this is not set a default
150      * will be provided by the server
151      */

152     protected ThreadingProfile threadingProfile;
153
154     /**
155      * the pooling configuration used when initialising the component described by
156      * this descriptor.
157      */

158     protected PoolingProfile poolingProfile;
159
160     /**
161      * The queuing profile for events received for this component
162      */

163     protected QueueProfile queueProfile;
164
165     /**
166      * Determines whether the component described by this descriptor is hosted in a
167      * container. If the value is false the component will not be pooled by Mule.
168      *
169      * @deprecated Use <code>container</code> instead.
170      * @see MULE-812
171      */

172     protected boolean containerManaged = true;
173
174     /**
175      * Determines the initial state of this component when the model starts. Can be
176      * 'stopped' or 'started' (default)
177      */

178     protected String JavaDoc initialState = INITIAL_STATE_STARTED;
179
180     /**
181      * Determines if this component is a singleton
182      */

183     protected boolean singleton = false;
184
185     protected List JavaDoc initialisationCallbacks = new ArrayList JavaDoc();
186
187     protected String JavaDoc encoding = null;
188
189     /**
190      * The name of the container that the component implementation resides in If
191      * null, the container is not known, if 'none' the component is instanciated from
192      * its implementation class name.
193      */

194     protected String JavaDoc container = null;
195
196     /**
197      * Default constructor. Initalises common properties for the MuleConfiguration
198      * object
199      *
200      * @see org.mule.config.MuleConfiguration
201      */

202     public ImmutableMuleDescriptor(ImmutableMuleDescriptor descriptor)
203     {
204         inboundRouter = descriptor.getInboundRouter();
205         outboundRouter = descriptor.getOutboundRouter();
206         responseRouter = descriptor.getResponseRouter();
207         inboundTransformer = descriptor.getInboundTransformer();
208         outboundTransformer = descriptor.getOutboundTransformer();
209         responseTransformer = descriptor.getResponseTransformer();
210         implementationReference = descriptor.getImplementation();
211         version = descriptor.getVersion();
212         intecerptorList = descriptor.getInterceptors();
213         properties = descriptor.getProperties();
214         name = descriptor.getName();
215         encoding = descriptor.getEncoding();
216
217         threadingProfile = descriptor.getThreadingProfile();
218         poolingProfile = descriptor.getPoolingProfile();
219         queueProfile = descriptor.getQueueProfile();
220         exceptionListener = descriptor.getExceptionListener();
221         initialState = descriptor.getInitialState();
222         singleton = descriptor.isSingleton();
223         containerManaged = descriptor.isContainerManaged();
224     }
225
226     /**
227      * Default constructor used by mutable versions of this class to provide defaults
228      * for certain properties
229      */

230     protected ImmutableMuleDescriptor()
231     {
232         inboundRouter = new InboundMessageRouter();
233         inboundRouter.addRouter(new InboundPassThroughRouter());
234     }
235
236     public void initialise() throws InitialisationException
237     {
238         MuleConfiguration config = MuleManager.getConfiguration();
239         if (threadingProfile == null)
240         {
241             threadingProfile = config.getComponentThreadingProfile();
242         }
243         if (poolingProfile == null)
244         {
245             poolingProfile = config.getPoolingProfile();
246         }
247         if (queueProfile == null)
248         {
249             queueProfile = config.getQueueProfile();
250         }
251
252         if (exceptionListener == null)
253         {
254             exceptionListener = MuleManager.getInstance().getModel().getExceptionListener();
255         }
256         else if (exceptionListener instanceof Initialisable)
257         {
258             ((Initialisable)exceptionListener).initialise();
259         }
260
261         if (inboundEndpoint != null)
262         {
263             if (inboundTransformer != null)
264             {
265                 inboundEndpoint.setTransformer(inboundTransformer);
266             }
267             ((MuleEndpoint)inboundEndpoint).initialise();
268             // If the transformer was set on the endpoint uri, it will only
269
// be initialised when the endpoint is initialised, hence we make
270
// this call here to ensure a consistent state
271
if (inboundTransformer == null)
272             {
273                 inboundTransformer = inboundEndpoint.getTransformer();
274             }
275         }
276
277         if (outboundEndpoint != null)
278         {
279             if (outboundTransformer != null)
280             {
281                 outboundEndpoint.setTransformer(outboundTransformer);
282             }
283             ((MuleEndpoint)outboundEndpoint).initialise();
284             // If the transformer was set on the endpoint uri, it will only
285
// be initialised when the endpoint is initialised, hence we make
286
// this call here to ensure a consistent state
287
if (outboundTransformer == null)
288             {
289                 outboundTransformer = outboundEndpoint.getTransformer();
290             }
291         }
292
293         if (exceptionListener instanceof Initialisable)
294         {
295             ((Initialisable)exceptionListener).initialise();
296         }
297
298         MuleEndpoint endpoint;
299         if (inboundRouter == null)
300         {
301             // Create Default routes that route to the default inbound and
302
// outbound endpoints
303
inboundRouter = new InboundMessageRouter();
304             inboundRouter.addRouter(new InboundPassThroughRouter());
305         }
306         else
307         {
308             if (inboundRouter.getCatchAllStrategy() != null
309                 && inboundRouter.getCatchAllStrategy().getEndpoint() != null)
310             {
311                 ((MuleEndpoint)inboundRouter.getCatchAllStrategy().getEndpoint()).initialise();
312             }
313             for (Iterator JavaDoc iterator = inboundRouter.getEndpoints().iterator(); iterator.hasNext();)
314             {
315                 endpoint = (MuleEndpoint)iterator.next();
316                 endpoint.initialise();
317             }
318         }
319
320         if (responseRouter != null)
321         {
322             for (Iterator JavaDoc iterator = responseRouter.getEndpoints().iterator(); iterator.hasNext();)
323             {
324                 endpoint = (MuleEndpoint)iterator.next();
325                 endpoint.initialise();
326             }
327         }
328
329         if (outboundRouter == null)
330         {
331             outboundRouter = new OutboundMessageRouter();
332             outboundRouter.addRouter(new OutboundPassThroughRouter(this));
333         }
334         else
335         {
336             if (outboundRouter.getCatchAllStrategy() != null
337                 && outboundRouter.getCatchAllStrategy().getEndpoint() != null)
338             {
339                 ((MuleEndpoint)outboundRouter.getCatchAllStrategy().getEndpoint()).initialise();
340             }
341             UMOOutboundRouter router = null;
342             for (Iterator JavaDoc iterator = outboundRouter.getRouters().iterator(); iterator.hasNext();)
343             {
344                 router = (UMOOutboundRouter)iterator.next();
345                 for (Iterator JavaDoc iterator1 = router.getEndpoints().iterator(); iterator1.hasNext();)
346                 {
347                     endpoint = (MuleEndpoint)iterator1.next();
348                     endpoint.initialise();
349                 }
350             }
351         }
352         // Is a reference of an implementation object?
353
if (implementationReference instanceof String JavaDoc)
354         {
355             if (DescriptorContainerContext.DESCRIPTOR_CONTAINER_NAME.equals(container))
356             {
357                 implementationReference = new DescriptorContainerKeyPair(name, implementationReference);
358             }
359             else
360             {
361                 implementationReference = new ContainerKeyPair(container, implementationReference);
362             }
363         }
364     }
365
366     /*
367      * (non-Javadoc)
368      *
369      * @see org.mule.umo.UMODescriptor#getExceptionListener()
370      */

371     public ExceptionListener JavaDoc getExceptionListener()
372     {
373         return exceptionListener;
374     }
375
376     /*
377      * (non-Javadoc)
378      *
379      * @see org.mule.transformers.HasTransformer#getInboundTransformer()
380      */

381     public UMOTransformer getInboundTransformer()
382     {
383         return inboundTransformer;
384     }
385
386     /*
387      * (non-Javadoc)
388      *
389      * @see org.mule.umo.UMODescriptor#getName()
390      */

391     public String JavaDoc getName()
392     {
393         return name;
394     }
395
396     /*
397      * (non-Javadoc)
398      *
399      * @see org.mule.impl.MuleDescriptor#getOutboundTransformer()
400      */

401     public UMOTransformer getOutboundTransformer()
402     {
403         return outboundTransformer;
404     }
405
406     /*
407      * (non-Javadoc)
408      *
409      * @see org.mule.impl.MuleDescriptor#getResponseTransformer()
410      */

411     public UMOTransformer getResponseTransformer()
412     {
413         return responseTransformer;
414     }
415
416     /*
417      * (non-Javadoc)
418      *
419      * @see org.mule.umo.UMODescriptor#getParams() Not HashMap is used instead of Map
420      * due to a Spring quirk where the property is not found if specified as a
421      * map
422      */

423     public Map JavaDoc getProperties()
424     {
425         return properties;
426     }
427
428     /*
429      * (non-Javadoc)
430      *
431      * @see org.mule.umo.UMODescriptor#getVersion()
432      */

433     public String JavaDoc getVersion()
434     {
435         return version;
436     }
437
438     /*
439      * (non-Javadoc)
440      *
441      * @see org.mule.umo.UMODescriptor#getinteceptorList()
442      */

443     public List JavaDoc getInterceptors()
444     {
445         return intecerptorList;
446     }
447
448     public String JavaDoc getEncoding()
449     {
450         return encoding;
451     }
452
453     /*
454      * (non-Javadoc)
455      *
456      * @see java.lang.Object#toString()
457      */

458     public String JavaDoc toString()
459     {
460         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
461         buffer.append("name=").append(name);
462         buffer.append(", outbound endpoint=").append(outboundEndpoint);
463         buffer.append(", send transformer=").append(outboundTransformer);
464         buffer.append(", inbound endpointUri=").append(inboundEndpoint);
465         buffer.append(", receive transformer=").append(inboundTransformer);
466         buffer.append(", encoding=").append(encoding);
467         return buffer.toString();
468     }
469
470     /*
471      * (non-Javadoc)
472      *
473      * @see org.mule.umo.UMODescriptor#getImplementation()
474      */

475     public Object JavaDoc getImplementation()
476     {
477         return implementationReference;
478     }
479
480     public UMOInboundMessageRouter getInboundRouter()
481     {
482         return inboundRouter;
483     }
484
485     public UMOOutboundMessageRouter getOutboundRouter()
486     {
487         return outboundRouter;
488     }
489
490     /**
491      * The threading profile used but the UMO when managing a component. can be used
492      * to allocate more or less resources to this particular umo component
493      */

494     public ThreadingProfile getThreadingProfile()
495     {
496         return threadingProfile;
497     }
498
499     public PoolingProfile getPoolingProfile()
500     {
501         return poolingProfile;
502     }
503
504     public QueueProfile getQueueProfile()
505     {
506         return queueProfile;
507     }
508
509     public boolean isContainerManaged()
510     {
511         return !MuleContainerContext.MULE_CONTAINER_NAME.equalsIgnoreCase(container);
512     }
513
514     public Class JavaDoc getImplementationClass() throws UMOException
515     {
516         // check for other types of references
517
Class JavaDoc implClass = null;
518         if (implementationReference instanceof String JavaDoc || implementationReference instanceof ContainerKeyPair)
519         {
520             Object JavaDoc object = MuleManager.getInstance().getContainerContext().getComponent(
521                 implementationReference);
522             implClass = object.getClass();
523         }
524         else
525         {
526             implClass = implementationReference.getClass();
527         }
528
529         return implClass;
530     }
531
532     /**
533      * A helper method that will resolved a component for a given reference id. For
534      * example, for a component declared in a Spring Application context the id would
535      * be the bean id, in Pico the id would be a fully qualified class name.
536      *
537      * @param reference the reference to use when resolving the component
538      * @return the Implementation of the component
539      */

540     protected Class JavaDoc getImplementationForReference(String JavaDoc reference) throws ContainerException
541     {
542         Object JavaDoc object = MuleManager.getInstance().getContainerContext().getComponent(reference);
543         return object.getClass();
544     }
545
546     public void fireInitialisationCallbacks(Object JavaDoc component) throws InitialisationException
547     {
548         InitialisationCallback callback;
549         for (Iterator JavaDoc iterator = initialisationCallbacks.iterator(); iterator.hasNext();)
550         {
551             callback = (InitialisationCallback)iterator.next();
552             callback.initialise(component);
553         }
554     }
555
556     /**
557      * The inbound Provider to use when receiveing an event. This may get overidden
558      * by the configured behaviour of the inbound router on this component
559      *
560      * @return the inbound endpoint or null if one is not set
561      * @see org.mule.umo.endpoint.UMOEndpoint
562      */

563     public UMOEndpoint getInboundEndpoint()
564     {
565         return inboundEndpoint;
566     }
567
568     /**
569      * The outbound Provider to use when sending an event. This may get overidden by
570      * the configured behaviour of the outbound router on this component
571      *
572      * @return the outbound endpoint or null if one is not set
573      * @see org.mule.umo.endpoint.UMOEndpoint
574      */

575     public UMOEndpoint getOutboundEndpoint()
576     {
577         return outboundEndpoint;
578     }
579
580     public UMOResponseMessageRouter getResponseRouter()
581     {
582         return responseRouter;
583     }
584
585     public boolean isSingleton()
586     {
587         return singleton;
588     }
589
590     public String JavaDoc getInitialState()
591     {
592         return initialState;
593     }
594
595     /**
596      * Returns the name of the contaier where the object for this descriptor resides.
597      * If this value is 'none' the 'implementaiton' attributed is expected to be a
598      * fully qualified class name that will be instanciated.
599      *
600      * @return the container name, or null if it is not known - in which case each
601      * container will be queried for the component implementation.
602      */

603     public String JavaDoc getContainer()
604     {
605         return container;
606     }
607 }
608
Popular Tags