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.broker.util;
018
019import java.io.IOException;
020
021import org.apache.activemq.broker.BrokerPluginSupport;
022import org.apache.activemq.command.MessageDispatch;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026/**
027 * The TraceBrokerPathPlugin can be used in a network of Brokers. Each Broker
028 * that has the plugin configured, will add it's brokerName to the content
029 * of a JMS Property. If all Brokers have this property enabled, the path the
030 * message actually took through the network can be seen in the defined property.
031 *
032 * @org.apache.xbean.XBean element="traceBrokerPathPlugin"
033 *
034 */
035@SuppressWarnings("unchecked")
036public class TraceBrokerPathPlugin extends BrokerPluginSupport {
037
038    private String stampProperty = "BrokerPath";
039    private static final Logger LOG = LoggerFactory.getLogger(TraceBrokerPathPlugin.class);
040
041    public String getStampProperty() {
042        return stampProperty;
043    }
044
045    public void setStampProperty(String stampProperty) {
046        if (stampProperty != null && !stampProperty.isEmpty()) {
047            this.stampProperty = stampProperty;
048        }
049    }
050
051    public void preProcessDispatch(MessageDispatch messageDispatch) {
052        try {
053            if (messageDispatch != null && messageDispatch.getMessage() != null) {
054                String brokerStamp = (String)messageDispatch.getMessage().getProperty(getStampProperty());
055                if (brokerStamp == null) {
056                    brokerStamp = getBrokerName();
057                } else {
058                    brokerStamp += "," + getBrokerName();
059                }
060                messageDispatch.getMessage().setProperty(getStampProperty(), brokerStamp);
061                messageDispatch.getMessage().setMarshalledProperties(null);
062            }
063        } catch (IOException ioe) {
064            LOG.warn("Setting broker property failed", ioe);
065        }
066        super.preProcessDispatch(messageDispatch);
067    }
068}