1 24 package com.scalagent.kjoram.messages; 25 26 import com.scalagent.kjoram.excepts.*; 27 28 import java.io.*; 29 import java.util.*; 30 31 39 public class Message { 40 41 int type; 42 43 44 boolean persistent = true; 45 46 47 String id = null; 48 49 int priority = 4; 50 51 long expiration = 0; 52 53 long timestamp; 54 55 String toId = null; 56 57 boolean toQueue; 58 59 String correlationId = null; 60 61 String replyToId = null; 62 63 boolean replyToQueue; 64 65 69 Hashtable optionalHeader = null; 70 71 72 byte[] body_bytes = null; 73 74 Hashtable body_map = null; 75 76 String body_text = null; 77 78 boolean bodyRO = false; 79 80 81 Hashtable properties = null; 82 83 boolean propertiesRO = false; 84 85 86 public int deliveryCount = 0; 87 91 public boolean denied = false; 92 93 94 public boolean deletedDest = false; 95 96 public boolean expired = false; 97 98 public boolean notWriteable = false; 99 100 public boolean undeliverable = false; 101 102 107 public transient int acksCounter; 108 113 public transient int durableAcksCounter; 114 115 118 public Message() 119 { 120 this.type = MessageType.SIMPLE; 121 } 122 123 124 125 public void setIdentifier(String id) 126 { 127 this.id = id; 128 } 129 130 131 public void setPersistent(boolean persistent) 132 { 133 this.persistent = persistent; 134 } 135 136 141 public void setPriority(int priority) 142 { 143 if (priority >= 0 && priority <= 9) 144 this.priority = priority; 145 } 146 147 148 public void setExpiration(long expiration) 149 { 150 if (expiration >= 0) 151 this.expiration = expiration; 152 } 153 154 155 public void setTimestamp(long timestamp) 156 { 157 this.timestamp = timestamp; 158 } 159 160 166 public void setDestination(String id, boolean queue) 167 { 168 this.toId = id; 169 this.toQueue = queue; 170 } 171 172 173 public void setCorrelationId(String correlationId) 174 { 175 this.correlationId = correlationId; 176 } 177 178 184 public void setReplyTo(String id, boolean queue) 185 { 186 this.replyToId = id; 187 this.replyToQueue = queue; 188 } 189 190 196 public void setOptionalHeader(String name, Object value) 197 { 198 if (name == null || name.equals("")) 199 throw new IllegalArgumentException ("Invalid header name: " + name); 200 201 if (value == null) 202 return; 203 204 if (optionalHeader == null) 205 optionalHeader = new Hashtable(); 206 207 optionalHeader.put(name, value); 208 } 209 210 211 public int getType() 212 { 213 return type; 214 } 215 216 217 public String getIdentifier() 218 { 219 return id; 220 } 221 222 223 public boolean getPersistent() 224 { 225 return persistent; 226 } 227 228 229 public int getPriority() 230 { 231 return priority; 232 } 233 234 235 public long getExpiration() 236 { 237 return expiration; 238 } 239 240 241 public long getTimestamp() 242 { 243 return timestamp; 244 } 245 246 247 public String getDestinationId() 248 { 249 return toId; 250 } 251 252 253 public boolean toQueue() 254 { 255 return toQueue; 256 } 257 258 259 public String getCorrelationId() 260 { 261 return correlationId; 262 } 263 264 265 public String getReplyToId() 266 { 267 return replyToId; 268 } 269 270 271 public boolean replyToQueue() 272 { 273 return replyToQueue; 274 } 275 276 281 public Object getOptionalHeader(String name) 282 { 283 if (optionalHeader == null) 284 return null; 285 286 return optionalHeader.get(name); 287 } 288 289 297 public void setBooleanProperty(String name, boolean value) 298 throws MessageROException 299 { 300 preparePropSetting(name); 301 properties.put(name, new Boolean (value)); 302 } 303 304 312 public void setByteProperty(String name, byte value) 313 throws MessageROException 314 { 315 preparePropSetting(name); 316 properties.put(name, new Byte (value)); 317 } 318 319 327 334 342 349 357 public void setIntProperty(String name, int value) throws MessageROException 358 { 359 preparePropSetting(name); 360 properties.put(name, new Integer (value)); 361 } 362 363 371 public void setLongProperty(String name, long value) 372 throws MessageROException 373 { 374 preparePropSetting(name); 375 properties.put(name, new Long (value)); 376 } 377 378 388 public void setObjectProperty(String name, Object value) 389 throws MessageException 390 { 391 preparePropSetting(name); 392 393 if (value instanceof Boolean 394 || value instanceof String 395 || value instanceof Integer 396 || value instanceof Long 397 || value instanceof Short 398 || value instanceof Byte ) 399 properties.put(name, value); 400 401 else 402 throw new MessageValueException("Can't set non primitive Java object" 403 + " as a property value."); 404 } 405 406 414 public void setShortProperty(String name, short value) 415 throws MessageROException 416 { 417 preparePropSetting(name); 418 properties.put(name, new Short (value)); 419 } 420 421 429 public void setStringProperty(String name, String value) 430 throws MessageROException 431 { 432 preparePropSetting(name); 433 properties.put(name, new String (value)); 434 } 435 436 441 public boolean getBooleanProperty(String name) throws MessageValueException 442 { 443 if (properties == null) 444 throw new RuntimeException ("getBooleanProperty properties = null"); 445 return ConversionHelper.toBoolean(properties.get(name)); 446 } 447 448 452 public byte getByteProperty(String name) throws MessageValueException 453 { 454 if (properties == null) 455 throw new RuntimeException ("getByteProperty properties = null"); 456 return ConversionHelper.toByte(properties.get(name)); 457 } 458 459 466 473 480 487 494 public int getIntProperty(String name) throws MessageValueException 495 { 496 if (properties == null) 497 return Integer.valueOf(null).intValue(); 498 return ConversionHelper.toInt(properties.get(name)); 499 } 500 501 508 public long getLongProperty(String name) throws MessageValueException 509 { 510 if (properties == null) 511 throw new RuntimeException ("getLongProperty properties = null"); 512 return ConversionHelper.toLong(properties.get(name)); 513 } 514 515 520 public Object getObjectProperty(String name) 521 { 522 if (properties == null) 523 return null; 524 return properties.get(name); 525 } 526 527 534 public short getShortProperty(String name) throws MessageValueException 535 { 536 if (properties == null) 537 throw new RuntimeException ("getShortProperty properties = null"); 538 return ConversionHelper.toShort(properties.get(name)); 539 } 540 541 546 public String getStringProperty(String name) 547 { 548 if (properties == null) 549 return null; 550 return ConversionHelper.toString(properties.get(name)); 551 } 552 553 558 public boolean propertyExists(String name) 559 { 560 if (properties == null) 561 return false; 562 563 return properties.containsKey(name); 564 } 565 566 567 public Enumeration getPropertyNames() 568 { 569 if (properties == null) 570 return (new Hashtable()).keys(); 571 572 return properties.keys(); 573 } 574 575 576 public void clearProperties() 577 { 578 propertiesRO = false; 579 580 if (properties == null) 581 return; 582 583 properties.clear(); 584 properties = null; 585 } 586 587 593 public void setMap(Hashtable map) throws Exception 594 { 595 if (bodyRO) 596 throw new MessageROException("Can't set the body as it is READ-ONLY."); 597 598 body_map = map; 599 type = MessageType.MAP; 600 } 601 602 607 public void setText(String text) throws MessageROException 608 { 609 if (bodyRO) 610 throw new MessageROException("Can't set the body as it is READ-ONLY."); 611 612 body_text = text; 613 type = MessageType.TEXT; 614 } 615 616 621 public void setStream(byte[] bytes) throws MessageROException 622 { 623 if (bodyRO) 624 throw new MessageROException("Can't set the body as it is READ-ONLY."); 625 626 body_bytes = bytes; 627 type = MessageType.STREAM; 628 } 629 630 635 public void setBytes(byte[] bytes) throws MessageROException 636 { 637 if (bodyRO) 638 throw new MessageROException("Can't set the body as it is READ-ONLY."); 639 640 body_bytes = bytes; 641 type = MessageType.BYTES; 642 } 643 644 647 public Hashtable getMap() 648 { 649 return body_map; 650 } 651 652 653 public String getText() 654 { 655 return body_text; 656 } 657 658 659 public byte[] getStream() 660 { 661 if (type != MessageType.STREAM) 662 return null; 663 664 return body_bytes; 665 } 666 667 668 public byte[] getBytes() 669 { 670 if (type != MessageType.BYTES) 671 return null; 672 673 return body_bytes; 674 } 675 676 679 public void clearBody() 680 { 681 body_bytes = null; 682 body_map = null; 683 body_text = null; 684 bodyRO = false; 685 } 686 687 688 public boolean isValid() 689 { 690 if (expiration == 0) 691 return true; 692 693 return ((expiration - System.currentTimeMillis()) > 0); 694 } 695 696 697 public Object clone() 698 { 699 Message clone = new Message(); 700 clone.type = type; 701 clone.persistent = persistent; 702 clone.id = id; 703 clone.priority = priority; 704 clone.expiration = expiration; 705 clone.timestamp = timestamp; 706 clone.toId = toId; 707 clone.toQueue = toQueue; 708 clone.correlationId = correlationId; 709 clone.replyToId = replyToId; 710 clone.replyToQueue = replyToQueue; 711 clone.body_text = body_text; 712 clone.bodyRO = bodyRO; 713 clone.propertiesRO = propertiesRO; 714 clone.deliveryCount = deliveryCount; 715 clone.denied = denied; 716 clone.deletedDest = deletedDest; 717 clone.expired = expired; 718 clone.notWriteable = notWriteable; 719 clone.undeliverable = undeliverable; 720 clone.acksCounter = acksCounter; 721 clone.durableAcksCounter = durableAcksCounter; 722 723 if (body_bytes != null) { 724 byte[] b = new byte[body_bytes.length]; 725 for (int i = 0; i < body_bytes.length; i++) 726 b[i] = body_bytes[i]; 727 clone.body_bytes = b; 728 } 729 if (body_map != null) { 730 clone.body_map = new Hashtable(); 731 for (Enumeration e = body_map.keys(); e.hasMoreElements(); ) { 732 Object key = e.nextElement(); 733 clone.body_map.put(key,body_map.get(key)); 734 } 735 } 736 if (optionalHeader != null) { 737 clone.optionalHeader = new Hashtable(); 738 for (Enumeration e = optionalHeader.keys(); e.hasMoreElements(); ) { 739 Object key = e.nextElement(); 740 clone.optionalHeader.put(key,optionalHeader.get(key)); 741 } 742 } 743 if (properties != null) { 744 clone.properties = new Hashtable(); 745 for (Enumeration e = properties.keys(); e.hasMoreElements(); ) { 746 Object key = e.nextElement(); 747 clone.properties.put(key,properties.get(key)); 748 } 749 } 750 return clone; 751 } 752 753 757 public Hashtable soapCode() { 758 Hashtable h = new Hashtable(); 759 760 Hashtable fieldsTb = new Hashtable(); 762 763 fieldsTb.put("type", new Integer (type)); 764 fieldsTb.put("id", id); 765 fieldsTb.put("persistent", new Boolean (persistent)); 766 fieldsTb.put("priority", new Integer (priority)); 767 fieldsTb.put("expiration", new Long (expiration)); 768 fieldsTb.put("timestamp", new Long (timestamp)); 769 fieldsTb.put("toId", toId); 770 fieldsTb.put("toQueue", new Boolean (toQueue)); 771 if (correlationId != null) 772 fieldsTb.put("correlationId", correlationId); 773 if (replyToId != null) { 774 fieldsTb.put("replyToId", replyToId); 775 fieldsTb.put("replyToQueue", new Boolean (replyToQueue)); 776 } 777 if (body_bytes != null) 778 fieldsTb.put("body_bytes", body_bytes); 779 else if (body_map != null) 780 fieldsTb.put("body_map", body_map); 781 else if (body_text != null) 782 fieldsTb.put("body_text", body_text); 783 fieldsTb.put("bodyRO", new Boolean (bodyRO)); 784 fieldsTb.put("propertiesRO", new Boolean (propertiesRO)); 785 fieldsTb.put("deliveryCount", new Integer (deliveryCount)); 786 fieldsTb.put("denied", new Boolean (denied)); 787 fieldsTb.put("deletedDest", new Boolean (deletedDest)); 788 fieldsTb.put("expired", new Boolean (expired)); 789 fieldsTb.put("notWriteable", new Boolean (notWriteable)); 790 fieldsTb.put("undeliverable", new Boolean (undeliverable)); 791 792 h.put("fieldsTb",fieldsTb); 793 794 if (optionalHeader != null) 796 h.put("optionalHeader",optionalHeader); 797 798 if (properties != null) 800 h.put("properties",properties); 801 802 return h; 803 } 804 805 809 public static Message soapDecode(Hashtable h) 810 { 811 if (h == null) return null; 812 813 Hashtable fieldsTb = (Hashtable) h.get("fieldsTb"); 814 815 Message msg = new Message(); 816 817 try { 818 msg.type = ConversionHelper.toInt(fieldsTb.get("type")); 819 msg.id = (String ) fieldsTb.get("id"); 820 msg.persistent = ConversionHelper.toBoolean(fieldsTb.get("persistent")); 821 msg.priority = ConversionHelper.toInt(fieldsTb.get("priority")); 822 msg.expiration = ConversionHelper.toLong(fieldsTb.get("expiration")); 823 msg.timestamp = ConversionHelper.toLong(fieldsTb.get("timestamp")); 824 msg.toId = (String ) fieldsTb.get("toId"); 825 msg.toQueue = ConversionHelper.toBoolean(fieldsTb.get("toQueue")); 826 msg.correlationId = (String ) fieldsTb.get("correlationId"); 827 msg.replyToId = (String ) fieldsTb.get("replyToId"); 828 if (msg.replyToId != null) { 829 msg.replyToQueue = 830 ConversionHelper.toBoolean(fieldsTb.get("replyToQueue")); 831 } 832 msg.body_bytes = ConversionHelper.toBytes(fieldsTb.get("body_bytes")); 833 msg.body_map = (Hashtable) fieldsTb.get("body_map"); 834 msg.body_text = (String ) fieldsTb.get("body_text"); 835 msg.bodyRO = ConversionHelper.toBoolean(fieldsTb.get("bodyRO")); 836 msg.propertiesRO = 837 ConversionHelper.toBoolean(fieldsTb.get("propertiesRO")); 838 msg.deliveryCount = ConversionHelper.toInt(fieldsTb.get("deliveryCount")); 839 msg.denied = ConversionHelper.toBoolean(fieldsTb.get("denied")); 840 msg.deletedDest = ConversionHelper.toBoolean(fieldsTb.get("deletedDest")); 841 msg.expired = ConversionHelper.toBoolean(fieldsTb.get("expired")); 842 msg.notWriteable = 843 ConversionHelper.toBoolean(fieldsTb.get("notWriteable")); 844 msg.undeliverable = 845 ConversionHelper.toBoolean(fieldsTb.get("undeliverable")); 846 847 msg.optionalHeader = (Hashtable) h.get("optionalHeader"); 848 msg.properties = (Hashtable) h.get("properties"); 849 } 850 catch (MessageValueException exc) { 852 exc.printStackTrace(); 853 } 854 855 return msg; 856 } 857 858 865 private void preparePropSetting(String name) throws MessageROException 866 { 867 if (propertiesRO) { 868 throw new MessageROException("Can't set property as the message " 869 + "properties are READ-ONLY."); 870 } 871 872 if (name == null || name.equals("")) 873 throw new IllegalArgumentException ("Invalid property name: " + name); 874 875 if (properties == null) 876 properties = new Hashtable(); 877 } 878 879 883 890 894 } 902 | Popular Tags |