1 24 package org.objectweb.joram.client.jms; 25 26 import org.objectweb.joram.shared.excepts.MessageValueException; 27 28 import java.io.*; 29 import java.util.*; 30 31 import javax.jms.JMSException ; 32 import javax.jms.MessageFormatException ; 33 import javax.jms.MessageNotWriteableException ; 34 35 import org.objectweb.joram.shared.messages.ConversionHelper; 36 import org.objectweb.joram.shared.excepts.MessageValueException; 37 38 41 public final class MapMessage extends Message implements javax.jms.MapMessage { 42 43 private transient HashMap map; 44 45 48 MapMessage() { 49 super(); 50 momMsg.type = momMsg.MAP; 51 52 map = new HashMap(); 53 } 54 55 65 MapMessage(Session session, 66 org.objectweb.joram.shared.messages.Message momMsg) throws MessageFormatException { 67 super(session, momMsg); 68 69 ByteArrayInputStream bais = null; 70 ObjectInputStream ois = null; 71 try { 72 bais = new ByteArrayInputStream(momMsg.body); 73 ois = new ObjectInputStream(bais); 74 map = (HashMap) ois.readObject(); 75 } catch (Exception exc) { 76 MessageFormatException jE = 77 new MessageFormatException ("Error while getting the body."); 78 jE.setLinkedException(exc); 79 throw jE; 80 } finally { 81 try { 82 ois.close(); 83 } catch (IOException exc) {} 84 try { 85 bais.close(); 86 } catch (IOException exc) {} 87 } 88 } 89 90 95 public void clearBody() throws JMSException { 96 super.clearBody(); 97 map.clear(); 98 } 99 100 101 106 public void setBoolean(String name, boolean value) throws JMSException { 107 setObject(name, new Boolean (value)); 108 } 109 110 115 public void setByte(String name, byte value) throws JMSException { 116 setObject(name, new Byte (value)); 117 } 118 119 124 public void setBytes(String name, byte[] value) throws JMSException { 125 setObject(name, value); 126 } 127 128 133 public void setBytes(String name, byte[] value, int offset, int length) throws JMSException { 134 byte[] buff = new byte[length]; 135 System.arraycopy(value, offset, buff, 0, length); 136 setObject(name, buff); 137 } 138 139 144 public void setChar(String name, char value) throws JMSException { 145 setObject(name, new Character (value)); 146 } 147 148 153 public void setDouble(String name, double value) throws JMSException { 154 setObject(name, new Double (value)); 155 } 156 157 162 public void setFloat(String name, float value) throws JMSException { 163 setObject(name, new Float (value)); 164 } 165 166 171 public void setInt(String name, int value) throws JMSException { 172 setObject(name, new Integer (value)); 173 } 174 175 180 public void setLong(String name, long value) throws JMSException { 181 setObject(name, new Long (value)); 182 } 183 184 189 public void setShort(String name, short value) throws JMSException { 190 setObject(name, new Short (value)); 191 } 192 193 198 public void setString(String name, String value) throws JMSException { 199 setObject(name, value); 200 } 201 202 208 public void setObject(String name, Object value) throws JMSException { 209 if (RObody) 210 throw new MessageNotWriteableException ("Can't set a value as the message" 211 + " body is read-only."); 212 213 if (name == null || name.equals("")) 214 throw new IllegalArgumentException ("Invalid null or empty value name."); 215 216 if (value instanceof Boolean || value instanceof Character || 217 value instanceof Number || value instanceof String || 218 value instanceof byte[] || value == null) 219 map.put(name, value); 220 else 221 throw new MessageFormatException ("Can't set non Java primitive type as" 222 + " a map value."); 223 } 224 225 230 public boolean getBoolean(String name) throws JMSException { 231 try { 232 return ConversionHelper.toBoolean(map.get(name)); 233 } catch (MessageValueException mE) { 234 throw new MessageFormatException (mE.getMessage()); 235 } 236 } 237 238 243 public byte getByte(String name) throws JMSException { 244 try { 245 return ConversionHelper.toByte(map.get(name)); 246 } catch (MessageValueException mE) { 247 throw new MessageFormatException (mE.getMessage()); 248 } 249 } 250 251 256 public byte[] getBytes(String name) throws JMSException { 257 try { 258 return ConversionHelper.toBytes(map.get(name)); 259 } catch (MessageValueException mE) { 260 throw new MessageFormatException (mE.getMessage()); 261 } 262 } 263 264 269 public char getChar(String name) throws JMSException { 270 try { 271 return ConversionHelper.toChar(map.get(name)); 272 } catch (MessageValueException mE) { 273 throw new MessageFormatException (mE.getMessage()); 274 } 275 } 276 277 282 public double getDouble(String name) throws JMSException { 283 try { 284 return ConversionHelper.toDouble(map.get(name)); 285 } catch (MessageValueException mE) { 286 throw new MessageFormatException (mE.getMessage()); 287 } 288 } 289 290 295 public float getFloat(String name) throws JMSException { 296 try { 297 return ConversionHelper.toFloat(map.get(name)); 298 } catch (MessageValueException mE) { 299 throw new MessageFormatException (mE.getMessage()); 300 } 301 } 302 303 308 public int getInt(String name) throws JMSException { 309 try { 310 return ConversionHelper.toInt(map.get(name)); 311 } catch (MessageValueException mE) { 312 throw new MessageFormatException (mE.getMessage()); 313 } 314 } 315 316 321 public long getLong(String name) throws JMSException { 322 try { 323 return ConversionHelper.toLong(map.get(name)); 324 } catch (MessageValueException mE) { 325 throw new MessageFormatException (mE.getMessage()); 326 } 327 } 328 329 334 public Object getObject(String name) throws JMSException { 335 return map.get(name); 336 } 337 338 343 public short getShort(String name) throws JMSException { 344 try { 345 return ConversionHelper.toShort(map.get(name)); 346 } catch (MessageValueException mE) { 347 throw new MessageFormatException (mE.getMessage()); 348 } 349 } 350 351 356 public String getString(String name) throws JMSException { 357 Object value = map.get(name); 358 if (value instanceof byte[]) 359 throw new MessageFormatException ("Type " + value.getClass().getName() 360 + " can't be converted to String."); 361 return ConversionHelper.toString(map.get(name)); 362 } 363 364 369 public boolean itemExists(String name) throws JMSException { 370 return map.containsKey(name); 371 } 372 373 378 public Enumeration getMapNames() throws JMSException { 379 Vector vec = new Vector(); 380 if (map.keySet() != null) { 381 for (Iterator it = map.keySet().iterator(); it.hasNext(); ) 382 vec.add(it.next()); 383 } 384 return vec.elements(); 385 } 386 387 393 protected void prepare() throws JMSException { 394 super.prepare(); 395 396 try { 397 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 398 ObjectOutputStream oos = new ObjectOutputStream(baos); 399 oos.writeObject(map); 400 oos.flush(); 401 momMsg.body = baos.toByteArray(); 402 oos.close(); 403 baos.close(); 404 } catch (IOException exc) { 405 MessageFormatException jExc = 406 new MessageFormatException ("The message body could not be serialized."); 407 jExc.setLinkedException(exc); 408 throw jExc; 409 } 410 } 411 } 412 | Popular Tags |