001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.camel.component;
018
019import javax.jms.Message;
020import javax.jms.Session;
021
022import org.apache.activemq.command.ActiveMQDestination;
023import org.apache.activemq.command.ActiveMQMessage;
024import org.apache.camel.Exchange;
025import org.apache.camel.component.jms.JmsMessage;
026import org.apache.camel.component.jms.MessageCreatedStrategy;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * A strategy to enrich JMS message with their original destination if the Camel
032 * route originates from a JMS destination.
033 */
034public class OriginalDestinationPropagateStrategy implements MessageCreatedStrategy {
035
036    private static final transient Logger LOG = LoggerFactory.getLogger(OriginalDestinationPropagateStrategy.class);
037
038    @Override
039    public void onMessageCreated(Message message, Session session, Exchange exchange, Throwable cause) {
040        if (exchange.getIn() instanceof JmsMessage) {
041            JmsMessage msg = exchange.getIn(JmsMessage.class);
042            Message jms = msg.getJmsMessage();
043            if (jms != null && jms instanceof ActiveMQMessage && message instanceof ActiveMQMessage) {
044                ActiveMQMessage amq = (ActiveMQMessage) jms;
045                if (amq != null) {
046                    if (amq.getOriginalDestination() == null) {
047                        ActiveMQDestination from = amq.getDestination();
048                        if (from != null) {
049                            LOG.trace("Setting OriginalDestination: {} on {}", from, message);
050                            ((ActiveMQMessage) message).setOriginalDestination(from);
051                        }
052                    }
053                }
054            }
055        }
056    }
057
058}