Coverage Report - org.apache.camel.component.jms.JmsBinding
 
Classes in this File Line Coverage Branch Coverage Complexity
JmsBinding
60% 
67% 
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 org.apache.camel.Exchange;
 21  
 
 22  
 import javax.jms.BytesMessage;
 23  
 import javax.jms.JMSException;
 24  
 import javax.jms.MapMessage;
 25  
 import javax.jms.Message;
 26  
 import javax.jms.ObjectMessage;
 27  
 import javax.jms.Session;
 28  
 import javax.jms.StreamMessage;
 29  
 import javax.jms.TextMessage;
 30  
 import java.io.Serializable;
 31  
 import java.util.Enumeration;
 32  
 import java.util.HashMap;
 33  
 import java.util.Map;
 34  
 import java.util.Set;
 35  
 
 36  
 /**
 37  
  * A Strategy used to convert between a Camel {@JmsExchange} and {@JmsMessage} to and from a
 38  
  * JMS {@link Message}
 39  
  *
 40  
  * @version $Revision: 534145 $
 41  
  */
 42  103
 public class JmsBinding {
 43  
     /**
 44  
      * Extracts the body from the JMS message
 45  
      *
 46  
      * @param exchange
 47  
      * @param message
 48  
      */
 49  
     public Object extractBodyFromJms(JmsExchange exchange, Message message) {
 50  
         try {
 51  20
             if (message instanceof ObjectMessage) {
 52  6
                 ObjectMessage objectMessage = (ObjectMessage) message;
 53  6
                 return objectMessage.getObject();
 54  
             }
 55  14
             else if (message instanceof TextMessage) {
 56  14
                 TextMessage textMessage = (TextMessage) message;
 57  14
                 return textMessage.getText();
 58  
             }
 59  0
             else if (message instanceof MapMessage) {
 60  0
                 return createMapFromMapMessage((MapMessage) message);
 61  
             }
 62  0
             else if (message instanceof BytesMessage || message instanceof StreamMessage) {
 63  
                 // TODO we need a decoder to be able to process the message
 64  0
                 return message;
 65  
             }
 66  
             else {
 67  0
                 return null;
 68  
             }
 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  24
         Message answer = createJmsMessage(exchange.getIn().getBody(), session);
 84  24
         appendJmsProperties(answer, exchange, session);
 85  24
         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  24
         org.apache.camel.Message in = exchange.getIn();
 93  24
         Set<Map.Entry<String, Object>> entries = in.getHeaders().entrySet();
 94  24
         for (Map.Entry<String, Object> entry : entries) {
 95  24
             String headerName = entry.getKey();
 96  24
             Object headerValue = entry.getValue();
 97  24
             if (shouldOutputHeader(in, headerName, headerValue)) {
 98  24
                 jmsMessage.setObjectProperty(headerName, headerValue);
 99  
             }
 100  24
         }
 101  24
     }
 102  
 
 103  
     protected Message createJmsMessage(Object body, Session session) throws JMSException {
 104  24
         if (body instanceof String) {
 105  18
             return session.createTextMessage((String) body);
 106  
         }
 107  6
         else if (body instanceof Serializable) {
 108  6
             return session.createObjectMessage((Serializable) body);
 109  
         }
 110  
         else {
 111  0
             return session.createMessage();
 112  
         }
 113  
     }
 114  
 
 115  
     /**
 116  
      * Extracts a {@link Map} from a {@link MapMessage}
 117  
      */
 118  
     public Map<String, Object> createMapFromMapMessage(MapMessage message) throws JMSException {
 119  0
         Map<String, Object> answer = new HashMap<String, Object>();
 120  0
         Enumeration names = message.getPropertyNames();
 121  0
         while (names.hasMoreElements()) {
 122  0
             String name = names.nextElement().toString();
 123  0
             Object value = message.getObject(name);
 124  0
             answer.put(name, value);
 125  0
         }
 126  0
         return answer;
 127  
     }
 128  
 
 129  
     /**
 130  
      * Strategy to allow filtering of headers which are put on the JMS message
 131  
      */
 132  
     protected boolean shouldOutputHeader(org.apache.camel.Message camelMessage, String headerName, Object headerValue) {
 133  24
         return true;
 134  
     }
 135  
 }