1 package net.walend.somnifugi; 2 3 import java.util.Enumeration ; 4 import java.util.Map ; 5 import java.util.HashMap ; 6 import java.util.Collections ; 7 import java.util.Set ; 8 import java.util.HashSet ; 9 import java.util.Arrays ; 10 11 import java.io.ByteArrayOutputStream ; 12 import java.io.ObjectOutputStream ; 13 import java.io.ByteArrayInputStream ; 14 import java.io.ObjectInputStream ; 15 import java.io.IOException ; 16 17 import javax.jms.Message ; 18 import javax.jms.JMSException ; 19 import javax.jms.Destination ; 20 import javax.jms.MessageNotWriteableException ; 21 import javax.jms.MessageFormatException ; 22 23 29 30 public class SomniMessage 31 implements Message 32 { 33 private static final Set <Class > PROPERTYCLASSES = new HashSet <Class >(Arrays.asList(new Class [] {Boolean .class,Byte .class,Short .class,Integer .class,Long .class,Float .class,Double .class,String .class})); 34 private static final String TRUE="true"; 35 private static final String FALSE="false"; 36 37 40 protected final Object guard = new Object (); 41 42 private String messageID; 43 private long timeStamp=0; 44 private String correlationID; 45 private Destination replyTo; 46 private Destination destination; 47 private int deliveryMode; 48 private long expirationTime; 49 private int priority=0; 50 private int producerCount=Integer.MIN_VALUE; 51 private String producerConnectionClientID; 52 private boolean readOnly=false; 53 private boolean redelivered=false; 54 55 58 private SomniMessageConsumer consumer = null; 60 61 private Map <String ,Object > properties=new HashMap <String ,Object >(); 62 private Map <String ,Class > propertiesToClasses=new HashMap <String ,Class >(); 63 64 protected SomniMessage() 65 { 66 } 67 68 71 protected SomniMessage(SomniMessage message,boolean deep) 72 throws JMSException 73 { 74 this.messageID = message.getJMSMessageID(); 75 this.timeStamp = message.getJMSTimestamp(); 76 this.correlationID = message.getJMSCorrelationID(); 77 this.replyTo = message.getJMSReplyTo(); 78 this.destination = message.getJMSDestination(); 79 this.deliveryMode = message.getJMSDeliveryMode(); 80 this.expirationTime = message.getJMSExpiration(); 81 this.priority = message.getJMSPriority(); 82 this.producerCount = message.getSomniProducerCount(); 83 this.producerConnectionClientID = message.getSomniProducerConnectionClientID(); 84 this.readOnly = message.getReadOnly(); 85 this.redelivered = message.getJMSRedelivered(); 86 87 if(deep) 88 { 89 this.properties = (Map <String ,Object >)serializedCopy(message.getProperties()); 90 this.propertiesToClasses = (Map <String ,Class >)serializedCopy(message.getPropertiesToClasses()); 91 } 92 else 93 { 94 this.properties = message.getProperties(); 95 this.propertiesToClasses = message.getPropertiesToClasses(); 96 } 97 } 98 99 @SuppressWarnings ("unchecked") 100 protected static <Thing> Thing serializedCopy(Thing victem) 101 throws SomniMessageCopyingException 102 { 103 try 104 { 105 ByteArrayOutputStream byteOut = new ByteArrayOutputStream (); 106 ObjectOutputStream objectOut = new ObjectOutputStream (byteOut); 107 108 objectOut.writeObject(victem); 109 objectOut.close(); 110 111 ByteArrayInputStream byteIn = new ByteArrayInputStream (byteOut.toByteArray()); 112 ObjectInputStream objectIn = new ObjectInputStream (byteIn); 113 114 Thing result = (Thing)objectIn.readObject(); 115 116 objectIn.close(); 117 118 return result; 119 } 120 catch(IOException ioe) 121 { 122 throw new SomniMessageCopyingException(ioe); 123 } 124 catch(ClassNotFoundException cnfe) 125 { 126 throw new SomniMessageCopyingException(cnfe); 127 } 128 } 129 130 170 public String getJMSMessageID() 171 throws JMSException 172 { 173 synchronized(guard) 174 { 175 return messageID; 176 } 177 } 178 179 191 public void setJMSMessageID(String id) 192 throws JMSException 193 { 194 synchronized(guard) 195 { 196 messageID = id; 197 } 198 } 199 200 233 public long getJMSTimestamp() 234 throws JMSException 235 { 236 synchronized(guard) 237 { 238 return timeStamp; 239 } 240 } 241 242 254 public void setJMSTimestamp(long timestamp) 255 throws JMSException 256 { 257 synchronized(guard) 258 { 259 timeStamp = timestamp; 260 } 261 } 262 263 277 public byte[] getJMSCorrelationIDAsBytes() 278 throws JMSException 279 { 280 synchronized(guard) 281 { 282 return correlationID.getBytes(); 283 } 284 } 285 286 311 public void setJMSCorrelationIDAsBytes(byte[] correlationID) 312 throws JMSException 313 { 314 synchronized(guard) 315 { 316 this.correlationID = new String (correlationID); 317 } 318 } 319 320 362 public void setJMSCorrelationID(String correlationID) 363 throws JMSException 364 { 365 synchronized(guard) 366 { 367 this.correlationID = correlationID; 368 } 369 } 370 371 386 public String getJMSCorrelationID() 387 throws JMSException 388 { 389 synchronized(guard) 390 { 391 return correlationID; 392 } 393 } 394 395 407 public Destination getJMSReplyTo() 408 throws JMSException 409 { 410 synchronized(guard) 411 { 412 return replyTo; 413 } 414 } 415 416 447 public void setJMSReplyTo(Destination replyTo) 448 throws JMSException 449 { 450 synchronized(guard) 451 { 452 this.replyTo = replyTo; 453 } 454 } 455 456 475 public Destination getJMSDestination() 476 throws JMSException 477 { 478 synchronized(guard) 479 { 480 return destination; 481 } 482 } 483 484 496 public void setJMSDestination(Destination destination) 497 throws JMSException 498 { 499 synchronized(guard) 500 { 501 this.destination = destination; 502 } 503 } 504 505 515 public int getJMSDeliveryMode() 516 throws JMSException 517 { 518 synchronized(guard) 519 { 520 return deliveryMode; 521 } 522 } 523 524 537 public void setJMSDeliveryMode(int deliveryMode) 538 throws JMSException 539 { 540 synchronized(guard) 541 { 542 this.deliveryMode = deliveryMode; 543 } 544 } 545 546 561 public boolean getJMSRedelivered() 562 throws JMSException 563 { 564 return redelivered; 565 } 566 567 581 public void setJMSRedelivered(boolean redelivered) 582 throws JMSException 583 { 584 this.redelivered=redelivered; 585 } 586 587 597 public String getJMSType() 598 throws JMSException 599 { 600 return this.getClass().getName(); 601 } 602 603 634 public void setJMSType(String type) 635 throws JMSException 636 { 637 throw new UnsupportedOperationException ("This JMS implementation isn't that sophisticated."); 638 } 639 640 668 public long getJMSExpiration() 669 throws JMSException 670 { 671 synchronized(guard) 672 { 673 return expirationTime; 674 } 675 } 676 677 689 public void setJMSExpiration(long expiration) 690 throws JMSException 691 { 692 synchronized(guard) 693 { 694 expirationTime = expiration; 695 } 696 } 697 698 718 public int getJMSPriority() 719 throws JMSException 720 { 721 synchronized(guard) 722 { 723 return priority; 724 } 725 } 726 727 728 public void checkPriority(int priority) 729 throws SomniBadPriorityException 730 { 731 if((priority<0)||(priority>9)) 732 { 733 throw new SomniBadPriorityException("priority is "+priority+" but must be 0-9.",priority); 734 } 735 } 736 737 738 750 public void setJMSPriority(int priority) 751 throws JMSException 752 { 753 checkPriority(priority); 754 synchronized(guard) 755 { 756 this.priority = priority; 757 } 758 } 759 760 767 public int getSomniProducerCount() 768 throws JMSException 769 { 770 synchronized(guard) 771 { 772 return producerCount; 773 } 774 } 775 776 783 void setSomniProducerCount(int producerCount) 784 throws JMSException 785 { 786 synchronized(guard) 787 { 788 this.producerCount = producerCount; 789 } 790 } 791 792 799 public String getSomniProducerConnectionClientID() 800 { 801 synchronized(guard) 802 { 803 return producerConnectionClientID; 804 } 805 } 806 807 814 void setSomniProducerConnectionClientID(String producerConnectionClientID) 815 { 816 synchronized(guard) 817 { 818 this.producerConnectionClientID = producerConnectionClientID; 819 } 820 } 821 822 823 824 831 public void clearProperties() 832 throws JMSException 833 { 834 properties.clear(); 835 propertiesToClasses.clear(); 836 } 837 838 847 public boolean propertyExists(String name) 848 throws JMSException 849 { 850 return properties.keySet().contains(name); 851 } 852 853 864 public boolean getBooleanProperty(String name) 865 throws JMSException 866 { 867 String value = (String )properties.get(name); 868 if(value==null||FALSE.equals(value)) 869 { 870 return false; 871 } 872 else if(TRUE.equals(value)) 873 { 874 return true; 875 } 876 else 877 { 878 throw new MessageFormatException ("Property "+name+" has value "+value+" but must be "+TRUE+" or "+FALSE+"."); 879 } 880 } 881 882 893 public byte getByteProperty(String name) 894 throws JMSException 895 { 896 String value = (String )properties.get(name); 897 try 898 { 899 return Byte.parseByte(value); 900 } 901 catch(NumberFormatException nfe) 902 { 903 throw new SomniMessageFormatException("Property "+name+" has value "+value+", which is not a valid byte.",nfe); 904 } 905 } 906 907 918 public short getShortProperty(String name) 919 throws JMSException 920 { 921 String value = (String )properties.get(name); 922 try 923 { 924 return Short.parseShort(value); 925 } 926 catch(NumberFormatException nfe) 927 { 928 throw new SomniMessageFormatException("Property "+name+" has value "+value+", which is not a valid short.",nfe); 929 } 930 } 931 932 943 public int getIntProperty(String name) 944 throws JMSException 945 { 946 String value = (String )properties.get(name); 947 try 948 { 949 return Integer.parseInt(value); 950 } 951 catch(NumberFormatException nfe) 952 { 953 throw new SomniMessageFormatException("Property "+name+" has value "+value+", which is not a valid int.",nfe); 954 } 955 } 956 957 968 public long getLongProperty(String name) 969 throws JMSException 970 { 971 String value = (String )properties.get(name); 972 try 973 { 974 return Long.parseLong(value); 975 } 976 catch(NumberFormatException nfe) 977 { 978 throw new SomniMessageFormatException("Property "+name+" has value "+value+", which is not a valid long.",nfe); 979 } 980 } 981 982 993 public float getFloatProperty(String name) 994 throws JMSException 995 { 996 String value = (String )properties.get(name); 997 try 998 { 999 return Float.parseFloat(value); 1000 } 1001 catch(NumberFormatException nfe) 1002 { 1003 throw new SomniMessageFormatException("Property "+name+" has value "+value+", which is not a valid float.",nfe); 1004 } 1005 } 1006 1007 1018 public double getDoubleProperty(String name) 1019 throws JMSException 1020 { 1021 String value = (String )properties.get(name); 1022 try 1023 { 1024 return Double.parseDouble(value); 1025 } 1026 catch(NumberFormatException nfe) 1027 { 1028 throw new SomniMessageFormatException("Property "+name+" has value "+value+", which is not a valid double.",nfe); 1029 } 1030 } 1031 1032 1044 public String getStringProperty(String name) 1045 throws JMSException 1046 { 1047 String value = (String )properties.get(name); 1048 1049 return value; 1050 } 1051 1052 1070 public Object getObjectProperty(String name) 1071 throws JMSException 1072 { 1073 if(!properties.containsKey(name)) 1074 { 1075 return null; 1076 } 1077 Class propClass = (Class )propertiesToClasses.get(name); 1078 String prop = (String )properties.get(name); 1079 if(propClass.equals(String .class)) 1080 { 1081 return prop; 1082 } 1083 else if(propClass.equals(Boolean .class)) 1084 { 1085 return new Boolean (prop); 1086 } 1087 else if(propClass.equals(Byte .class)) 1088 { 1089 return new Byte (prop); 1090 } 1091 else if(propClass.equals(Short .class)) 1092 { 1093 return new Short (prop); 1094 } 1095 else if(propClass.equals(Integer .class)) 1096 { 1097 return new Integer (prop); 1098 } 1099 else if(propClass.equals(Long .class)) 1100 { 1101 return new Long (prop); 1102 } 1103 else if(propClass.equals(Float .class)) 1104 { 1105 return new Float (prop); 1106 } 1107 else if(propClass.equals(Double .class)) 1108 { 1109 return new Double (prop); 1110 } 1111 else 1112 { 1113 throw new SomniMessageFormatException("Property "+name+" with value "+prop+" has type "+propClass.getName()); 1114 } 1115 } 1116 1117 1127 public Enumeration getPropertyNames() 1128 throws JMSException 1129 { 1130 return Collections.enumeration(properties.keySet()); 1131 } 1132 1133 1143 public void setBooleanProperty(String name, boolean value) 1144 throws JMSException 1145 { 1146 checkWritable(); 1147 propertiesToClasses.put(name,Boolean .class); 1148 if(value) 1149 { 1150 properties.put(name,TRUE); 1151 } 1152 else 1153 { 1154 properties.put(name,FALSE); 1155 } 1156 } 1157 1158 1168 public void setByteProperty(String name, byte value) 1169 throws JMSException 1170 { 1171 checkWritable(); 1172 propertiesToClasses.put(name,Byte .class); 1173 properties.put(name,Byte.toString(value)); 1174 } 1175 1176 1186 public void setShortProperty(String name, short value) 1187 throws JMSException 1188 { 1189 checkWritable(); 1190 propertiesToClasses.put(name,Short .class); 1191 properties.put(name,Short.toString(value)); 1192 } 1193 1194 1204 public void setIntProperty(String name, int value) 1205 throws JMSException 1206 { 1207 checkWritable(); 1208 propertiesToClasses.put(name,Integer .class); 1209 properties.put(name,Integer.toString(value)); 1210 } 1211 1212 1222 public void setLongProperty(String name, long value) 1223 throws JMSException 1224 { 1225 checkWritable(); 1226 propertiesToClasses.put(name,Long .class); 1227 properties.put(name,Long.toString(value)); 1228 } 1229 1230 1240 public void setFloatProperty(String name, float value) 1241 throws JMSException 1242 { 1243 checkWritable(); 1244 propertiesToClasses.put(name,Float .class); 1245 properties.put(name,Float.toString(value)); 1246 } 1247 1248 1258 public void setDoubleProperty(String name, double value) 1259 throws JMSException 1260 { 1261 checkWritable(); 1262 propertiesToClasses.put(name,Double .class); 1263 properties.put(name,Double.toString(value)); 1264 } 1265 1266 1276 public void setStringProperty(String name, String value) 1277 throws JMSException 1278 { 1279 checkWritable(); 1280 if(value==null) 1281 { 1282 throw new SomniMessageFormatException("value can not be null"); 1283 } 1284 propertiesToClasses.put(name,String .class); 1285 properties.put(name,value); 1286 } 1287 1288 1303 public void setObjectProperty(String name, Object value) 1304 throws JMSException 1305 { 1306 checkWritable(); 1307 if(!PROPERTYCLASSES.contains(value.getClass())) 1308 { 1309 throw new SomniMessageFormatException("value's class can not be "+value.getClass()+", and must be one of "+PROPERTYCLASSES); 1310 } 1311 propertiesToClasses.put(name,value.getClass()); 1312 properties.put(name,value.toString()); 1313 } 1314 1315 1343 public void acknowledge() 1344 throws JMSException 1345 { 1346 synchronized(guard) 1347 { 1348 if(consumer!=null) 1350 { 1351 consumer.acknowledge(); 1352 } 1353 else 1354 { 1355 } 1358 } 1359 } 1360 1361 1372 public void clearBody() 1373 throws JMSException 1374 { 1375 synchronized(guard) 1376 { 1377 readOnly=false; 1378 } 1379 } 1380 1381 1382 void checkWritable() 1384 throws MessageNotWriteableException 1385 { 1386 synchronized(guard) 1387 { 1388 if(readOnly) 1389 { 1390 throw new MessageNotWriteableException ("Don't call mutators on SomniMessages after they have been sent."); 1391 } 1392 } 1393 } 1394 1395 void setReadOnly() 1396 { 1397 synchronized(guard) 1398 { 1399 readOnly=true; 1400 } 1401 } 1402 1403 boolean getReadOnly() 1404 { 1405 synchronized(guard) 1406 { 1407 return readOnly; 1408 } 1409 } 1410 1411 Map <String ,Object > getProperties() 1412 { 1413 synchronized(guard) 1414 { 1415 return properties; 1416 } 1417 } 1418 1419 private Map <String ,Class > getPropertiesToClasses() 1420 { 1421 synchronized(guard) 1422 { 1423 return propertiesToClasses; 1424 } 1425 } 1426 1427 1430 public SomniMessage copy(String copyMode) 1431 { 1432 try 1433 { 1434 if (SomniProperties.SHALLOWCOPY.equals(copyMode)) 1436 { 1437 return shallowCopy(); 1438 } 1439 if (SomniProperties.DEEPCOPY.equals(copyMode)) 1441 { 1442 return deepCopy(); 1443 } 1444 return this; 1445 } 1446 catch(JMSException jmse) 1447 { 1448 throw new SomniRuntimeException("Can not copy message.",jmse); 1449 } 1450 } 1451 1452 1453 1456 protected SomniMessage shallowCopy() 1458 throws JMSException 1459 { 1460 synchronized(guard) 1461 { 1462 return new SomniMessage(this,false); 1463 } 1464 } 1465 1466 1469 protected SomniMessage deepCopy() 1471 throws JMSException 1472 { 1473 synchronized(guard) 1474 { 1475 return new SomniMessage(this,true); 1476 } 1477 } 1478 1479 1482 void setConsumer(SomniMessageConsumer consumer) 1483 { 1484 synchronized(guard) 1485 { 1486 this.consumer = consumer; 1487 } 1488 } 1489 1490 public String toString() 1492 { 1493 synchronized(guard) 1494 { 1495 StringBuffer buffy = new StringBuffer (); 1496 buffy.append(getClass().getName()); 1497 buffy.append(":"); 1498 buffy.append(messageID); 1499 buffy.append(" ts:"+timeStamp); 1500 if(correlationID!=null) 1501 { 1502 buffy.append(" correlationID:"); 1503 buffy.append(correlationID); 1504 } 1505 if(replyTo!=null) 1506 { 1507 buffy.append(" replyTo:"); 1508 buffy.append(replyTo.toString()); 1509 } 1510 if(destination!=null) 1511 { 1512 buffy.append(" destination:"); 1513 buffy.append(destination.toString()); 1514 } 1515 buffy.append(" deliveryMode:"); 1516 buffy.append(deliveryMode); 1517 if(expirationTime!=0) 1518 { 1519 buffy.append(" expirationTime:"+expirationTime); 1520 } 1521 if(priority!=0) 1522 { 1523 buffy.append(" priority:"+priority); 1524 } 1525 if(producerCount!=Integer.MIN_VALUE) 1526 { 1527 buffy.append(" producerCount:"+producerCount); 1528 } 1529 if(producerConnectionClientID!=null) 1530 { 1531 buffy.append(" producerConnectionClientID:"+producerConnectionClientID); 1532 } 1533 if(readOnly) 1534 { 1535 buffy.append(" readOnly"); 1536 } 1537 if(redelivered) 1538 { 1539 buffy.append(" redelivered"); 1540 } 1541 return buffy.toString(); 1542 } 1543 } 1544} 1545 1546 1566 | Popular Tags |