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