1 21 package com.presumo.jms.message; 22 23 import com.presumo.jms.client.JmsDestination; 24 import com.presumo.jms.client.JmsTopic; 25 import com.presumo.jms.client.JmsSession; 26 27 import java.util.BitSet ; 28 import java.util.Enumeration ; 29 import java.util.TimeZone ; 30 31 import java.io.ByteArrayInputStream ; 32 import java.io.ByteArrayOutputStream ; 33 import java.io.DataOutput ; 34 import java.io.DataOutputStream ; 35 import java.io.DataInput ; 36 import java.io.DataInputStream ; 37 import java.io.Externalizable ; 38 import java.io.IOException ; 39 import java.io.ObjectInput ; 40 import java.io.ObjectOutput ; 41 42 import javax.jms.Message ; 43 import javax.jms.JMSException ; 44 import javax.jms.Destination ; 45 import javax.jms.MessageFormatException ; 46 import javax.jms.MessageNotWriteableException ; 47 48 49 54 public class JmsMessage 55 implements Message , Externalizable , Cloneable 56 { 57 protected boolean readOnly = false; 58 59 private MessageHeader header; 60 private PrimitiveMap properties; 61 62 private boolean local; private int sendingTargetID = -1; private AckHelper ackHelper; private JmsSession sessionCallback; 66 private BitSet routingMask; 67 68 72 public JmsMessage() 73 { 74 header = new MessageHeader(); 75 properties = new PrimitiveMap(); 76 } 77 78 public JmsMessage(String routername) 79 { 80 header = new MessageHeader(); 81 properties = new PrimitiveMap(); 82 83 header.setJMSMessageID(new MessageID(routername)); 84 } 85 86 public JmsMessage(byte [] buf, int offset, int length) 87 { 88 header = new MessageHeader(); 89 properties = new PrimitiveMap(); 90 91 try { 92 ByteArrayInputStream byteStream; 93 byteStream = new ByteArrayInputStream (buf, offset, length); 94 DataInputStream dataStream = new DataInputStream (byteStream); 95 this.unmarshal(dataStream); 96 dataStream.close(); 97 byteStream.close(); 98 } catch (IOException ioe) {} 99 } 100 101 105 119 public final Object clone() 120 { 121 try { 122 return super.clone(); 123 } catch (CloneNotSupportedException e) { e.printStackTrace(); } 124 return null; 125 } 126 127 public int hashCode() 128 { 129 return header.jmsMessageID.hashCode(); 130 } 131 132 public boolean equals(Object obj) 133 { 134 if ( (obj != null) && (obj instanceof JmsMessage) ) { 135 JmsMessage msg = (JmsMessage) obj; 136 return header.jmsMessageID.equals(msg.header.jmsMessageID); 137 } 138 return false; 139 } 140 141 145 public String toString() 146 { 147 return super.toString() + "\n" +header.toString() + properties.toString(); 148 } 149 150 151 154 public final byte[] toByteArray() 155 { 156 byte [] result = null; 157 try { 158 159 ByteArrayOutputStream byteStream = new ByteArrayOutputStream (); 160 DataOutputStream dataStream = new DataOutputStream (byteStream); 161 this.marshal(dataStream); 162 dataStream.flush(); 163 result = byteStream.toByteArray(); 164 dataStream.close(); 165 byteStream.close(); 166 167 } catch (IOException ioe) {} 168 return result; 169 } 170 171 174 public final void writeExternal(ObjectOutput out) throws IOException 175 { 176 marshal(out); 177 } 178 179 182 public final void readExternal(ObjectInput in) throws IOException 183 { 184 unmarshal(in); 185 } 186 187 190 public void marshal(DataOutput out) throws IOException 191 { 192 header.marshal(out); 193 properties.marshal(out); 194 } 195 196 199 public void unmarshal(DataInput in) throws IOException 200 { 201 header.unmarshal(in); 202 203 if (header.jmsDeliveryMode == javax.jms.DeliveryMode.PERSISTENT) 206 this.ackHelper = new AckHelper(this); 207 208 properties.unmarshal(in); 209 } 210 211 212 216 public void acknowledge() 217 { 218 if (sessionCallback != null) { 219 sessionCallback.acknowledge(); 220 } 221 } 222 223 public void clearBody() throws JMSException 224 { 225 checkWrite(); 226 } 227 228 232 public final void setJMSMessageID(String messageID) 233 { 234 header.jmsMessageID = MessageID.unmarshal(messageID); 235 } 236 237 public final void setJMSTimestamp(long timestamp) 238 { 239 header.setJMSTimestamp(timestamp); 240 } 241 242 public final String getJMSMessageID() 243 { 244 return header.jmsMessageID.toString(); 245 } 246 247 public final long getJMSTimestamp() 248 { 249 return header.jmsTimestamp; 250 } 251 252 public final byte[] getJMSCorrelationIDAsBytes() throws JMSException 253 { 254 byte [] result = null; 255 try { 256 257 ByteArrayOutputStream byteStream = new ByteArrayOutputStream (); 258 DataOutputStream dataStream = new DataOutputStream (byteStream); 259 header.jmsCorrelationID.marshal(dataStream); 260 dataStream.flush(); 261 result = byteStream.toByteArray(); 262 dataStream.close(); 263 byteStream.close(); 264 265 } catch (IOException ioe) {} 266 return result; 267 } 268 269 public final void setJMSCorrelationIDAsBytes(byte[] correlationID) 270 throws JMSException 271 { 272 try { 273 ByteArrayInputStream byteStream; 274 byteStream = new ByteArrayInputStream (correlationID); 275 DataInputStream dataStream = new DataInputStream (byteStream); 276 header.setJMSCorrelationID(MessageID.unmarshal(dataStream)); 277 dataStream.close(); 278 byteStream.close(); 279 } catch (IOException ioe) { 280 JMSException jmsex = new JMSException ("Correlation ID is corrupt"); 281 jmsex.setLinkedException(ioe); 282 throw jmsex; 283 } 284 } 285 286 public final String getJMSCorrelationID() throws JMSException 287 { 288 return header.jmsCorrelationID.toString(); 289 } 290 291 public final void setJMSCorrelationID(String correlationID) 292 { 293 header.setJMSCorrelationID(MessageID.unmarshal(correlationID)); 294 } 295 296 public final Destination getJMSReplyTo() throws JMSException 297 { 298 if (header.jmsReplyTo == null) 299 return null; 300 if (header.jmsReplyTo.startsWith(JmsTopic.TOPIC_PREFIX)) 301 return new JmsTopic(header.jmsReplyTo); 302 303 throw new RuntimeException ("fix me"); 306 } 307 308 public final void setJMSReplyTo(Destination replyTo) 309 { 310 header.setJMSReplyTo(replyTo); 311 } 312 313 public final Destination getJMSDestination() throws JMSException 314 { 315 if (header.jmsDestination == null) 316 return null; 317 if (header.jmsDestination.startsWith(JmsTopic.TOPIC_PREFIX)) 318 return new JmsTopic(header.jmsDestination); 319 320 throw new RuntimeException ("fix me"); 323 } 324 325 public final void setJMSDestination(Destination destination) 326 { 327 header.setJMSDestination(destination); 328 } 329 330 public final int getJMSDeliveryMode() 331 { 332 return header.jmsDeliveryMode; 333 } 334 335 public final void setJMSDeliveryMode(int deliveryMode) 336 { 337 header.setJMSDeliveryMode(deliveryMode); 338 339 if (deliveryMode == javax.jms.DeliveryMode.PERSISTENT) 341 this.ackHelper = new AckHelper(this); 342 } 343 344 public final boolean getJMSRedelivered() 345 { 346 return header.jmsRedelivered; 347 } 348 349 public final void setJMSRedelivered(boolean redelivered) 350 { 351 header.setJMSRedelivered(redelivered); 352 } 353 354 public final String getJMSType() 355 { 356 return header.jmsType; 357 } 358 359 public final void setJMSType(String type) 360 { 361 header.setJMSType(type); 362 } 363 364 public final long getJMSExpiration() 365 { 366 return header.jmsExpiration; 367 } 368 369 public final void setJMSExpiration(long expiration) 370 { 371 header.setJMSExpiration(expiration); 372 } 373 374 public final int getJMSPriority() 375 { 376 return header.jmsPriority; 377 } 378 379 public final void setJMSPriority(int priority) 380 { 381 header.setJMSPriority(priority); 382 } 383 384 388 public final void clearProperties() throws JMSException 389 { 390 checkWrite(); 391 properties.clear(); 392 } 393 394 public final boolean propertyExists(String name) throws JMSException 395 { 396 return properties.primitiveExists(name); 397 } 398 399 public final boolean getBooleanProperty(String name) throws JMSException 400 { 401 return properties.getBoolean(name); 402 } 403 404 public final byte getByteProperty(String name) 405 throws JMSException , NumberFormatException 406 { 407 return properties.getByte(name); 408 } 409 410 public final short getShortProperty(String name) 411 throws JMSException , NumberFormatException 412 { 413 return properties.getShort(name); 414 } 415 416 public final int getIntProperty(String name) 417 throws JMSException , NumberFormatException 418 { 419 return properties.getInt(name); 420 } 421 422 public final long getLongProperty(String name) 423 throws JMSException , NumberFormatException 424 { 425 return properties.getLong(name); 426 } 427 428 public final float getFloatProperty(String name) 429 throws JMSException , NumberFormatException 430 { 431 return properties.getFloat(name); 432 } 433 434 public final double getDoubleProperty(String name) 435 throws JMSException , NumberFormatException 436 { 437 return properties.getDouble(name); 438 } 439 440 public final String getStringProperty(String name) throws JMSException 441 { 442 return properties.getString(name); 443 } 444 445 public final Object getObjectProperty(String name) 446 { 447 if (name.equals(JmsDestination.DESTINATION_PROPERTY_NAME)) 451 return header.jmsDestination; 452 453 return properties.getObject(name); 454 } 455 456 public final Enumeration getPropertyNames() throws JMSException 457 { 458 return properties.getPrimitiveNames(); 459 } 460 461 462 public final void setBooleanProperty(String name, boolean value) throws JMSException 463 { 464 checkWrite(); 465 properties.setBoolean(name, value); 466 } 467 468 public final void setByteProperty(String name, byte value) throws JMSException 469 { 470 checkWrite(); 471 properties.setByte(name, value); 472 } 473 474 public final void setShortProperty(String name, short value) throws JMSException 475 { 476 checkWrite(); 477 properties.setShort(name, value); 478 } 479 480 public final void setIntProperty(String name, int value) throws JMSException 481 { 482 checkWrite(); 483 properties.setInt(name, value); 484 } 485 486 public final void setLongProperty(String name, long value) throws JMSException 487 { 488 checkWrite(); 489 properties.setLong(name, value); 490 } 491 492 public final void setFloatProperty(String name, float value) throws JMSException 493 { 494 checkWrite(); 495 properties.setFloat(name, value); 496 } 497 498 public final void setDoubleProperty(String name, double value) throws JMSException 499 { 500 checkWrite(); 501 properties.setDouble(name, value); 502 } 503 504 public final void setStringProperty(String name, String value) throws JMSException 505 { 506 checkWrite(); 507 properties.setString(name, value); 508 } 509 510 public final void setObjectProperty(String name, Object value) throws JMSException 511 { 512 checkWrite(); 513 properties.setObject(name, value); 514 } 515 516 517 521 public void setRoutingMask(BitSet mask) { routingMask = mask; } 522 public BitSet getRoutingMask() { return routingMask; } 523 524 public void setSessionCallback(JmsSession session) { this.sessionCallback = session; } 525 526 public final void setSendingTargetID(int id) { this.sendingTargetID = id; } 527 public final int getSendingTargetID() { return this.sendingTargetID; } 528 529 public final void setLocal(boolean val) { this.local = val; } 530 public final boolean isLocal() { return this.local; } 531 532 533 540 public final AckHelper getAckHelper() { return this.ackHelper; } 541 542 545 public final void setJMSSystemMsgType(int type) 546 { 547 header.setJMSSystemMsgType(type); 548 } 549 550 553 public final int getJMSSystemMsgType() 554 { 555 return header.jmsSystemMsgType; 556 } 557 558 561 public final void setReadOnly() 562 { 563 readOnly = true; 564 } 565 566 570 571 574 byte getMarshalingID() 575 { 576 return MessageEncoder.MESSAGE; 577 } 578 579 580 584 protected void checkWrite() throws JMSException 585 { 586 if (this.readOnly) { 587 JMSException jmse = new 588 MessageNotWriteableException ("This Message is readOnly"); 589 throw jmse; 590 } 591 } 592 593 } 594 | Popular Tags |