1 10 11 package org.mule.impl; 12 13 import edu.emory.mathcs.backport.java.util.concurrent.Callable; 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.mule.MuleManager; 17 import org.mule.config.MuleProperties; 18 import org.mule.config.i18n.Message; 19 import org.mule.config.i18n.Messages; 20 import org.mule.impl.endpoint.MuleEndpoint; 21 import org.mule.transaction.TransactionCoordination; 22 import org.mule.umo.FutureMessageResult; 23 import org.mule.umo.TransactionException; 24 import org.mule.umo.UMODescriptor; 25 import org.mule.umo.UMOEvent; 26 import org.mule.umo.UMOEventContext; 27 import org.mule.umo.UMOException; 28 import org.mule.umo.UMOMessage; 29 import org.mule.umo.UMOSession; 30 import org.mule.umo.UMOTransaction; 31 import org.mule.umo.endpoint.UMOEndpoint; 32 import org.mule.umo.endpoint.UMOEndpointURI; 33 import org.mule.umo.endpoint.UMOImmutableEndpoint; 34 import org.mule.umo.transformer.TransformerException; 35 36 import java.io.OutputStream ; 37 38 43 public class MuleEventContext implements UMOEventContext 44 { 45 48 protected static final Log logger = LogFactory.getLog(MuleEventContext.class); 49 50 private final UMOEvent event; 51 private final UMOSession session; 52 53 MuleEventContext(UMOEvent event) 54 { 55 this.event = event; 56 this.session = event.getSession(); 57 } 58 59 64 public UMOMessage getMessage() 65 { 66 return event.getMessage(); 67 } 68 69 76 public byte[] getMessageAsBytes() throws UMOException 77 { 78 return event.getMessageAsBytes(); 79 } 80 81 91 public Object getTransformedMessage() throws TransformerException 92 { 93 return event.getTransformedMessage(); 94 } 95 96 110 public Object getTransformedMessage(Class expectedType) throws TransformerException 111 { 112 Object message = getTransformedMessage(); 113 if (expectedType != null && expectedType.isAssignableFrom(message.getClass())) 114 { 115 return message; 116 } 117 else 118 { 119 throw new TransformerException(new Message(Messages.TRANSFORM_ON_X_NOT_OF_SPECIFIED_TYPE_X, 120 this.getComponentDescriptor().getName(), expectedType), this.event.getEndpoint() 121 .getTransformer()); 122 } 123 } 124 125 136 public byte[] getTransformedMessageAsBytes() throws TransformerException 137 { 138 return event.getTransformedMessageAsBytes(); 139 } 140 141 152 public String getTransformedMessageAsString(String encoding) throws TransformerException 153 { 154 return event.getTransformedMessageAsString(encoding); 155 } 156 157 164 public String getMessageAsString(String encoding) throws UMOException 165 { 166 return event.getMessageAsString(encoding); 167 } 168 169 181 public String getTransformedMessageAsString() throws TransformerException 182 { 183 return event.getTransformedMessageAsString(); 184 } 185 186 194 public String getMessageAsString() throws UMOException 195 { 196 return event.getMessageAsString(); 197 } 198 199 205 public UMOTransaction getCurrentTransaction() 206 { 207 return TransactionCoordination.getInstance().getTransaction(); 208 } 209 210 public void markTransactionForRollback() throws TransactionException 211 { 212 if (getCurrentTransaction() != null) 213 { 214 getCurrentTransaction().setRollbackOnly(); 215 } 216 } 217 218 226 public UMOMessage sendEvent(Object message) throws UMOException 227 { 228 return sendEvent(new MuleMessage(message, event.getMessage())); 229 } 230 231 242 public UMOMessage sendEvent(UMOMessage message, UMOEndpoint endpoint) throws UMOException 243 { 244 setRemoteSync(message, endpoint); 246 return session.sendEvent(message, endpoint); 247 } 248 249 259 public UMOMessage sendEvent(UMOMessage message) throws UMOException 260 { 261 setRemoteSync(message, event.getEndpoint()); 264 return session.sendEvent(message); 265 } 266 267 278 public UMOMessage sendEvent(UMOMessage message, UMOEndpointURI endpointUri) throws UMOException 279 { 280 UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(endpointUri, 281 UMOEndpoint.ENDPOINT_TYPE_SENDER); 282 283 setRemoteSync(message, endpoint); 286 return session.sendEvent(message, endpoint); 287 } 288 289 305 public FutureMessageResult sendEventAsync(final Object message, final int timeout) throws UMOException 306 { 307 Callable callable = new Callable() 308 { 309 public Object call() throws Exception 310 { 311 UMOMessage umoMessage = new MuleMessage(message, event.getMessage()); 312 umoMessage.setBooleanProperty(MuleProperties.MULE_REMOTE_SYNC_PROPERTY, true); 313 umoMessage.setIntProperty(MuleProperties.MULE_EVENT_TIMEOUT_PROPERTY, timeout); 314 return sendEvent(umoMessage); 315 } 316 }; 317 318 FutureMessageResult result = new FutureMessageResult(callable); 319 result.execute(); 321 return result; 322 } 323 324 340 public FutureMessageResult sendEventAsync(final UMOMessage message, final int timeout) 341 throws UMOException 342 { 343 Callable callable = new Callable() 344 { 345 public Object call() throws Exception 346 { 347 message.setBooleanProperty(MuleProperties.MULE_REMOTE_SYNC_PROPERTY, true); 348 message.setIntProperty(MuleProperties.MULE_EVENT_TIMEOUT_PROPERTY, timeout); 349 return sendEvent(message); 350 } 351 }; 352 353 FutureMessageResult result = new FutureMessageResult(callable); 354 result.execute(); 356 return result; 357 } 358 359 376 public FutureMessageResult sendEventAsync(final UMOMessage message, 377 final UMOEndpointURI endpointUri, 378 final int timeout) throws UMOException 379 { 380 Callable callable = new Callable() 381 { 382 public Object call() throws Exception 383 { 384 message.setBooleanProperty(MuleProperties.MULE_REMOTE_SYNC_PROPERTY, true); 385 message.setIntProperty(MuleProperties.MULE_EVENT_TIMEOUT_PROPERTY, timeout); 386 return sendEvent(message, endpointUri); 387 } 388 }; 389 390 FutureMessageResult result = new FutureMessageResult(callable); 391 result.execute(); 393 return result; 394 } 395 396 415 public FutureMessageResult sendEventAsync(final UMOMessage message, 416 final String endpointName, 417 final int timeout) throws UMOException 418 { 419 Callable callable = new Callable() 420 { 421 public Object call() throws Exception 422 { 423 message.setBooleanProperty(MuleProperties.MULE_REMOTE_SYNC_PROPERTY, true); 424 message.setIntProperty(MuleProperties.MULE_EVENT_TIMEOUT_PROPERTY, timeout); 425 return sendEvent(message, endpointName); 426 } 427 }; 428 429 FutureMessageResult result = new FutureMessageResult(callable); 430 result.execute(); 432 return result; 433 } 434 435 448 public UMOMessage sendEvent(UMOMessage message, String endpointName) throws UMOException 449 { 450 UMOEndpoint endpoint = MuleManager.getInstance().lookupEndpoint(endpointName); 451 setRemoteSync(message, endpoint); 452 return session.sendEvent(message, endpoint); 453 } 454 455 463 public void dispatchEvent(Object message) throws UMOException 464 { 465 session.dispatchEvent(new MuleMessage(message, event.getMessage())); 466 } 467 468 476 public void dispatchEvent(UMOMessage message) throws UMOException 477 { 478 session.dispatchEvent(message); 479 } 480 481 492 public void dispatchEvent(UMOMessage message, UMOEndpointURI endpointUri) throws UMOException 493 { 494 UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(endpointUri, 495 UMOEndpoint.ENDPOINT_TYPE_SENDER); 496 session.dispatchEvent(message, endpoint); 497 } 498 499 511 public void dispatchEvent(UMOMessage message, String endpointName) throws UMOException 512 { 513 session.dispatchEvent(message, endpointName); 514 } 515 516 526 public void dispatchEvent(UMOMessage message, UMOEndpoint endpoint) throws UMOException 527 { 528 session.dispatchEvent(message, endpoint); 529 } 530 531 540 public UMOMessage receiveEvent(UMOEndpoint endpoint, long timeout) throws UMOException 541 { 542 return session.receiveEvent(endpoint, timeout); 543 } 544 545 554 public UMOMessage receiveEvent(String endpointName, long timeout) throws UMOException 555 { 556 return session.receiveEvent(endpointName, timeout); 557 } 558 559 567 public UMOMessage receiveEvent(UMOEndpointURI endpointUri, long timeout) throws UMOException 568 { 569 UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(endpointUri, 570 UMOEndpoint.ENDPOINT_TYPE_SENDER); 571 return session.receiveEvent(endpoint, timeout); 572 } 573 574 577 public UMODescriptor getComponentDescriptor() 578 { 579 if (event.getComponent() != null) 580 { 581 return event.getComponent().getDescriptor(); 582 } 583 else 584 { 585 return null; 586 } 587 } 588 589 596 public Object getProperty(String name) 597 { 598 return event.getMessage().getProperty(name); 599 } 600 601 609 public Object getProperty(String name, Object defaultValue) 610 { 611 return event.getMessage().getProperty(name, defaultValue); 612 } 613 614 623 public int getIntProperty(String name, int defaultValue) 624 { 625 return event.getMessage().getIntProperty(name, defaultValue); 626 } 627 628 637 public long getLongProperty(String name, long defaultValue) 638 { 639 return event.getMessage().getLongProperty(name, defaultValue); 640 } 641 642 651 public double getDoubleProperty(String name, double defaultValue) 652 { 653 return event.getMessage().getDoubleProperty(name, defaultValue); 654 } 655 656 665 public boolean getBooleanProperty(String name, boolean defaultValue) 666 { 667 return event.getMessage().getBooleanProperty(name, defaultValue); 668 } 669 670 677 public void setProperty(String name, Object value) 678 { 679 event.getMessage().setProperty(name, value); 680 } 681 682 690 public void setBooleanProperty(String name, boolean value) 691 { 692 event.getMessage().setBooleanProperty(name, value); 693 } 694 695 703 public void setIntProperty(String name, int value) 704 { 705 event.getMessage().setIntProperty(name, value); 706 } 707 708 716 public void setLongProperty(String name, long value) 717 { 718 event.getMessage().setLongProperty(name, value); 719 } 720 721 729 public void setDoubleProperty(String name, double value) 730 { 731 event.getMessage().setDoubleProperty(name, value); 732 } 733 734 749 public boolean isStopFurtherProcessing() 750 { 751 return RequestContext.getEvent().isStopFurtherProcessing(); 752 } 753 754 766 public void setStopFurtherProcessing(boolean stopFurtherProcessing) 767 { 768 RequestContext.getEvent().setStopFurtherProcessing(stopFurtherProcessing); 769 } 770 771 778 public OutputStream getOutputStream() 779 { 780 return event.getOutputStream(); 781 } 782 783 788 public boolean isSynchronous() 789 { 790 return event.isSynchronous(); 791 } 792 793 public UMOEndpointURI getEndpointURI() 794 { 795 return event.getEndpoint().getEndpointURI(); 796 } 797 798 805 public UMOTransaction getTransaction() 806 { 807 return TransactionCoordination.getInstance().getTransaction(); 808 } 809 810 815 public int getTimeout() 816 { 817 return event.getTimeout(); 818 } 819 820 private void setRemoteSync(UMOMessage message, UMOImmutableEndpoint endpoint) 821 { 822 if (endpoint.isRemoteSync()) 823 { 824 if (getTransaction() == null) 825 { 826 message.setBooleanProperty(MuleProperties.MULE_REMOTE_SYNC_PROPERTY, true); 827 } 828 else 829 { 830 throw new IllegalStateException ( 831 new Message(Messages.CANNOT_USE_TX_AND_REMOTE_SYNC).getMessage()); 832 } 833 } 834 } 835 836 840 public String getStringProperty(String name) 841 { 842 Object result = getProperty(name); 843 if (result == null) 844 { 845 return null; 846 } 847 else 848 { 849 return result.toString(); 850 } 851 } 852 853 858 public String getStringProperty(String name, String defaultValue) 859 { 860 String result = getStringProperty(name); 861 if (result == null) 862 { 863 result = defaultValue; 864 } 865 return result; 866 } 867 868 873 public boolean isStreaming() 874 { 875 return event.getEndpoint().isStreaming(); 876 } 877 878 886 public String getEncoding() 887 { 888 return event.getEncoding(); 889 } 890 891 public UMOSession getSession() 892 { 893 return event.getSession(); 894 } 895 896 public String toString() 897 { 898 return event.toString(); 899 } 900 } 901 | Popular Tags |