package org.huihoo.jfox.jms;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Map;
import java.util.HashMap;
import java.util.Vector;
import javax.jms.Message;
import javax.jms.JMSException;
import javax.jms.Destination;
import javax.jms.MessageFormatException;
import javax.jms.MessageNotWriteableException;
public abstract class AbstractMessage implements Message, Serializable, Cloneable {
protected String type = "Default";
protected String messageId = null;
protected long timeStamp = 0L;
protected String correlationId;
protected Destination replyTo;
protected Destination destination = null;
protected int deliveryMode = Message.DEFAULT_DELIVERY_MODE;
protected boolean redelivered;
protected long expiration = Message.DEFAULT_TIME_TO_LIVE;
protected int priority = Message.DEFAULT_PRIORITY;
protected boolean readonly = false;
protected Map properties = new HashMap();
protected AbstractSession session = null;
protected AbstractMessage(AbstractSession session) {
this.session = session;
}
public String getJMSMessageID() throws JMSException {
return messageId;
}
public void setJMSMessageID(String id) throws JMSException {
this.messageId = id;
}
public long getJMSTimestamp() throws JMSException {
return timeStamp;
}
public void setJMSTimestamp(long timeStamp) throws JMSException {
this.timeStamp = timeStamp;
}
public byte[] getJMSCorrelationIDAsBytes() throws JMSException {
return correlationId != null ? correlationId.getBytes() : null;
}
public void setJMSCorrelationIDAsBytes(byte[] correlationId) throws JMSException {
this.correlationId = new String(correlationId);
}
public void setJMSCorrelationID(String correlationId) throws JMSException {
this.correlationId = correlationId;
}
public String getJMSCorrelationID() throws JMSException {
return correlationId;
}
public Destination getJMSReplyTo() throws JMSException {
return replyTo;
}
public void setJMSReplyTo(Destination replyTo) throws JMSException {
this.replyTo = replyTo;
}
public Destination getJMSDestination() throws JMSException {
return destination;
}
public void setJMSDestination(Destination destination) throws JMSException {
if(destination == null) {
throw new JMSException("Invalid destination - null!");
}
this.destination = destination;
}
public int getJMSDeliveryMode() throws JMSException {
return deliveryMode;
}
public void setJMSDeliveryMode(int deliveryMode) throws JMSException {
this.deliveryMode = deliveryMode;
}
public boolean getJMSRedelivered() throws JMSException {
return redelivered;
}
public void setJMSRedelivered(boolean redelivered) throws JMSException {
this.redelivered = redelivered;
}
public String getJMSType() throws JMSException {
return type;
}
public void setJMSType(String type) throws JMSException {
this.type = type;
}
public long getJMSExpiration() throws JMSException {
return expiration;
}
public void setJMSExpiration(long expiration) throws JMSException {
if (expiration >= 0) this.expiration = expiration;
}
public int getJMSPriority() throws JMSException {
return priority;
}
public void setJMSPriority(int priority) throws JMSException {
if (priority >= 0 && priority <= 9) this.priority = priority;
}
public void clearProperties() throws JMSException {
properties.clear();
readonly = false;
}
public boolean propertyExists(String name) throws JMSException {
return properties.containsKey(name);
}
public boolean getBooleanProperty(String name) throws JMSException {
if(this.propertyExists(name)) {
Object obj = properties.get(name);
boolean _boolean = false;
try {
_boolean = Boolean.valueOf(obj.toString()).booleanValue();
}
catch(Throwable thr){
throw new MessageFormatException(thr.toString());
}
return _boolean;
}
throw new JMSException("property " + name + " not exists.");
}
public byte getByteProperty(String name) throws JMSException {
if(this.propertyExists(name)) {
Object obj = properties.get(name);
byte _byte = 0;
try {
_byte = Byte.valueOf(obj.toString()).byteValue();
}
catch(Throwable thr){
throw new MessageFormatException(thr.toString());
}
return _byte;
}
throw new JMSException("property " + name + " not exists.");
}
public short getShortProperty(String name) throws JMSException {
if(this.propertyExists(name)) {
Object obj = properties.get(name);
short _short = 0;
try {
_short = Short.valueOf(obj.toString()).shortValue();
}
catch(Throwable thr){
throw new MessageFormatException(thr.toString());
}
return _short;
}
throw new JMSException("property " + name + " not exists.");
}
public int getIntProperty(String name) throws JMSException {
if(this.propertyExists(name)) {
Object obj = properties.get(name);
int _int = 0;
try {
_int = Integer.valueOf(obj.toString()).intValue();
}
catch(Throwable thr){
throw new MessageFormatException(thr.toString());
}
return _int;
}
throw new JMSException("property " + name + " not exists.");
}
public long getLongProperty(String name) throws JMSException {
if(this.propertyExists(name)) {
Object obj = properties.get(name);
long _long = 0;
try {
_long = Long.valueOf(obj.toString()).longValue();
}
catch(Throwable thr){
throw new MessageFormatException(thr.toString());
}
return _long;
}
throw new JMSException("property " + name + " not exists.");
}
public float getFloatProperty(String name) throws JMSException {
if(this.propertyExists(name)) {
Object obj = properties.get(name);
float _float = 0;
try {
_float = Float.valueOf(obj.toString()).floatValue();
}
catch(Throwable thr){
throw new MessageFormatException(thr.toString());
}
return _float;
}
throw new JMSException("property " + name + " not exists.");
}
public double getDoubleProperty(String name) throws JMSException {
if(this.propertyExists(name)) {
Object obj = properties.get(name);
double _double = 0;
try {
_double = Byte.valueOf(obj.toString()).doubleValue();
}
catch(Throwable thr){
throw new MessageFormatException(thr.toString());
}
return _double;
}
throw new JMSException("property " + name + " not exists.");
}
public String getStringProperty(String name) throws JMSException {
if(this.propertyExists(name)) {
Object obj = properties.get(name);
return obj.toString();
}
throw new JMSException("property " + name + " not exists.");
}
public Object getObjectProperty(String name) throws JMSException {
if(this.propertyExists(name)) {
Object obj = properties.get(name);
return obj;
}
throw new JMSException("property " + name + " not exists.");
}
public Enumeration getPropertyNames() throws JMSException {
return new Vector(properties.keySet()).elements();
}
public void setBooleanProperty(String name, boolean value) throws JMSException {
checkWriteable();
if(name == null || name.equals("")) throw new IllegalArgumentException("Invalid property name: " + name);
properties.put(name, new Boolean(value));
}
public void setByteProperty(String name, byte value) throws JMSException {
checkWriteable();
if(name == null || name.equals("")) throw new IllegalArgumentException("Invalid property name: " + name);
properties.put(name,new Byte(value));
}
public void setShortProperty(String name, short value) throws JMSException {
checkWriteable();
if(name == null || name.equals("")) throw new IllegalArgumentException("Invalid property name: " + name);
properties.put(name,new Short(value));
}
public void setIntProperty(String name, int value) throws JMSException {
checkWriteable();
if(name == null || name.equals("")) throw new IllegalArgumentException("Invalid property name: " + name);
properties.put(name,new Integer(value));
}
public void setLongProperty(String name, long value) throws JMSException {
checkWriteable();
if(name == null || name.equals("")) throw new IllegalArgumentException("Invalid property name: " + name);
properties.put(name,new Long(value));
}
public void setFloatProperty(String name, float value) throws JMSException {
checkWriteable();
if(name == null || name.equals("")) throw new IllegalArgumentException("Invalid property name: " + name);
properties.put(name,new Float(value));
}
public void setDoubleProperty(String name, double value) throws JMSException {
checkWriteable();
if(name == null || name.equals("")) throw new IllegalArgumentException("Invalid property name: " + name);
properties.put(name,new Double(value));
}
public void setStringProperty(String name, String value) throws JMSException {
checkWriteable();
if(name == null || name.equals("")) throw new IllegalArgumentException("Invalid property name: " + name);
properties.put(name,value);
}
public void setObjectProperty(String name, Object value) throws JMSException {
checkWriteable();
if( value != null ) {
String className = value.getClass().getName();
if ((className.equals("java.lang.Boolean")) || (className.equals("java.lang.Byte"))
|| (className.equals("java.lang.Short")) || (className.equals("java.lang.Integer"))
|| (className.equals("java.lang.Long")) || (className.equals("java.lang.Float"))
|| (className.equals("java.lang.Double"))
|| (className.equals("java.lang.String"))) {
properties.put(name,value);
}
else {
throw new MessageFormatException("unsupported object type " + className);
}
}
else {
properties.put(name,value);
}
}
public void acknowledge() throws JMSException {
if(session != null) session.acknowneledge();
}
private void checkWriteable() throws MessageNotWriteableException {
if(readonly) throw new MessageNotWriteableException("message's properties are READ-ONLY.");
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(super.toString());
sb.append("\nmessageId = ");
sb.append(messageId);
sb.append("\ntimeStamp = ");
sb.append(timeStamp);
sb.append("\ncorrelationId = ");
sb.append(correlationId);
sb.append("\nreplyTo = ");
sb.append(replyTo);
sb.append("\ndestination = ");
sb.append(destination);
sb.append("\ndeliveryMode = ");
sb.append(deliveryMode);
sb.append("\nredelivered = ");
sb.append(redelivered);
sb.append("\ntype = ");
sb.append(type);
sb.append("\nexpiration = ");
sb.append(expiration);
sb.append("\npriority = ");
sb.append(priority);
sb.append("\nprops = ");
sb.append(properties);
sb.append("\nreadOnly = ");
sb.append(readonly);
return sb.toString();
}
}