1 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 ; 40 import java.util.Properties ; 41 42 50 public class QuickConfigurationBuilder implements ConfigurationBuilder 51 { 52 private static final String MODEL_NOT_SET = "not set"; 53 54 private UMOManager manager; 55 56 59 public QuickConfigurationBuilder() 60 { 61 manager = MuleManager.getInstance(); 62 } 63 64 70 public QuickConfigurationBuilder(boolean disposeCurrent) 71 { 72 if (disposeCurrent) 73 { 74 disposeCurrent(); 75 } 76 77 manager = MuleManager.getInstance(); 78 } 79 80 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 } 104 } 105 } 106 107 public void setModel(String model) throws UMOException 108 { 109 manager.setModel(ModelFactory.createModel(model)); 110 } 111 112 122 public UMOManager createStartedManager(boolean synchronous, String serverUrl, String 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 153 public UMOManager createStartedManager(boolean synchronous, String serverUrl) throws UMOException 154 { 155 return createStartedManager(synchronous, serverUrl, MODEL_NOT_SET); 156 } 157 158 169 public UMOManager createStartedManager(boolean synchronous, String serverUrl, UMOConnector serverConnector) 170 throws UMOException 171 { 172 if (serverConnector != null) 173 { 174 manager.registerConnector(serverConnector); 175 } 176 else 177 { 178 throw new IllegalArgumentException ("Cannot create started manager from null serverConnector"); 179 } 180 181 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 208 public UMODescriptor registerComponentInstance(Object component, 209 String name, 210 UMOEndpointURI listenerEndpointUri) throws UMOException 211 { 212 return registerComponentInstance(component, name, listenerEndpointUri, null); 213 } 214 215 228 public UMODescriptor registerComponentInstance(Object component, 229 String 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 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 manager.getModel().registerComponent(descriptor); 255 return descriptor; 256 } 257 258 public UMOComponent registerComponent(String implementation, 259 String name, 260 String inboundEndpoint, 261 String outboundEndpoint, 262 Map 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 implementation, 287 String name, 288 UMOEndpoint inEndpoint, 289 UMOEndpoint outEndpoint, 290 Map properties) throws UMOException 291 { 292 UMODescriptor d = createDescriptor(implementation, name, inEndpoint, outEndpoint, properties); 293 return registerComponent(d); 294 } 295 296 311 public UMOComponent registerComponent(UMODescriptor descriptor) throws UMOException 312 { 313 return manager.getModel().registerComponent(descriptor); 314 } 315 316 328 public UMOComponent registerComponent(String implementation, 329 String name, 330 UMOEndpointURI inboundEndpointUri) throws UMOException 331 { 332 return registerComponent(implementation, name, inboundEndpointUri, null, null); 333 } 334 335 348 public UMOComponent registerComponent(String implementation, 349 String name, 350 UMOEndpointURI inboundEndpointUri, 351 Map properties) throws UMOException 352 { 353 return registerComponent(implementation, name, inboundEndpointUri, null, properties); 354 } 355 356 370 public UMOComponent registerComponent(String implementation, 371 String name, 372 UMOEndpointURI inboundEndpointUri, 373 UMOEndpointURI outboundEndpointUri) throws UMOException 374 { 375 return registerComponent(implementation, name, inboundEndpointUri, outboundEndpointUri, null); 376 } 377 378 393 public UMOComponent registerComponent(String implementation, 394 String name, 395 UMOEndpointURI inboundEndpointUri, 396 UMOEndpointURI outboundEndpointUri, 397 Map properties) throws UMOException 398 { 399 UMODescriptor d = createDescriptor(implementation, name, inboundEndpointUri, outboundEndpointUri, 400 properties); 401 return manager.getModel().registerComponent(d); 402 } 403 404 418 public UMODescriptor createDescriptor(String implementation, 419 String name, 420 String inboundEndpointUri, 421 String outboundEndpointUri, 422 Map 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 452 public UMODescriptor createDescriptor(String implementation, 453 String name, 454 UMOEndpointURI inboundEndpointUri, 455 UMOEndpointURI outboundEndpointUri, 456 Map properties) throws UMOException 457 { 458 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 488 public UMODescriptor createDescriptor(String implementation, 489 String name, 490 UMOEndpoint inboundEndpoint, 491 UMOEndpoint outboundEndpoint, 492 Map 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 515 public void setContainerContext(UMOContainerContext ctx) throws UMOException 516 { 517 manager.setContainerContext(ctx); 518 } 519 520 532 public void unregisterComponent(String 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 uri, String name, boolean inbound) throws UMOException 542 { 543 return createEndpoint(uri, name, inbound, null, null); 544 } 545 546 public UMOEndpoint createEndpoint(String uri, String name, boolean inbound, String transformers) 547 throws UMOException 548 { 549 return createEndpoint(uri, name, inbound, transformers, null); 550 } 551 552 public UMOEndpoint createEndpoint(String uri, String name, boolean inbound, UMOFilter filter) 553 throws UMOException 554 { 555 return createEndpoint(uri, name, inbound, null, filter); 556 } 557 558 public UMOEndpoint createEndpoint(String uri, 559 String name, 560 boolean inbound, 561 String 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 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 uri, String 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 uri, String name, boolean inbound, Map 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 uri, 595 String name, 596 boolean inbound, 597 Map 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 configResources) throws ConfigurationException 625 { 626 return configure(configResources, null); 627 } 628 629 public UMOManager configure(String configResources, String 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 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 |