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 java.beans.DesignMode;
021 import java.io.File;
022 import java.util.Enumeration;
023 import java.util.Map;
024 import javax.jms.Destination;
025 import javax.jms.JMSException;
026 import javax.jms.Message;
027 import javax.jms.Queue;
028 import javax.jms.TemporaryTopic;
029 import javax.jms.Topic;
030 import org.apache.camel.impl.DefaultMessage;
031 import org.apache.commons.logging.Log;
032 import org.apache.commons.logging.LogFactory;
033
034 /**
035 * Represents a {@link org.apache.camel.Message} for working with JMS
036 *
037 * @version $Revision:520964 $
038 */
039 public class JmsMessage extends DefaultMessage {
040 private static final transient Log log = LogFactory.getLog(JmsMessage.class);
041 private Message jmsMessage;
042
043 public JmsMessage() {
044 }
045
046 public JmsMessage(Message jmsMessage) {
047 setJmsMessage(jmsMessage);
048 }
049
050 @Override
051 public String toString() {
052 if (jmsMessage != null) {
053 return "JmsMessage: " + jmsMessage;
054 }
055 else {
056 return "JmsMessage: " + getBody();
057 }
058 }
059
060
061 /**
062 * Returns the underlying JMS message
063 *
064 * @return the underlying JMS message
065 */
066 public Message getJmsMessage() {
067 return jmsMessage;
068 }
069
070 public void setJmsMessage(Message jmsMessage){
071 this.jmsMessage=jmsMessage;
072 try{
073 String id=getDestinationAsString(jmsMessage.getJMSDestination());
074 id+=getSanitizedString(jmsMessage.getJMSMessageID());
075 setMessageId(id);
076 }catch(JMSException e){
077 log.error("Failed to get message id from message "+jmsMessage,e);
078 }
079 }
080
081 public Object getHeader(String name) {
082 Object answer = null;
083 if (jmsMessage != null) {
084 try {
085 answer = jmsMessage.getObjectProperty(name);
086 }
087 catch (JMSException e) {
088 throw new MessagePropertyAccessException(name, e);
089 }
090 }
091 if (answer == null) {
092 answer = super.getHeader(name);
093 }
094 return answer;
095 }
096
097 @Override
098 public JmsMessage newInstance() {
099 return new JmsMessage();
100 }
101
102 @Override
103 protected Object createBody() {
104 if (jmsMessage != null && getExchange() instanceof JmsExchange) {
105 JmsExchange exchange = (JmsExchange)getExchange();
106 return (exchange.getBinding().extractBodyFromJms(exchange, jmsMessage));
107 }
108 return null;
109 }
110
111 @Override
112 protected void populateInitialHeaders(Map<String, Object> map) {
113 if (jmsMessage != null) {
114 Enumeration names;
115 try {
116 names = jmsMessage.getPropertyNames();
117 }
118 catch (JMSException e) {
119 throw new MessagePropertyNamesAccessException(e);
120 }
121 while (names.hasMoreElements()) {
122 String name = names.nextElement().toString();
123 try {
124 Object value = jmsMessage.getObjectProperty(name);
125 map.put(name, value);
126 }
127 catch (JMSException e) {
128 throw new MessagePropertyAccessException(name, e);
129 }
130 }
131 }
132 }
133
134 private String getDestinationAsString(Destination destination) throws JMSException {
135 String result = "";
136 if (destination instanceof Topic) {
137 result += "topic" + File.separator + getSanitizedString(((Topic)destination).getTopicName());
138 }else {
139 result += "queue" + File.separator + getSanitizedString(((Queue)destination).getQueueName());
140 }
141 result += File.separator;
142 return result;
143 }
144 private String getSanitizedString(Object value) {
145 return value != null ? value.toString().replaceAll("[^a-zA-Z0-9\\.\\_\\-]", "_") : "";
146 }
147 }
148