Coverage Report - org.apache.camel.component.jms.JmsBinding
 
Classes in this File Line Coverage Branch Coverage Complexity
JmsBinding
65% 
80% 
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 org.apache.camel.Exchange;
 20  
 import org.apache.camel.util.ExchangeHelper;
 21  
 import org.apache.commons.logging.Log;
 22  
 import org.apache.commons.logging.LogFactory;
 23  
 
 24  
 import javax.jms.BytesMessage;
 25  
 import javax.jms.Destination;
 26  
 import javax.jms.JMSException;
 27  
 import javax.jms.MapMessage;
 28  
 import javax.jms.Message;
 29  
 import javax.jms.ObjectMessage;
 30  
 import javax.jms.Session;
 31  
 import javax.jms.StreamMessage;
 32  
 import javax.jms.TextMessage;
 33  
 import java.io.Serializable;
 34  
 import java.util.Enumeration;
 35  
 import java.util.HashMap;
 36  
 import java.util.Map;
 37  
 import java.util.Set;
 38  
 
 39  
 /**
 40  
  * A Strategy used to convert between a Camel {@JmsExchange} and {@JmsMessage}
 41  
  * to and from a JMS {@link Message}
 42  
  * 
 43  
  * @version $Revision: 564568 $
 44  
  */
 45  103
 public class JmsBinding {
 46  1
     private static final transient Log LOG = LogFactory.getLog(JmsBinding.class);
 47  
 
 48  
     /**
 49  
      * Extracts the body from the JMS message
 50  
      * 
 51  
      * @param exchange
 52  
      * @param message
 53  
      */
 54  
     public Object extractBodyFromJms(JmsExchange exchange, Message message) {
 55  
         try {
 56  19
             if (message instanceof ObjectMessage) {
 57  6
                 ObjectMessage objectMessage = (ObjectMessage)message;
 58  6
                 return objectMessage.getObject();
 59  13
             } else if (message instanceof TextMessage) {
 60  13
                 TextMessage textMessage = (TextMessage)message;
 61  13
                 return textMessage.getText();
 62  0
             } else if (message instanceof MapMessage) {
 63  0
                 return createMapFromMapMessage((MapMessage)message);
 64  0
             } else if (message instanceof BytesMessage || message instanceof StreamMessage) {
 65  
                 // TODO we need a decoder to be able to process the message
 66  0
                 return message;
 67  
             } else {
 68  0
                 return null;
 69  
             }
 70  0
         } catch (JMSException e) {
 71  0
             throw new RuntimeJmsException("Failed to extract body due to: " + e + ". Message: " + message, e);
 72  
         }
 73  
     }
 74  
 
 75  
     /**
 76  
      * Creates a JMS message from the Camel exchange and message
 77  
      * 
 78  
      * @param session the JMS session used to create the message
 79  
      * @return a newly created JMS Message instance containing the
 80  
      * @throws JMSException if the message could not be created
 81  
      */
 82  
     public Message makeJmsMessage(Exchange exchange, Session session) throws JMSException {
 83  27
         Message answer = createJmsMessage(exchange.getIn().getBody(), session);
 84  27
         appendJmsProperties(answer, exchange, session);
 85  27
         return answer;
 86  
     }
 87  
 
 88  
     /**
 89  
      * Appends the JMS headers from the Camel {@link JmsMessage}
 90  
      */
 91  
     protected void appendJmsProperties(Message jmsMessage, Exchange exchange, Session session) throws JMSException {
 92  27
         org.apache.camel.Message in = exchange.getIn();
 93  27
         Set<Map.Entry<String, Object>> entries = in.getHeaders().entrySet();
 94  27
         for (Map.Entry<String, Object> entry : entries) {
 95  197
             String headerName = entry.getKey();
 96  197
             Object headerValue = entry.getValue();
 97  
             
 98  197
             if (headerName.startsWith("JMS")) {
 99  160
                 if (headerName.equals("JMSCorrelationID")) {
 100  16
                     jmsMessage.setJMSCorrelationID(ExchangeHelper.convertToType(exchange, String.class, headerValue));
 101  16
                 }
 102  144
                 else if (headerName.equals("JMSCorrelationID")) {
 103  0
                     jmsMessage.setJMSCorrelationID(ExchangeHelper.convertToType(exchange, String.class, headerValue));
 104  0
                 }
 105  144
                 else if (headerName.equals("JMSReplyTo")) {
 106  16
                     jmsMessage.setJMSReplyTo(ExchangeHelper.convertToType(exchange, Destination.class, headerValue));
 107  16
                 }
 108  128
                 else if (headerName.equals("JMSType")) {
 109  16
                     jmsMessage.setJMSType(ExchangeHelper.convertToType(exchange, String.class, headerValue));
 110  16
                 }
 111  112
                 else if (LOG.isDebugEnabled()) {
 112  
                     // The following properties are set by the MessageProducer
 113  
                     //   JMSDeliveryMode, JMSDestination, JMSExpiration, JMSPriority,
 114  
                     // The following are set on the underlying JMS provider
 115  
                     //   JMSMessageID, JMSTimestamp, JMSRedelivered
 116  0
                     LOG.debug("Ignoring JMS header: " + headerName + " with value: " + headerValue);
 117  0
                 }
 118  
             }
 119  37
             else if (shouldOutputHeader(in, headerName, headerValue)) {
 120  37
                 jmsMessage.setObjectProperty(headerName, headerValue);
 121  
             }
 122  197
         }
 123  27
     }
 124  
 
 125  
     protected Message createJmsMessage(Object body, Session session) throws JMSException {
 126  27
         if (body instanceof String) {
 127  21
             return session.createTextMessage((String)body);
 128  6
         } else if (body instanceof Serializable) {
 129  6
             return session.createObjectMessage((Serializable)body);
 130  
         } else {
 131  0
             return session.createMessage();
 132  
         }
 133  
     }
 134  
 
 135  
     /**
 136  
      * Extracts a {@link Map} from a {@link MapMessage}
 137  
      */
 138  
     public Map<String, Object> createMapFromMapMessage(MapMessage message) throws JMSException {
 139  0
         Map<String, Object> answer = new HashMap<String, Object>();
 140  0
         Enumeration names = message.getPropertyNames();
 141  0
         while (names.hasMoreElements()) {
 142  0
             String name = names.nextElement().toString();
 143  0
             Object value = message.getObject(name);
 144  0
             answer.put(name, value);
 145  0
         }
 146  0
         return answer;
 147  
     }
 148  
 
 149  
     /**
 150  
      * Strategy to allow filtering of headers which are put on the JMS message
 151  
      */
 152  
     protected boolean shouldOutputHeader(org.apache.camel.Message camelMessage, String headerName, Object headerValue) {
 153  37
         return true;
 154  
     }
 155  
 }