1 24 package com.scalagent.kjoram; 25 26 import com.scalagent.kjoram.excepts.*; 27 import com.scalagent.kjoram.messages.ConversionHelper; 28 29 import java.util.*; 30 31 public class MapMessage extends Message 32 { 33 34 private Hashtable map; 35 36 private boolean RObody = false; 37 38 39 42 MapMessage() 43 { 44 super(); 45 map = new Hashtable(); 46 } 47 48 58 MapMessage(Session sess, com.scalagent.kjoram.messages.Message momMsg) 59 throws MessageFormatException 60 { 61 super(sess, momMsg); 62 try { 63 map = momMsg.getMap(); 64 } 65 catch (Exception exc) { 66 MessageFormatException jE = 67 new MessageFormatException("Error while getting the body."); 68 jE.setLinkedException(exc); 69 throw jE; 70 } 71 RObody = true; 72 } 73 74 75 80 public void clearBody() throws JMSException 81 { 82 super.clearBody(); 83 map.clear(); 84 RObody = false; 85 } 86 87 88 93 public void setBoolean(String name, boolean value) throws JMSException 94 { 95 setObject(name, new Boolean (value)); 96 } 97 98 103 public void setByte(String name, byte value) throws JMSException 104 { 105 setObject(name, new Byte (value)); 106 } 107 108 113 public void setBytes(String name, byte[] value) throws JMSException 114 { 115 setObject(name, value); 116 } 117 118 123 public void setBytes(String name, byte[] value, int offset, int length) 124 throws JMSException 125 { 126 byte[] buff = new byte[length]; 127 128 for (int i = 0; i < length; i++) 129 buff[i] = value[i + offset]; 130 131 setObject(name, buff); 132 } 133 134 139 public void setChar(String name, char value) throws JMSException 140 { 141 setObject(name, new Character (value)); 142 } 143 144 149 154 159 164 169 public void setInt(String name, int value) throws JMSException 170 { 171 setObject(name, new Integer (value)); 172 } 173 174 179 public void setLong(String name, long value) throws JMSException 180 { 181 setObject(name, new Long (value)); 182 } 183 184 189 public void setShort(String name, short value) throws JMSException 190 { 191 setObject(name, new Short (value)); 192 } 193 194 199 public void setString(String name, String value) throws JMSException 200 { 201 setObject(name, value); 202 } 203 204 210 public void setObject(String name, Object value) throws JMSException 211 { 212 if (RObody) 213 throw new MessageNotWriteableException("Can't set a value as the message" 214 + " body is read-only."); 215 if (name == null || name.equals("")) 216 throw new IllegalArgumentException ("Invalid null or empty value name."); 217 218 if (value == null) 219 return; 220 221 if (value instanceof Boolean || 222 value instanceof Character 223 || value instanceof Long 224 || value instanceof Short 225 || value instanceof Byte 226 || value instanceof Integer 227 || value instanceof String 228 || value instanceof byte[]) 229 map.put(name, value); 230 else 231 throw new MessageFormatException("Can't set non Java primitive type as" 232 + " a map value."); 233 } 234 235 240 public boolean getBoolean(String name) throws JMSException 241 { 242 try { 243 return ConversionHelper.toBoolean(map.get(name)); 244 } 245 catch (MessageValueException mE) { 246 throw new MessageFormatException(mE.getMessage()); 247 } 248 } 249 250 255 public byte getByte(String name) throws JMSException 256 { 257 try { 258 return ConversionHelper.toByte(map.get(name)); 259 } 260 catch (MessageValueException mE) { 261 throw new MessageFormatException(mE.getMessage()); 262 } 263 } 264 265 270 public byte[] getBytes(String name) throws JMSException 271 { 272 try { 273 return ConversionHelper.toBytes(map.get(name)); 274 } 275 catch (MessageValueException mE) { 276 throw new MessageFormatException(mE.getMessage()); 277 } 278 } 279 280 285 public char getChar(String name) throws JMSException 286 { 287 try { 288 return ConversionHelper.toChar(map.get(name)); 289 } 290 catch (MessageValueException mE) { 291 throw new MessageFormatException(mE.getMessage()); 292 } 293 } 294 295 300 310 315 325 330 public int getInt(String name) throws JMSException 331 { 332 try { 333 return ConversionHelper.toInt(map.get(name)); 334 } 335 catch (MessageValueException mE) { 336 throw new MessageFormatException(mE.getMessage()); 337 } 338 } 339 340 345 public long getLong(String name) throws JMSException 346 { 347 try { 348 return ConversionHelper.toLong(map.get(name)); 349 } 350 catch (MessageValueException mE) { 351 throw new MessageFormatException(mE.getMessage()); 352 } 353 } 354 355 360 public Object getObject(String name) throws JMSException 361 { 362 return map.get(name); 363 } 364 365 370 public short getShort(String name) throws JMSException 371 { 372 try { 373 return ConversionHelper.toShort(map.get(name)); 374 } 375 catch (MessageValueException mE) { 376 throw new MessageFormatException(mE.getMessage()); 377 } 378 } 379 380 385 public String getString(String name) throws JMSException 386 { 387 return ConversionHelper.toString(map.get(name)); 388 } 389 390 395 public boolean itemExists(String name) throws JMSException 396 { 397 return (map.get(name) != null); 398 } 399 400 405 public Enumeration getMapNames() throws JMSException 406 { 407 return map.keys(); 408 } 409 410 411 417 protected void prepare() throws Exception 418 { 419 super.prepare(); 420 421 momMsg.clearBody(); 422 momMsg.setMap(map); 423 } 424 } 425 | Popular Tags |