00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 package com.scalagent.kjoram;
00025
00026 import java.util.Enumeration;
00027
00028 import com.scalagent.kjoram.excepts.IllegalStateException;
00029 import com.scalagent.kjoram.excepts.JMSException;
00030 import com.scalagent.kjoram.excepts.MessageEOFException;
00031 import com.scalagent.kjoram.excepts.MessageException;
00032 import com.scalagent.kjoram.excepts.MessageFormatException;
00033 import com.scalagent.kjoram.excepts.MessageNotWriteableException;
00034 import com.scalagent.kjoram.excepts.MessageROException;
00035 import com.scalagent.kjoram.excepts.MessageValueException;
00036 import com.scalagent.kjoram.messages.ConversionHelper;
00037 import com.scalagent.kjoram.messages.MessageType;
00038
00043 public class Message
00044 {
00046 protected com.scalagent.kjoram.messages.Message momMsg;
00051 protected Session sess = null;
00052
00053
00057 Message()
00058 {
00059 momMsg = new com.scalagent.kjoram.messages.Message();
00060 }
00061
00069 Message(Session sess, com.scalagent.kjoram.messages.Message momMsg)
00070 {
00071 this.sess = sess;
00072 this.momMsg = momMsg;
00073 }
00074
00075
00083 public void acknowledge() throws JMSException
00084 {
00085 if (sess == null
00086 || sess.transacted
00087 || sess.acknowledgeMode != Session.CLIENT_ACKNOWLEDGE)
00088 return;
00089
00090 if (sess.closed)
00091 throw new IllegalStateException("Forbidden call on a closed session.");
00092
00093 sess.acknowledge();
00094 }
00095
00101 public void clearBody() throws JMSException
00102 {
00103 momMsg.clearBody();
00104 }
00105
00111 public void clearProperties() throws JMSException
00112 {
00113 momMsg.clearProperties();
00114 }
00115
00121 public boolean propertyExists(String name) throws JMSException
00122 {
00123 return momMsg.propertyExists(name);
00124 }
00125
00131 public Enumeration getPropertyNames() throws JMSException
00132 {
00133 return momMsg.getPropertyNames();
00134 }
00135
00136
00142 public void setJMSMessageID(String id) throws JMSException
00143 {
00144 momMsg.setIdentifier(id);
00145 }
00146
00147
00153 public void setJMSPriority(int priority) throws JMSException
00154 {
00155 if (0 <= priority && priority <= 9)
00156 momMsg.setPriority(priority);
00157 else
00158 throw new JMSException("Priority of "+ priority +" is not valid"
00159 + " (should be an integer between 0 and 9).");
00160 }
00161
00167 public void setJMSDestination(Destination dest) throws JMSException
00168 {
00169 if (dest == null)
00170 momMsg.setDestination(null, true);
00171
00172 if (dest instanceof Queue)
00173 momMsg.setDestination(((Queue) dest).getQueueName(), true);
00174 else
00175 momMsg.setDestination(((Topic) dest).getTopicName(), false);
00176
00177 if (dest instanceof TemporaryQueue || dest instanceof TemporaryTopic)
00178 momMsg.setOptionalHeader("JMSTempDestination", new Boolean(true));
00179 else
00180 momMsg.setOptionalHeader("JMSTempDestination", new Boolean(false));
00181 }
00182
00188 public void setJMSExpiration(long expiration) throws JMSException
00189 {
00190 momMsg.setExpiration(expiration);
00191 }
00192
00198 public void setJMSRedelivered(boolean redelivered) throws JMSException
00199 {
00200 momMsg.denied = redelivered;
00201 }
00202
00208 public void setJMSReplyTo(Destination replyTo) throws JMSException
00209 {
00210 if (replyTo == null)
00211 momMsg.setReplyTo(null, true);
00212
00213 if (replyTo instanceof Queue)
00214 momMsg.setReplyTo(((Queue) replyTo).getQueueName(), true);
00215 else
00216 momMsg.setReplyTo(((Topic) replyTo).getTopicName(), false);
00217
00218 if (replyTo instanceof TemporaryQueue || replyTo instanceof TemporaryTopic)
00219 momMsg.setOptionalHeader("JMSTempReplyTo", new Boolean(true));
00220 else
00221 momMsg.setOptionalHeader("JMSTempReplyTo", new Boolean(false));
00222 }
00223
00229 public void setJMSTimestamp(long timestamp) throws JMSException
00230 {
00231 momMsg.setTimestamp(timestamp);
00232 }
00233
00239 public void setJMSCorrelationID(String correlationID) throws JMSException
00240 {
00241 momMsg.setCorrelationId(correlationID);
00242 }
00243
00249 public void setJMSCorrelationIDAsBytes(byte[] correlationID)
00250 throws JMSException
00251 {
00252 momMsg.setCorrelationId(ConversionHelper.toString(correlationID));
00253 }
00254
00260 public void setJMSType(String type) throws JMSException
00261 {
00262 momMsg.setOptionalHeader("JMSType", type);
00263 }
00264
00270 public void setJMSDeliveryMode(int deliveryMode) throws JMSException
00271 {
00272 if (deliveryMode != DeliveryMode.PERSISTENT
00273 && deliveryMode != DeliveryMode.NON_PERSISTENT)
00274 throw new JMSException("Invalid delivery mode.");
00275
00276 momMsg.setPersistent(deliveryMode == DeliveryMode.PERSISTENT);
00277 }
00278
00284 public String getJMSMessageID() throws JMSException
00285 {
00286 return momMsg.getIdentifier();
00287 }
00288
00294 public int getJMSPriority() throws JMSException
00295 {
00296 return momMsg.getPriority();
00297 }
00298
00304 public int getJMSDeliveryMode() throws JMSException
00305 {
00306 if (momMsg.getPersistent())
00307 return DeliveryMode.PERSISTENT;
00308 else
00309 return DeliveryMode.NON_PERSISTENT;
00310 }
00311
00317 public Destination getJMSDestination() throws JMSException
00318 {
00319 String id = momMsg.getDestinationId();
00320 boolean queue = momMsg.toQueue();
00321 Object temporaryValue = null;
00322 boolean temporary = false;
00323
00324 try {
00325 temporaryValue = momMsg.getOptionalHeader("JMSTempDestination");
00326 temporary = ConversionHelper.toBoolean(temporaryValue);
00327 }
00328
00329
00330 catch (Exception exc) {}
00331
00332 if (queue) {
00333 if (temporary)
00334 return new TemporaryQueue(id, null);
00335 else
00336 return new Queue(id);
00337 }
00338 else {
00339 if (temporary)
00340 return new TemporaryTopic(id, null);
00341 else
00342 return new Topic(id);
00343 }
00344 }
00345
00351 public long getJMSExpiration() throws JMSException
00352 {
00353 return momMsg.getExpiration();
00354 }
00355
00361 public boolean getJMSRedelivered() throws JMSException
00362 {
00363 return momMsg.denied;
00364 }
00365
00371 public Destination getJMSReplyTo() throws JMSException
00372 {
00373 String id = momMsg.getReplyToId();
00374 boolean queue = momMsg.replyToQueue();
00375 Object temporaryValue = null;
00376 boolean temporary = false;
00377
00378 if (id == null)
00379 return null;
00380
00381 try {
00382 temporaryValue = momMsg.getOptionalHeader("JMSTempReplyTo");
00383 temporary = ConversionHelper.toBoolean(temporaryValue);
00384 }
00385
00386
00387 catch (Exception exc) {}
00388
00389 if (queue) {
00390 if (temporary)
00391 return new TemporaryQueue(id, null);
00392 else
00393 return new Queue(id);
00394 }
00395 else {
00396 if (temporary)
00397 return new TemporaryTopic(id, null);
00398 else
00399 return new Topic(id);
00400 }
00401 }
00402
00408 public long getJMSTimestamp() throws JMSException
00409 {
00410 return momMsg.getTimestamp();
00411 }
00412
00418 public String getJMSType() throws JMSException
00419 {
00420 Object value = momMsg.getOptionalHeader("JMSType");
00421 return ConversionHelper.toString(value);
00422 }
00423
00429 public String getJMSCorrelationID() throws JMSException
00430 {
00431 return momMsg.getCorrelationId();
00432 }
00433
00440 public byte[] getJMSCorrelationIDAsBytes() throws JMSException
00441 {
00442 try {
00443 return ConversionHelper.toBytes(momMsg.getCorrelationId());
00444 }
00445 catch (MessageValueException mE) {
00446 throw new MessageFormatException(mE.getMessage());
00447 }
00448 }
00449
00456 public void setBooleanProperty(String name, boolean value)
00457 throws JMSException
00458 {
00459 doSetProperty(name, new Boolean(value));
00460 }
00461
00468 public void setByteProperty(String name, byte value) throws JMSException
00469 {
00470 doSetProperty(name, new Byte(value));
00471 }
00472
00479
00480
00481
00482
00483
00490
00491
00492
00493
00494
00501 public void setIntProperty(String name, int value) throws JMSException
00502 {
00503 doSetProperty(name, new Integer(value));
00504 }
00505
00512 public void setLongProperty(String name, long value) throws JMSException
00513 {
00514 doSetProperty(name, new Long(value));
00515 }
00516
00524 public void setObjectProperty(String name, Object value) throws JMSException
00525 {
00526 doSetProperty(name, value);
00527 }
00528
00535 public void setShortProperty(String name, short value) throws JMSException
00536 {
00537 doSetProperty(name, new Short(value));
00538 }
00539
00546 public void setStringProperty(String name, String value) throws JMSException
00547 {
00548 doSetProperty(name, value);
00549 }
00550
00551
00558 public boolean getBooleanProperty(String name) throws JMSException
00559 {
00560 try {
00561 return ConversionHelper.toBoolean(doGetProperty(name));
00562 }
00563 catch (MessageValueException mE) {
00564 throw new MessageFormatException(mE.getMessage());
00565 }
00566 }
00567
00574 public byte getByteProperty(String name) throws JMSException
00575 {
00576 try {
00577 return ConversionHelper.toByte(doGetProperty(name));
00578 }
00579 catch (MessageValueException mE) {
00580 throw new MessageFormatException(mE.getMessage());
00581 }
00582 }
00583
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00622 public int getIntProperty(String name) throws JMSException
00623 {
00624 try {
00625 return ConversionHelper.toInt(doGetProperty(name));
00626 }
00627 catch (MessageValueException mE) {
00628 throw new MessageFormatException(mE.getMessage());
00629 }
00630 }
00631
00638 public long getLongProperty(String name) throws JMSException
00639 {
00640 try {
00641 return ConversionHelper.toLong(doGetProperty(name));
00642 }
00643 catch (MessageValueException mE) {
00644 throw new MessageFormatException(mE.getMessage());
00645 }
00646 }
00647
00653 public Object getObjectProperty(String name) throws JMSException
00654 {
00655 return doGetProperty(name);
00656 }
00657
00664 public short getShortProperty(String name) throws JMSException
00665 {
00666 try {
00667 return ConversionHelper.toShort(doGetProperty(name));
00668 }
00669 catch (MessageValueException mE) {
00670 throw new MessageFormatException(mE.getMessage());
00671 }
00672 }
00673
00679 public String getStringProperty(String name) throws JMSException
00680 {
00681 return ConversionHelper.toString(doGetProperty(name));
00682 }
00683
00695 private void doSetProperty(String name, Object value) throws JMSException
00696 {
00697 if (name == null || name.equals(""))
00698 throw new IllegalArgumentException("Invalid property name: " + name);
00699
00700 String upName = name.toUpperCase();
00701
00702 try {
00703 if (name.startsWith("JMSX")) {
00704 if (name.equals("JMSXGroupID"))
00705 momMsg.setOptionalHeader(name, ConversionHelper.toString(value));
00706 else if (name.equals("JMSXGroupSeq"))
00707 momMsg.setOptionalHeader(name,
00708 new Integer(ConversionHelper.toInt(value)));
00709 else
00710 throw new JMSException("Property names with prefix 'JMSX' are"
00711 + " reserved.");
00712 }
00713 else if (name.startsWith("JMS_"))
00714 throw new JMSException("Property names with prefix 'JMS_' are"
00715 + " reserved.");
00716 else if (name.startsWith("JMS"))
00717 throw new JMSException("Property names with prefix 'JMS' are"
00718 + " reserved.");
00719 else if (upName.equals("NULL")
00720 || upName.equals("TRUE")
00721 || upName.equals("FALSE")
00722 || upName.equals("NOT")
00723 || upName.equals("AND")
00724 || upName.equals("OR")
00725 || upName.equals("BETWEEN")
00726 || upName.equals("LIKE")
00727 || upName.equals("IN")
00728 || upName.equals("IS")
00729 || upName.equals("ESCAPE"))
00730 throw new JMSException("Invalid property name: " + name + " is a"
00731 + " SQL terminal.");
00732 else
00733 momMsg.setObjectProperty(name, value);
00734 }
00735 catch (MessageException mE) {
00736 if (mE instanceof MessageValueException)
00737 throw new MessageFormatException(mE.getMessage());
00738 if (mE instanceof MessageROException)
00739 throw new MessageNotWriteableException(mE.getMessage());
00740 }
00741 }
00742
00748 private Object doGetProperty(String name)
00749 {
00750 if (name == null || name.equals(""))
00751 throw new IllegalArgumentException("Invalid property name: " + name);
00752
00753 Object value = null;
00754
00755 if (name.startsWith("JMSX")) {
00756 if (name.equals("JMSXDeliveryCount"))
00757 value = new Integer(momMsg.deliveryCount);
00758 else
00759 value = momMsg.getOptionalHeader(name);
00760 }
00761 else
00762 value = momMsg.getObjectProperty(name);
00763
00764 return value;
00765 }
00766
00767
00774 com.scalagent.kjoram.messages.Message getMomMessage()
00775 throws MessageFormatException
00776 {
00777 try {
00778 prepare();
00779 return momMsg;
00780 }
00781 catch (Exception e) {
00782 MessageFormatException jE =
00783 new MessageFormatException("The message body could not be"
00784 + " serialized.");
00785 jE.setLinkedException(e);
00786 throw jE;
00787 }
00788 }
00789
00798 static Message
00799 wrapMomMessage(Session sess, com.scalagent.kjoram.messages.Message momMsg)
00800 throws JMSException
00801 {
00802 Message msg = null;
00803
00804 if (momMsg.getType() == MessageType.SIMPLE)
00805 msg = new Message(sess, momMsg);
00806 else if (momMsg.getType() == MessageType.TEXT)
00807 msg = new TextMessage(sess, momMsg);
00808 else if (momMsg.getType() == MessageType.MAP)
00809 msg = new MapMessage(sess, momMsg);
00810 else if (momMsg.getType() == MessageType.BYTES)
00811 msg = new BytesMessage(sess, momMsg);
00812
00813 return msg;
00814 }
00815
00821 static Message convertJMSMessage(Message jmsMsg)
00822 throws JMSException
00823 {
00824 Message msg = null;
00825 if (jmsMsg instanceof TextMessage) {
00826 msg = new TextMessage();
00827 ((TextMessage) msg).setText(((TextMessage)jmsMsg).getText());
00828 }
00829 else if (jmsMsg instanceof BytesMessage) {
00830 msg = new BytesMessage();
00831 try {
00832 while (true)
00833 ((BytesMessage) msg).writeByte(((BytesMessage)jmsMsg).readByte());
00834 }
00835 catch (MessageEOFException mE) {}
00836 }
00837 else if (jmsMsg instanceof MapMessage) {
00838 msg = new MapMessage();
00839 Enumeration mapNames = ((MapMessage) jmsMsg).getMapNames();
00840 }
00841 else
00842 msg = new Message();
00843
00844 msg.setJMSCorrelationID(jmsMsg.getJMSCorrelationID());
00845 msg.setJMSReplyTo(jmsMsg.getJMSReplyTo());
00846 msg.setJMSType(jmsMsg.getJMSType());
00847
00848 return msg;
00849 }
00850
00857 protected void prepare() throws Exception
00858 {
00859 momMsg.denied = false;
00860 momMsg.deletedDest = false;
00861 momMsg.expired = false;
00862 momMsg.notWriteable = false;
00863 momMsg.undeliverable = false;
00864 }
00865 }