1 10 11 package org.mule.impl; 12 13 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; 14 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean; 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 import org.mule.MuleException; 18 import org.mule.MuleManager; 19 import org.mule.config.i18n.Message; 20 import org.mule.config.i18n.Messages; 21 import org.mule.impl.endpoint.MuleEndpoint; 22 import org.mule.impl.endpoint.MuleEndpointURI; 23 import org.mule.providers.AbstractConnector; 24 import org.mule.providers.service.ConnectorFactory; 25 import org.mule.providers.service.ConnectorFactoryException; 26 import org.mule.umo.UMOException; 27 import org.mule.umo.UMOFilter; 28 import org.mule.umo.UMOTransactionConfig; 29 import org.mule.umo.endpoint.UMOEndpoint; 30 import org.mule.umo.endpoint.UMOEndpointURI; 31 import org.mule.umo.endpoint.UMOImmutableEndpoint; 32 import org.mule.umo.lifecycle.InitialisationException; 33 import org.mule.umo.provider.UMOConnector; 34 import org.mule.umo.security.UMOEndpointSecurityFilter; 35 import org.mule.umo.transformer.UMOTransformer; 36 import org.mule.util.ClassUtils; 37 import org.mule.util.MuleObjectHelper; 38 import org.mule.util.ObjectNameHelper; 39 import org.mule.util.StringUtils; 40 41 import java.util.Collections ; 42 import java.util.Map ; 43 44 48 public class ImmutableMuleEndpoint implements UMOImmutableEndpoint 49 { 50 53 private static final long serialVersionUID = -2431378111247771909L; 54 55 58 protected static final Log logger = LogFactory.getLog(ImmutableMuleEndpoint.class); 59 60 63 protected UMOConnector connector = null; 64 65 68 protected UMOEndpointURI endpointUri = null; 69 70 73 protected UMOTransformer transformer = null; 74 75 78 protected UMOTransformer responseTransformer = null; 79 80 83 protected String name = null; 84 85 88 protected String type = ENDPOINT_TYPE_SENDER_AND_RECEIVER; 89 90 93 protected Map properties = null; 94 95 98 protected UMOTransactionConfig transactionConfig = null; 99 100 103 protected UMOFilter filter = null; 104 105 110 protected boolean deleteUnacceptedMessages = false; 111 112 115 protected AtomicBoolean initialised = new AtomicBoolean(false); 116 117 120 protected UMOEndpointSecurityFilter securityFilter = null; 121 122 125 protected Boolean synchronous = null; 126 127 135 protected Boolean remoteSync = null; 136 137 142 protected Integer remoteSyncTimeout = null; 143 144 147 protected boolean streaming = false; 148 149 152 protected String initialState = INITIAL_STATE_STARTED; 153 154 protected String endpointEncoding = null; 155 156 159 protected int createConnector = ConnectorFactory.GET_OR_CREATE_CONNECTOR; 160 161 164 private ImmutableMuleEndpoint() 165 { 166 super(); 167 } 168 169 172 public ImmutableMuleEndpoint(String name, 173 UMOEndpointURI endpointUri, 174 UMOConnector connector, 175 UMOTransformer transformer, 176 String type, 177 int createConnector, 178 String endpointEncoding, 179 Map props) 180 { 181 this.name = name; 182 this.connector = connector; 183 this.createConnector = createConnector; 184 this.endpointEncoding = endpointEncoding; 185 this.type = type; 186 187 if (endpointUri != null) 188 { 189 this.endpointUri = new MuleEndpointURI(endpointUri); 190 } 191 192 if (transformer != null) 193 { 194 transformer.setEndpoint(this); 195 this.transformer = transformer; 196 } 197 198 this.properties = new ConcurrentHashMap(); 199 200 if (props != null) 201 { 202 this.properties.putAll(props); 203 } 204 205 if (endpointUri != null) 206 { 207 properties.putAll(endpointUri.getParams()); 208 } 209 210 if (!(this instanceof MuleEndpoint)) 213 { 214 this.properties = Collections.unmodifiableMap(this.properties); 215 } 216 217 transactionConfig = new MuleTransactionConfig(); 219 } 220 221 public ImmutableMuleEndpoint(UMOImmutableEndpoint source) 222 { 223 this(); 224 this.initFromDescriptor(source); 225 } 226 227 public ImmutableMuleEndpoint(String endpointName, boolean receiver) throws UMOException 228 { 229 this(); 230 String type = (receiver ? UMOEndpoint.ENDPOINT_TYPE_RECEIVER : UMOEndpoint.ENDPOINT_TYPE_SENDER); 231 UMOEndpoint p = getOrCreateEndpointForUri(new MuleEndpointURI(endpointName), type); 232 this.initFromDescriptor(p); 233 } 234 235 protected void initFromDescriptor(UMOImmutableEndpoint source) 236 { 237 if (this.name == null) 238 { 239 this.name = source.getName(); 240 } 241 242 if (this.endpointUri == null && source.getEndpointURI() != null) 243 { 244 this.endpointUri = new MuleEndpointURI(source.getEndpointURI()); 245 } 246 247 if (this.endpointEncoding == null) 248 { 249 this.endpointEncoding = source.getEncoding(); 250 } 251 252 if (this.connector == null) 253 { 254 this.connector = source.getConnector(); 255 } 256 257 if (this.transformer == null) 258 { 259 this.transformer = source.getTransformer(); 260 } 261 262 if (this.transformer != null) 263 { 264 this.transformer.setEndpoint(this); 265 } 266 267 if (this.responseTransformer == null) 268 { 269 this.responseTransformer = source.getResponseTransformer(); 270 } 271 272 if (responseTransformer != null) 273 { 274 this.responseTransformer.setEndpoint(this); 275 } 276 277 this.properties = new ConcurrentHashMap(); 278 279 if (source.getProperties() != null) 280 { 281 this.properties.putAll(source.getProperties()); 282 } 283 284 if (endpointUri != null && endpointUri.getParams() != null) 285 { 286 this.properties.putAll(endpointUri.getParams()); 287 } 288 289 if (!(this instanceof MuleEndpoint)) 292 { 293 this.properties = Collections.unmodifiableMap(this.properties); 294 } 295 296 this.type = source.getType(); 297 this.transactionConfig = source.getTransactionConfig(); 298 this.deleteUnacceptedMessages = source.isDeleteUnacceptedMessages(); 299 this.initialState = source.getInitialState(); 300 301 remoteSyncTimeout = new Integer (source.getRemoteSyncTimeout()); 302 remoteSync = Boolean.valueOf(source.isRemoteSync()); 303 304 filter = source.getFilter(); 305 securityFilter = source.getSecurityFilter(); 306 } 307 308 313 public UMOEndpointURI getEndpointURI() 314 { 315 return endpointUri; 316 } 317 318 public String getEncoding() 319 { 320 return endpointEncoding; 321 } 322 323 328 public String getType() 329 { 330 return type; 331 } 332 333 338 public UMOConnector getConnector() 339 { 340 return connector; 341 } 342 343 348 public String getName() 349 { 350 return name; 351 } 352 353 358 public UMOTransformer getTransformer() 359 { 360 return transformer; 361 } 362 363 368 public Map getProperties() 369 { 370 return properties; 371 } 372 373 378 public Object clone() 382 { 383 UMOEndpoint clone = new MuleEndpoint(name, endpointUri, connector, transformer, type, 384 createConnector, endpointEncoding, properties); 385 386 clone.setTransactionConfig(transactionConfig); 387 clone.setFilter(filter); 388 clone.setSecurityFilter(securityFilter); 389 390 if (remoteSync != null) 391 { 392 clone.setRemoteSync(isRemoteSync()); 393 } 394 if (remoteSyncTimeout != null) 395 { 396 clone.setRemoteSyncTimeout(getRemoteSyncTimeout()); 397 } 398 399 if (synchronous != null) 400 { 401 clone.setSynchronous(synchronous.booleanValue()); 402 } 403 404 clone.setDeleteUnacceptedMessages(deleteUnacceptedMessages); 405 406 clone.setInitialState(initialState); 407 if (initialised.get()) 408 { 409 try 410 { 411 clone.initialise(); 412 } 413 catch (InitialisationException e) 414 { 415 logger.error(e.getMessage(), e); 418 } 419 } 420 421 return clone; 422 } 423 424 429 public boolean isReadOnly() 430 { 431 return true; 432 } 433 434 public String toString() 435 { 436 return ClassUtils.getClassName(this.getClass()) + "{connector=" + connector + ", endpointUri=" 437 + endpointUri + ", transformer=" + transformer + ", name='" + name + "'" + ", type='" + type 438 + "'" + ", properties=" + properties + ", transactionConfig=" + transactionConfig 439 + ", filter=" + filter + ", deleteUnacceptedMessages=" + deleteUnacceptedMessages 440 + ", initialised=" + initialised + ", securityFilter=" + securityFilter + ", synchronous=" 441 + synchronous + ", initialState=" + initialState + ", createConnector=" + createConnector 442 + ", remoteSync=" + remoteSync + ", remoteSyncTimeout=" + remoteSyncTimeout 443 + ", endpointEncoding=" + endpointEncoding + "}"; 444 } 445 446 451 public String getProtocol() 452 { 453 return connector.getProtocol(); 454 } 455 456 461 public boolean canReceive() 462 { 463 return (getType().equals(ENDPOINT_TYPE_RECEIVER) || getType().equals( 464 ENDPOINT_TYPE_SENDER_AND_RECEIVER)); 465 } 466 467 472 public boolean canSend() 473 { 474 return (getType().equals(ENDPOINT_TYPE_SENDER) || getType().equals(ENDPOINT_TYPE_SENDER_AND_RECEIVER)); 475 } 476 477 482 public UMOTransactionConfig getTransactionConfig() 483 { 484 return transactionConfig; 485 } 486 487 public boolean equals(Object o) 488 { 489 if (this == o) 490 { 491 return true; 492 } 493 if (!(o instanceof ImmutableMuleEndpoint)) 494 { 495 return false; 496 } 497 498 final ImmutableMuleEndpoint immutableMuleProviderDescriptor = (ImmutableMuleEndpoint)o; 499 500 if (!connector.getName().equals(immutableMuleProviderDescriptor.connector.getName())) 501 { 502 return false; 503 } 504 if (endpointUri != null && immutableMuleProviderDescriptor.endpointUri != null 505 ? !endpointUri.getAddress().equals( 506 immutableMuleProviderDescriptor.endpointUri.getAddress()) 507 : immutableMuleProviderDescriptor.endpointUri != null) 508 { 509 return false; 510 } 511 if (!name.equals(immutableMuleProviderDescriptor.name)) 512 { 513 return false; 514 } 515 if (transformer != null 516 ? !transformer.equals(immutableMuleProviderDescriptor.transformer) 517 : immutableMuleProviderDescriptor.transformer != null) 518 { 519 return false; 520 } 521 if (!type.equals(immutableMuleProviderDescriptor.type)) 522 { 523 return false; 524 } 525 526 return true; 527 } 528 529 public int hashCode() 530 { 531 int result = (connector != null ? connector.hashCode() : 0); 532 result = 29 * result + (endpointUri != null ? endpointUri.hashCode() : 0); 533 result = 29 * result + (transformer != null ? transformer.hashCode() : 0); 534 result = 29 * result + (name != null ? name.hashCode() : 0); 535 return 29 * result + (type != null ? type.hashCode() : 0); 536 } 537 538 public UMOFilter getFilter() 539 { 540 return filter; 541 } 542 543 public static UMOEndpoint createEndpointFromUri(UMOEndpointURI uri, String type) throws UMOException 544 { 545 return ConnectorFactory.createEndpoint(uri, type); 546 } 547 548 public static UMOEndpoint getEndpointFromUri(String uri) 549 { 550 UMOEndpoint endpoint = null; 551 if (uri != null) 552 { 553 String endpointString = MuleManager.getInstance().lookupEndpointIdentifier(uri, uri); 554 endpoint = MuleManager.getInstance().lookupEndpoint(endpointString); 555 } 556 return endpoint; 557 } 558 559 public static UMOEndpoint getEndpointFromUri(UMOEndpointURI uri) throws UMOException 560 { 561 String endpointName = uri.getEndpointName(); 562 if (endpointName != null) 563 { 564 String endpointString = MuleManager.getInstance().lookupEndpointIdentifier(endpointName, 565 endpointName); 566 UMOEndpoint endpoint = MuleManager.getInstance().lookupEndpoint(endpointString); 567 if (endpoint != null) 568 { 569 if (StringUtils.isNotEmpty(uri.getAddress())) 570 { 571 endpoint.setEndpointURI(uri); 572 } 573 } 574 return endpoint; 575 } 576 577 return null; 578 } 579 580 public static UMOEndpoint getOrCreateEndpointForUri(String uriIdentifier, String type) 581 throws UMOException 582 { 583 UMOEndpoint endpoint = getEndpointFromUri(uriIdentifier); 584 if (endpoint == null) 585 { 586 endpoint = createEndpointFromUri(new MuleEndpointURI(uriIdentifier), type); 587 } 588 else 589 { 590 if (endpoint.getType().equals(UMOEndpoint.ENDPOINT_TYPE_SENDER_AND_RECEIVER)) 591 { 592 endpoint.setType(type); 593 } 594 else if (!endpoint.getType().equals(type)) 595 { 596 throw new IllegalArgumentException ("Endpoint matching: " + uriIdentifier 597 + " is not of type: " + type + ". It is of type: " 598 + endpoint.getType()); 599 600 } 601 } 602 return endpoint; 603 } 604 605 public static UMOEndpoint getOrCreateEndpointForUri(UMOEndpointURI uri, String type) throws UMOException 606 { 607 UMOEndpoint endpoint = getEndpointFromUri(uri); 608 if (endpoint == null) 609 { 610 endpoint = createEndpointFromUri(uri, type); 611 } 612 return endpoint; 613 } 614 615 public boolean isDeleteUnacceptedMessages() 616 { 617 return deleteUnacceptedMessages; 618 } 619 620 public void initialise() throws InitialisationException 621 { 622 if (initialised.get()) 623 { 624 logger.debug("Already initialised: " + toString()); 625 return; 626 } 627 if (connector == null) 628 { 629 if (endpointUri.getConnectorName() != null) 630 { 631 connector = MuleManager.getInstance().lookupConnector(endpointUri.getConnectorName()); 632 if (connector == null) 633 { 634 throw new IllegalArgumentException ("Connector not found: " 635 + endpointUri.getConnectorName()); 636 } 637 } 638 else 639 { 640 try 641 { 642 connector = ConnectorFactory.getOrCreateConnectorByProtocol(this); 643 if (connector == null) 644 { 645 throw new InitialisationException(new Message( 646 Messages.CONNECTOR_WITH_PROTOCOL_X_NOT_REGISTERED, endpointUri.getScheme()), this); 647 } 648 } 649 catch (ConnectorFactoryException e) 650 { 651 throw new InitialisationException(new Message( 652 Messages.FAILED_TO_CREATE_CONNECTOR_FROM_URI_X, endpointUri), e, this); 653 } 654 } 655 656 if (endpointUri.getEndpointName() != null && name == null) 657 { 658 name = endpointUri.getEndpointName(); 659 } 660 } 661 name = ObjectNameHelper.getEndpointName(this); 662 663 String sync = endpointUri.getParams().getProperty("synchronous", null); 664 if (sync != null) 665 { 666 synchronous = Boolean.valueOf(sync); 667 } 668 if (properties != null && endpointUri.getParams() != null) 669 { 670 properties.putAll(endpointUri.getParams()); 671 } 672 673 if (endpointUri.getTransformers() != null) 674 { 675 try 676 { 677 transformer = MuleObjectHelper.getTransformer(endpointUri.getTransformers(), ","); 678 } 679 catch (MuleException e) 680 { 681 throw new InitialisationException(e, this); 682 } 683 } 684 685 if (transformer == null) 686 { 687 if (connector instanceof AbstractConnector) 688 { 689 if (UMOEndpoint.ENDPOINT_TYPE_SENDER.equals(type)) 690 { 691 transformer = ((AbstractConnector)connector).getDefaultOutboundTransformer(); 692 } 693 else 694 { 695 transformer = ((AbstractConnector)connector).getDefaultInboundTransformer(); 696 } 697 } 698 } 699 if (transformer != null) 700 { 701 transformer.setEndpoint(this); 702 } 703 704 if (endpointUri.getResponseTransformers() != null) 705 { 706 try 707 { 708 responseTransformer = MuleObjectHelper.getTransformer(endpointUri.getResponseTransformers(), 709 ","); 710 } 711 catch (MuleException e) 712 { 713 throw new InitialisationException(e, this); 714 } 715 } 716 if (responseTransformer == null) 717 { 718 if (connector instanceof AbstractConnector) 719 { 720 responseTransformer = ((AbstractConnector)connector).getDefaultResponseTransformer(); 721 } 722 } 723 if (responseTransformer != null) 724 { 725 responseTransformer.setEndpoint(this); 726 } 727 728 if (securityFilter != null) 729 { 730 securityFilter.setEndpoint(this); 731 securityFilter.initialise(); 732 } 733 734 String rs = (String )endpointUri.getParams().remove("remoteSync"); 736 if (rs != null) 737 { 738 remoteSync = Boolean.valueOf(rs); 739 } 740 741 String rsTimeout = (String )endpointUri.getParams().remove("remoteSyncTimeout"); 742 if (rsTimeout != null) 743 { 744 remoteSyncTimeout = Integer.valueOf(rsTimeout); 745 } 746 747 initialised.set(true); 748 } 749 750 758 public UMOEndpointSecurityFilter getSecurityFilter() 759 { 760 return securityFilter; 761 } 762 763 771 public boolean isSynchronous() 772 { 773 if (synchronous == null) 774 { 775 return MuleManager.getConfiguration().isSynchronous(); 776 } 777 return synchronous.booleanValue(); 778 } 779 780 public boolean isSynchronousSet() 781 { 782 return (synchronous != null); 783 } 784 785 public int getCreateConnector() 786 { 787 return createConnector; 788 } 789 790 798 public boolean isRemoteSync() 799 { 800 if (remoteSync == null) 801 { 802 if (connector == null || connector.isRemoteSyncEnabled()) 803 { 804 remoteSync = Boolean.valueOf(MuleManager.getConfiguration().isRemoteSync()); 805 } 806 else 807 { 808 remoteSync = Boolean.FALSE; 809 } 810 } 811 return remoteSync.booleanValue(); 812 } 813 814 819 public int getRemoteSyncTimeout() 820 { 821 if (remoteSyncTimeout == null) 822 { 823 remoteSyncTimeout = new Integer (MuleManager.getConfiguration().getSynchronousEventTimeout()); 824 } 825 return remoteSyncTimeout.intValue(); 826 } 827 828 834 public String getInitialState() 835 { 836 return initialState; 837 } 838 839 public UMOTransformer getResponseTransformer() 840 { 841 return responseTransformer; 842 } 843 844 849 public boolean isStreaming() 850 { 851 return streaming; 852 } 853 854 public Object getProperty(Object key) 855 { 856 Object value = properties.get(key); 857 if (value == null) 858 { 859 value = endpointUri.getParams().get(key); 860 } 861 return value; 862 } 863 } 864 | Popular Tags |