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.activemq.camel.converter;
019    
020    import javax.jms.JMSException;
021    import javax.jms.Message;
022    import javax.jms.Session;
023    
024    import org.apache.activemq.command.ActiveMQMessage;
025    import org.springframework.jms.support.converter.MessageConversionException;
026    import org.springframework.jms.support.converter.MessageConverter;
027    import org.springframework.util.ObjectUtils;
028    
029    /**
030     * Identity conversion, return the original ActiveMQMessage as is, useful when camel does message
031     * redelivery routing. ReadOnlyPropertes flag inverted to allow 
032     * additional properties to be appended or existing properties to be modified
033     */
034    public class IdentityMessageReuseConverter implements MessageConverter {
035    
036        /* (non-Javadoc)
037         * @see org.springframework.jms.support.converter.MessageConverter#fromMessage(javax.jms.Message)
038         */
039        public Object fromMessage(Message message) throws JMSException, MessageConversionException {
040            return message;
041        }
042    
043        /* (non-Javadoc)
044         * @see org.springframework.jms.support.converter.MessageConverter#toMessage(java.lang.Object, javax.jms.Session)
045         */
046        public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
047            if (object instanceof ActiveMQMessage) {
048                // allow setting additional properties
049                ((ActiveMQMessage)object).setReadOnlyProperties(false);
050                return (Message)object;
051            } else {
052                throw new MessageConversionException("Cannot reuse object of type [" +
053                        ObjectUtils.nullSafeClassName(object) + "] as ActiveMQMessage message. Message must already be an ActiveMQMessage.");
054            }
055        }
056    }