1 18 package org.apache.activemq.command; 19 20 import java.io.DataInputStream ; 21 import java.io.DataOutputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.util.Collections ; 26 import java.util.Enumeration ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.zip.DeflaterOutputStream ; 30 import java.util.zip.InflaterInputStream ; 31 32 import javax.jms.JMSException ; 33 import javax.jms.MapMessage ; 34 import javax.jms.MessageFormatException ; 35 import javax.jms.MessageNotWriteableException ; 36 37 import org.apache.activemq.ActiveMQConnection; 38 import org.apache.activemq.util.ByteArrayInputStream; 39 import org.apache.activemq.util.ByteArrayOutputStream; 40 import org.apache.activemq.util.ByteSequence; 41 import org.apache.activemq.util.JMSExceptionSupport; 42 import org.apache.activemq.util.MarshallingSupport; 43 import org.apache.activemq.wireformat.WireFormat; 44 45 81 public class ActiveMQMapMessage extends ActiveMQMessage implements MapMessage { 82 83 public static final byte DATA_STRUCTURE_TYPE = CommandTypes.ACTIVEMQ_MAP_MESSAGE; 84 85 transient protected Map map = new HashMap (); 86 87 public Message copy() { 88 ActiveMQMapMessage copy = new ActiveMQMapMessage(); 89 copy(copy); 90 return copy; 91 } 92 93 private void copy(ActiveMQMapMessage copy) { 94 storeContent(); 95 super.copy(copy); 96 } 97 98 public void beforeMarshall(WireFormat wireFormat) throws IOException { 100 super.beforeMarshall(wireFormat); 101 storeContent(); 102 } 103 104 private void storeContent() { 105 try { 106 if( getContent()==null && !map.isEmpty()) { 107 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); 108 OutputStream os = bytesOut; 109 ActiveMQConnection connection = getConnection(); 110 if( connection!= null && connection.isUseCompression() ) { 111 compressed = true; 112 os = new DeflaterOutputStream (os); 113 } 114 DataOutputStream dataOut = new DataOutputStream (os); 115 MarshallingSupport.marshalPrimitiveMap(map, dataOut); 116 dataOut.close(); 117 setContent(bytesOut.toByteSequence()); 118 } 119 } catch (IOException e) { 120 throw new RuntimeException (e); 121 } 122 } 123 124 130 private void loadContent() throws JMSException { 131 try { 132 if( getContent()!=null && map.isEmpty() ) { 133 ByteSequence content = getContent(); 134 InputStream is = new ByteArrayInputStream(content); 135 if( isCompressed() ) { 136 is = new InflaterInputStream (is); 137 } 138 DataInputStream dataIn = new DataInputStream (is); 139 map = MarshallingSupport.unmarshalPrimitiveMap(dataIn); 140 dataIn.close(); 141 } 142 } catch (IOException e) { 143 throw JMSExceptionSupport.create(e); 144 } 145 } 146 147 public byte getDataStructureType() { 148 return DATA_STRUCTURE_TYPE; 149 } 150 151 public String getJMSXMimeType() { 152 return "jms/map-message"; 153 } 154 155 156 161 public void clearBody() throws JMSException { 162 super.clearBody(); 163 map.clear(); 164 } 165 166 174 public boolean getBoolean(String name) throws JMSException { 175 initializeReading(); 176 Object value = map.get(name); 177 if (value == null) { 178 return false; 179 } 180 if (value instanceof Boolean ) { 181 return ((Boolean ) value).booleanValue(); 182 } 183 if (value instanceof String ) { 184 return Boolean.valueOf(value.toString()).booleanValue(); 185 } else { 186 throw new MessageFormatException (" cannot read a boolean from " + value.getClass().getName()); 187 } 188 } 189 190 198 public byte getByte(String name) throws JMSException { 199 initializeReading(); 200 Object value = map.get(name); 201 if (value == null) { 202 return 0; 203 } 204 if (value instanceof Byte ) { 205 return ((Byte ) value).byteValue(); 206 } 207 if (value instanceof String ) { 208 return Byte.valueOf(value.toString()).byteValue(); 209 } else { 210 throw new MessageFormatException (" cannot read a byte from " + value.getClass().getName()); 211 } 212 } 213 214 222 public short getShort(String name) throws JMSException { 223 initializeReading(); 224 Object value = map.get(name); 225 if (value == null) { 226 return 0; 227 } 228 if (value instanceof Short ) { 229 return ((Short ) value).shortValue(); 230 } 231 if (value instanceof Byte ) { 232 return ((Byte ) value).shortValue(); 233 } 234 if (value instanceof String ) { 235 return Short.valueOf(value.toString()).shortValue(); 236 } else { 237 throw new MessageFormatException (" cannot read a short from " + value.getClass().getName()); 238 } 239 } 240 241 249 public char getChar(String name) throws JMSException { 250 initializeReading(); 251 Object value = map.get(name); 252 if (value == null) { 253 throw new NullPointerException (); 254 } 255 if (value instanceof Character ) { 256 return ((Character ) value).charValue(); 257 } else { 258 throw new MessageFormatException (" cannot read a short from " + value.getClass().getName()); 259 } 260 } 261 262 270 public int getInt(String name) throws JMSException { 271 initializeReading(); 272 Object value = map.get(name); 273 if (value == null) { 274 return 0; 275 } 276 if (value instanceof Integer ) { 277 return ((Integer ) value).intValue(); 278 } 279 if (value instanceof Short ) { 280 return ((Short ) value).intValue(); 281 } 282 if (value instanceof Byte ) { 283 return ((Byte ) value).intValue(); 284 } 285 if (value instanceof String ) { 286 return Integer.valueOf(value.toString()).intValue(); 287 } else { 288 throw new MessageFormatException (" cannot read an int from " + value.getClass().getName()); 289 } 290 } 291 292 300 public long getLong(String name) throws JMSException { 301 initializeReading(); 302 Object value = map.get(name); 303 if (value == null) { 304 return 0; 305 } 306 if (value instanceof Long ) { 307 return ((Long ) value).longValue(); 308 } 309 if (value instanceof Integer ) { 310 return ((Integer ) value).longValue(); 311 } 312 if (value instanceof Short ) { 313 return ((Short ) value).longValue(); 314 } 315 if (value instanceof Byte ) { 316 return ((Byte ) value).longValue(); 317 } 318 if (value instanceof String ) { 319 return Long.valueOf(value.toString()).longValue(); 320 } else { 321 throw new MessageFormatException (" cannot read a long from " + value.getClass().getName()); 322 } 323 } 324 325 333 public float getFloat(String name) throws JMSException { 334 initializeReading(); 335 Object value = map.get(name); 336 if (value == null) { 337 return 0; 338 } 339 if (value instanceof Float ) { 340 return ((Float ) value).floatValue(); 341 } 342 if (value instanceof String ) { 343 return Float.valueOf(value.toString()).floatValue(); 344 } else { 345 throw new MessageFormatException (" cannot read a float from " + value.getClass().getName()); 346 } 347 } 348 349 357 public double getDouble(String name) throws JMSException { 358 initializeReading(); 359 Object value = map.get(name); 360 if (value == null) { 361 return 0; 362 } 363 if (value instanceof Double ) { 364 return ((Double ) value).doubleValue(); 365 } 366 if (value instanceof Float ) { 367 return ((Float ) value).floatValue(); 368 } 369 if (value instanceof String ) { 370 return Float.valueOf(value.toString()).floatValue(); 371 } else { 372 throw new MessageFormatException (" cannot read a double from " + value.getClass().getName()); 373 } 374 } 375 376 385 public String getString(String name) throws JMSException { 386 initializeReading(); 387 Object value = map.get(name); 388 if (value == null) { 389 return null; 390 } 391 if (value instanceof byte[]) { 392 throw new MessageFormatException ("Use getBytes to read a byte array"); 393 } else { 394 return value.toString(); 395 } 396 } 397 398 407 public byte[] getBytes(String name) throws JMSException { 408 initializeReading(); 409 Object value = map.get(name); 410 if ( value instanceof byte[] ) { 411 return (byte[]) value; 412 } else { 413 throw new MessageFormatException (" cannot read a byte[] from " + value.getClass().getName()); 414 } 415 } 416 417 429 public Object getObject(String name) throws JMSException { 430 initializeReading(); 431 return map.get(name); 432 } 433 434 440 public Enumeration getMapNames() throws JMSException { 441 initializeReading(); 442 return Collections.enumeration(map.keySet()); 443 } 444 445 protected void put(String name, Object value) throws JMSException { 446 if (name == null) { 447 throw new IllegalArgumentException ("The name of the property cannot be null."); 448 } 449 if (name.length() == 0) { 450 throw new IllegalArgumentException ("The name of the property cannot be an emprty string."); 451 } 452 map.put(name, value); 453 } 454 455 464 public void setBoolean(String name, boolean value) throws JMSException { 465 initializeWriting(); 466 put(name, (value) ? Boolean.TRUE : Boolean.FALSE); 467 } 468 469 478 public void setByte(String name, byte value) throws JMSException { 479 initializeWriting(); 480 put(name, new Byte (value)); 481 } 482 483 492 public void setShort(String name, short value) throws JMSException { 493 initializeWriting(); 494 put(name, new Short (value)); 495 } 496 497 506 public void setChar(String name, char value) throws JMSException { 507 initializeWriting(); 508 put(name, new Character (value)); 509 } 510 511 520 public void setInt(String name, int value) throws JMSException { 521 initializeWriting(); 522 put(name, new Integer (value)); 523 } 524 525 534 public void setLong(String name, long value) throws JMSException { 535 initializeWriting(); 536 put(name, new Long (value)); 537 } 538 539 548 public void setFloat(String name, float value) throws JMSException { 549 initializeWriting(); 550 put(name, new Float (value)); 551 } 552 553 562 public void setDouble(String name, double value) throws JMSException { 563 initializeWriting(); 564 put(name, new Double (value)); 565 } 566 567 576 public void setString(String name, String value) throws JMSException { 577 initializeWriting(); 578 put(name, value); 579 } 580 581 591 public void setBytes(String name, byte[] value) throws JMSException { 592 initializeWriting(); 593 if (value != null) { 594 put(name, value); 595 } else { 596 map.remove(name); 597 } 598 } 599 600 611 public void setBytes(String name, byte[] value, int offset, int length) throws JMSException { 612 initializeWriting(); 613 byte[] data = new byte[length]; 614 System.arraycopy(value, offset, data, 0, length); 615 put(name, data); 616 } 617 618 630 public void setObject(String name, Object value) throws JMSException { 631 initializeWriting(); 632 if (value != null) { 633 if (!(value instanceof byte[])) { 635 checkValidObject(value); 636 } 637 put(name, value); 638 } else { 639 put(name, null); 640 } 641 } 642 643 650 public boolean itemExists(String name) throws JMSException { 651 initializeReading(); 652 return map.containsKey(name); 653 } 654 655 private void initializeReading() throws JMSException { 656 loadContent(); 657 } 658 659 private void initializeWriting() throws MessageNotWriteableException { 660 checkReadOnlyBody(); 661 setContent(null); 662 } 663 664 public String toString() { 665 return super.toString() + " ActiveMQMapMessage{ " + 666 "theTable = " + map + 667 " }"; 668 } 669 670 public Map getContentMap() throws JMSException { 671 initializeReading(); 672 return map; 673 } 674 } 675 | Popular Tags |