Coverage Report - org.apache.camel.component.jms.JmsMessage
 
Classes in this File Line Coverage Branch Coverage Complexity
JmsMessage
76% 
89% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.component.jms;
 18  
 
 19  
 import java.io.File;
 20  
 import java.util.Enumeration;
 21  
 import java.util.Map;
 22  
 
 23  
 import javax.jms.Destination;
 24  
 import javax.jms.JMSException;
 25  
 import javax.jms.Message;
 26  
 import javax.jms.Queue;
 27  
 import javax.jms.Topic;
 28  
 
 29  
 import org.apache.camel.impl.DefaultMessage;
 30  
 import org.apache.commons.logging.Log;
 31  
 import org.apache.commons.logging.LogFactory;
 32  
 
 33  
 /**
 34  
  * Represents a {@link org.apache.camel.Message} for working with JMS
 35  
  * 
 36  
  * @version $Revision:520964 $
 37  
  */
 38  0
 public class JmsMessage extends DefaultMessage {
 39  1
     private static final transient Log LOG = LogFactory.getLog(JmsMessage.class);
 40  
     private Message jmsMessage;
 41  
 
 42  46
     public JmsMessage() {
 43  46
     }
 44  
 
 45  19
     public JmsMessage(Message jmsMessage) {
 46  19
         setJmsMessage(jmsMessage);
 47  19
     }
 48  
 
 49  
     @Override
 50  
     public String toString() {
 51  26
         if (jmsMessage != null) {
 52  13
             return "JmsMessage: " + jmsMessage;
 53  
         } else {
 54  13
             return "JmsMessage: " + getBody();
 55  
         }
 56  
     }
 57  
 
 58  
     /**
 59  
      * Returns the underlying JMS message
 60  
      * 
 61  
      * @return the underlying JMS message
 62  
      */
 63  
     public Message getJmsMessage() {
 64  0
         return jmsMessage;
 65  
     }
 66  
 
 67  
     public void setJmsMessage(Message jmsMessage) {
 68  19
         this.jmsMessage = jmsMessage;
 69  
         try {
 70  19
             String id = getDestinationAsString(jmsMessage.getJMSDestination());
 71  19
             id += getSanitizedString(jmsMessage.getJMSMessageID());
 72  19
             setMessageId(id);
 73  0
         } catch (JMSException e) {
 74  0
             LOG.error("Failed to get message id from message " + jmsMessage, e);
 75  19
         }
 76  19
     }
 77  
 
 78  
     public Object getHeader(String name) {
 79  18
         Object answer = null;
 80  
         
 81  
         // we will exclude using JMS-prefixed headers here to avoid strangeness with some JMS providers
 82  
         // e.g. ActiveMQ returns the String not the Destination type for "JMSReplyTo"!
 83  18
         if (jmsMessage != null && !name.startsWith("JMS")) {
 84  
             try {
 85  18
                 answer = jmsMessage.getObjectProperty(name);
 86  0
             } catch (JMSException e) {
 87  0
                 throw new MessagePropertyAccessException(name, e);
 88  18
             }
 89  
         }
 90  18
         if (answer == null) {
 91  12
             answer = super.getHeader(name);
 92  
         }
 93  18
         return answer;
 94  
     }
 95  
 
 96  
     @Override
 97  
     public JmsMessage newInstance() {
 98  0
         return new JmsMessage();
 99  
     }
 100  
 
 101  
     @Override
 102  
     protected Object createBody() {
 103  51
         if (jmsMessage != null && getExchange() instanceof JmsExchange) {
 104  19
             JmsExchange exchange = (JmsExchange)getExchange();
 105  19
             return exchange.getBinding().extractBodyFromJms(exchange, jmsMessage);
 106  
         }
 107  32
         return null;
 108  
     }
 109  
 
 110  
     @Override
 111  
     protected void populateInitialHeaders(Map<String, Object> map) {
 112  51
         if (jmsMessage != null) {
 113  
             // lets populate the standard JMS message headers
 114  
             try {
 115  11
                 map.put("JMSCorrelationID", jmsMessage.getJMSCorrelationID());
 116  11
                 map.put("JMSDeliveryMode", jmsMessage.getJMSDeliveryMode());
 117  11
                 map.put("JMSDestination", jmsMessage.getJMSDestination());
 118  11
                 map.put("JMSExpiration", jmsMessage.getJMSExpiration());
 119  11
                 map.put("JMSMessageID", jmsMessage.getJMSMessageID());
 120  11
                 map.put("JMSPriority", jmsMessage.getJMSPriority());
 121  11
                 map.put("JMSRedelivered", jmsMessage.getJMSRedelivered());
 122  11
                 map.put("JMSReplyTo", jmsMessage.getJMSReplyTo());
 123  11
                 map.put("JMSTimestamp", jmsMessage.getJMSTimestamp());
 124  11
                 map.put("JMSType", jmsMessage.getJMSType());
 125  
             }
 126  0
             catch (JMSException e) {
 127  0
                 throw new MessageJMSPropertyAccessException(e);
 128  11
             }
 129  
 
 130  
             Enumeration names;
 131  
             try {
 132  11
                 names = jmsMessage.getPropertyNames();
 133  0
             } catch (JMSException e) {
 134  0
                 throw new MessagePropertyNamesAccessException(e);
 135  11
             }
 136  22
             while (names.hasMoreElements()) {
 137  11
                 String name = names.nextElement().toString();
 138  
                 try {
 139  11
                     Object value = jmsMessage.getObjectProperty(name);
 140  11
                     map.put(name, value);
 141  0
                 } catch (JMSException e) {
 142  0
                     throw new MessagePropertyAccessException(name, e);
 143  11
                 }
 144  11
             }
 145  
         }
 146  51
     }
 147  
 
 148  
     private String getDestinationAsString(Destination destination) throws JMSException {
 149  19
         String result = "";
 150  19
         if (destination == null) {
 151  0
             result = "null destination!";
 152  0
         } else if (destination instanceof Topic) {
 153  0
             result += "topic" + File.separator + getSanitizedString(((Topic)destination).getTopicName());
 154  0
         } else {
 155  19
             result += "queue" + File.separator + getSanitizedString(((Queue)destination).getQueueName());
 156  
         }
 157  19
         result += File.separator;
 158  19
         return result;
 159  
     }
 160  
 
 161  
     private String getSanitizedString(Object value) {
 162  38
         return value != null ? value.toString().replaceAll("[^a-zA-Z0-9\\.\\_\\-]", "_") : "";
 163  
     }
 164  
 }