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