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 org.apache.camel.Exchange;
021    
022    import javax.jms.BytesMessage;
023    import javax.jms.JMSException;
024    import javax.jms.MapMessage;
025    import javax.jms.Message;
026    import javax.jms.ObjectMessage;
027    import javax.jms.Session;
028    import javax.jms.StreamMessage;
029    import javax.jms.TextMessage;
030    import java.io.Serializable;
031    import java.util.Enumeration;
032    import java.util.HashMap;
033    import java.util.Map;
034    import java.util.Set;
035    
036    /**
037     * A Strategy used to convert between a Camel {@JmsExchange} and {@JmsMessage} to and from a
038     * JMS {@link Message}
039     *
040     * @version $Revision: 534145 $
041     */
042    public class JmsBinding {
043        /**
044         * Extracts the body from the JMS message
045         *
046         * @param exchange
047         * @param message
048         */
049        public Object extractBodyFromJms(JmsExchange exchange, Message message) {
050            try {
051                if (message instanceof ObjectMessage) {
052                    ObjectMessage objectMessage = (ObjectMessage) message;
053                    return objectMessage.getObject();
054                }
055                else if (message instanceof TextMessage) {
056                    TextMessage textMessage = (TextMessage) message;
057                    return textMessage.getText();
058                }
059                else if (message instanceof MapMessage) {
060                    return createMapFromMapMessage((MapMessage) message);
061                }
062                else if (message instanceof BytesMessage || message instanceof StreamMessage) {
063                    // TODO we need a decoder to be able to process the message
064                    return message;
065                }
066                else {
067                    return null;
068                }
069            }
070            catch (JMSException e) {
071                throw new RuntimeJmsException("Failed to extract body due to: " + e + ". Message: " + message, e);
072            }
073        }
074    
075        /**
076         * Creates a JMS message from the Camel exchange and message
077         *
078         * @param session the JMS session used to create the message
079         * @return a newly created JMS Message instance containing the
080         * @throws JMSException if the message could not be created
081         */
082        public Message makeJmsMessage(Exchange exchange, Session session) throws JMSException {
083            Message answer = createJmsMessage(exchange.getIn().getBody(), session);
084            appendJmsProperties(answer, exchange, session);
085            return answer;
086        }
087    
088        /**
089         * Appends the JMS headers from the Camel {@link JmsMessage}
090         */
091        protected void appendJmsProperties(Message jmsMessage, Exchange exchange, Session session) throws JMSException {
092            org.apache.camel.Message in = exchange.getIn();
093            Set<Map.Entry<String, Object>> entries = in.getHeaders().entrySet();
094            for (Map.Entry<String, Object> entry : entries) {
095                String headerName = entry.getKey();
096                Object headerValue = entry.getValue();
097                if (shouldOutputHeader(in, headerName, headerValue)) {
098                    jmsMessage.setObjectProperty(headerName, headerValue);
099                }
100            }
101        }
102    
103        protected Message createJmsMessage(Object body, Session session) throws JMSException {
104            if (body instanceof String) {
105                return session.createTextMessage((String) body);
106            }
107            else if (body instanceof Serializable) {
108                return session.createObjectMessage((Serializable) body);
109            }
110            else {
111                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            Map<String, Object> answer = new HashMap<String, Object>();
120            Enumeration names = message.getPropertyNames();
121            while (names.hasMoreElements()) {
122                String name = names.nextElement().toString();
123                Object value = message.getObject(name);
124                answer.put(name, value);
125            }
126            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            return true;
134        }
135    }