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 | 0 | { |
60 | 0 | manager = MuleManager.getInstance(); |
61 | 0 | } |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public QuickConfigurationBuilder(boolean disposeCurrent) |
70 | 22 | { |
71 | 22 | if (disposeCurrent) |
72 | |
{ |
73 | 22 | disposeCurrent(); |
74 | |
} |
75 | |
|
76 | 22 | manager = MuleManager.getInstance(); |
77 | 22 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public void disposeCurrent() |
83 | |
{ |
84 | 22 | if (MuleManager.isInstanciated()) |
85 | |
{ |
86 | 0 | MuleManager.getInstance().dispose(); |
87 | |
} |
88 | 22 | } |
89 | |
|
90 | |
public void disableAdminAgent() |
91 | |
{ |
92 | 0 | MuleManager.getConfiguration().setServerUrl(StringUtils.EMPTY); |
93 | 0 | if (manager != null) |
94 | |
{ |
95 | |
try |
96 | |
{ |
97 | 0 | manager.unregisterAgent(MuleAdminAgent.AGENT_NAME); |
98 | |
} |
99 | 0 | catch (UMOException e) |
100 | |
{ |
101 | 0 | throw new RuntimeException("Exception trying to remove admin agent", e); |
102 | 0 | } |
103 | |
} |
104 | 0 | } |
105 | |
|
106 | |
public void registerModel(String modelType, String name) throws UMOException |
107 | |
{ |
108 | 0 | UMOModel model = ModelFactory.createModel(modelType); |
109 | 0 | model.setName(name); |
110 | 0 | manager.registerModel(model); |
111 | 0 | } |
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 | 22 | if (manager.isStarted()) |
127 | |
{ |
128 | 0 | throw new InitialisationException(CoreMessages.managerAlreadyStarted(), this); |
129 | |
} |
130 | 22 | if (serverUrl == null) |
131 | |
{ |
132 | 0 | serverUrl = ""; |
133 | |
} |
134 | 22 | MuleManager.getConfiguration().setServerUrl(serverUrl); |
135 | 22 | MuleManager.getConfiguration().setSynchronous(synchronous); |
136 | 22 | if (!MODEL_NOT_SET.equals(modeltype)) |
137 | |
{ |
138 | 0 | model = ModelFactory.createModel(modeltype); |
139 | |
} |
140 | |
else |
141 | |
{ |
142 | 22 | model = ModelFactory.createModel("seda"); |
143 | |
} |
144 | 22 | manager.registerModel(model); |
145 | |
|
146 | 22 | manager.start(); |
147 | 12 | 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 | 22 | 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 | 0 | if (serverConnector != null) |
180 | |
{ |
181 | 0 | manager.registerConnector(serverConnector); |
182 | |
} |
183 | |
else |
184 | |
{ |
185 | 0 | throw new IllegalArgumentException("Cannot create started manager from null serverConnector"); |
186 | |
} |
187 | |
|
188 | |
|
189 | 0 | int param = serverUrl.indexOf('?'); |
190 | 0 | if (param == -1) |
191 | |
{ |
192 | 0 | serverUrl += '?'; |
193 | |
} |
194 | |
else |
195 | |
{ |
196 | 0 | serverUrl += '&'; |
197 | |
} |
198 | |
|
199 | 0 | serverUrl += UMOEndpointURI.PROPERTY_CREATE_CONNECTOR + "=" + serverConnector.getName(); |
200 | 0 | 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 | 0 | 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 | 0 | MuleDescriptor descriptor = new MuleDescriptor(); |
241 | 0 | descriptor.setName(name); |
242 | 0 | descriptor.setImplementationInstance(component); |
243 | |
|
244 | |
|
245 | 0 | UMOEndpoint inboundProvider = null; |
246 | 0 | UMOEndpoint outboundProvider = null; |
247 | 0 | if (listenerEndpointUri != null) |
248 | |
{ |
249 | 0 | inboundProvider = TransportFactory.createEndpoint(listenerEndpointUri, |
250 | |
UMOEndpoint.ENDPOINT_TYPE_RECEIVER); |
251 | |
} |
252 | 0 | if (sendEndpointUri != null) |
253 | |
{ |
254 | 0 | outboundProvider = TransportFactory.createEndpoint(sendEndpointUri, |
255 | |
UMOEndpoint.ENDPOINT_TYPE_SENDER); |
256 | |
} |
257 | 0 | descriptor.setInboundEndpoint(inboundProvider); |
258 | 0 | descriptor.setOutboundEndpoint(outboundProvider); |
259 | |
|
260 | |
|
261 | 0 | getModel().registerComponent(descriptor); |
262 | 0 | 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 | 20 | UMOEndpoint inEndpoint = null; |
272 | 20 | UMOEndpoint outEndpoint = null; |
273 | 20 | if (inboundEndpoint != null) |
274 | |
{ |
275 | 20 | inEndpoint = manager.lookupEndpoint(inboundEndpoint); |
276 | 20 | if (inEndpoint == null) |
277 | |
{ |
278 | 20 | inEndpoint = createEndpoint(inboundEndpoint, null, true); |
279 | |
} |
280 | |
} |
281 | 20 | if (outboundEndpoint != null) |
282 | |
{ |
283 | 4 | outEndpoint = manager.lookupEndpoint(outboundEndpoint); |
284 | 4 | if (outEndpoint == null) |
285 | |
{ |
286 | 4 | outEndpoint = createEndpoint(outboundEndpoint, null, false); |
287 | |
} |
288 | |
} |
289 | 20 | UMODescriptor d = createDescriptor(implementation, name, inEndpoint, outEndpoint, properties); |
290 | 20 | 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 | 0 | UMODescriptor d = createDescriptor(implementation, name, inEndpoint, outEndpoint, properties); |
300 | 0 | 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 | 22 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | UMODescriptor d = createDescriptor(implementation, name, inboundEndpointUri, outboundEndpointUri, |
407 | |
properties); |
408 | 0 | 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 | 0 | UMOEndpointURI inEndpointUri = null; |
432 | 0 | UMOEndpointURI outEndpointUri = null; |
433 | 0 | if (inboundEndpointUri != null) |
434 | |
{ |
435 | 0 | inEndpointUri = new MuleEndpointURI(inboundEndpointUri); |
436 | |
} |
437 | 0 | if (outboundEndpointUri != null) |
438 | |
{ |
439 | 0 | outEndpointUri = new MuleEndpointURI(outboundEndpointUri); |
440 | |
} |
441 | |
|
442 | 0 | 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 | 0 | UMOEndpoint inboundEndpoint = null; |
467 | 0 | UMOEndpoint outboundEndpoint = null; |
468 | 0 | if (inboundEndpointUri != null) |
469 | |
{ |
470 | 0 | inboundEndpoint = TransportFactory.createEndpoint(inboundEndpointUri, |
471 | |
UMOEndpoint.ENDPOINT_TYPE_RECEIVER); |
472 | |
} |
473 | 0 | if (outboundEndpointUri != null) |
474 | |
{ |
475 | 0 | outboundEndpoint = TransportFactory.createEndpoint(outboundEndpointUri, |
476 | |
UMOEndpoint.ENDPOINT_TYPE_SENDER); |
477 | |
} |
478 | 0 | 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 | 22 | MuleDescriptor descriptor = new MuleDescriptor(); |
502 | 22 | descriptor.setImplementation(implementation); |
503 | 22 | descriptor.setName(name); |
504 | 22 | if (properties != null) |
505 | |
{ |
506 | 16 | descriptor.getProperties().putAll(properties); |
507 | |
} |
508 | |
|
509 | 22 | descriptor.setInboundEndpoint(inboundEndpoint); |
510 | 22 | descriptor.setOutboundEndpoint(outboundEndpoint); |
511 | |
|
512 | 22 | return descriptor; |
513 | |
} |
514 | |
|
515 | |
|
516 | |
|
517 | |
|
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
public void setContainerContext(UMOContainerContext ctx) throws UMOException |
523 | |
{ |
524 | 0 | manager.setContainerContext(ctx); |
525 | 0 | } |
526 | |
|
527 | |
|
528 | |
|
529 | |
|
530 | |
|
531 | |
|
532 | |
|
533 | |
|
534 | |
|
535 | |
|
536 | |
|
537 | |
|
538 | |
|
539 | |
public void unregisterComponent(String name) throws UMOException |
540 | |
{ |
541 | 0 | UMODescriptor descriptor = model.getDescriptor(name); |
542 | 0 | if (descriptor != null) |
543 | |
{ |
544 | 0 | getModel().unregisterComponent(descriptor); |
545 | |
} |
546 | 0 | } |
547 | |
|
548 | |
public UMOEndpoint createEndpoint(String uri, String name, boolean inbound) throws UMOException |
549 | |
{ |
550 | 26 | 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 | 0 | 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 | 0 | 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 | 26 | UMOEndpoint ep = MuleEndpoint.createEndpointFromUri(new MuleEndpointURI(uri), (inbound |
572 | |
? UMOEndpoint.ENDPOINT_TYPE_RECEIVER : UMOEndpoint.ENDPOINT_TYPE_SENDER)); |
573 | 26 | ep.setName(name); |
574 | 26 | if (transformers != null) |
575 | |
{ |
576 | 0 | String delim = (transformers.indexOf(",") > -1 ? "," : " "); |
577 | 0 | ep.setTransformer(MuleObjectHelper.getTransformer(transformers, delim)); |
578 | |
} |
579 | 26 | ep.setFilter(filter); |
580 | 26 | return ep; |
581 | |
} |
582 | |
|
583 | |
public UMOEndpoint registerEndpoint(String uri, String name, boolean inbound) throws UMOException |
584 | |
{ |
585 | 0 | UMOEndpoint ep = createEndpoint(uri, name, inbound); |
586 | 0 | ep.initialise(); |
587 | 0 | manager.registerEndpoint(ep); |
588 | 0 | return ep; |
589 | |
} |
590 | |
|
591 | |
public UMOEndpoint registerEndpoint(String uri, String name, boolean inbound, Map properties) |
592 | |
throws UMOException |
593 | |
{ |
594 | 0 | UMOEndpoint ep = createEndpoint(uri, name, inbound); |
595 | 0 | ep.getProperties().putAll(properties); |
596 | 0 | ep.initialise(); |
597 | 0 | manager.registerEndpoint(ep); |
598 | 0 | 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 | 0 | UMOEndpoint ep = createEndpoint(uri, name, inbound); |
608 | 0 | if (properties != null) |
609 | |
{ |
610 | 0 | ep.getProperties().putAll(properties); |
611 | |
} |
612 | 0 | if (filter != null) |
613 | |
{ |
614 | 0 | ep.setFilter(filter); |
615 | |
} |
616 | 0 | ep.initialise(); |
617 | 0 | manager.registerEndpoint(ep); |
618 | 0 | return ep; |
619 | |
} |
620 | |
|
621 | |
public void registerModel(UMOModel model) throws UMOException |
622 | |
{ |
623 | 0 | this.model = model; |
624 | 0 | manager.registerModel(model); |
625 | 0 | } |
626 | |
|
627 | |
public UMOManager getManager() |
628 | |
{ |
629 | 4 | return manager; |
630 | |
} |
631 | |
|
632 | |
public UMOManager configure(String configResources) throws ConfigurationException |
633 | |
{ |
634 | 0 | return configure(configResources, null); |
635 | |
} |
636 | |
|
637 | |
public UMOManager configure(String configResources, String startupPropertiesFile) |
638 | |
throws ConfigurationException |
639 | |
{ |
640 | 0 | return configure(new ReaderResource[0], null); |
641 | |
} |
642 | |
|
643 | |
public UMOManager configure(ReaderResource[] configResources) throws ConfigurationException |
644 | |
{ |
645 | 0 | return configure(configResources, null); |
646 | |
} |
647 | |
|
648 | |
public UMOManager configure(ReaderResource[] configResources, Properties startupProperties) |
649 | |
throws ConfigurationException |
650 | |
{ |
651 | |
try |
652 | |
{ |
653 | 0 | manager.start(); |
654 | |
} |
655 | 0 | catch (UMOException e) |
656 | |
{ |
657 | 0 | throw new ConfigurationException(e); |
658 | 0 | } |
659 | 0 | return manager; |
660 | |
} |
661 | |
|
662 | |
public boolean isConfigured() |
663 | |
{ |
664 | 0 | return manager != null; |
665 | |
} |
666 | |
|
667 | |
protected UMOModel getModel() throws UMOException |
668 | |
{ |
669 | 22 | if (model == null) |
670 | |
{ |
671 | 22 | model = new SedaModel(); |
672 | 22 | model.setName("main"); |
673 | 22 | manager.registerModel(model); |
674 | |
} |
675 | 22 | return model; |
676 | |
} |
677 | |
} |