1 7 package org.jboss.jms; 8 9 import java.io.Serializable ; 10 import java.util.Enumeration ; 11 12 import javax.jms.BytesMessage ; 13 import javax.jms.Destination ; 14 import javax.jms.JMSException ; 15 import javax.jms.MapMessage ; 16 import javax.jms.Message ; 17 import javax.jms.MessageNotWriteableException ; 18 import javax.jms.ObjectMessage ; 19 import javax.jms.StreamMessage ; 20 import javax.jms.TextMessage ; 21 22 import org.jboss.jms.client.SessionDelegate; 23 import org.jboss.jms.client.p2p.P2PSessionDelegate; 24 import org.jboss.jms.message.JBossMessage; 25 import org.jboss.jms.util.MessageProperties; 26 import org.jboss.util.id.GUID; 27 28 33 public class MessageImpl implements JBossMessage, Cloneable , Serializable 34 { 35 static final String BYTES_MESSAGE_NAME = "BytesMessage"; 36 static final String MAP_MESSAGE_NAME = "MapMessage"; 37 static final String MESSAGE_NAME = "Message"; 38 static final String OBJECT_MESSAGE_NAME = "ObjectMessage"; 39 static final String STREAM_MESSGE_NAME = "StreamMessage"; 40 static final String TEXT_MESSAGE_NAME = "TextMessage"; 41 42 protected Object body = null; 43 private String correlationID = null; 44 private int deliveryMode = Message.DEFAULT_DELIVERY_MODE; 45 46 private Destination destination = null; 47 private long expiration = 0; 48 private String messageID = null; 49 private int priority = Message.DEFAULT_PRIORITY; 50 private MessageProperties properties = new MessageProperties(); 51 private boolean readOnly = false; 52 private boolean redelivered = false; 53 private Destination replyTo = null; 54 private long timestamp = 0; 55 protected String type = null; 56 57 private P2PSessionDelegate session = null; 58 public long deliveryId = -1L; 59 private boolean local = false; 60 private String originClientId = null; 61 62 public static Message create(Class type) throws JMSException 63 { 64 if (type.equals(BytesMessage .class)) 65 { 66 return new BytesMessageImpl(); 67 } 68 if (type.equals(MapMessage .class)) 69 { 70 return new MapMessageImpl(); 71 } 72 if (type.equals(Message .class)) 73 { 74 return new MessageImpl(); 75 } 76 if (type.equals(ObjectMessage .class)) 77 { 78 return new ObjectMessageImpl(); 79 } 80 else if (type.equals(StreamMessage .class)) 81 { 82 return new StreamMessageImpl(); 83 } 84 else if (type.equals(BytesMessage .class)) 85 { 86 return new BytesMessageImpl(); 87 } 88 else if (type.equals(TextMessage .class)) 89 { 90 return new TextMessageImpl(); 91 } 92 else 93 { 94 throw new JMSException ("'" + type.getName() + "' is an invalid message type."); 95 } 96 } 97 98 public void acknowledge() throws JMSException 99 { 100 if (this.session != null) 101 { 102 this.session.acknowledge(this, true); 103 } 104 } 105 106 public void clearBody() 107 { 108 this.body = null; 110 this.readOnly = false; 111 } 112 113 public final void clearProperties() 114 { 115 this.properties.clear(); 116 } 117 118 public final boolean getBooleanProperty(String name) throws JMSException 119 { 120 return this.properties.getBoolean(name); 121 } 122 123 public final byte getByteProperty(String name) throws JMSException 124 { 125 return this.properties.getByte(name); 126 } 127 128 public final double getDoubleProperty(String name) throws JMSException 129 { 130 return this.properties.getDouble(name); 131 } 132 133 public final float getFloatProperty(String name) throws JMSException 134 { 135 return this.properties.getFloat(name); 136 } 137 138 public final int getIntProperty(String name) throws JMSException 139 { 140 return this.properties.getInt(name); 141 } 142 143 public final String getJMSCorrelationID() 144 { 145 return this.correlationID; 146 } 147 148 public final byte[] getJMSCorrelationIDAsBytes() 149 { 150 return this.correlationID.getBytes(); 151 } 152 153 public final int getJMSDeliveryMode() 154 { 155 return this.deliveryMode; 156 } 157 158 public final Destination getJMSDestination() 159 { 160 return this.destination; 161 } 162 163 public final long getJMSExpiration() 164 { 165 return this.expiration; 166 } 167 168 public final String getJMSMessageID() 169 { 170 return this.messageID; 171 } 172 173 public final int getJMSPriority() 174 { 175 return this.priority; 176 } 177 178 public final boolean getJMSRedelivered() 179 { 180 return this.redelivered; 181 } 182 183 public final Destination getJMSReplyTo() 184 { 185 return this.replyTo; 186 } 187 188 public final long getJMSTimestamp() 189 { 190 return this.timestamp; 191 } 192 193 public final String getJMSType() 194 { 195 return this.type; 196 } 197 198 public final long getLongProperty(String name) throws JMSException 199 { 200 return this.properties.getLong(name); 201 } 202 203 public final Object getObjectProperty(String name) throws JMSException 204 { 205 return this.properties.getObject(name); 206 } 207 208 public final Enumeration getPropertyNames() throws JMSException 209 { 210 return this.properties.getMapNames(); 211 } 212 213 public final short getShortProperty(String name) throws JMSException 214 { 215 return this.properties.getShort(name); 216 } 217 218 public final String getStringProperty(String name) throws JMSException 219 { 220 return this.properties.getString(name); 221 } 222 223 public final boolean isReadOnly() 224 { 225 return this.readOnly; 226 } 227 228 public final boolean hasExpired() 229 { 230 if (this.expiration < 1) 231 { 232 return false; 233 } 234 else 235 { 236 return System.currentTimeMillis() > this.expiration; 237 } 238 } 239 240 public final void setSession(P2PSessionDelegate session) 241 { 242 this.session = session; 243 } 244 245 public final void setDeliveryId(long id) 246 { 247 this.deliveryId = id; 248 } 249 250 public final void setOriginClientID(String clientId) 251 { 252 this.originClientId = clientId; 253 } 254 255 public final String getOrigianClientID() 256 { 257 return this.originClientId; 258 } 259 260 public final void setIsLocal(boolean value) 261 { 262 this.local = value; 263 } 264 265 public boolean isLocal() 266 { 267 return this.local; 268 } 269 270 public final boolean propertyExists(String name) 271 { 272 return this.properties.itemExists(name); 273 } 274 275 public final void setBooleanProperty(String name, boolean value) throws JMSException 276 { 277 this.properties.setBoolean(name, value); 278 } 279 280 public final void setByteProperty(String name, byte value) throws JMSException 281 { 282 this.properties.setByte(name, value); 283 } 284 285 public final void setDoubleProperty(String name, double value) throws JMSException 286 { 287 this.properties.setDouble(name, value); 288 } 289 290 public final void setFloatProperty(String name, float value) throws JMSException 291 { 292 this.properties.setFloat(name, value); 293 } 294 295 public final void setIntProperty(String name, int value) throws JMSException 296 { 297 this.properties.setInt(name, value); 298 } 299 300 public final void setJMSCorrelationID(String correlationID) 301 { 302 this.correlationID = correlationID; 303 } 304 305 public final void setJMSCorrelationIDAsBytes(byte[] correlationID) 306 { 307 throw new UnsupportedOperationException ("Not implemented."); 308 } 309 310 public final void setJMSDeliveryMode(int deliveryMode) 311 { 312 this.deliveryMode = deliveryMode; 313 } 314 315 public final void setJMSDestination(Destination destination) 316 { 317 this.destination = destination; 318 } 319 320 public final void setJMSExpiration(long expiration) 321 { 322 this.expiration = expiration; 323 } 324 325 public final void setJMSMessageID(String messageID) 326 { 327 this.messageID = messageID; 328 } 329 330 public final void setJMSPriority(int priority) 331 { 332 this.priority = priority; 333 } 334 335 public final void setJMSRedelivered(boolean redelivered) 336 { 337 this.redelivered = redelivered; 338 } 339 340 public final void setJMSReplyTo(Destination replyTo) 341 { 342 this.replyTo = replyTo; 343 } 344 345 public final void setJMSTimestamp(long timestamp) 346 { 347 this.timestamp = timestamp; 348 } 349 350 public final void setJMSType(String type) 351 { 352 this.type = type; 353 } 354 355 public final void setLongProperty(String name, long value) throws JMSException 356 { 357 this.properties.setLong(name, value); 358 } 359 360 public final void setObjectProperty(String name, Object value) throws JMSException 361 { 362 this.properties.setObject(name, value); 363 } 364 365 public final void setReadOnly(boolean value) 366 { 367 this.readOnly = value; 368 } 369 370 public final void setShortProperty(String name, short value) throws JMSException 371 { 372 this.properties.setShort(name, value); 373 } 374 375 public final void setStringProperty(String name, String value) throws JMSException 376 { 377 this.properties.setString(name, value); 378 } 379 public void generateMessageID() throws JMSException 380 { 381 messageID = GUID.asString(); 382 } 383 384 public void generateTimestamp() throws JMSException 385 { 386 timestamp = System.currentTimeMillis(); 387 } 388 389 public SessionDelegate getSessionDelegate() throws JMSException 390 { 391 return session; 392 } 393 394 public void makeReadOnly() 395 { 396 setReadOnly(true); 397 properties.setReadOnly(true); 398 } 399 400 public Object clone() 401 { 402 try{ 403 return super.clone(); 404 } 405 catch (CloneNotSupportedException ignored) 406 { 407 return null; } 409 } 410 411 protected void throwExceptionIfReadOnly() throws MessageNotWriteableException 412 { 413 if (this.readOnly) 414 { 415 throw new MessageNotWriteableException ("Unable to write body: the message body is currently read only."); 416 } 417 } 418 419 } | Popular Tags |