1 46 package org.mr.api.jms; 47 import java.io.IOException ; 48 import java.util.Enumeration ; 49 import java.util.HashMap ; 50 import java.util.Iterator ; 51 import java.util.Map ; 52 53 import javax.jms.JMSException ; 54 import javax.jms.MapMessage ; 55 import javax.jms.MessageFormatException ; 56 import javax.jms.MessageNotWriteableException ; 57 58 import org.mr.core.util.byteable.Byteable; 59 import org.mr.core.util.byteable.ByteableInputStream; 60 import org.mr.core.util.byteable.ByteableOutputStream; 61 import org.mr.core.util.byteable.ByteableRegistry; 62 63 105 public class MantaMapMessage extends MantaMessage implements MapMessage { 106 107 111 MantaMapMessage(){ 112 113 } 114 120 public MantaMapMessage(MantaSession sess) throws JMSException { 121 this(sess,new HashMap (INITIAL_SIZE)); 122 123 } 125 126 132 public MantaMapMessage(MantaSession sess,Map message) throws JMSException { 133 super(); 134 flags = (flags & ONLY_MSG_TYPES)| MAP_MESSAGE; 135 136 this.message = message; 137 this.creatingSession = sess; 138 } 140 143 public boolean getBoolean(String name) throws JMSException { 144 145 Object val = message.get(name); 146 if (val == null) 147 return Boolean.valueOf(null).booleanValue(); 148 else if (val instanceof Boolean ) 149 return ((Boolean ) val).booleanValue(); 150 else if (val instanceof String ) 151 return Boolean.valueOf(val.toString()).booleanValue(); 152 else 153 throw new MessageFormatException ("MNJMS00039 : CAN NOT CONVERT "+val.getClass().getName()+ 154 " TO BOOLEAN. FAILED ON METHOD getBoolean()."); 155 156 } 158 161 public byte getByte(String name) throws JMSException { 162 163 Object val = message.get(name); 164 if (val == null) 165 return Byte.valueOf(null).byteValue(); 166 else if (val instanceof Byte ) 167 return ((Byte ) val).byteValue(); 168 else if (val instanceof String ) 169 try { 170 return Byte.valueOf(val.toString()).byteValue(); 171 } 172 catch (NumberFormatException nfe) { 173 throw new MessageFormatException ("MNJMS0003A : FAILED ON METHOD getByte(). ERROR TEXT :"+nfe.getMessage()); 174 175 } 176 else 177 throw new MessageFormatException ("MNJMS0003B : CAN NOT CONVERT "+val.getClass().getName()+ 178 " TO BYTE. FAILED ON METHOD getByte()."); 179 180 181 } 183 186 public short getShort(String name) throws JMSException { 187 Object val = message.get(name); 188 if (val == null) 189 return Short.valueOf(null).shortValue(); 190 else if (val instanceof Short ) 191 return ((Short ) val).shortValue(); 192 else if (val instanceof Byte ) 193 return ((Byte ) val).shortValue(); 194 else if (val instanceof String ) 195 try { 196 return Short.valueOf(val.toString()).shortValue(); 197 } 198 catch (NumberFormatException nfe) { 199 throw new MessageFormatException ("MNJMS0003C : FAILED ON METHOD getShort(). ERROR TEXT :"+nfe.getMessage()); 200 } 201 202 else 203 throw new MessageFormatException ("MNJMS0003D : CAN NOT CONVERT "+val.getClass().getName()+ 204 " TO SHORT. FAILED ON METHOD getShort()."); 205 206 207 } 209 212 public char getChar(String name) throws JMSException { 213 Object val = message.get(name); 214 if (val == null) 215 throw new NullPointerException (); 216 217 else if (val instanceof Character ) 218 return ((Character ) val).charValue(); 219 else 220 throw new MessageFormatException ("MNJMS0003F : CAN NOT CONVERT "+val.getClass().getName()+ 221 " TO CHAR. FAILED ON METHOD getChar()."); 222 223 } 225 226 229 public int getInt(String name) throws JMSException { 230 Object val = message.get(name); 231 if (val == null) 232 return Integer.valueOf(null).intValue(); 233 else if (val instanceof Integer ) 234 return ((Integer ) val).intValue(); 235 else if (val instanceof Short ) 236 return ((Short ) val).intValue(); 237 else if (val instanceof Byte ) 238 return ((Byte ) val).intValue(); 239 else if (val instanceof String ) 240 try { 241 return Integer.valueOf(val.toString()).intValue(); 242 } 243 catch (NumberFormatException nfe) { 244 throw new MessageFormatException ("MNJMS00040 : FAILED ON METHOD getInt(). ERROR TEXT :"+nfe.getMessage()); 245 } 246 247 else 248 throw new MessageFormatException ("MNJMS00041 : CAN NOT CONVERT "+val.getClass().getName()+ 249 " TO INTEGER. FAILED ON METHOD getInt()."); 250 } 252 255 public long getLong(String name) throws JMSException { 256 Object val = message.get(name); 257 if (val == null) 258 return Long.valueOf(null).longValue(); 259 else if (val instanceof Long ) 260 return ((Long ) val).longValue(); 261 else if (val instanceof Integer ) 262 return ((Integer ) val).longValue(); 263 else if (val instanceof Short ) 264 return ((Short ) val).longValue(); 265 else if (val instanceof Byte ) 266 return ((Byte ) val).longValue(); 267 else if (val instanceof String ) 268 try { 269 return Long.valueOf(val.toString()).longValue(); 270 } 271 catch (NumberFormatException nfe) { 272 throw new MessageFormatException ("MNJMS00042 : FAILED ON METHOD getLong(). ERROR TEXT :"+nfe.getMessage()); 273 } 274 275 else 276 throw new MessageFormatException ("MNJMS00043 : CAN NOT CONVERT "+val.getClass().getName()+ 277 " TO LONG. FAILED ON METHOD getLong()."); 278 } 280 283 public float getFloat(String name) throws JMSException { 284 Object val = message.get(name); 285 if (val == null) 286 return Float.valueOf(null).floatValue(); 287 else if (val instanceof Float ) 288 return ((Float ) val).floatValue(); 289 if (val instanceof String ) 290 try { 291 return Float.valueOf(val.toString()).floatValue(); 292 } 293 catch (NumberFormatException nfe) { 294 throw new MessageFormatException ("MNJMS00044 : FAILED ON METHOD getFloat(). ERROR TEXT :"+nfe.getMessage()); 295 } 296 297 else 298 throw new MessageFormatException ("MNJMS00045 : CAN NOT CONVERT "+val.getClass().getName()+ 299 " TO FLOAT. FAILED ON METHOD getFloat()."); 300 301 } 303 306 public double getDouble(String name) throws JMSException { 307 Object val = message.get(name); 308 if (val == null) 309 return Double.valueOf(null).doubleValue(); 310 else if (val instanceof Double ) 311 return ((Double ) val).doubleValue(); 312 else if (val instanceof Float ) 313 return ((Float ) val).floatValue(); 314 else if (val instanceof String ) 315 try { 316 return Float.valueOf(val.toString()).floatValue(); 317 } 318 catch (NumberFormatException nfe) { 319 throw new MessageFormatException ("MNJMS00046 : FAILED ON METHOD getDouble(). ERROR TEXT :"+nfe.getMessage()); 320 } 321 322 else 323 throw new MessageFormatException ("MNJMS00047 : CAN NOT CONVERT "+val.getClass().getName()+ 324 " TO DOUBLE. FAILED ON METHOD getDouble()."); 325 326 } 328 331 public String getString(String name) throws JMSException { 332 Object val = message.get(name); 333 if (val == null) 334 return null; 335 else if (val instanceof byte[]) 336 throw new MessageFormatException ("MNJMS00048 : CAN NOT CONVERT A BYTE ARRAY TO STRING. FAILED ON METHOD getString()."); 337 338 else 339 return val.toString(); 340 341 } 343 346 public byte[] getBytes(String name) throws JMSException { 347 Object val = message.get(name); 348 if (val==null || val instanceof byte[]) 349 return (byte[]) val; 350 else 351 throw new MessageFormatException ("MNJMS00049 : CAN NOT GET A BYTE ARRAY FROM "+ 352 val.getClass().getName()+" FAILED ON METHOD getBytes()."); 353 354 } 356 359 public Object getObject(String name) throws JMSException { 360 361 return message.get(name); 362 363 } 365 366 369 public Enumeration getMapNames() throws JMSException { 370 371 final Iterator i = message.keySet().iterator(); 373 374 return new Enumeration () { 376 public boolean hasMoreElements() { 377 return i.hasNext(); 378 } 380 public Object nextElement() { 381 return i.next(); 382 } }; 384 } 386 387 404 public void setBoolean(String name, boolean value) throws JMSException { 405 checkUpdateOperation(name); 406 message.put(name, Boolean.valueOf(value)); 407 } 409 412 public void setByte(String name, byte value) throws JMSException { 413 checkUpdateOperation(name); 414 message.put(name, new Byte (value)); 415 } 417 420 public void setShort(String name, short value) throws JMSException { 421 checkUpdateOperation(name); 422 message.put(name, new Short (value)); 423 } 425 428 public void setChar(String name, char value) throws JMSException { 429 checkUpdateOperation(name); 430 message.put(name, new Character (value)); 431 } 433 436 public void setInt(String name, int value) throws JMSException { 437 checkUpdateOperation(name); 438 message.put(name, new Integer (value)); 439 } 441 444 public void setLong(String name, long value) throws JMSException { 445 checkUpdateOperation(name); 446 message.put(name, new Long (value)); 447 } 449 452 public void setFloat(String name, float value) throws JMSException { 453 checkUpdateOperation(name); 454 message.put(name, new Float (value)); 455 } 457 460 public void setDouble(String name, double value) throws JMSException { 461 checkUpdateOperation(name); 462 message.put(name, new Double (value)); 463 } 465 468 public void setString(String name, String value) throws JMSException { 469 checkUpdateOperation(name); 470 message.put(name, value); 471 } 473 476 public void setBytes(String name, byte[] value) throws JMSException { 477 checkUpdateOperation(name); 478 message.put(name, value); 479 } 481 485 public void setBytes(String name, byte[] values, int offset, int length) throws JMSException { 486 checkUpdateOperation(name); 487 try { 488 byte[] a = new byte[length]; 489 for(int i = 0;i < length;i++) { 490 a[i] = values[i + offset]; 491 } message.put(name, a); 493 } catch(Exception x) { 495 throw new JMSException ("MNJMS0004A : FAILED ON METHOD setBytes(). ERROR TEXT : "+x.getMessage()); 496 } } 499 502 public void setObject(String name, Object value) throws JMSException { 503 504 checkUpdateOperation(name); 505 506 if (value instanceof String || value instanceof Short || 507 value instanceof Byte || value instanceof Long || 508 value instanceof Boolean || value instanceof Float || 509 value instanceof Integer || value instanceof Double || 510 value instanceof byte[] || value instanceof Character ) 511 { 512 message.put(name, value); 513 } else 514 throw new MessageFormatException ("MNJMS0004B : FAILED ON METHOD setObject(). INVALID OBJECT TYPE : "+value.getClass().getName()); 515 } 516 517 518 519 522 public boolean itemExists(String name) throws JMSException { 523 return (message.get(name) != null); 524 } 526 528 public void toBytes(ByteableOutputStream out) throws IOException { 529 530 super.toBytes(out); 531 532 out.writeInt(message.size()); 534 Iterator msgIt = message.keySet().iterator(); 535 while (msgIt.hasNext()) { 536 537 String key = (String )msgIt.next(); 539 Object val = message.get(key); 540 541 out.writeUTF(key); 543 544 if (val instanceof Boolean ) { 546 out.write(BOOLEAN); 547 out.writeBoolean( ((Boolean )val).booleanValue()); 548 } 549 else if (val instanceof Byte ) { 550 out.write(BYTE); 551 out.writeByte( ((Byte )val).byteValue()); 552 } 553 else if (val instanceof byte[]) { 554 out.write(BYTEARR); 555 out.writeInt( ((byte[])val).length); 556 out.write((byte[])val); 557 } 558 else if (val instanceof Character ) { 559 out.write(CHAR); 560 out.writeChar(((Character )val).charValue()); 561 } 562 else if (val instanceof Double ) { 563 out.write(DOUBLE); 564 out.writeDouble( ((Double )val).doubleValue()); 565 } 566 else if (val instanceof Float ) { 567 out.write(FLOAT); 568 out.writeFloat( ((Float )val).floatValue()); 569 } 570 else if (val instanceof Integer ) { 571 out.write(INTEGER); 572 out.writeInt( ((Integer )val).intValue()); 573 } 574 else if (val instanceof Long ) { 575 out.write(LONG); 576 out.writeLong( ((Long )val).longValue()); 577 } 578 else if (val instanceof Short ) { 579 out.write(SHORT); 580 out.writeShort( ((Short )val).shortValue()); 581 } 582 else if (val instanceof String ) { 583 out.write(STRING); 584 out.writeUTF((String )val); 585 } 586 587 } 588 589 out.flush(); 590 591 592 } 593 594 595 public Byteable createInstance(ByteableInputStream in) throws IOException { 596 597 MantaMapMessage result; 598 try { 599 result = new MantaMapMessage(null); 600 createHeadersAndProperties(result,in); 601 } 602 catch (JMSException jmse) { 603 throw new IOException ("MNJMS00FFD : METHOD createInstance() FAILED ON MantaMapMessage. EXCEPTION TEXT : "+jmse.getMessage()); 604 } 605 606 int size = in.readInt(); 608 for (int i = 0; i<size ; i++) { 609 String key = in.readUTF(); 611 byte type = in.readByte(); 612 switch (type) { 613 case BYTE : result.message.put(key,new Byte (in.readByte())); break ; 614 case BOOLEAN : result.message.put(key, new Boolean (in.readBoolean())); break; 615 case SHORT : result.message.put(key,new Short (in.readShort())); break; 616 case INTEGER : result.message.put(key,new Integer (in.readInt())); break; 617 case DOUBLE : result.message.put(key,new Double (in.readDouble())); break; 618 case FLOAT : result.message.put(key,new Float (in.readFloat())); break; 619 case STRING : result.message.put(key,in.readUTF()); break; 620 case LONG : result.message.put(key,new Long (in.readLong())); break; 621 case BYTEARR : byte[] surreal = new byte[in.readInt()]; 622 in.read(surreal); 623 result.message.put(key,surreal); break; 624 625 case CHAR : result.message.put(key,new Character (in.readChar()));break; 626 627 } 628 } 629 return result; 630 } 631 632 public void registerToByteableRegistry() { 633 ByteableRegistry.registerByteableFactory(getByteableName() , this); 634 } 635 636 public final static String BYTEABLENAME = "MantaMapMsg"; 637 public String getByteableName() { 638 639 return BYTEABLENAME; 640 } 641 642 public static void register() throws JMSException { 643 MantaMapMessage instance = new MantaMapMessage(null); 644 instance.registerToByteableRegistry(); 645 } 646 private void checkUpdateOperation(String str) throws IllegalArgumentException , MessageNotWriteableException { 647 if (!isWriteable()) 648 throw new MessageNotWriteableException ("MNJMS0004C : MESSAGE IS NOT WRITEABLE. FAILED IN METHOD checkUpdateOperation()."); 649 if (str == null ||str.length() == 0) 650 throw new IllegalArgumentException ("MNJMS0004D : ILLEGAL PROPERTY NAME. FAILED IN METHOD checkUpdateOperation()."); 651 } 653 655 658 public final void clearBody() throws JMSException { 659 super.clearBody(); 660 message = new HashMap (INITIAL_SIZE); 665 666 } 668 public MantaMessage makeCopy() throws JMSException { 669 MantaMapMessage copy = new MantaMapMessage(); 670 initCopy(copy); 671 copy.message = new HashMap (message); 672 return copy; 673 } 674 675 688 private Map message = null; 690 private final static int INITIAL_SIZE = 11; 692 693 } | Popular Tags |