1 10 11 package org.mule.impl; 12 13 import org.apache.commons.beanutils.PropertyUtils; 14 import org.apache.commons.collections.MapUtils; 15 import org.apache.commons.lang.SerializationUtils; 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.mule.MuleException; 19 import org.mule.MuleManager; 20 import org.mule.config.i18n.Message; 21 import org.mule.config.i18n.Messages; 22 import org.mule.config.MuleProperties; 23 import org.mule.impl.endpoint.MuleEndpoint; 24 import org.mule.impl.security.MuleCredentials; 25 import org.mule.umo.UMOComponent; 26 import org.mule.umo.UMOEvent; 27 import org.mule.umo.UMOException; 28 import org.mule.umo.UMOMessage; 29 import org.mule.umo.UMOSession; 30 import org.mule.umo.endpoint.UMOEndpoint; 31 import org.mule.umo.endpoint.UMOImmutableEndpoint; 32 import org.mule.umo.security.UMOCredentials; 33 import org.mule.umo.transformer.TransformerException; 34 import org.mule.umo.transformer.UMOTransformer; 35 import org.mule.util.UUID; 36 37 import java.io.IOException ; 38 import java.io.ObjectInputStream ; 39 import java.io.ObjectOutputStream ; 40 import java.io.OutputStream ; 41 import java.io.Serializable ; 42 import java.io.UnsupportedEncodingException ; 43 import java.util.EventObject ; 44 import java.util.Iterator ; 45 46 54 55 public class MuleEvent extends EventObject implements UMOEvent 56 { 57 60 private static final long serialVersionUID = 7568207722883309919L; 61 64 protected transient Log logger = LogFactory.getLog(getClass()); 65 68 private transient UMOImmutableEndpoint endpoint = null; 69 70 73 private String id = null; 74 75 78 private UMOMessage message = null; 79 80 private transient UMOSession session; 81 82 private boolean stopFurtherProcessing = false; 83 84 private boolean synchronous = false; 85 86 private int timeout = TIMEOUT_NOT_SET_VALUE; 87 88 private transient ResponseOutputStream outputStream = null; 89 90 private transient Object transformedMessage = null; 91 92 private UMOCredentials credentials = null; 93 94 protected String [] ignoredPropertyOverrides = new String []{MuleProperties.MULE_METHOD_PROPERTY}; 95 96 101 public MuleEvent(UMOMessage message, 102 UMOImmutableEndpoint endpoint, 103 UMOComponent component, 104 UMOEvent previousEvent) 105 { 106 super(message.getPayload()); 107 this.message = message; 108 this.id = generateEventId(); 109 this.session = previousEvent.getSession(); 110 ((MuleSession)session).setComponent(component); 111 this.endpoint = endpoint; 112 this.synchronous = previousEvent.isSynchronous(); 113 this.timeout = previousEvent.getTimeout(); 114 this.outputStream = (ResponseOutputStream)previousEvent.getOutputStream(); 115 fillProperties(previousEvent); 116 } 117 118 public MuleEvent(UMOMessage message, 119 UMOImmutableEndpoint endpoint, 120 UMOSession session, 121 boolean synchronous) 122 { 123 this(message, endpoint, session, synchronous, null); 124 } 125 126 134 public MuleEvent(UMOMessage message, 135 UMOImmutableEndpoint endpoint, 136 UMOSession session, 137 boolean synchronous, 138 ResponseOutputStream outputStream) 139 { 140 super(message.getPayload()); 141 this.message = message; 142 this.endpoint = endpoint; 143 this.session = session; 144 this.id = generateEventId(); 145 this.synchronous = synchronous; 146 this.outputStream = outputStream; 147 fillProperties(null); 148 } 149 150 158 public MuleEvent(UMOMessage message, 159 UMOImmutableEndpoint endpoint, 160 UMOSession session, 161 String eventId, 162 boolean synchronous) 163 { 164 super(message.getPayload()); 165 this.message = message; 166 this.endpoint = endpoint; 167 this.session = session; 168 this.id = eventId; 169 this.synchronous = synchronous; 170 fillProperties(null); 171 } 172 173 179 public MuleEvent(UMOMessage message, UMOEvent rewriteEvent) 180 { 181 super(message.getPayload()); 182 this.message = message; 183 this.id = rewriteEvent.getId(); 184 this.session = rewriteEvent.getSession(); 185 ((MuleSession)session).setComponent(rewriteEvent.getComponent()); 186 this.endpoint = rewriteEvent.getEndpoint(); 187 this.synchronous = rewriteEvent.isSynchronous(); 188 this.timeout = rewriteEvent.getTimeout(); 189 this.outputStream = (ResponseOutputStream)rewriteEvent.getOutputStream(); 190 if (rewriteEvent instanceof MuleEvent) 191 { 192 this.transformedMessage = ((MuleEvent)rewriteEvent).getCachedMessage(); 193 } 194 fillProperties(rewriteEvent); 195 } 196 197 protected void fillProperties(UMOEvent previousEvent) 198 { 199 if (previousEvent != null) 200 { 201 UMOMessage msg = previousEvent.getMessage(); 202 synchronized (msg) 203 { 204 for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext();) 205 { 206 String prop = (String )iterator.next(); 207 Object value = msg.getProperty(prop); 208 if (!ignoreProperty(prop)) 210 { 211 message.setProperty(prop, value); 212 } 213 214 if (logger.isDebugEnabled()) 215 { 216 Object currentValue = message.getProperty(prop); 217 if (!value.equals(currentValue)) 218 { 219 logger.warn("Property on the current message " + prop + "=" + currentValue 220 + " overrides property on the previous event: " + prop + "=" + value); 221 } 222 } 223 } 224 } 225 } 226 227 if (endpoint != null && endpoint.getProperties() != null) 228 { 229 for (Iterator iterator = endpoint.getProperties().keySet().iterator(); iterator.hasNext();) 230 { 231 String prop = (String )iterator.next(); 232 Object value = endpoint.getProperties().get(prop); 233 if (!ignoreProperty(prop)) 235 { 236 message.setProperty(prop, value); 237 } 238 239 if (logger.isDebugEnabled()) 240 { 241 Object currentValue = message.getProperty(prop); 242 if (!value.equals(currentValue)) 243 { 244 logger.warn("Property on the current message " + prop + "=" + currentValue 245 + " overrides property on the endpoint: " + prop + "=" + value); 246 } 247 } 248 } 249 } 250 251 setCredentials(); 252 } 253 254 265 protected boolean ignoreProperty(String key) 266 { 267 if (key == null) 268 { 269 return true; 270 } 271 272 for (int i = 0; i < ignoredPropertyOverrides.length; i++) 273 { 274 if (key.equals(ignoredPropertyOverrides[i])) 275 { 276 return false; 277 } 278 } 279 Object value = message.getProperty(key); 280 281 if (value != null) 282 { 283 return true; 284 } 285 286 return false; 287 } 288 289 protected void setCredentials() 290 { 291 if (endpoint.getEndpointURI().getUserInfo() != null) 292 { 293 final String userName = endpoint.getEndpointURI().getUsername(); 294 final String password = endpoint.getEndpointURI().getPassword(); 295 if (password != null && userName != null) 296 { 297 credentials = new MuleCredentials(userName, password.toCharArray()); 298 } 299 } 300 } 301 302 public UMOCredentials getCredentials() 303 { 304 return credentials; 305 } 306 307 Object getCachedMessage() 308 { 309 return transformedMessage; 310 } 311 312 317 public UMOMessage getMessage() 318 { 319 return message; 320 } 321 322 327 public byte[] getMessageAsBytes() throws MuleException 328 { 329 try 330 { 331 return message.getPayloadAsBytes(); 332 } 333 catch (Exception e) 334 { 335 throw new MuleException(new Message(Messages.CANT_READ_PAYLOAD_AS_BYTES_TYPE_IS_X, 336 message.getPayload().getClass().getName()), e); 337 } 338 } 339 340 345 public Object getTransformedMessage() throws TransformerException 346 { 347 if (isStreaming()) 348 { 349 return message.getAdapter(); 350 } 351 if (transformedMessage == null) 352 { 353 UMOTransformer tran = endpoint.getTransformer(); 354 if (tran != null) 355 { 356 transformedMessage = tran.transform(message.getPayload()); 357 } 358 else 359 { 360 transformedMessage = message.getPayload(); 361 } 362 } 363 return transformedMessage; 364 } 365 366 377 public byte[] getTransformedMessageAsBytes() throws TransformerException 378 { 379 Object msg = getTransformedMessage(); 380 if (msg instanceof byte[]) 381 { 382 return (byte[])msg; 383 } 384 else if (msg instanceof String ) 385 { 386 try 387 { 388 return msg.toString().getBytes(getEncoding()); 389 } 390 catch (UnsupportedEncodingException e) 391 { 392 throw new TransformerException(new Message(Messages.TRANSFORM_FAILED_FROM_X, msg.getClass() 393 .getName(), e)); 394 } 395 } 396 else if (msg instanceof Serializable ) 397 { 398 try 399 { 400 return SerializationUtils.serialize((Serializable )msg); 401 } 402 catch (Exception e) 403 { 404 throw new TransformerException(new Message(Messages.TRANSFORM_FAILED_FROM_X_TO_X, 405 msg.getClass().getName(), "byte[]"), e); 406 } 407 } 408 else 409 { 410 throw new TransformerException(new Message(Messages.TRANSFORM_ON_X_NOT_OF_SPECIFIED_TYPE_X, 411 msg.getClass().getName(), "byte[] or " + Serializable .class.getName())); 412 } 413 } 414 415 426 public String getTransformedMessageAsString() throws TransformerException 427 { 428 return getTransformedMessageAsString(getEncoding()); 429 } 430 431 436 public String getMessageAsString() throws UMOException 437 { 438 return getMessageAsString(getEncoding()); 439 } 440 441 453 public String getTransformedMessageAsString(String encoding) throws TransformerException 454 { 455 try 456 { 457 return new String (getTransformedMessageAsBytes(), encoding); 458 } 459 catch (UnsupportedEncodingException e) 460 { 461 throw new TransformerException(endpoint.getTransformer(), e); 462 } 463 } 464 465 473 public String getMessageAsString(String encoding) throws UMOException 474 { 475 try 476 { 477 return message.getPayloadAsString(encoding); 478 } 479 catch (Exception e) 480 { 481 throw new MuleException(new Message(Messages.CANT_READ_PAYLOAD_AS_STRING_TYPE_IS_X, 482 message.getClass().getName()), e); 483 } 484 } 485 486 491 public String getId() 492 { 493 return id; 494 } 495 496 500 public Object getProperty(String name) 501 { 502 return message.getProperty(name); 503 } 504 505 508 public Object getProperty(String name, boolean exhaustiveSearch) 509 { 510 return getProperty(name, null, exhaustiveSearch); 511 } 512 513 519 public Object getProperty(String name, Object defaultValue) 520 { 521 return message.getProperty(name, defaultValue); 522 } 523 524 530 public Object getProperty(String name, Object defaultValue, boolean exhaustiveSearch) 531 { 532 Object property = getProperty(name); 533 534 if (exhaustiveSearch) 535 { 536 if (property == null) 538 { 539 property = MapUtils.getObject(getEndpoint().getEndpointURI().getParams(), name, null); 540 } 541 542 if (property == null) 544 { 545 try 546 { 547 property = PropertyUtils.getProperty(getEndpoint().getConnector(), name); 548 } 549 catch (Exception e) 550 { 551 } 554 } 555 } 556 return (property == null ? defaultValue : property); 557 } 558 559 564 public void setProperty(String name, Object value) 565 { 566 message.setProperty(name, value); 567 } 568 569 574 public UMOImmutableEndpoint getEndpoint() 575 { 576 return endpoint; 577 } 578 579 584 public String toString() 585 { 586 StringBuffer buf = new StringBuffer (64); 587 buf.append("Event: ").append(getId()); 588 buf.append(", sync=").append(isSynchronous()); 589 buf.append(", stop processing=").append(isStopFurtherProcessing()); 590 buf.append(", ").append(endpoint); 591 592 return buf.toString(); 593 } 594 595 protected String generateEventId() 596 { 597 return UUID.getUUID(); 598 } 599 600 public UMOSession getSession() 601 { 602 return session; 603 } 604 605 void setSession(UMOSession session) 606 { 607 this.session = session; 608 } 609 610 613 public UMOComponent getComponent() 614 { 615 return session.getComponent(); 616 } 617 618 623 public boolean isStopFurtherProcessing() 624 { 625 return stopFurtherProcessing; 626 } 627 628 639 public void setStopFurtherProcessing(boolean stopFurtherProcessing) 640 { 641 this.stopFurtherProcessing = stopFurtherProcessing; 642 } 643 644 public boolean equals(Object o) 645 { 646 if (this == o) 647 { 648 return true; 649 } 650 if (!(o instanceof MuleEvent)) 651 { 652 return false; 653 } 654 655 final MuleEvent event = (MuleEvent)o; 656 657 if (message != null ? !message.equals(event.message) : event.message != null) 658 { 659 return false; 660 } 661 return id.equals(event.id); 662 } 663 664 public int hashCode() 665 { 666 return 29 * id.hashCode() + (message != null ? message.hashCode() : 0); 667 } 668 669 public boolean isSynchronous() 670 { 671 return synchronous; 672 } 673 674 public void setSynchronous(boolean value) 675 { 676 synchronous = value; 677 } 678 679 public int getTimeout() 680 { 681 if (timeout == TIMEOUT_NOT_SET_VALUE) 682 { 683 timeout = endpoint.getRemoteSyncTimeout(); 685 } 686 return timeout; 687 } 688 689 public void setTimeout(int timeout) 690 { 691 this.timeout = timeout; 692 } 693 694 699 public int getIntProperty(String name, int defaultValue) 700 { 701 return message.getIntProperty(name, defaultValue); 702 } 703 704 709 public long getLongProperty(String name, long defaultValue) 710 { 711 return message.getLongProperty(name, defaultValue); 712 } 713 714 719 public double getDoubleProperty(String name, double defaultValue) 720 { 721 return message.getDoubleProperty(name, defaultValue); 722 } 723 724 729 public boolean getBooleanProperty(String name, boolean defaultValue) 730 { 731 return message.getBooleanProperty(name, defaultValue); 732 } 733 734 740 public void setBooleanProperty(String name, boolean value) 741 { 742 message.setBooleanProperty(name, value); 743 } 744 745 751 public void setIntProperty(String name, int value) 752 { 753 message.setIntProperty(name, value); 754 } 755 756 762 public void setLongProperty(String name, long value) 763 { 764 message.setLongProperty(name, value); 765 } 766 767 773 public void setDoubleProperty(String name, double value) 774 { 775 message.setDoubleProperty(name, value); 776 } 777 778 785 public OutputStream getOutputStream() 786 { 787 return outputStream; 788 } 789 790 797 public Object removeProperty(String key) 798 { 799 return message.removeProperty(key); 800 } 801 802 private void writeObject(ObjectOutputStream out) throws IOException 803 { 804 out.defaultWriteObject(); 805 out.writeObject(endpoint.getEndpointURI().toString()); 806 } 807 808 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException 809 { 810 in.defaultReadObject(); 811 String uri = (String )in.readObject(); 812 try 813 { 814 endpoint = MuleEndpoint.getOrCreateEndpointForUri(uri, UMOEndpoint.ENDPOINT_TYPE_SENDER); 815 } 816 catch (UMOException e) 817 { 818 throw (IOException )new IOException ().initCause(e); 819 } 820 } 821 822 830 public String getStringProperty(String name, String defaultValue) 831 { 832 return message.getStringProperty(name, defaultValue); 833 } 834 835 public void setStringProperty(String name, String value) 836 { 837 setProperty(name, value); 838 } 839 840 845 public boolean isStreaming() 846 { 847 return endpoint.isStreaming(); 848 } 849 850 858 public String getEncoding() 859 { 860 String encoding = endpoint.getEncoding(); 861 if (encoding == null) 862 { 863 encoding = message.getEncoding(); 864 } 865 if (encoding == null) 866 { 867 encoding = MuleManager.getConfiguration().getEncoding(); 868 } 869 return encoding; 870 } 871 872 } 873 | Popular Tags |