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.component.jms;
019
020 import org.apache.camel.InvalidHeaderTypeException;
021 import org.apache.camel.impl.HeadersSupport;
022
023 import javax.jms.JMSException;
024 import javax.jms.Message;
025 import java.util.Enumeration;
026 import java.util.HashMap;
027 import java.util.Map;
028
029 /**
030 * @version $Revision:520964 $
031 */
032 public class JmsHeaders extends HeadersSupport {
033 private final DefaultJmsMessage message;
034 private Map<String, Object> lazyHeaders;
035
036 public JmsHeaders(DefaultJmsMessage message) {
037 this.message = message;
038 }
039
040 public Object getHeader(String name) {
041 Message request = message.getJmsMessage();
042 if (request != null) {
043 try {
044 Object value = request.getObjectProperty(name);
045 try {
046 return value;
047 }
048 catch (ClassCastException e) {
049 throw new InvalidHeaderTypeException(e.getMessage(), value);
050 }
051 }
052 catch (JMSException e) {
053 throw new MessagePropertyAcessException(name, e);
054 }
055 }
056 return null;
057 }
058
059 public void setHeader(String name, Object value) {
060 Message request = message.getJmsMessage();
061 if (request != null) {
062 try {
063 request.setObjectProperty(name, value);
064 }
065 catch (JMSException e) {
066 throw new MessagePropertyAcessException(name, e);
067 }
068 }
069 else {
070 if (lazyHeaders == null) {
071 lazyHeaders = new HashMap<String, Object>();
072 }
073 lazyHeaders.put(name, value);
074 }
075 }
076
077 public Map<String, Object> getHeaders() {
078 Message request = message.getJmsMessage();
079 if (request != null) {
080 Map<String, Object> answer = new HashMap<String, Object>();
081 Enumeration names;
082 try {
083 names = request.getPropertyNames();
084 }
085 catch (JMSException e) {
086 throw new MessagePropertyNamesAcessException(e);
087 }
088 while (names.hasMoreElements()) {
089 String name = names.nextElement().toString();
090 try {
091 Object value = request.getObjectProperty(name);
092 answer.put(name, value);
093 }
094 catch (JMSException e) {
095 throw new MessagePropertyAcessException(name, e);
096 }
097 }
098 return answer;
099 }
100 else {
101 return lazyHeaders;
102 }
103 }
104
105 }