001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.jms;
019    
020    import org.apache.camel.Headers;
021    import org.apache.camel.InvalidHeaderTypeException;
022    import org.apache.camel.impl.HeadersSupport;
023    
024    import javax.jms.JMSException;
025    import javax.jms.Message;
026    import java.util.Enumeration;
027    import java.util.HashMap;
028    import java.util.Map;
029    
030    /**
031     * @version $Revision: 520502 $
032     */
033    public class JmsHeaders extends HeadersSupport {
034        private final DefaultJmsMessage message;
035        private Map<String, Object> lazyHeaders;
036    
037        public JmsHeaders(DefaultJmsMessage message) {
038            this.message = message;
039        }
040    
041        public Object getHeader(String name) {
042            Message request = message.getJmsMessage();
043            if (request != null) {
044                try {
045                    Object value = request.getObjectProperty(name);
046                    try {
047                        return value;
048                    }
049                    catch (ClassCastException e) {
050                        throw new InvalidHeaderTypeException(e.getMessage(), value);
051                    }
052                }
053                catch (JMSException e) {
054                    throw new MessagePropertyAcessException(name, e);
055                }
056            }
057            return null;
058        }
059    
060        public void setHeader(String name, Object value) {
061            Message request = message.getJmsMessage();
062            if (request != null) {
063                try {
064                    request.setObjectProperty(name, value);
065                }
066                catch (JMSException e) {
067                    throw new MessagePropertyAcessException(name, e);
068                }
069            }
070            else {
071                if (lazyHeaders == null) {
072                    lazyHeaders = new HashMap<String, Object>();
073                }
074                lazyHeaders.put(name, value);
075            }
076        }
077    
078        public Map<String, Object> getHeaders() {
079            Message request = message.getJmsMessage();
080            if (request != null) {
081                Map<String, Object> answer = new HashMap<String, Object>();
082                Enumeration names;
083                try {
084                    names = request.getPropertyNames();
085                }
086                catch (JMSException e) {
087                    throw new MessagePropertyNamesAcessException(e);
088                }
089                while (names.hasMoreElements()) {
090                    String name = names.nextElement().toString();
091                    try {
092                        Object value = request.getObjectProperty(name);
093                        answer.put(name, value);
094                    }
095                    catch (JMSException e) {
096                        throw new MessagePropertyAcessException(name, e);
097                    }
098                }
099                return answer;
100            }
101            else {
102                return lazyHeaders;
103            }
104        }
105    
106    }