View Javadoc

1   /*
2    * $Id: QuickConfigurationBuilder.java 10415 2008-01-21 10:46:37Z 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.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.CoreMessages;
18  import org.mule.impl.MuleDescriptor;
19  import org.mule.impl.endpoint.MuleEndpoint;
20  import org.mule.impl.endpoint.MuleEndpointURI;
21  import org.mule.impl.internal.admin.MuleAdminAgent;
22  import org.mule.impl.model.ModelFactory;
23  import org.mule.impl.model.seda.SedaModel;
24  import org.mule.providers.service.TransportFactory;
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;
40  import java.util.Properties;
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  public class QuickConfigurationBuilder implements ConfigurationBuilder
48  {
49      private static final String MODEL_NOT_SET = "not set";
50  
51      private UMOManager manager;
52  
53      private UMOModel model;
54  
55      /**
56       * Constructs a default builder
57       */
58      public QuickConfigurationBuilder()
59      {
60          manager = MuleManager.getInstance();
61      }
62  
63      /**
64       * Will construct a new Quick Config builder with the option of disposing of the
65       * current Manager if one exists
66       * 
67       * @param disposeCurrent true to dispose the current manager
68       */
69      public QuickConfigurationBuilder(boolean disposeCurrent)
70      {
71          if (disposeCurrent)
72          {
73              disposeCurrent();
74          }
75  
76          manager = MuleManager.getInstance();
77      }
78  
79      /**
80       * Disposes the current MuleManager if there is one.
81       */
82      public void disposeCurrent()
83      {
84          if (MuleManager.isInstanciated())
85          {
86              MuleManager.getInstance().dispose();
87          }
88      }
89  
90      public void disableAdminAgent()
91      {
92          MuleManager.getConfiguration().setServerUrl(StringUtils.EMPTY);
93          if (manager != null)
94          {
95              try
96              {
97                  manager.unregisterAgent(MuleAdminAgent.AGENT_NAME);
98              }
99              catch (UMOException e)
100             {
101                 throw new RuntimeException("Exception trying to remove admin agent", e);
102             }
103         }
104     }
105 
106     public void registerModel(String modelType, String name) throws UMOException
107     {
108         UMOModel model = ModelFactory.createModel(modelType);
109         model.setName(name);
110         manager.registerModel(model);
111     }
112 
113     /**
114      * Configures a started manager. This method will throw InitialisationException
115      * if the current manager is already started
116      * 
117      * @param synchronous whether to start the manager in synchronous mode
118      * @param serverUrl the url used to receive client requests, or null if the
119      *            server listening components should not be set up
120      * @return the configured manager
121      * @throws UMOException if the manager is already started or it fails to start
122      */
123     public UMOManager createStartedManager(boolean synchronous, String serverUrl, String modeltype)
124         throws UMOException
125     {
126         if (manager.isStarted())
127         {
128             throw new InitialisationException(CoreMessages.managerAlreadyStarted(), this);
129         }
130         if (serverUrl == null)
131         {
132             serverUrl = "";
133         }
134         MuleManager.getConfiguration().setServerUrl(serverUrl);
135         MuleManager.getConfiguration().setSynchronous(synchronous);
136         if (!MODEL_NOT_SET.equals(modeltype))
137         {
138             model = ModelFactory.createModel(modeltype);
139         }
140         else
141         {
142             model = ModelFactory.createModel("seda");
143         }
144         manager.registerModel(model);
145 
146         manager.start();
147         return manager;
148     }
149 
150     /**
151      * Configures a started manager. This method will throw InitialisationException
152      * if the current manager is already started
153      * 
154      * @param synchronous whether to start the manager in synchronous mode
155      * @param serverUrl the url used to receive client requests, or null if the
156      *            server listening components should not be set up
157      * @return the configured manager
158      * @throws UMOException if the manager is already started or it fails to start
159      */
160     public UMOManager createStartedManager(boolean synchronous, String serverUrl) throws UMOException
161     {
162         return createStartedManager(synchronous, serverUrl, MODEL_NOT_SET);
163     }
164 
165     /**
166      * Configures a started manager. This method will throw InitialisationException
167      * if the current manager is already started
168      * 
169      * @param synchronous whether to start the manager in synchronous mode
170      * @param serverUrl the url used to receive client requests, or null if the
171      *            server listening components should not be set up
172      * @param serverConnector The server connector to use for the serverUrl
173      * @return the configured manager
174      * @throws UMOException if the manager is already started or it fails to start
175      */
176     public UMOManager createStartedManager(boolean synchronous, String serverUrl, UMOConnector serverConnector)
177         throws UMOException
178     {
179         if (serverConnector != null)
180         {
181             manager.registerConnector(serverConnector);
182         }
183         else
184         {
185             throw new IllegalArgumentException("Cannot create started manager from null serverConnector");
186         }
187 
188         // set the connector on the endpointUri
189         int param = serverUrl.indexOf('?');
190         if (param == -1)
191         {
192             serverUrl += '?';
193         }
194         else
195         {
196             serverUrl += '&';
197         }
198 
199         serverUrl += UMOEndpointURI.PROPERTY_CREATE_CONNECTOR + "=" + serverConnector.getName();
200         return createStartedManager(synchronous, serverUrl);
201     }
202 
203     /**
204      * Registers a java object as a Umo pcomponent that listens for events on the
205      * given url. By default the ThreadingProfile for the components will be set so
206      * that there will only be one thread of execution.
207      * 
208      * @param component any java object, Mule will it's endpointUri discovery to
209      *            determine which event to invoke based on the evnet payload type
210      * @param name The identifying name of the components. This can be used to later
211      *            unregister it
212      * @param listenerEndpointUri The url endpointUri to listen to
213      * @throws org.mule.umo.UMOException
214      */
215     public UMODescriptor registerComponentInstance(Object component,
216                                                    String name,
217                                                    UMOEndpointURI listenerEndpointUri) throws UMOException
218     {
219         return registerComponentInstance(component, name, listenerEndpointUri, null);
220     }
221 
222     /**
223      * Registers a java object as a Umo pcomponent that listens for and sends events
224      * on the given urls. By default the ThreadingProfile for the components will be
225      * set so that there will only be one thread of execution.
226      * 
227      * @param component any java object, Mule will it's endpointUri discovery to
228      *            determine which event to invoke based on the evnet payload type
229      * @param name The identifying name of the components. This can be used to later
230      *            unregister it
231      * @param listenerEndpointUri The url endpointUri to listen to
232      * @param sendEndpointUri The url endpointUri to dispatch to
233      * @throws UMOException
234      */
235     public UMODescriptor registerComponentInstance(Object component,
236                                                    String name,
237                                                    UMOEndpointURI listenerEndpointUri,
238                                                    UMOEndpointURI sendEndpointUri) throws UMOException
239     {
240         MuleDescriptor descriptor = new MuleDescriptor();
241         descriptor.setName(name);
242         descriptor.setImplementationInstance(component);
243 
244         // Create the endpoints
245         UMOEndpoint inboundProvider = null;
246         UMOEndpoint outboundProvider = null;
247         if (listenerEndpointUri != null)
248         {
249             inboundProvider = TransportFactory.createEndpoint(listenerEndpointUri,
250                 UMOEndpoint.ENDPOINT_TYPE_RECEIVER);
251         }
252         if (sendEndpointUri != null)
253         {
254             outboundProvider = TransportFactory.createEndpoint(sendEndpointUri,
255                 UMOEndpoint.ENDPOINT_TYPE_SENDER);
256         }
257         descriptor.setInboundEndpoint(inboundProvider);
258         descriptor.setOutboundEndpoint(outboundProvider);
259 
260         // register the components descriptor
261         getModel().registerComponent(descriptor);
262         return descriptor;
263     }
264 
265     public UMOComponent registerComponent(String implementation,
266                                           String name,
267                                           String inboundEndpoint,
268                                           String outboundEndpoint,
269                                           Map properties) throws UMOException
270     {
271         UMOEndpoint inEndpoint = null;
272         UMOEndpoint outEndpoint = null;
273         if (inboundEndpoint != null)
274         {
275             inEndpoint = manager.lookupEndpoint(inboundEndpoint);
276             if (inEndpoint == null)
277             {
278                 inEndpoint = createEndpoint(inboundEndpoint, null, true);
279             }
280         }
281         if (outboundEndpoint != null)
282         {
283             outEndpoint = manager.lookupEndpoint(outboundEndpoint);
284             if (outEndpoint == null)
285             {
286                 outEndpoint = createEndpoint(outboundEndpoint, null, false);
287             }
288         }
289         UMODescriptor d = createDescriptor(implementation, name, inEndpoint, outEndpoint, properties);
290         return registerComponent(d);
291     }
292 
293     public UMOComponent registerComponent(String implementation,
294                                           String name,
295                                           UMOEndpoint inEndpoint,
296                                           UMOEndpoint outEndpoint,
297                                           Map properties) throws UMOException
298     {
299         UMODescriptor d = createDescriptor(implementation, name, inEndpoint, outEndpoint, properties);
300         return registerComponent(d);
301     }
302 
303     /**
304      * Registers a user configured MuleDescriptor of a components to the server. If
305      * users want to register object instances with the server rather than class
306      * names that get created at runtime or reference to objects in the container,
307      * the user must call the descriptors setImplementationInstance() method - <code>
308      *     MyBean implementation = new MyBean();
309      *     descriptor.setImplementationInstance(implementation);
310      * </code>
311      * Calling this method is equivilent to calling UMOModel.registerComponent(..)
312      * 
313      * @param descriptor the componet descriptor to register
314      * @throws UMOException the descriptor is invalid or cannot be initialised or
315      *             started
316      * @see org.mule.umo.model.UMOModel
317      */
318     public UMOComponent registerComponent(UMODescriptor descriptor) throws UMOException
319     {
320         return getModel().registerComponent(descriptor);
321     }
322 
323     /**
324      * Registers a java object as a Umo pcomponent that listens for events on the
325      * given url. By default the ThreadingProfile for the components will be set so
326      * that there will only be one thread of execution.
327      * 
328      * @param implementation either a container refernece to an object or a fully
329      *            qualified class name to use as the component implementation
330      * @param name The identifying name of the components. This can be used to later
331      *            unregister it
332      * @param inboundEndpointUri The url endpointUri to listen to
333      * @throws org.mule.umo.UMOException
334      */
335     public UMOComponent registerComponent(String implementation,
336                                           String name,
337                                           UMOEndpointURI inboundEndpointUri) throws UMOException
338     {
339         return registerComponent(implementation, name, inboundEndpointUri, null, null);
340     }
341 
342     /**
343      * Registers a java object as a Umo pcomponent that listens for events on the
344      * given url. By default the ThreadingProfile for the components will be set so
345      * that there will only be one thread of execution.
346      * 
347      * @param implementation either a container refernece to an object or a fully
348      *            qualified class name to use as the component implementation
349      * @param name The identifying name of the components. This can be used to later
350      *            unregister it
351      * @param inboundEndpointUri The url endpointUri to listen to
352      * @param properties properties to set on the component
353      * @throws org.mule.umo.UMOException
354      */
355     public UMOComponent registerComponent(String implementation,
356                                           String name,
357                                           UMOEndpointURI inboundEndpointUri,
358                                           Map properties) throws UMOException
359     {
360         return registerComponent(implementation, name, inboundEndpointUri, null, properties);
361     }
362 
363     /**
364      * Registers a java object as a Umo pcomponent that listens for and sends events
365      * on the given urls. By default the ThreadingProfile for the components will be
366      * set so that there will only be one thread of execution.
367      * 
368      * @param implementation either a container refernece to an object or a fully
369      *            qualified class name to use as the component implementation which
370      *            event to invoke based on the evnet payload type
371      * @param name The identifying name of the components. This can be used to later
372      *            unregister it
373      * @param inboundEndpointUri The url endpointUri to listen to
374      * @param outboundEndpointUri The url endpointUri to dispatch to
375      * @throws UMOException
376      */
377     public UMOComponent registerComponent(String implementation,
378                                           String name,
379                                           UMOEndpointURI inboundEndpointUri,
380                                           UMOEndpointURI outboundEndpointUri) throws UMOException
381     {
382         return registerComponent(implementation, name, inboundEndpointUri, outboundEndpointUri, null);
383     }
384 
385     /**
386      * Registers a java object as a Umo pcomponent that listens for and sends events
387      * on the given urls. By default the ThreadingProfile for the components will be
388      * set so that there will only be one thread of execution.
389      * 
390      * @param implementation either a container refernece to an object or a fully
391      *            qualified class name to use as the component implementation which
392      *            event to invoke based on the evnet payload type
393      * @param name The identifying name of the components. This can be used to later
394      *            unregister it
395      * @param inboundEndpointUri The url endpointUri to listen to
396      * @param outboundEndpointUri The url endpointUri to dispatch to
397      * @param properties properties to set on the component
398      * @throws UMOException
399      */
400     public UMOComponent registerComponent(String implementation,
401                                           String name,
402                                           UMOEndpointURI inboundEndpointUri,
403                                           UMOEndpointURI outboundEndpointUri,
404                                           Map properties) throws UMOException
405     {
406         UMODescriptor d = createDescriptor(implementation, name, inboundEndpointUri, outboundEndpointUri,
407             properties);
408         return getModel().registerComponent(d);
409     }
410 
411     /**
412      * Creates a Mule Descriptor that can be further maniputalted by the calling
413      * class before registering it with the UMOModel
414      * 
415      * @param implementation either a container refernece to an object or a fully
416      *            qualified class name to use as the component implementation which
417      *            event to invoke based on the evnet payload type
418      * @param name The identifying name of the component. This can be used to later
419      *            unregister it
420      * @param inboundEndpointUri The url endpointUri to listen to. Can be null
421      * @param outboundEndpointUri The url endpointUri to dispatch to. Can be null
422      * @param properties properties to set on the component. Can be null
423      * @throws UMOException
424      */
425     public UMODescriptor createDescriptor(String implementation,
426                                           String name,
427                                           String inboundEndpointUri,
428                                           String outboundEndpointUri,
429                                           Map properties) throws UMOException
430     {
431         UMOEndpointURI inEndpointUri = null;
432         UMOEndpointURI outEndpointUri = null;
433         if (inboundEndpointUri != null)
434         {
435             inEndpointUri = new MuleEndpointURI(inboundEndpointUri);
436         }
437         if (outboundEndpointUri != null)
438         {
439             outEndpointUri = new MuleEndpointURI(outboundEndpointUri);
440         }
441 
442         return createDescriptor(implementation, name, inEndpointUri, outEndpointUri, properties);
443     }
444 
445     /**
446      * Creates a Mule Descriptor that can be further maniputalted by the calling
447      * class before registering it with the UMOModel
448      * 
449      * @param implementation either a container refernece to an object or a fully
450      *            qualified class name to use as the component implementation which
451      *            event to invoke based on the evnet payload type
452      * @param name The identifying name of the component. This can be used to later
453      *            unregister it
454      * @param inboundEndpointUri The url endpointUri to listen to. Can be null
455      * @param outboundEndpointUri The url endpointUri to dispatch to. Can be null
456      * @param properties properties to set on the component. Can be null
457      * @throws UMOException
458      */
459     public UMODescriptor createDescriptor(String implementation,
460                                           String name,
461                                           UMOEndpointURI inboundEndpointUri,
462                                           UMOEndpointURI outboundEndpointUri,
463                                           Map properties) throws UMOException
464     {
465         // Create the endpoints
466         UMOEndpoint inboundEndpoint = null;
467         UMOEndpoint outboundEndpoint = null;
468         if (inboundEndpointUri != null)
469         {
470             inboundEndpoint = TransportFactory.createEndpoint(inboundEndpointUri,
471                 UMOEndpoint.ENDPOINT_TYPE_RECEIVER);
472         }
473         if (outboundEndpointUri != null)
474         {
475             outboundEndpoint = TransportFactory.createEndpoint(outboundEndpointUri,
476                 UMOEndpoint.ENDPOINT_TYPE_SENDER);
477         }
478         return createDescriptor(implementation, name, inboundEndpoint, outboundEndpoint, properties);
479     }
480 
481     /**
482      * Creates a Mule Descriptor that can be further maniputalted by the calling
483      * class before registering it with the UMOModel
484      * 
485      * @param implementation either a container refernece to an object or a fully
486      *            qualified class name to use as the component implementation which
487      *            event to invoke based on the evnet payload type
488      * @param name The identifying name of the component. This can be used to later
489      *            unregister it
490      * @param inboundEndpoint The endpoint to listen to. Can be null
491      * @param outboundEndpoint The endpoint to dispatch to. Can be null
492      * @param properties properties to set on the component. Can be null
493      * @throws UMOException
494      */
495     public UMODescriptor createDescriptor(String implementation,
496                                           String name,
497                                           UMOEndpoint inboundEndpoint,
498                                           UMOEndpoint outboundEndpoint,
499                                           Map properties) throws UMOException
500     {
501         MuleDescriptor descriptor = new MuleDescriptor();
502         descriptor.setImplementation(implementation);
503         descriptor.setName(name);
504         if (properties != null)
505         {
506             descriptor.getProperties().putAll(properties);
507         }
508 
509         descriptor.setInboundEndpoint(inboundEndpoint);
510         descriptor.setOutboundEndpoint(outboundEndpoint);
511 
512         return descriptor;
513     }
514 
515     /**
516      * Sets the component resolver on the model. Component resolver is used to look
517      * up components in an external container such as Spring or Pico
518      * 
519      * @param ctx
520      * @throws UMOException
521      */
522     public void setContainerContext(UMOContainerContext ctx) throws UMOException
523     {
524         manager.setContainerContext(ctx);
525     }
526 
527     /**
528      * Unregisters a previously register components. This will also unregister any
529      * listeners for the components Calling this method is equivilent to calling
530      * UMOModel.unregisterComponent(..)
531      * 
532      * @param name the name of the componet to unregister
533      * @throws UMOException if unregistering the components fails, i.e. The
534      *             underlying transport fails to unregister a listener. If the
535      *             components does not exist, this method should not throw an
536      *             exception.
537      * @see org.mule.umo.model.UMOModel
538      */
539     public void unregisterComponent(String name) throws UMOException
540     {
541         UMODescriptor descriptor = model.getDescriptor(name);
542         if (descriptor != null)
543         {
544             getModel().unregisterComponent(descriptor);
545         }
546     }
547 
548     public UMOEndpoint createEndpoint(String uri, String name, boolean inbound) throws UMOException
549     {
550         return createEndpoint(uri, name, inbound, null, null);
551     }
552 
553     public UMOEndpoint createEndpoint(String uri, String name, boolean inbound, String transformers)
554         throws UMOException
555     {
556         return createEndpoint(uri, name, inbound, transformers, null);
557     }
558 
559     public UMOEndpoint createEndpoint(String uri, String name, boolean inbound, UMOFilter filter)
560         throws UMOException
561     {
562         return createEndpoint(uri, name, inbound, null, filter);
563     }
564 
565     public UMOEndpoint createEndpoint(String uri,
566                                       String name,
567                                       boolean inbound,
568                                       String transformers,
569                                       UMOFilter filter) throws UMOException
570     {
571         UMOEndpoint ep = MuleEndpoint.createEndpointFromUri(new MuleEndpointURI(uri), (inbound
572                         ? UMOEndpoint.ENDPOINT_TYPE_RECEIVER : UMOEndpoint.ENDPOINT_TYPE_SENDER));
573         ep.setName(name);
574         if (transformers != null)
575         {
576             String delim = (transformers.indexOf(",") > -1 ? "," : " ");
577             ep.setTransformer(MuleObjectHelper.getTransformer(transformers, delim));
578         }
579         ep.setFilter(filter);
580         return ep;
581     }
582 
583     public UMOEndpoint registerEndpoint(String uri, String name, boolean inbound) throws UMOException
584     {
585         UMOEndpoint ep = createEndpoint(uri, name, inbound);
586         ep.initialise();
587         manager.registerEndpoint(ep);
588         return ep;
589     }
590 
591     public UMOEndpoint registerEndpoint(String uri, String name, boolean inbound, Map properties)
592         throws UMOException
593     {
594         UMOEndpoint ep = createEndpoint(uri, name, inbound);
595         ep.getProperties().putAll(properties);
596         ep.initialise();
597         manager.registerEndpoint(ep);
598         return ep;
599     }
600 
601     public UMOEndpoint registerEndpoint(String uri,
602                                         String name,
603                                         boolean inbound,
604                                         Map properties,
605                                         UMOFilter filter) throws UMOException
606     {
607         UMOEndpoint ep = createEndpoint(uri, name, inbound);
608         if (properties != null)
609         {
610             ep.getProperties().putAll(properties);
611         }
612         if (filter != null)
613         {
614             ep.setFilter(filter);
615         }
616         ep.initialise();
617         manager.registerEndpoint(ep);
618         return ep;
619     }
620 
621     public void registerModel(UMOModel model) throws UMOException
622     {
623         this.model = model;
624         manager.registerModel(model);
625     }
626 
627     public UMOManager getManager()
628     {
629         return manager;
630     }
631 
632     public UMOManager configure(String configResources) throws ConfigurationException
633     {
634         return configure(configResources, null);
635     }
636 
637     public UMOManager configure(String configResources, String startupPropertiesFile)
638         throws ConfigurationException
639     {
640         return configure(new ReaderResource[0], null);
641     }
642 
643     public UMOManager configure(ReaderResource[] configResources) throws ConfigurationException
644     {
645         return configure(configResources, null);
646     }
647 
648     public UMOManager configure(ReaderResource[] configResources, Properties startupProperties)
649         throws ConfigurationException
650     {
651         try
652         {
653             manager.start();
654         }
655         catch (UMOException e)
656         {
657             throw new ConfigurationException(e);
658         }
659         return manager;
660     }
661 
662     public boolean isConfigured()
663     {
664         return manager != null;
665     }
666 
667     protected UMOModel getModel() throws UMOException
668     {
669         if (model == null)
670         {
671             model = new SedaModel();
672             model.setName("main");
673             manager.registerModel(model);
674         }
675         return model;
676     }
677 }