public String getStringProperty(final String name) throws JMSException {
    if (JMSXDELIVERYCOUNT.equals(name)) {
      return String.valueOf(message.getDeliveryCount());
    }
    Object value;
    if (JMSXGROUPID.equals(name)) {
      value = message.getProperty(MessageImpl.GROUP_ID);
    } else {
      value = message.getProperty(new SimpleString(name));
    }
    if (value == null) return null;

    if (value instanceof SimpleString) {
      return ((SimpleString) value).toString();
    } else if (value instanceof Boolean) {
      return value.toString();
    } else if (value instanceof Byte) {
      return value.toString();
    } else if (value instanceof Short) {
      return value.toString();
    } else if (value instanceof Integer) {
      return value.toString();
    } else if (value instanceof Long) {
      return value.toString();
    } else if (value instanceof Float) {
      return value.toString();
    } else if (value instanceof Double) {
      return value.toString();
    } else {
      throw new MessageFormatException("Invalid conversion");
    }
  }
  public long getLongProperty(final String name) throws JMSException {
    if (JMSXDELIVERYCOUNT.equals(name)) {
      return message.getDeliveryCount();
    }

    Object value = message.getProperty(new SimpleString(name));

    if (value == null) {
      throw new NumberFormatException("Message property '" + name + "' not set.");
    }

    if (value instanceof Byte) {
      return ((Byte) value).longValue();
    } else if (value instanceof Short) {
      return ((Short) value).longValue();
    } else if (value instanceof Integer) {
      return ((Integer) value).longValue();
    } else if (value instanceof Long) {
      return ((Long) value).longValue();
    } else if (value instanceof SimpleString) {
      return Long.parseLong(((SimpleString) value).toString());
    } else {
      throw new MessageFormatException("Invalid conversion");
    }
  }
 public void setStringProperty(final String name, final String value) throws JMSException {
   checkProperty(name, value);
   if (JMSXGROUPID.equals(name)) {
     message.putStringProperty(MessageImpl.GROUP_ID, new SimpleString(value));
   } else {
     message.putStringProperty(new SimpleString(name), new SimpleString(value));
   }
 }
 public Object getObjectProperty(final String name) throws JMSException {
   if (JMSXDELIVERYCOUNT.equals(name)) {
     return String.valueOf(message.getDeliveryCount());
   }
   Object val = message.getProperty(new SimpleString(name));
   if (val instanceof SimpleString) {
     val = ((SimpleString) val).toString();
   }
   return val;
 }
 public void setJMSRedelivered(final boolean redelivered) throws JMSException {
   if (!redelivered) {
     message.setDeliveryCount(1);
   } else {
     if (message.getDeliveryCount() > 1) {
       // do nothing
     } else {
       message.setDeliveryCount(2);
     }
   }
 }
 public void setJMSDeliveryMode(final int deliveryMode) throws JMSException {
   if (deliveryMode == DeliveryMode.PERSISTENT) {
     message.setDurable(true);
   } else if (deliveryMode == DeliveryMode.NON_PERSISTENT) {
     message.setDurable(false);
   } else {
     throw new JMSException(
         "DeliveryImpl mode must be either DeliveryMode.PERSISTENT "
             + "or DeliveryMode.NON_PERSISTENT");
   }
 }
  public void setJMSCorrelationID(final String correlationID) throws JMSException {
    if (correlationID == null) {
      message.removeProperty(CORRELATIONID_HEADER_NAME);

      jmsCorrelationID = null;
    } else {
      message.putStringProperty(CORRELATIONID_HEADER_NAME, new SimpleString(correlationID));

      jmsCorrelationID = correlationID;
    }
  }
 public void setJMSMessageID(final String jmsMessageID) throws JMSException {
   if (jmsMessageID != null && !jmsMessageID.startsWith("ID:")) {
     throw new JMSException("JMSMessageID must start with ID:");
   }
   if (jmsMessageID == null) {
     message.removeProperty(JBM_MESSAGE_ID);
   } else {
     message.putStringProperty(JBM_MESSAGE_ID, new SimpleString(jmsMessageID));
   }
   msgID = jmsMessageID;
 }
  public void clearProperties() throws JMSException {
    List<SimpleString> toRemove = new ArrayList<SimpleString>();

    for (SimpleString propName : message.getPropertyNames()) {
      if (!propName.startsWith(JMS) || propName.startsWith(JMSX) || propName.startsWith(JMS_)) {
        toRemove.add(propName);
      }
    }

    for (SimpleString propName : toRemove) {
      message.removeProperty(propName);
    }
  }
  public void setObjectProperty(final String name, final Object value) throws JMSException {
    checkProperty(name, value);

    SimpleString key = new SimpleString(name);

    if (value instanceof Boolean) {
      message.putBooleanProperty(key, (Boolean) value);
    } else if (value instanceof Byte) {
      message.putByteProperty(key, (Byte) value);
    } else if (value instanceof Short) {
      message.putShortProperty(key, (Short) value);
    } else if (value instanceof Integer) {
      message.putIntProperty(key, (Integer) value);
    } else if (value instanceof Long) {
      message.putLongProperty(key, (Long) value);
    } else if (value instanceof Float) {
      message.putFloatProperty(key, (Float) value);
    } else if (value instanceof Double) {
      message.putDoubleProperty(key, (Double) value);
    } else if (value instanceof String) {
      message.putStringProperty(key, new SimpleString((String) value));
    } else {
      throw new MessageFormatException("Invalid property type");
    }
  }
  public void setJMSType(final String type) throws JMSException {
    if (type != null) {
      message.putStringProperty(TYPE_HEADER_NAME, new SimpleString(type));

      jmsType = type;
    }
  }
  public void setJMSReplyTo(final Destination dest) throws JMSException {
    if (dest == null) {
      message.removeProperty(REPLYTO_HEADER_NAME);

      replyTo = null;
    } else {
      if (dest instanceof JBossDestination == false) {
        throw new InvalidDestinationException("Not a JBoss destination " + dest);
      }

      JBossDestination jbd = (JBossDestination) dest;

      message.putStringProperty(REPLYTO_HEADER_NAME, jbd.getSimpleAddress());

      replyTo = jbd;
    }
  }
  public String getJMSMessageID() {
    if (msgID == null) {
      SimpleString id = (SimpleString) message.getProperty(JBM_MESSAGE_ID);

      msgID = id == null ? null : id.toString();
    }
    return msgID;
  }
 public String toString() {
   StringBuffer sb = new StringBuffer("JBossMessage[");
   sb.append("");
   sb.append(getJMSMessageID());
   sb.append("]:");
   sb.append(message.isDurable() ? "PERSISTENT" : "NON-PERSISTENT");
   return sb.toString();
 }
 public void setLongProperty(final String name, final long value) throws JMSException {
   Long l = new Long(value);
   checkProperty(name, l);
   if (JMS_JBOSS_SCHEDULED_DELIVERY_PROP_NAME.equals(name)) {
     scheduledDeliveryTime = l;
   }
   message.putLongProperty(new SimpleString(name), value);
 }
  /** Constructor for when receiving a message from the server */
  public JBossMessage(final ClientMessage message, ClientSession session) {
    this.message = message;

    this.readOnly = true;

    this.session = session;

    this.body = message.getBody();
  }
  public byte[] getJMSCorrelationIDAsBytes() throws JMSException {
    Object obj = message.getProperty(CORRELATIONID_HEADER_NAME);

    if (obj instanceof byte[]) {
      return (byte[]) obj;
    } else {
      return null;
    }
  }
  public float getFloatProperty(final String name) throws JMSException {
    Object value = message.getProperty(new SimpleString(name));
    if (value == null) return Float.valueOf(null).floatValue();

    if (value instanceof Float) return ((Float) value).floatValue();
    else if (value instanceof SimpleString)
      return Float.parseFloat(((SimpleString) value).toString());
    else throw new MessageFormatException("Invalid conversion");
  }
  public Destination getJMSDestination() throws JMSException {
    if (dest == null) {
      SimpleString sdest = message.getDestination();

      dest = sdest == null ? null : JBossDestination.fromAddress(sdest.toString());
    }

    return dest;
  }
  public byte getByteProperty(final String name) throws JMSException {
    Object value = message.getProperty(new SimpleString(name));
    if (value == null) throw new NumberFormatException("Message property '" + name + "' not set.");

    if (value instanceof Byte) return ((Byte) value).byteValue();
    else if (value instanceof SimpleString)
      return Byte.parseByte(((SimpleString) value).toString());
    else throw new MessageFormatException("Invalid conversion");
  }
  public boolean getBooleanProperty(final String name) throws JMSException {
    Object value = message.getProperty(new SimpleString(name));
    if (value == null) return Boolean.valueOf(null).booleanValue();

    if (value instanceof Boolean) return ((Boolean) value).booleanValue();
    else if (value instanceof SimpleString)
      return Boolean.valueOf(((SimpleString) value).toString()).booleanValue();
    else throw new MessageFormatException("Invalid conversion");
  }
  public Destination getJMSReplyTo() throws JMSException {
    if (replyTo == null) {
      SimpleString repl = (SimpleString) message.getProperty(REPLYTO_HEADER_NAME);

      if (repl != null) {
        replyTo = JBossDestination.fromAddress(repl.toString());
      }
    }
    return replyTo;
  }
  public double getDoubleProperty(final String name) throws JMSException {
    Object value = message.getProperty(new SimpleString(name));
    if (value == null) return Double.valueOf(null).doubleValue();

    if (value instanceof Float) return ((Float) value).doubleValue();
    else if (value instanceof Double) return ((Double) value).doubleValue();
    else if (value instanceof SimpleString)
      return Double.parseDouble(((SimpleString) value).toString());
    else throw new MessageFormatException("Invalid conversion");
  }
  public String getJMSType() throws JMSException {
    if (jmsType == null) {
      SimpleString ss = (SimpleString) message.getProperty(TYPE_HEADER_NAME);

      if (ss != null) {
        jmsType = ss.toString();
      }
    }
    return jmsType;
  }
  public String getJMSCorrelationID() throws JMSException {
    if (jmsCorrelationID == null) {
      Object obj = message.getProperty(CORRELATIONID_HEADER_NAME);

      if (obj != null) {
        jmsCorrelationID = ((SimpleString) obj).toString();
      }
    }

    return jmsCorrelationID;
  }
  public Enumeration getPropertyNames() throws JMSException {
    HashSet<String> set = new HashSet<String>();

    for (SimpleString propName : message.getPropertyNames()) {
      if (!propName.startsWith(JMS) || propName.startsWith(JMSX) || propName.startsWith(JMS_)) {
        set.add(propName.toString());
      }
    }

    set.add(JMSXDELIVERYCOUNT);

    return Collections.enumeration(set);
  }
  public JBossMessage(byte type) {
    message =
        new ClientMessageImpl(
            type,
            true,
            0,
            System.currentTimeMillis(),
            (byte) 4,
            new ByteBufferWrapper(ByteBuffer.allocate(1024)));

    // TODO - can we lazily create this?
    body = message.getBody();
  }
  public static JBossMessage createMessage(
      final ClientMessage message, final ClientSession session) {
    int type = message.getType();

    JBossMessage msg;

    switch (type) {
      case JBossMessage.TYPE:
        {
          msg = new JBossMessage(message, session);
          break;
        }
      case JBossBytesMessage.TYPE:
        {
          msg = new JBossBytesMessage(message, session);
          break;
        }
      case JBossMapMessage.TYPE:
        {
          msg = new JBossMapMessage(message, session);
          break;
        }
      case JBossObjectMessage.TYPE:
        {
          msg = new JBossObjectMessage(message, session);
          break;
        }
      case JBossStreamMessage.TYPE:
        {
          msg = new JBossStreamMessage(message, session);
          break;
        }
      case JBossTextMessage.TYPE:
        {
          msg = new JBossTextMessage(message, session);
          break;
        }
      default:
        {
          throw new IllegalArgumentException("Invalid message type " + type);
        }
    }

    return msg;
  }
 public void doBeforeReceive() throws Exception {
   body = message.getBody();
 }
  public void doBeforeSend() throws Exception {
    body.flip();

    message.setBody(body);
  }