1 22 package org.jboss.mq; 23 24 import java.io.Externalizable ; 25 import java.io.IOException ; 26 import java.io.ObjectInput ; 27 import java.io.ObjectOutput ; 28 import java.util.Collections ; 29 import java.util.Enumeration ; 30 import java.util.HashMap ; 31 import java.util.Hashtable ; 32 33 import javax.jms.JMSException ; 34 import javax.jms.MapMessage ; 35 import javax.jms.MessageFormatException ; 36 import javax.jms.MessageNotWriteableException ; 37 38 import org.jboss.util.Primitives; 39 40 47 public class SpyMapMessage extends SpyMessage implements MapMessage , Externalizable 48 { 49 51 52 private final static long serialVersionUID = -4917633165373197269L; 53 54 56 57 HashMap content; 58 59 61 63 66 public SpyMapMessage() 67 { 68 content = new HashMap (); 69 } 70 71 73 75 public void setBoolean(String name, boolean value) throws JMSException 76 { 77 checkName(name); 78 if (header.msgReadOnly) 79 throw new MessageNotWriteableException ("Message is ReadOnly !"); 80 synchronized (content) 81 { 82 content.put(name, Primitives.valueOf(value)); 83 } 84 } 85 86 public void setByte(String name, byte value) throws JMSException 87 { 88 checkName(name); 89 if (header.msgReadOnly) 90 throw new MessageNotWriteableException ("Message is ReadOnly !"); 91 synchronized (content) 92 { 93 content.put(name, new Byte (value)); 94 } 95 } 96 97 public void setShort(String name, short value) throws JMSException 98 { 99 checkName(name); 100 if (header.msgReadOnly) 101 throw new MessageNotWriteableException ("Message is ReadOnly !"); 102 synchronized (content) 103 { 104 content.put(name, new Short (value)); 105 } 106 } 107 108 public void setChar(String name, char value) throws JMSException 109 { 110 checkName(name); 111 if (header.msgReadOnly) 112 throw new MessageNotWriteableException ("Message is ReadOnly !"); 113 synchronized (content) 114 { 115 content.put(name, new Character (value)); 116 } 117 } 118 119 public void setInt(String name, int value) throws JMSException 120 { 121 checkName(name); 122 if (header.msgReadOnly) 123 throw new MessageNotWriteableException ("Message is ReadOnly !"); 124 synchronized (content) 125 { 126 content.put(name, new Integer (value)); 127 } 128 } 129 130 public void setLong(String name, long value) throws JMSException 131 { 132 checkName(name); 133 if (header.msgReadOnly) 134 throw new MessageNotWriteableException ("Message is ReadOnly !"); 135 synchronized (content) 136 { 137 content.put(name, new Long (value)); 138 } 139 } 140 141 public void setFloat(String name, float value) throws JMSException 142 { 143 checkName(name); 144 if (header.msgReadOnly) 145 throw new MessageNotWriteableException ("Message is ReadOnly !"); 146 synchronized (content) 147 { 148 content.put(name, new Float (value)); 149 } 150 } 151 152 public void setDouble(String name, double value) throws JMSException 153 { 154 checkName(name); 155 if (header.msgReadOnly) 156 throw new MessageNotWriteableException ("Message is ReadOnly !"); 157 synchronized (content) 158 { 159 content.put(name, new Double (value)); 160 } 161 } 162 163 public void setString(String name, String value) throws JMSException 164 { 165 checkName(name); 166 if (header.msgReadOnly) 167 throw new MessageNotWriteableException ("Message is ReadOnly !"); 168 synchronized (content) 169 { 170 content.put(name, value); 171 } 172 } 173 174 public void setBytes(String name, byte[] value) throws JMSException 175 { 176 checkName(name); 177 if (header.msgReadOnly) 178 throw new MessageNotWriteableException ("Message is ReadOnly !"); 179 synchronized (content) 180 { 181 content.put(name, value.clone()); 182 } 183 } 184 185 public void setBytes(String name, byte[] value, int offset, int length) throws JMSException 186 { 187 checkName(name); 188 if (header.msgReadOnly) 189 throw new MessageNotWriteableException ("Message is ReadOnly !"); 190 191 if (offset + length > value.length) 192 throw new JMSException ("Array is too small"); 193 byte[] temp = new byte[length]; 194 for (int i = 0; i < length; i++) 195 temp[i] = value[i + offset]; 196 197 synchronized (content) 198 { 199 content.put(name, temp); 200 } 201 } 202 203 public void setObject(String name, Object value) throws JMSException 204 { 205 checkName(name); 206 if (header.msgReadOnly) 207 throw new MessageNotWriteableException ("Message is ReadOnly !"); 208 209 synchronized (content) 210 { 211 if (value instanceof Boolean ) 212 content.put(name, value); 213 else if (value instanceof Byte ) 214 content.put(name, value); 215 else if (value instanceof Short ) 216 content.put(name, value); 217 else if (value instanceof Character ) 218 content.put(name, value); 219 else if (value instanceof Integer ) 220 content.put(name, value); 221 else if (value instanceof Long ) 222 content.put(name, value); 223 else if (value instanceof Float ) 224 content.put(name, value); 225 else if (value instanceof Double ) 226 content.put(name, value); 227 else if (value instanceof String ) 228 content.put(name, value); 229 else if (value instanceof byte[]) 230 content.put(name, ((byte[]) value).clone()); 231 else 232 throw new MessageFormatException ("Invalid object type."); 233 } 234 } 235 236 public boolean getBoolean(String name) throws JMSException 237 { 238 Object value; 239 synchronized (content) 240 { 241 value = content.get(name); 242 } 243 if (value == null) 244 return Boolean.valueOf(null).booleanValue(); 245 246 if (value instanceof Boolean ) 247 return ((Boolean ) value).booleanValue(); 248 else if (value instanceof String ) 249 return Boolean.valueOf((String ) value).booleanValue(); 250 else 251 throw new MessageFormatException ("Invalid conversion"); 252 } 253 254 public byte getByte(String name) throws JMSException 255 { 256 Object value; 257 synchronized (content) 258 { 259 value = content.get(name); 260 } 261 if (value == null) 262 return Byte.parseByte(null); 263 264 if (value instanceof Byte ) 265 return ((Byte ) value).byteValue(); 266 else if (value instanceof String ) 267 return Byte.parseByte((String ) value); 268 else 269 throw new MessageFormatException ("Invalid conversion"); 270 } 271 272 public short getShort(String name) throws JMSException 273 { 274 Object value; 275 synchronized (content) 276 { 277 value = content.get(name); 278 } 279 if (value == null) 280 return Short.parseShort(null); 281 282 if (value instanceof Byte ) 283 return ((Byte ) value).shortValue(); 284 else if (value instanceof Short ) 285 return ((Short ) value).shortValue(); 286 else if (value instanceof String ) 287 return Short.parseShort((String ) value); 288 else 289 throw new MessageFormatException ("Invalid conversion"); 290 } 291 292 public char getChar(String name) throws JMSException 293 { 294 Object value; 295 synchronized (content) 296 { 297 value = content.get(name); 298 } 299 if (value == null) 300 throw new NullPointerException ("Invalid conversion"); 301 302 if (value instanceof Character ) 303 return ((Character ) value).charValue(); 304 else 305 throw new MessageFormatException ("Invalid conversion"); 306 } 307 308 public int getInt(String name) throws JMSException 309 { 310 Object value; 311 synchronized (content) 312 { 313 value = content.get(name); 314 } 315 if (value == null) 316 return Integer.parseInt(null); 317 318 if (value instanceof Byte ) 319 return ((Byte ) value).intValue(); 320 else if (value instanceof Short ) 321 return ((Short ) value).intValue(); 322 else if (value instanceof Integer ) 323 return ((Integer ) value).intValue(); 324 else if (value instanceof String ) 325 return Integer.parseInt((String ) value); 326 else 327 throw new MessageFormatException ("Invalid conversion"); 328 } 329 330 public long getLong(String name) throws JMSException 331 { 332 Object value; 333 synchronized (content) 334 { 335 value = content.get(name); 336 } 337 if (value == null) 338 return Long.parseLong(null); 339 340 if (value instanceof Byte ) 341 return ((Byte ) value).longValue(); 342 else if (value instanceof Short ) 343 return ((Short ) value).longValue(); 344 else if (value instanceof Integer ) 345 return ((Integer ) value).longValue(); 346 else if (value instanceof Long ) 347 return ((Long ) value).longValue(); 348 else if (value instanceof String ) 349 return Long.parseLong((String ) value); 350 else 351 throw new MessageFormatException ("Invalid conversion"); 352 } 353 354 public float getFloat(String name) throws JMSException 355 { 356 Object value; 357 synchronized (content) 358 { 359 value = content.get(name); 360 } 361 if (value == null) 362 return Float.parseFloat(null); 363 364 if (value instanceof Float ) 365 return ((Float ) value).floatValue(); 366 else if (value instanceof String ) 367 return Float.parseFloat((String ) value); 368 else 369 throw new MessageFormatException ("Invalid conversion"); 370 } 371 372 public double getDouble(String name) throws JMSException 373 { 374 Object value; 375 synchronized (content) 376 { 377 value = content.get(name); 378 } 379 if (value == null) 380 return Double.parseDouble(null); 381 382 if (value instanceof Float ) 383 return ((Float ) value).doubleValue(); 384 else if (value instanceof Double ) 385 return ((Double ) value).doubleValue(); 386 else if (value instanceof String ) 387 return Double.parseDouble((String ) value); 388 else 389 throw new MessageFormatException ("Invalid conversion"); 390 } 391 392 public String getString(String name) throws JMSException 393 { 394 Object value; 395 synchronized (content) 396 { 397 value = content.get(name); 398 } 399 if (value == null) 400 return null; 401 402 if (value instanceof Boolean ) 403 return ((Boolean ) value).toString(); 404 else if (value instanceof Byte ) 405 return ((Byte ) value).toString(); 406 else if (value instanceof Short ) 407 return ((Short ) value).toString(); 408 else if (value instanceof Character ) 409 return ((Character ) value).toString(); 410 else if (value instanceof Integer ) 411 return ((Integer ) value).toString(); 412 else if (value instanceof Long ) 413 return ((Long ) value).toString(); 414 else if (value instanceof Float ) 415 return ((Float ) value).toString(); 416 else if (value instanceof Double ) 417 return ((Double ) value).toString(); 418 else if (value instanceof String ) 419 return (String ) value; 420 else 421 throw new MessageFormatException ("Invalid conversion"); 422 } 423 424 public byte[] getBytes(String name) throws JMSException 425 { 426 Object value; 427 synchronized (content) 428 { 429 value = content.get(name); 430 } 431 if (value == null) 432 return null; 433 if (value instanceof byte[]) 434 return (byte[]) value; 435 else 436 throw new MessageFormatException ("Invalid conversion"); 437 } 438 439 public Object getObject(String name) throws JMSException 440 { 441 synchronized (content) 442 { 443 return content.get(name); 444 } 445 } 446 447 public Enumeration getMapNames() throws JMSException 448 { 449 synchronized (content) 450 { 451 return Collections.enumeration(new HashMap (content).keySet()); 452 } 453 } 454 455 public boolean itemExists(String name) throws JMSException 456 { 457 synchronized (content) 458 { 459 return content.containsKey(name); 460 } 461 } 462 463 465 public void clearBody() throws JMSException 466 { 467 content = new HashMap (); 468 super.clearBody(); 469 } 470 471 public SpyMessage myClone() throws JMSException 472 { 473 SpyMapMessage result = MessagePool.getMapMessage(); 474 result.copyProps(this); 475 synchronized (content) 476 { 477 result.content = (HashMap ) this.content.clone(); 478 } 479 return result; 480 } 481 482 484 public void writeExternal(ObjectOutput out) throws IOException 485 { 486 super.writeExternal(out); 487 out.writeObject(content); 491 } 492 493 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 494 { 495 super.readExternal(in); 496 Object object = in.readObject(); 497 if (object instanceof Hashtable ) 499 { 500 Hashtable ht = (Hashtable ) object; 501 content = new HashMap (ht.size()); 502 content.putAll(ht); 503 } 504 else 505 content = (HashMap ) object; 506 } 507 508 510 512 514 519 private void checkName(String name) 520 { 521 if (name == null) 522 throw new IllegalArgumentException ("Name must not be null."); 523 524 if (name.equals("")) 525 throw new IllegalArgumentException ("Name must not be an empty String."); 526 } 527 528 530 } 531 534 | Popular Tags |