1
2
3
4
5
6
7
8
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
44
45
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
57
58 public QuickConfigurationBuilder()
59 {
60 manager = MuleManager.getInstance();
61 }
62
63
64
65
66
67
68
69 public QuickConfigurationBuilder(boolean disposeCurrent)
70 {
71 if (disposeCurrent)
72 {
73 disposeCurrent();
74 }
75
76 manager = MuleManager.getInstance();
77 }
78
79
80
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
115
116
117
118
119
120
121
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
152
153
154
155
156
157
158
159
160 public UMOManager createStartedManager(boolean synchronous, String serverUrl) throws UMOException
161 {
162 return createStartedManager(synchronous, serverUrl, MODEL_NOT_SET);
163 }
164
165
166
167
168
169
170
171
172
173
174
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
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
205
206
207
208
209
210
211
212
213
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
224
225
226
227
228
229
230
231
232
233
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
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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318 public UMOComponent registerComponent(UMODescriptor descriptor) throws UMOException
319 {
320 return getModel().registerComponent(descriptor);
321 }
322
323
324
325
326
327
328
329
330
331
332
333
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
344
345
346
347
348
349
350
351
352
353
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
365
366
367
368
369
370
371
372
373
374
375
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
387
388
389
390
391
392
393
394
395
396
397
398
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
413
414
415
416
417
418
419
420
421
422
423
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
447
448
449
450
451
452
453
454
455
456
457
458
459 public UMODescriptor createDescriptor(String implementation,
460 String name,
461 UMOEndpointURI inboundEndpointUri,
462 UMOEndpointURI outboundEndpointUri,
463 Map properties) throws UMOException
464 {
465
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
483
484
485
486
487
488
489
490
491
492
493
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
517
518
519
520
521
522 public void setContainerContext(UMOContainerContext ctx) throws UMOException
523 {
524 manager.setContainerContext(ctx);
525 }
526
527
528
529
530
531
532
533
534
535
536
537
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 }