1 28 29 package com.caucho.jms.message; 30 31 import com.caucho.jms.session.SessionImpl; 32 import com.caucho.log.Log; 33 import com.caucho.util.L10N; 34 import com.caucho.util.NullEnumeration; 35 36 import javax.jms.Destination ; 37 import javax.jms.JMSException ; 38 import javax.jms.Message ; 39 import javax.jms.MessageFormatException ; 40 import javax.jms.MessageNotReadableException ; 41 import javax.jms.MessageNotWriteableException ; 42 import java.lang.ref.SoftReference ; 43 import java.util.Collections ; 44 import java.util.Enumeration ; 45 import java.util.HashMap ; 46 import java.util.HashSet ; 47 import java.util.logging.Level ; 48 import java.util.logging.Logger ; 49 50 53 public class MessageImpl implements Message { 54 static final Logger log = Log.open(MessageImpl.class); 55 static final L10N L = new L10N(MessageImpl.class); 56 57 private static final HashSet <String > _reserved; 58 59 private volatile SoftReference <SessionImpl> _sessionRef; 60 61 private String _messageId; 62 private String _correlationId; 63 64 private long _timestamp; 65 private long _expiration; 66 67 private Destination _destination; 68 private Destination _replyTo; 69 70 private int _deliveryMode; 71 private boolean _isRedelivered; 72 73 private String _messageType; 74 private int _priority = 4; 75 76 private HashMap <String ,Object > _properties; 77 78 private boolean _isHeaderWriteable = true; 79 private boolean _isBodyWriteable = true; 80 81 public MessageImpl() 82 { 83 } 84 85 88 public void setSession(SessionImpl session) 89 { 90 _sessionRef = new SoftReference <SessionImpl>(session); 91 } 92 93 96 public String getJMSMessageID() 97 throws JMSException 98 { 99 return _messageId; 100 } 101 102 107 public void setJMSMessageID(String id) 108 throws JMSException 109 { 110 _messageId = id; 111 } 112 113 116 public long getJMSTimestamp() 117 throws JMSException 118 { 119 return _timestamp; 120 } 121 122 127 public void setJMSTimestamp(long time) 128 throws JMSException 129 { 130 _timestamp = time; 131 } 132 133 136 public byte []getJMSCorrelationIDAsBytes() 137 throws JMSException 138 { 139 try { 140 if (_correlationId == null) 141 return null; 142 else 143 return _correlationId.getBytes("UTF8"); 144 } catch (Throwable e) { 145 log.log(Level.WARNING, e.toString(), e); 146 147 return null; 148 } 149 } 150 151 156 public void setJMSCorrelationIDAsBytes(byte []id) 157 throws JMSException 158 { 159 try { 160 _correlationId = new String (id, 0, id.length, "UTF8"); 161 } catch (Exception e) { 162 log.log(Level.WARNING, e.toString(), e); 163 } 164 } 165 166 169 public String getJMSCorrelationID() 170 throws JMSException 171 { 172 return _correlationId; 173 } 174 175 180 public void setJMSCorrelationID(String id) 181 throws JMSException 182 { 183 _correlationId = id; 184 } 185 186 189 public Destination getJMSReplyTo() 190 throws JMSException 191 { 192 return _replyTo; 193 } 194 195 200 public void setJMSReplyTo(Destination replyTo) 201 throws JMSException 202 { 203 _replyTo = replyTo; 204 } 205 206 209 public Destination getJMSDestination() 210 throws JMSException 211 { 212 return _destination; 213 } 214 215 220 public void setJMSDestination(Destination destination) 221 throws JMSException 222 { 223 _destination = destination; 224 } 225 226 229 public int getJMSDeliveryMode() 230 throws JMSException 231 { 232 return _deliveryMode; 233 } 234 235 240 public void setJMSDeliveryMode(int deliveryMode) 241 throws JMSException 242 { 243 _deliveryMode = deliveryMode; 244 } 245 246 249 public boolean getJMSRedelivered() 250 throws JMSException 251 { 252 return _isRedelivered; 253 } 254 255 260 public void setJMSRedelivered(boolean isRedelivered) 261 throws JMSException 262 { 263 _isRedelivered = isRedelivered; 264 } 265 266 269 public String getJMSType() 270 throws JMSException 271 { 272 return _messageType; 273 } 274 275 280 public void setJMSType(String type) 281 throws JMSException 282 { 283 _messageType = type; 284 } 285 286 289 public long getJMSExpiration() 290 throws JMSException 291 { 292 return _expiration; 293 } 294 295 300 public void setJMSExpiration(long time) 301 throws JMSException 302 { 303 _expiration = time; 304 } 305 306 309 public int getJMSPriority() 310 throws JMSException 311 { 312 return _priority; 313 } 314 315 320 public void setJMSPriority(int priority) 321 throws JMSException 322 { 323 _priority = priority; 324 } 325 326 329 public void clearProperties() 330 throws JMSException 331 { 332 if (_properties != null) 333 _properties.clear(); 334 _isHeaderWriteable = true; 335 } 336 337 340 public boolean propertyExists(String name) 341 throws JMSException 342 { 343 if (_properties == null) 344 return false; 345 346 return _properties.keySet().contains(name); 347 } 348 349 352 public boolean getBooleanProperty(String name) 353 throws JMSException 354 { 355 return ObjectConverter.toBoolean(getObjectProperty(name)); 356 } 357 358 361 public byte getByteProperty(String name) 362 throws JMSException 363 { 364 return ObjectConverter.toByte(getObjectProperty(name)); 365 } 366 367 370 public short getShortProperty(String name) 371 throws JMSException 372 { 373 return ObjectConverter.toShort(getObjectProperty(name)); 374 } 375 376 379 public int getIntProperty(String name) 380 throws JMSException 381 { 382 return ObjectConverter.toInt(getObjectProperty(name)); 383 } 384 385 388 public long getLongProperty(String name) 389 throws JMSException 390 { 391 return ObjectConverter.toLong(getObjectProperty(name)); 392 } 393 394 397 public float getFloatProperty(String name) 398 throws JMSException 399 { 400 return ObjectConverter.toFloat(getObjectProperty(name)); 401 } 402 403 406 public double getDoubleProperty(String name) 407 throws JMSException 408 { 409 return ObjectConverter.toDouble(getObjectProperty(name)); 410 } 411 412 415 public String getStringProperty(String name) 416 throws JMSException 417 { 418 Object prop = getObjectProperty(name); 419 420 if (prop == null) 421 return null; 422 423 return String.valueOf(prop); 424 } 425 426 429 public Object getObjectProperty(String name) 430 throws JMSException 431 { 432 if (_properties == null || name == null) 433 return null; 434 else 435 return _properties.get(name); 436 } 437 438 441 public Enumeration getPropertyNames() 442 throws JMSException 443 { 444 if (_properties == null) 445 return NullEnumeration.create(); 446 else 447 return Collections.enumeration(_properties.keySet()); 448 } 449 450 456 public void setBooleanProperty(String name, boolean value) 457 throws JMSException 458 { 459 setObjectProperty(name, new Boolean (value)); 460 } 461 462 468 public void setByteProperty(String name, byte value) 469 throws JMSException 470 { 471 setObjectProperty(name, new Byte (value)); 472 } 473 474 480 public void setShortProperty(String name, short value) 481 throws JMSException 482 { 483 setObjectProperty(name, new Short (value)); 484 } 485 486 492 public void setIntProperty(String name, int value) 493 throws JMSException 494 { 495 setObjectProperty(name, new Integer (value)); 496 } 497 498 504 public void setLongProperty(String name, long value) 505 throws JMSException 506 { 507 setObjectProperty(name, new Long (value)); 508 } 509 510 516 public void setFloatProperty(String name, float value) 517 throws JMSException 518 { 519 setObjectProperty(name, new Float (value)); 520 } 521 522 528 public void setDoubleProperty(String name, double value) 529 throws JMSException 530 { 531 setObjectProperty(name, new Double (value)); 532 } 533 534 540 public void setStringProperty(String name, String value) 541 throws JMSException 542 { 543 setObjectProperty(name, value); 544 } 545 546 552 public void setObjectProperty(String name, Object value) 553 throws JMSException 554 { 555 checkPropertyWriteable(); 556 557 if (name == null) 558 throw new NullPointerException (); 559 if (isReserved(name)) 560 throw new JMSException (L.l("'{0}' is a reserved property name.", 561 name)); 562 563 if (! (value == null || 564 value instanceof Number || 565 value instanceof String || 566 value instanceof Boolean )) 567 throw new MessageFormatException (L.l("{0} is an illegal object property value", 568 value.getClass().getName())); 569 570 if (_properties == null) 571 _properties = new HashMap <String ,Object >(); 572 573 _properties.put(name, value); 574 } 575 576 579 public void acknowledge() 580 throws JMSException 581 { 582 SoftReference <SessionImpl> sessionRef = _sessionRef; 583 SessionImpl session; 584 585 if (sessionRef != null && (session = sessionRef.get()) != null) { 586 session.acknowledge(); 587 } 588 589 _sessionRef = null; 590 } 591 592 595 public void clearBody() 596 throws JMSException 597 { 598 _isBodyWriteable = true; 599 } 600 601 604 public void setReceive() 605 throws JMSException 606 { 607 _isHeaderWriteable = false; 608 _isBodyWriteable = false; 609 } 610 611 614 protected void setBodyReadOnly() 615 throws JMSException 616 { 617 _isBodyWriteable = false; 618 } 619 620 623 public HashMap <String ,Object > getProperties() 624 { 625 return _properties; 626 } 627 628 public MessageImpl copy() 629 { 630 MessageImpl msg = new MessageImpl(); 631 632 copy(msg); 633 634 return msg; 635 } 636 637 protected void checkHeaderWriteable() 638 throws JMSException 639 { 640 if (! _isHeaderWriteable) 641 throw new MessageNotWriteableException (L.l("received messages can't be written.")); 642 } 643 644 protected void checkPropertyWriteable() 645 throws JMSException 646 { 647 if (! _isHeaderWriteable) 648 throw new MessageNotWriteableException (L.l("properties for received messages are read-only.")); 649 } 650 651 protected void checkBodyWriteable() 652 throws JMSException 653 { 654 if (! _isBodyWriteable) 655 throw new MessageNotWriteableException (L.l("received messages can't be written.")); 656 } 657 658 protected void checkBodyReadable() 659 throws JMSException 660 { 661 if (_isBodyWriteable) 662 throw new MessageNotReadableException (L.l("write-only messages can't be read until reset() is called.")); 663 } 664 665 protected void copy(MessageImpl newMsg) 666 { 667 if (_properties != null) { 668 newMsg._properties = new HashMap <String ,Object >(_properties); 669 } 670 671 newMsg._messageId = _messageId; 672 newMsg._correlationId = _correlationId; 673 674 newMsg._timestamp = _timestamp; 675 newMsg._expiration = _expiration; 676 677 newMsg._destination = _destination; 678 newMsg._replyTo = _replyTo; 679 680 newMsg._deliveryMode = _deliveryMode; 681 newMsg._isRedelivered = _isRedelivered; 682 683 newMsg._messageType = _messageType; 684 newMsg._priority = _priority; 685 } 686 687 public String toString() 688 { 689 if (_messageId != null) 690 return getClass().getName() + "[" + _messageId + "]"; 691 else 692 return getClass().getName() + "@" + System.identityHashCode(this); 693 } 694 695 public static boolean isReserved(String name) 696 { 697 return _reserved.contains(name.toUpperCase()); 698 } 699 700 static { 701 _reserved = new HashSet <String >(); 702 _reserved.add("TRUE"); 703 _reserved.add("FALSE"); 704 _reserved.add("NULL"); 705 _reserved.add("NOT"); 706 _reserved.add("AND"); 707 _reserved.add("OR"); 708 _reserved.add("BETWEEN"); 709 _reserved.add("LIKE"); 710 _reserved.add("IN"); 711 _reserved.add("IS"); 712 _reserved.add("ESCAPE"); 713 } 714 } 715 716 | Popular Tags |