static

in qpid-jms-client/src/main/java/org/apache/qpid/jms/message/JmsMessagePropertyIntercepter.java [128:673]


    static {
        STANDARD_HEADERS.add(JMS_MESSAGEID);
        STANDARD_HEADERS.add(JMS_TIMESTAMP);
        STANDARD_HEADERS.add(JMS_CORRELATIONID);
        STANDARD_HEADERS.add(JMS_REPLYTO);
        STANDARD_HEADERS.add(JMS_DESTINATION);
        STANDARD_HEADERS.add(JMS_DELIVERY_MODE);
        STANDARD_HEADERS.add(JMS_REDELIVERED);
        STANDARD_HEADERS.add(JMS_TYPE);
        STANDARD_HEADERS.add(JMS_EXPIRATION);
        STANDARD_HEADERS.add(JMS_PRIORITY);
        STANDARD_HEADERS.add(JMS_DELIVERYTIME);

        VENDOR_PROPERTIES.add(JMS_AMQP_ACK_TYPE);

        PROPERTY_INTERCEPTERS.put(JMS_DESTINATION, new PropertyIntercepter() {
            @Override
            public void setProperty(JmsMessage message, Object value) throws JMSException {
                throw new JMSException("Cannot set JMS Destination as a property, use setJMSDestination() instead");
            }

            @Override
            public Object getProperty(JmsMessage message) throws JMSException {
                Destination dest = message.getFacade().getDestination();
                if (dest == null) {
                    return null;
                }
                return dest.toString();
            }

            @Override
            public boolean propertyExists(JmsMessage message) {
                return message.getFacade().getDestination() != null;
            }

            @Override
            public void clearProperty(JmsMessage message) {
                message.getFacade().setDestination(null);
            }

            @Override
            public boolean isAlwaysWritable() {
                return false;
            }
        });
        PROPERTY_INTERCEPTERS.put(JMS_REPLYTO, new PropertyIntercepter() {
            @Override
            public void setProperty(JmsMessage message, Object value) throws JMSException {
                throw new JMSException("Cannot set JMS ReplyTo as a property, use setJMSReplTo() instead");
            }

            @Override
            public Object getProperty(JmsMessage message) throws JMSException {
                if (message.getFacade().getReplyTo() == null) {
                    return null;
                }
                return message.getFacade().getReplyTo().toString();
            }

            @Override
            public boolean propertyExists(JmsMessage message) {
                return message.getFacade().getReplyTo() != null;
            }

            @Override
            public void clearProperty(JmsMessage message) {
                message.getFacade().setReplyTo(null);
            }

            @Override
            public boolean isAlwaysWritable() {
                return false;
            }
        });
        PROPERTY_INTERCEPTERS.put(JMS_TYPE, new PropertyIntercepter() {
            @Override
            public Object getProperty(JmsMessage message) throws JMSException {
                return message.getFacade().getType();
            }

            @Override
            public void setProperty(JmsMessage message, Object value) throws JMSException {
                String rc = (String) TypeConversionSupport.convert(value, String.class);
                if (rc == null) {
                    throw new JMSException("Property JMSType cannot be set from a " + value.getClass().getName() + ".");
                }
                message.getFacade().setType(rc);
            }

            @Override
            public boolean propertyExists(JmsMessage message) {
                return message.getFacade().getType() != null;
            }

            @Override
            public void clearProperty(JmsMessage message) {
                message.getFacade().setType(null);
            }

            @Override
            public boolean isAlwaysWritable() {
                return false;
            }
        });
        PROPERTY_INTERCEPTERS.put(JMS_DELIVERY_MODE, new PropertyIntercepter() {
            @Override
            public Object getProperty(JmsMessage message) throws JMSException {
                return message.getFacade().isPersistent() ? "PERSISTENT" : "NON_PERSISTENT";
            }

            @Override
            public void setProperty(JmsMessage message, Object value) throws JMSException {
                Integer rc = null;
                try {
                    rc = (Integer) TypeConversionSupport.convert(value, Integer.class);
                } catch (NumberFormatException nfe) {
                    if (value instanceof String) {
                        if (((String) value).equalsIgnoreCase("PERSISTENT")) {
                            rc = DeliveryMode.PERSISTENT;
                        } else if (((String) value).equalsIgnoreCase("NON_PERSISTENT")) {
                            rc = DeliveryMode.NON_PERSISTENT;
                        }
                    }

                    if (rc == null) {
                        throw nfe;
                    }
                }
                if (rc == null) {
                    Boolean bool = (Boolean) TypeConversionSupport.convert(value, Boolean.class);
                    if (bool == null) {
                        throw new JMSException("Property JMSDeliveryMode cannot be set from a " + value.getClass().getName() + ".");
                    } else {
                        message.getFacade().setPersistent(bool.booleanValue());
                    }
                } else {
                    message.getFacade().setPersistent(rc == DeliveryMode.PERSISTENT);
                }
            }

            @Override
            public boolean propertyExists(JmsMessage message) {
                return true;
            }

            @Override
            public void clearProperty(JmsMessage message) {
                message.getFacade().setPersistent(true); // Default value
            }

            @Override
            public boolean isAlwaysWritable() {
                return false;
            }
        });
        PROPERTY_INTERCEPTERS.put(JMS_PRIORITY, new PropertyIntercepter() {
            @Override
            public Object getProperty(JmsMessage message) throws JMSException {
                return Integer.valueOf(message.getFacade().getPriority());
            }

            @Override
            public void setProperty(JmsMessage message, Object value) throws JMSException {
                Integer rc = (Integer) TypeConversionSupport.convert(value, Integer.class);
                if (rc == null) {
                    throw new JMSException("Property JMSPriority cannot be set from a " + value.getClass().getName() + ".");
                }
                message.getFacade().setPriority(rc.byteValue());
            }

            @Override
            public boolean propertyExists(JmsMessage message) {
                return true;
            }

            @Override
            public void clearProperty(JmsMessage message) {
                message.getFacade().setPriority(Message.DEFAULT_PRIORITY);
            }

            @Override
            public boolean isAlwaysWritable() {
                return false;
            }
        });
        PROPERTY_INTERCEPTERS.put(JMS_MESSAGEID, new PropertyIntercepter() {
            @Override
            public Object getProperty(JmsMessage message) throws JMSException {
                if (message.getFacade().getMessageId() == null) {
                    return null;
                }
                return message.getFacade().getMessageId();
            }

            @Override
            public void setProperty(JmsMessage message, Object value) throws JMSException {
                String rc = (String) TypeConversionSupport.convert(value, String.class);
                if (rc == null) {
                    throw new JMSException("Property JMSMessageID cannot be set from a " + value.getClass().getName() + ".");
                }
                message.getFacade().setMessageId(rc);
            }

            @Override
            public boolean propertyExists(JmsMessage message) {
                return message.getFacade().getMessageId() != null;
            }

            @Override
            public void clearProperty(JmsMessage message) throws JMSException {
                message.getFacade().setMessageId(null);
            }

            @Override
            public boolean isAlwaysWritable() {
                return false;
            }
        });
        PROPERTY_INTERCEPTERS.put(JMS_TIMESTAMP, new PropertyIntercepter() {
            @Override
            public Object getProperty(JmsMessage message) throws JMSException {
                return Long.valueOf(message.getFacade().getTimestamp());
            }

            @Override
            public void setProperty(JmsMessage message, Object value) throws JMSException {
                Long rc = (Long) TypeConversionSupport.convert(value, Long.class);
                if (rc == null) {
                    throw new JMSException("Property JMSTimestamp cannot be set from a " + value.getClass().getName() + ".");
                }
                message.getFacade().setTimestamp(rc.longValue());
            }

            @Override
            public boolean propertyExists(JmsMessage message) {
                return message.getFacade().getTimestamp() > 0;
            }

            @Override
            public void clearProperty(JmsMessage message) {
                message.getFacade().setTimestamp(0);
            }

            @Override
            public boolean isAlwaysWritable() {
                return false;
            }
        });
        PROPERTY_INTERCEPTERS.put(JMS_CORRELATIONID, new PropertyIntercepter() {
            @Override
            public Object getProperty(JmsMessage message) throws JMSException {
                return message.getFacade().getCorrelationId();
            }

            @Override
            public void setProperty(JmsMessage message, Object value) throws JMSException {
                String rc = (String) TypeConversionSupport.convert(value, String.class);
                if (rc == null) {
                    throw new JMSException("Property JMSCorrelationID cannot be set from a " + value.getClass().getName() + ".");
                }
                message.getFacade().setCorrelationId(rc);
            }

            @Override
            public boolean propertyExists(JmsMessage message) {
                return message.getFacade().getCorrelationId() != null;
            }

            @Override
            public void clearProperty(JmsMessage message) throws JMSException {
                message.getFacade().setCorrelationId(null);
            }

            @Override
            public boolean isAlwaysWritable() {
                return false;
            }
        });
        PROPERTY_INTERCEPTERS.put(JMS_EXPIRATION, new PropertyIntercepter() {
            @Override
            public Object getProperty(JmsMessage message) throws JMSException {
                return Long.valueOf(message.getFacade().getExpiration());
            }

            @Override
            public void setProperty(JmsMessage message, Object value) throws JMSException {
                Long rc = (Long) TypeConversionSupport.convert(value, Long.class);
                if (rc == null) {
                    throw new JMSException("Property JMSExpiration cannot be set from a " + value.getClass().getName() + ".");
                }
                message.getFacade().setExpiration(rc.longValue());
            }

            @Override
            public boolean propertyExists(JmsMessage message) {
                return message.getFacade().getExpiration() > 0;
            }

            @Override
            public void clearProperty(JmsMessage message) {
                message.getFacade().setExpiration(0);
            }

            @Override
            public boolean isAlwaysWritable() {
                return false;
            }
        });
        PROPERTY_INTERCEPTERS.put(JMS_REDELIVERED, new PropertyIntercepter() {
            @Override
            public Object getProperty(JmsMessage message) throws JMSException {
                return Boolean.valueOf(message.getFacade().isRedelivered());
            }

            @Override
            public void setProperty(JmsMessage message, Object value) throws JMSException {
                Boolean rc = (Boolean) TypeConversionSupport.convert(value, Boolean.class);
                if (rc == null) {
                    throw new JMSException("Property JMSRedelivered cannot be set from a " + value.getClass().getName() + ".");
                }
                message.getFacade().setRedelivered(rc.booleanValue());
            }

            @Override
            public boolean propertyExists(JmsMessage message) {
                return message.getFacade().isRedelivered();
            }

            @Override
            public void clearProperty(JmsMessage message) {
                message.getFacade().setRedelivered(false);
            }

            @Override
            public boolean isAlwaysWritable() {
                return false;
            }
        });
        PROPERTY_INTERCEPTERS.put(JMSX_DELIVERY_COUNT, new PropertyIntercepter() {
            @Override
            public void setProperty(JmsMessage message, Object value) throws JMSException {
                Integer rc = (Integer) TypeConversionSupport.convert(value, Integer.class);
                if (rc == null) {
                    throw new JMSException("Property JMSXDeliveryCount cannot be set from a " + value.getClass().getName() + ".");
                }
                message.getFacade().setDeliveryCount(rc.intValue());
            }

            @Override
            public Object getProperty(JmsMessage message) throws JMSException {
                return Integer.valueOf(message.getFacade().getDeliveryCount());
            }

            @Override
            public boolean propertyExists(JmsMessage message) {
                return true;
            }

            @Override
            public void clearProperty(JmsMessage message) {
                message.getFacade().setDeliveryCount(1);
            }

            @Override
            public boolean isAlwaysWritable() {
                return false;
            }
        });
        PROPERTY_INTERCEPTERS.put(JMSX_GROUPID, new PropertyIntercepter() {
            @Override
            public Object getProperty(JmsMessage message) throws JMSException {
                return message.getFacade().getGroupId();
            }

            @Override
            public void setProperty(JmsMessage message, Object value) throws JMSException {
                String rc = (String) TypeConversionSupport.convert(value, String.class);
                if (rc == null) {
                    throw new JMSException("Property JMSXGroupID cannot be set from a " + value.getClass().getName() + ".");
                }
                message.getFacade().setGroupId(rc);
            }

            @Override
            public boolean propertyExists(JmsMessage message) {
                return message.getFacade().getGroupId() != null;
            }

            @Override
            public void clearProperty(JmsMessage message) {
                message.getFacade().setGroupId(null);
            }

            @Override
            public boolean isAlwaysWritable() {
                return false;
            }
        });
        PROPERTY_INTERCEPTERS.put(JMSX_GROUPSEQ, new PropertyIntercepter() {
            @Override
            public Object getProperty(JmsMessage message) throws JMSException {
                return message.getFacade().getGroupSequence();
            }

            @Override
            public void setProperty(JmsMessage message, Object value) throws JMSException {
                Integer rc = (Integer) TypeConversionSupport.convert(value, Integer.class);
                if (rc == null) {
                    throw new JMSException("Property JMSXGroupSeq cannot be set from a " + value.getClass().getName() + ".");
                }
                message.getFacade().setGroupSequence(rc.intValue());
            }

            @Override
            public boolean propertyExists(JmsMessage message) {
                return message.getFacade().getGroupSequence() != 0;
            }

            @Override
            public void clearProperty(JmsMessage message) {
                message.getFacade().setGroupSequence(0);
            }

            @Override
            public boolean isAlwaysWritable() {
                return false;
            }
        });
        PROPERTY_INTERCEPTERS.put(JMSX_USERID, new PropertyIntercepter() {
            @Override
            public Object getProperty(JmsMessage message) throws JMSException {
                Object userId = message.getFacade().getUserId();
                if (userId == null) {
                    try {
                        userId = message.getFacade().getProperty("JMSXUserID");
                    } catch (Exception e) {
                        throw JmsExceptionSupport.create(e);
                    }
                }

                return userId;
            }

            @Override
            public void setProperty(JmsMessage message, Object value) throws JMSException {
                if (value != null && !(value instanceof String)) {
                    throw new JMSException("Property JMSXUserID cannot be set from a " + value.getClass().getName() + ".");
                }
                message.getFacade().setUserId((String) value);
            }

            @Override
            public boolean propertyExists(JmsMessage message) {
                return message.getFacade().getUserId() != null;
            }

            @Override
            public void clearProperty(JmsMessage message) {
                message.getFacade().setUserId(null);
            }

            @Override
            public boolean isAlwaysWritable() {
                return false;
            }
        });
        PROPERTY_INTERCEPTERS.put(JMS_AMQP_ACK_TYPE, new PropertyIntercepter() {
            @Override
            public Object getProperty(JmsMessage message) throws JMSException {
                Object ackType = null;

                if (message.getAcknowledgeCallback() != null &&
                    message.getAcknowledgeCallback().isAckTypeSet()) {

                    ackType = message.getAcknowledgeCallback().getAckType();
                }

                return ackType;
            }

            @Override
            public void setProperty(JmsMessage message, Object value) throws JMSException {
                if (message.getAcknowledgeCallback() == null) {
                    throw new JMSException("Session Acknowledgement Mode does not allow setting: " + JMS_AMQP_ACK_TYPE);
                }

                Integer ackType = (Integer) TypeConversionSupport.convert(value, Integer.class);
                if (ackType == null) {
                    throw new JMSException("Property " + JMS_AMQP_ACK_TYPE + " cannot be set from a " + value.getClass().getName() + ".");
                }

                message.getAcknowledgeCallback().setAckType(ackType);
            }

            @Override
            public boolean propertyExists(JmsMessage message) {
                if (message.getAcknowledgeCallback() != null) {
                    return message.getAcknowledgeCallback().isAckTypeSet();
                }

                return false;
            }

            @Override
            public void clearProperty(JmsMessage message) throws JMSException {
                if (message.getAcknowledgeCallback() != null) {
                    message.getAcknowledgeCallback().clearAckType();
                }
            }

            @Override
            public boolean isAlwaysWritable() {
                return true;
            }
        });
        PROPERTY_INTERCEPTERS.put(JMS_DELIVERYTIME, new PropertyIntercepter() {
            @Override
            public Object getProperty(JmsMessage message) throws JMSException {
                return Long.valueOf(message.getFacade().getDeliveryTime());
            }

            @Override
            public void setProperty(JmsMessage message, Object value) throws JMSException {
                Long rc = (Long) TypeConversionSupport.convert(value, Long.class);
                if (rc == null) {
                    throw new JMSException("Property JMSDeliveryTime cannot be set from a " + value.getClass().getName() + ".");
                }
                message.getFacade().setDeliveryTime(rc.longValue(), true);
            }

            @Override
            public boolean propertyExists(JmsMessage message) {
                return message.getFacade().getDeliveryTime() > 0;
            }

            @Override
            public void clearProperty(JmsMessage message) {
                message.getFacade().setDeliveryTime(0, true);
            }

            @Override
            public boolean isAlwaysWritable() {
                return false;
            }
        });
    }