KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > config > builders > QuickConfigurationBuilder


1 /*
2  * $Id: QuickConfigurationBuilder.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.config.builders;
12
13 import org.mule.MuleManager;
14 import org.mule.config.ConfigurationBuilder;
15 import org.mule.config.ConfigurationException;
16 import org.mule.config.ReaderResource;
17 import org.mule.config.i18n.Message;
18 import org.mule.config.i18n.Messages;
19 import org.mule.impl.MuleDescriptor;
20 import org.mule.impl.internal.admin.MuleAdminAgent;
21 import org.mule.impl.endpoint.MuleEndpoint;
22 import org.mule.impl.endpoint.MuleEndpointURI;
23 import org.mule.impl.model.ModelFactory;
24 import org.mule.providers.service.ConnectorFactory;
25 import org.mule.umo.UMOComponent;
26 import org.mule.umo.UMODescriptor;
27 import org.mule.umo.UMOException;
28 import org.mule.umo.UMOFilter;
29 import org.mule.umo.endpoint.UMOEndpoint;
30 import org.mule.umo.endpoint.UMOEndpointURI;
31 import org.mule.umo.lifecycle.InitialisationException;
32 import org.mule.umo.manager.UMOContainerContext;
33 import org.mule.umo.manager.UMOManager;
34 import org.mule.umo.model.UMOModel;
35 import org.mule.umo.provider.UMOConnector;
36 import org.mule.util.MuleObjectHelper;
37 import org.mule.util.StringUtils;
38
39 import java.util.Map JavaDoc;
40 import java.util.Properties JavaDoc;
41
42 /**
43  * <code>QuickConfigurationBuilder</code> is a configuration helper that can be
44  * used by clients, configuration scripts or test cases to quickly configure a
45  * manager
46  *
47  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
48  * @version $Revision: 3798 $
49  */

50 public class QuickConfigurationBuilder implements ConfigurationBuilder
51 {
52     private static final String JavaDoc MODEL_NOT_SET = "not set";
53
54     private UMOManager manager;
55
56     /**
57      * Constructs a default builder
58      */

59     public QuickConfigurationBuilder()
60     {
61         manager = MuleManager.getInstance();
62     }
63
64     /**
65      * Will construct a new Quick Config builder with the option of disposing of the
66      * current Manager if one exists
67      *
68      * @param disposeCurrent true to dispose the current manager
69      */

70     public QuickConfigurationBuilder(boolean disposeCurrent)
71     {
72         if (disposeCurrent)
73         {
74             disposeCurrent();
75         }
76
77         manager = MuleManager.getInstance();
78     }
79
80     /**
81      * Disposes the current MuleManager if there is one.
82      */

83     public void disposeCurrent()
84     {
85         if (MuleManager.isInstanciated())
86         {
87             MuleManager.getInstance().dispose();
88         }
89     }
90
91     public void disableAdminAgent()
92     {
93         MuleManager.getConfiguration().setServerUrl(StringUtils.EMPTY);
94         if (manager != null)
95         {
96             try
97             {
98                 manager.unregisterAgent(MuleAdminAgent.AGENT_NAME);
99             }
100             catch (UMOException e)
101             {
102                 // ignore
103
}
104         }
105     }
106
107     public void setModel(String JavaDoc model) throws UMOException
108     {
109         manager.setModel(ModelFactory.createModel(model));
110     }
111
112     /**
113      * Configures a started manager. This method will throw InitialisationException
114      * if the current manager is already started
115      *
116      * @param synchronous whether to start the manager in synchronous mode
117      * @param serverUrl the url used to receive client requests, or null if the
118      * server listening components should not be set up
119      * @return the configured manager
120      * @throws UMOException if the manager is already started or it fails to start
121      */

122     public UMOManager createStartedManager(boolean synchronous, String JavaDoc serverUrl, String JavaDoc modeltype)
123         throws UMOException
124     {
125         if (manager.isStarted())
126         {
127             throw new InitialisationException(new Message(Messages.MANAGER_ALREADY_STARTED), this);
128         }
129         if (serverUrl == null)
130         {
131             serverUrl = "";
132         }
133         MuleManager.getConfiguration().setServerUrl(serverUrl);
134         MuleManager.getConfiguration().setSynchronous(synchronous);
135         if (!MODEL_NOT_SET.equals(modeltype))
136         {
137             manager.setModel(ModelFactory.createModel(modeltype));
138         }
139         manager.start();
140         return manager;
141     }
142
143     /**
144      * Configures a started manager. This method will throw InitialisationException
145      * if the current manager is already started
146      *
147      * @param synchronous whether to start the manager in synchronous mode
148      * @param serverUrl the url used to receive client requests, or null if the
149      * server listening components should not be set up
150      * @return the configured manager
151      * @throws UMOException if the manager is already started or it fails to start
152      */

153     public UMOManager createStartedManager(boolean synchronous, String JavaDoc serverUrl) throws UMOException
154     {
155         return createStartedManager(synchronous, serverUrl, MODEL_NOT_SET);
156     }
157
158     /**
159      * Configures a started manager. This method will throw InitialisationException
160      * if the current manager is already started
161      *
162      * @param synchronous whether to start the manager in synchronous mode
163      * @param serverUrl the url used to receive client requests, or null if the
164      * server listening components should not be set up
165      * @param serverConnector The server connector to use for the serverUrl
166      * @return the configured manager
167      * @throws UMOException if the manager is already started or it fails to start
168      */

169     public UMOManager createStartedManager(boolean synchronous, String JavaDoc serverUrl, UMOConnector serverConnector)
170         throws UMOException
171     {
172         if (serverConnector != null)
173         {
174             manager.registerConnector(serverConnector);
175         }
176         else
177         {
178             throw new IllegalArgumentException JavaDoc("Cannot create started manager from null serverConnector");
179         }
180
181         // set the connector on the endpointUri
182
int param = serverUrl.indexOf('?');
183         if (param == -1)
184         {
185             serverUrl += '?';
186         }
187         else
188         {
189             serverUrl += '&';
190         }
191
192         serverUrl += UMOEndpointURI.PROPERTY_CREATE_CONNECTOR + "=" + serverConnector.getName();
193         return createStartedManager(synchronous, serverUrl);
194     }
195
196     /**
197      * Registers a java object as a Umo pcomponent that listens for events on the
198      * given url. By default the ThreadingProfile for the components will be set so
199      * that there will only be one thread of execution.
200      *
201      * @param component any java object, Mule will it's endpointUri discovery to
202      * determine which event to invoke based on the evnet payload type
203      * @param name The identifying name of the components. This can be used to later
204      * unregister it
205      * @param listenerEndpointUri The url endpointUri to listen to
206      * @throws org.mule.umo.UMOException
207      */

208     public UMODescriptor registerComponentInstance(Object JavaDoc component,
209                                                    String JavaDoc name,
210                                                    UMOEndpointURI listenerEndpointUri) throws UMOException
211     {
212         return registerComponentInstance(component, name, listenerEndpointUri, null);
213     }
214
215     /**
216      * Registers a java object as a Umo pcomponent that listens for and sends events
217      * on the given urls. By default the ThreadingProfile for the components will be
218      * set so that there will only be one thread of execution.
219      *
220      * @param component any java object, Mule will it's endpointUri discovery to
221      * determine which event to invoke based on the evnet payload type
222      * @param name The identifying name of the components. This can be used to later
223      * unregister it
224      * @param listenerEndpointUri The url endpointUri to listen to
225      * @param sendEndpointUri The url endpointUri to dispatch to
226      * @throws UMOException
227      */

228     public UMODescriptor registerComponentInstance(Object JavaDoc component,
229                                                    String JavaDoc name,
230                                                    UMOEndpointURI listenerEndpointUri,
231                                                    UMOEndpointURI sendEndpointUri) throws UMOException
232     {
233         MuleDescriptor descriptor = new MuleDescriptor();
234         descriptor.setName(name);
235         descriptor.setImplementationInstance(component);
236
237         // Create the endpoints
238
UMOEndpoint inboundProvider = null;
239         UMOEndpoint outboundProvider = null;
240         if (listenerEndpointUri != null)
241         {
242             inboundProvider = ConnectorFactory.createEndpoint(listenerEndpointUri,
243                 UMOEndpoint.ENDPOINT_TYPE_RECEIVER);
244         }
245         if (sendEndpointUri != null)
246         {
247             outboundProvider = ConnectorFactory.createEndpoint(sendEndpointUri,
248                 UMOEndpoint.ENDPOINT_TYPE_SENDER);
249         }
250         descriptor.setInboundEndpoint(inboundProvider);
251         descriptor.setOutboundEndpoint(outboundProvider);
252
253         // register the components descriptor
254
manager.getModel().registerComponent(descriptor);
255         return descriptor;
256     }
257
258     public UMOComponent registerComponent(String JavaDoc implementation,
259                                           String JavaDoc name,
260                                           String JavaDoc inboundEndpoint,
261                                           String JavaDoc outboundEndpoint,
262                                           Map JavaDoc properties) throws UMOException
263     {
264         UMOEndpoint inEndpoint = null;
265         UMOEndpoint outEndpoint = null;
266         if (inboundEndpoint != null)
267         {
268             inEndpoint = manager.lookupEndpoint(inboundEndpoint);
269             if (inEndpoint == null)
270             {
271                 inEndpoint = createEndpoint(inboundEndpoint, null, true);
272             }
273         }
274         if (outboundEndpoint != null)
275         {
276             outEndpoint = manager.lookupEndpoint(outboundEndpoint);
277             if (outEndpoint == null)
278             {
279                 outEndpoint = createEndpoint(outboundEndpoint, null, false);
280             }
281         }
282         UMODescriptor d = createDescriptor(implementation, name, inEndpoint, outEndpoint, properties);
283         return registerComponent(d);
284     }
285
286     public UMOComponent registerComponent(String JavaDoc implementation,
287                                           String JavaDoc name,
288                                           UMOEndpoint inEndpoint,
289                                           UMOEndpoint outEndpoint,
290                                           Map JavaDoc properties) throws UMOException
291     {
292         UMODescriptor d = createDescriptor(implementation, name, inEndpoint, outEndpoint, properties);
293         return registerComponent(d);
294     }
295
296     /**
297      * Registers a user configured MuleDescriptor of a components to the server. If
298      * users want to register object instances with the server rather than class
299      * names that get created at runtime or reference to objects in the container,
300      * the user must call the descriptors setImplementationInstance() method - <code>
301      * MyBean implementation = new MyBean();
302      * descriptor.setImplementationInstance(implementation);
303      * </code>
304      * Calling this method is equivilent to calling UMOModel.registerComponent(..)
305      *
306      * @param descriptor the componet descriptor to register
307      * @throws UMOException the descriptor is invalid or cannot be initialised or
308      * started
309      * @see org.mule.umo.model.UMOModel
310      */

311     public UMOComponent registerComponent(UMODescriptor descriptor) throws UMOException
312     {
313         return manager.getModel().registerComponent(descriptor);
314     }
315
316     /**
317      * Registers a java object as a Umo pcomponent that listens for events on the
318      * given url. By default the ThreadingProfile for the components will be set so
319      * that there will only be one thread of execution.
320      *
321      * @param implementation either a container refernece to an object or a fully
322      * qualified class name to use as the component implementation
323      * @param name The identifying name of the components. This can be used to later
324      * unregister it
325      * @param inboundEndpointUri The url endpointUri to listen to
326      * @throws org.mule.umo.UMOException
327      */

328     public UMOComponent registerComponent(String JavaDoc implementation,
329                                           String JavaDoc name,
330                                           UMOEndpointURI inboundEndpointUri) throws UMOException
331     {
332         return registerComponent(implementation, name, inboundEndpointUri, null, null);
333     }
334
335     /**
336      * Registers a java object as a Umo pcomponent that listens for events on the
337      * given url. By default the ThreadingProfile for the components will be set so
338      * that there will only be one thread of execution.
339      *
340      * @param implementation either a container refernece to an object or a fully
341      * qualified class name to use as the component implementation
342      * @param name The identifying name of the components. This can be used to later
343      * unregister it
344      * @param inboundEndpointUri The url endpointUri to listen to
345      * @param properties properties to set on the component
346      * @throws org.mule.umo.UMOException
347      */

348     public UMOComponent registerComponent(String JavaDoc implementation,
349                                           String JavaDoc name,
350                                           UMOEndpointURI inboundEndpointUri,
351                                           Map JavaDoc properties) throws UMOException
352     {
353         return registerComponent(implementation, name, inboundEndpointUri, null, properties);
354     }
355
356     /**
357      * Registers a java object as a Umo pcomponent that listens for and sends events
358      * on the given urls. By default the ThreadingProfile for the components will be
359      * set so that there will only be one thread of execution.
360      *
361      * @param implementation either a container refernece to an object or a fully
362      * qualified class name to use as the component implementation which
363      * event to invoke based on the evnet payload type
364      * @param name The identifying name of the components. This can be used to later
365      * unregister it
366      * @param inboundEndpointUri The url endpointUri to listen to
367      * @param outboundEndpointUri The url endpointUri to dispatch to
368      * @throws UMOException
369      */

370     public UMOComponent registerComponent(String JavaDoc implementation,
371                                           String JavaDoc name,
372                                           UMOEndpointURI inboundEndpointUri,
373                                           UMOEndpointURI outboundEndpointUri) throws UMOException
374     {
375         return registerComponent(implementation, name, inboundEndpointUri, outboundEndpointUri, null);
376     }
377
378     /**
379      * Registers a java object as a Umo pcomponent that listens for and sends events
380      * on the given urls. By default the ThreadingProfile for the components will be
381      * set so that there will only be one thread of execution.
382      *
383      * @param implementation either a container refernece to an object or a fully
384      * qualified class name to use as the component implementation which
385      * event to invoke based on the evnet payload type
386      * @param name The identifying name of the components. This can be used to later
387      * unregister it
388      * @param inboundEndpointUri The url endpointUri to listen to
389      * @param outboundEndpointUri The url endpointUri to dispatch to
390      * @param properties properties to set on the component
391      * @throws UMOException
392      */

393     public UMOComponent registerComponent(String JavaDoc implementation,
394                                           String JavaDoc name,
395                                           UMOEndpointURI inboundEndpointUri,
396                                           UMOEndpointURI outboundEndpointUri,
397                                           Map JavaDoc properties) throws UMOException
398     {
399         UMODescriptor d = createDescriptor(implementation, name, inboundEndpointUri, outboundEndpointUri,
400             properties);
401         return manager.getModel().registerComponent(d);
402     }
403
404     /**
405      * Creates a Mule Descriptor that can be further maniputalted by the calling
406      * class before registering it with the UMOModel
407      *
408      * @param implementation either a container refernece to an object or a fully
409      * qualified class name to use as the component implementation which
410      * event to invoke based on the evnet payload type
411      * @param name The identifying name of the component. This can be used to later
412      * unregister it
413      * @param inboundEndpointUri The url endpointUri to listen to. Can be null
414      * @param outboundEndpointUri The url endpointUri to dispatch to. Can be null
415      * @param properties properties to set on the component. Can be null
416      * @throws UMOException
417      */

418     public UMODescriptor createDescriptor(String JavaDoc implementation,
419                                           String JavaDoc name,
420                                           String JavaDoc inboundEndpointUri,
421                                           String JavaDoc outboundEndpointUri,
422                                           Map JavaDoc properties) throws UMOException
423     {
424         UMOEndpointURI inEndpointUri = null;
425         UMOEndpointURI outEndpointUri = null;
426         if (inboundEndpointUri != null)
427         {
428             inEndpointUri = new MuleEndpointURI(inboundEndpointUri);
429         }
430         if (outboundEndpointUri != null)
431         {
432             outEndpointUri = new MuleEndpointURI(outboundEndpointUri);
433         }
434
435         return createDescriptor(implementation, name, inEndpointUri, outEndpointUri, properties);
436     }
437
438     /**
439      * Creates a Mule Descriptor that can be further maniputalted by the calling
440      * class before registering it with the UMOModel
441      *
442      * @param implementation either a container refernece to an object or a fully
443      * qualified class name to use as the component implementation which
444      * event to invoke based on the evnet payload type
445      * @param name The identifying name of the component. This can be used to later
446      * unregister it
447      * @param inboundEndpointUri The url endpointUri to listen to. Can be null
448      * @param outboundEndpointUri The url endpointUri to dispatch to. Can be null
449      * @param properties properties to set on the component. Can be null
450      * @throws UMOException
451      */

452     public UMODescriptor createDescriptor(String JavaDoc implementation,
453                                           String JavaDoc name,
454                                           UMOEndpointURI inboundEndpointUri,
455                                           UMOEndpointURI outboundEndpointUri,
456                                           Map JavaDoc properties) throws UMOException
457     {
458         // Create the endpoints
459
UMOEndpoint inboundEndpoint = null;
460         UMOEndpoint outboundEndpoint = null;
461         if (inboundEndpointUri != null)
462         {
463             inboundEndpoint = ConnectorFactory.createEndpoint(inboundEndpointUri,
464                 UMOEndpoint.ENDPOINT_TYPE_RECEIVER);
465         }
466         if (outboundEndpointUri != null)
467         {
468             outboundEndpoint = ConnectorFactory.createEndpoint(outboundEndpointUri,
469                 UMOEndpoint.ENDPOINT_TYPE_SENDER);
470         }
471         return createDescriptor(implementation, name, inboundEndpoint, outboundEndpoint, properties);
472     }
473
474     /**
475      * Creates a Mule Descriptor that can be further maniputalted by the calling
476      * class before registering it with the UMOModel
477      *
478      * @param implementation either a container refernece to an object or a fully
479      * qualified class name to use as the component implementation which
480      * event to invoke based on the evnet payload type
481      * @param name The identifying name of the component. This can be used to later
482      * unregister it
483      * @param inboundEndpoint The endpoint to listen to. Can be null
484      * @param outboundEndpoint The endpoint to dispatch to. Can be null
485      * @param properties properties to set on the component. Can be null
486      * @throws UMOException
487      */

488     public UMODescriptor createDescriptor(String JavaDoc implementation,
489                                           String JavaDoc name,
490                                           UMOEndpoint inboundEndpoint,
491                                           UMOEndpoint outboundEndpoint,
492                                           Map JavaDoc properties) throws UMOException
493     {
494         MuleDescriptor descriptor = new MuleDescriptor();
495         descriptor.setImplementation(implementation);
496         descriptor.setName(name);
497         if (properties != null)
498         {
499             descriptor.getProperties().putAll(properties);
500         }
501
502         descriptor.setInboundEndpoint(inboundEndpoint);
503         descriptor.setOutboundEndpoint(outboundEndpoint);
504
505         return descriptor;
506     }
507
508     /**
509      * Sets the component resolver on the model. Component resolver is used to look
510      * up components in an external container such as Spring or Pico
511      *
512      * @param ctx
513      * @throws UMOException
514      */

515     public void setContainerContext(UMOContainerContext ctx) throws UMOException
516     {
517         manager.setContainerContext(ctx);
518     }
519
520     /**
521      * Unregisters a previously register components. This will also unregister any
522      * listeners for the components Calling this method is equivilent to calling
523      * UMOModel.unregisterComponent(..)
524      *
525      * @param name the name of the componet to unregister
526      * @throws UMOException if unregistering the components fails, i.e. The
527      * underlying transport fails to unregister a listener. If the
528      * components does not exist, this method should not throw an
529      * exception.
530      * @see org.mule.umo.model.UMOModel
531      */

532     public void unregisterComponent(String JavaDoc name) throws UMOException
533     {
534         UMODescriptor descriptor = manager.getModel().getDescriptor(name);
535         if (descriptor != null)
536         {
537             manager.getModel().unregisterComponent(descriptor);
538         }
539     }
540
541     public UMOEndpoint createEndpoint(String JavaDoc uri, String JavaDoc name, boolean inbound) throws UMOException
542     {
543         return createEndpoint(uri, name, inbound, null, null);
544     }
545
546     public UMOEndpoint createEndpoint(String JavaDoc uri, String JavaDoc name, boolean inbound, String JavaDoc transformers)
547         throws UMOException
548     {
549         return createEndpoint(uri, name, inbound, transformers, null);
550     }
551
552     public UMOEndpoint createEndpoint(String JavaDoc uri, String JavaDoc name, boolean inbound, UMOFilter filter)
553         throws UMOException
554     {
555         return createEndpoint(uri, name, inbound, null, filter);
556     }
557
558     public UMOEndpoint createEndpoint(String JavaDoc uri,
559                                       String JavaDoc name,
560                                       boolean inbound,
561                                       String JavaDoc transformers,
562                                       UMOFilter filter) throws UMOException
563     {
564         UMOEndpoint ep = MuleEndpoint.createEndpointFromUri(new MuleEndpointURI(uri), (inbound
565                         ? UMOEndpoint.ENDPOINT_TYPE_RECEIVER : UMOEndpoint.ENDPOINT_TYPE_SENDER));
566         ep.setName(name);
567         if (transformers != null)
568         {
569             String JavaDoc delim = (transformers.indexOf(",") > -1 ? "," : " ");
570             ep.setTransformer(MuleObjectHelper.getTransformer(transformers, delim));
571         }
572         ep.setFilter(filter);
573         return ep;
574     }
575
576     public UMOEndpoint registerEndpoint(String JavaDoc uri, String JavaDoc name, boolean inbound) throws UMOException
577     {
578         UMOEndpoint ep = createEndpoint(uri, name, inbound);
579         ep.initialise();
580         manager.registerEndpoint(ep);
581         return ep;
582     }
583
584     public UMOEndpoint registerEndpoint(String JavaDoc uri, String JavaDoc name, boolean inbound, Map JavaDoc properties)
585         throws UMOException
586     {
587         UMOEndpoint ep = createEndpoint(uri, name, inbound);
588         ep.getProperties().putAll(properties);
589         ep.initialise();
590         manager.registerEndpoint(ep);
591         return ep;
592     }
593
594     public UMOEndpoint registerEndpoint(String JavaDoc uri,
595                                         String JavaDoc name,
596                                         boolean inbound,
597                                         Map JavaDoc properties,
598                                         UMOFilter filter) throws UMOException
599     {
600         UMOEndpoint ep = createEndpoint(uri, name, inbound);
601         if (properties != null)
602         {
603             ep.getProperties().putAll(properties);
604         }
605         if (filter != null)
606         {
607             ep.setFilter(filter);
608         }
609         ep.initialise();
610         manager.registerEndpoint(ep);
611         return ep;
612     }
613
614     public void registerModel(UMOModel model) throws UMOException
615     {
616         manager.setModel(model);
617     }
618
619     public UMOManager getManager()
620     {
621         return manager;
622     }
623
624     public UMOManager configure(String JavaDoc configResources) throws ConfigurationException
625     {
626         return configure(configResources, null);
627     }
628
629     public UMOManager configure(String JavaDoc configResources, String JavaDoc startupPropertiesFile)
630         throws ConfigurationException
631     {
632         return configure(new ReaderResource[0], null);
633     }
634
635     public UMOManager configure(ReaderResource[] configResources) throws ConfigurationException
636     {
637         return configure(configResources, null);
638     }
639
640     public UMOManager configure(ReaderResource[] configResources, Properties JavaDoc startupProperties)
641         throws ConfigurationException
642     {
643         try
644         {
645             manager.start();
646         }
647         catch (UMOException e)
648         {
649             throw new ConfigurationException(e);
650         }
651         return manager;
652     }
653
654     public boolean isConfigured()
655     {
656         return manager != null;
657     }
658 }
659
Popular Tags