001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.jms;
018    
019    import java.util.Enumeration;
020    import java.util.LinkedHashMap;
021    import java.util.Map;
022    import javax.jms.JMSException;
023    import javax.jms.Message;
024    
025    /**
026     * Utility class for {@link javax.jms.Message}.
027     *
028     * @version $Revision: 19423 $
029     */
030    public final class JmsMessageHelper {
031    
032        private JmsMessageHelper() {
033        }
034    
035        /**
036         * Removes the property from the JMS message.
037         *
038         * @param jmsMessage the JMS message
039         * @param name       name of the property to remove
040         * @return the old value of the property or <tt>null</tt> if not exists
041         * @throws JMSException can be thrown
042         */
043        public static Object removeJmsProperty(Message jmsMessage, String name) throws JMSException {
044            // check if the property exists
045            if (!jmsMessage.propertyExists(name)) {
046                return null;
047            }
048    
049            Object answer = null;
050    
051            // store the properties we want to keep in a temporary map
052            // as the JMS API is a bit strict as we are not allowed to
053            // clear a single property, but must clear them all and redo
054            // the properties
055            Map<String, Object> map = new LinkedHashMap<String, Object>();
056            Enumeration en = jmsMessage.getPropertyNames();
057            while (en.hasMoreElements()) {
058                String key = (String) en.nextElement();
059                if (name.equals(key)) {
060                    answer = key;
061                } else {
062                    map.put(key, jmsMessage.getObjectProperty(key));
063                }
064            }
065    
066            // redo the properties to keep
067            jmsMessage.clearProperties();
068            for (String key : map.keySet()) {
069                jmsMessage.setObjectProperty(key, map.get(key));
070            }
071    
072            return answer;
073        }
074    
075        /**
076         * Tests whether a given property with the name exists
077         *
078         * @param jmsMessage the JMS message
079         * @param name       name of the property to test if exists
080         * @return <tt>true</tt> if the property exists, <tt>false</tt> if not.
081         * @throws JMSException can be thrown
082         */
083        public static boolean hasProperty(Message jmsMessage, String name) throws JMSException {
084            Enumeration en = jmsMessage.getPropertyNames();
085            while (en.hasMoreElements()) {
086                String key = (String) en.nextElement();
087                if (name.equals(key)) {
088                    return true;
089                }
090            }
091            return false;
092        }
093    
094        /**
095         * Sets the property on the given JMS message.
096         *
097         * @param jmsMessage  the JMS message
098         * @param name        name of the property to set
099         * @param value       the value
100         * @throws JMSException can be thrown
101         */
102        public static void setProperty(Message jmsMessage, String name, Object value) throws JMSException {
103            if (value == null) {
104                return;
105            }
106            if (value instanceof Byte) {
107                jmsMessage.setByteProperty(name, (Byte) value);
108            } else if (value instanceof Boolean) {
109                jmsMessage.setBooleanProperty(name, (Boolean) value);
110            } else if (value instanceof Double) {
111                jmsMessage.setDoubleProperty(name, (Double) value);
112            } else if (value instanceof Float) {
113                jmsMessage.setFloatProperty(name, (Float) value);
114            } else if (value instanceof Integer) {
115                jmsMessage.setIntProperty(name, (Integer) value);
116            } else if (value instanceof Long) {
117                jmsMessage.setLongProperty(name, (Long) value);
118            } else if (value instanceof Short) {
119                jmsMessage.setShortProperty(name, (Short) value);
120            } else if (value instanceof String) {
121                jmsMessage.setStringProperty(name, (String) value);
122            } else {
123                // fallback to Object
124                jmsMessage.setObjectProperty(name, value);
125            }
126        }
127    
128    }