1 28 29 package com.caucho.jms.message; 30 31 import javax.jms.JMSException ; 32 import javax.jms.MapMessage ; 33 import javax.jms.MessageFormatException ; 34 import java.util.Collections ; 35 import java.util.Enumeration ; 36 import java.util.HashMap ; 37 38 41 public class MapMessageImpl extends MessageImpl implements MapMessage { 42 private HashMap <String ,Object > _map = new HashMap <String ,Object >(); 43 44 47 public boolean itemExists(String name) 48 throws JMSException 49 { 50 return getObject(name) != null; 51 } 52 53 56 public Enumeration getMapNames() 57 throws JMSException 58 { 59 return Collections.enumeration(_map.keySet()); 60 } 61 62 65 public boolean getBoolean(String name) 66 throws JMSException 67 { 68 return ObjectConverter.toBoolean(getObject(name)); 69 } 70 71 74 public byte getByte(String name) 75 throws JMSException 76 { 77 return ObjectConverter.toByte(getObject(name)); 78 } 79 80 83 public short getShort(String name) 84 throws JMSException 85 { 86 return ObjectConverter.toShort(getObject(name)); 87 } 88 89 92 public int getInt(String name) 93 throws JMSException 94 { 95 return ObjectConverter.toInt(getObject(name)); 96 } 97 98 101 public long getLong(String name) 102 throws JMSException 103 { 104 return ObjectConverter.toLong(getObject(name)); 105 } 106 107 110 public float getFloat(String name) 111 throws JMSException 112 { 113 return ObjectConverter.toFloat(getObject(name)); 114 } 115 116 119 public double getDouble(String name) 120 throws JMSException 121 { 122 return ObjectConverter.toDouble(getObject(name)); 123 } 124 125 128 public char getChar(String name) 129 throws JMSException 130 { 131 return ObjectConverter.toChar(getObject(name)); 132 } 133 134 137 public String getString(String name) 138 throws JMSException 139 { 140 return ObjectConverter.toString(getObject(name)); 141 } 142 143 146 public byte []getBytes(String name) 147 throws JMSException 148 { 149 return ObjectConverter.toBytes(getObject(name)); 150 } 151 152 155 public int getBytes(String name, byte []value) 156 throws JMSException 157 { 158 byte []bytes = ObjectConverter.toBytes(getObject(name)); 159 160 if (bytes == null) 161 return 0; 162 163 int sublen = bytes.length; 164 if (value.length < sublen) 165 sublen = value.length; 166 167 for (int i = 0; i < sublen; i++) 168 value[i] = bytes[i]; 169 170 return sublen; 171 } 172 173 176 public Object getObject(String name) 177 throws JMSException 178 { 179 return _map.get(name); 180 } 181 182 185 public void clearBody() 186 throws JMSException 187 { 188 super.clearBody(); 189 190 _map.clear(); 191 } 192 193 196 public void setBoolean(String name, boolean b) 197 throws JMSException 198 { 199 setObject(name, new Boolean (b)); 200 } 201 202 205 public void setByte(String name, byte b) 206 throws JMSException 207 { 208 setObject(name, new Byte (b)); 209 } 210 211 214 public void setShort(String name, short s) 215 throws JMSException 216 { 217 setObject(name, new Short (s)); 218 } 219 220 223 public void setInt(String name, int i) 224 throws JMSException 225 { 226 setObject(name, new Integer (i)); 227 } 228 229 232 public void setLong(String name, long l) 233 throws JMSException 234 { 235 setObject(name, new Long (l)); 236 } 237 238 241 public void setFloat(String name, float f) 242 throws JMSException 243 { 244 setObject(name, new Float (f)); 245 } 246 247 250 public void setDouble(String name, double d) 251 throws JMSException 252 { 253 setObject(name, new Double (d)); 254 } 255 256 259 public void setString(String name, String s) 260 throws JMSException 261 { 262 setObject(name, s); 263 } 264 265 268 public void setChar(String name, char ch) 269 throws JMSException 270 { 271 setObject(name, new Character (ch)); 272 } 273 274 277 public void setBytes(String name, byte []buf) 278 throws JMSException 279 { 280 setBytes(name, buf, 0, buf.length); 281 } 282 283 286 public void setBytes(String name, byte []buf, int offset, int length) 287 throws JMSException 288 { 289 byte []newBuf = new byte[length]; 290 291 System.arraycopy(buf, offset, newBuf, 0, length); 292 293 setObject(name, newBuf); 294 } 295 296 299 public void setObject(String name, Object obj) 300 throws JMSException 301 { 302 checkBodyWriteable(); 303 304 if (obj == null) { 305 } 306 else if (obj instanceof byte[]) { 307 } 308 else if (! obj.getClass().getName().startsWith("java.lang.")) 309 throw new MessageFormatException (L.l("'{0}' is an invalid value for a map message.", 310 obj.getClass().getName())); 311 _map.put(name, obj); 312 } 313 314 public MessageImpl copy() 315 { 316 MapMessageImpl msg = new MapMessageImpl(); 317 318 copy(msg); 319 320 return msg; 321 } 322 323 protected void copy(MapMessageImpl newMsg) 324 { 325 super.copy(newMsg); 326 327 newMsg._map = new HashMap <String ,Object >(_map); 328 } 329 } 330 331 | Popular Tags |