/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.activemq.broker.util;

import java.io.IOException;
import java.util.Map;

import org.apache.activemq.broker.BrokerPluginSupport;
import org.apache.activemq.command.MessageDispatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The TraceBrokerPathPlugin can be used in a network of Brokers. Each Broker 
 * that has the plugin configured, will add it's brokerName to the content 
 * of a JMS Property. If all Brokers have this property enabled, the path the 
 * message actually took through the network can be seen in the defined property.
 * 
 * @org.apache.xbean.XBean element="traceBrokerPathPlugin"
 * 
 * 
 */

public class TraceBrokerPathPlugin extends BrokerPluginSupport {

	private String stampProperty = "BrokerPath";
    private static final Logger LOG = LoggerFactory.getLogger(TraceBrokerPathPlugin.class);
	
	public String getStampProperty() {
		return stampProperty;
	}

	public void setStampProperty(String stampProperty) {
		this.stampProperty = stampProperty;
	}

	public void preProcessDispatch(MessageDispatch messageDispatch) {
		try {
    		String brokerStamp = (String)messageDispatch.getMessage().getProperty(getStampProperty());
    		if (brokerStamp == null) {
    			brokerStamp = getBrokerName();
    		} else {
    			brokerStamp += "," + getBrokerName();
    		}
    		messageDispatch.getMessage().setProperty(getStampProperty(), brokerStamp);
		} catch (IOException ioe) {
			LOG.warn("Setting broker property failed " + ioe, ioe);
		}
		super.preProcessDispatch(messageDispatch);
	}
}

@@ -870,30 +870,8 @@
 ion;
-%0Aimport java.util.Map;
 %0A%0Aim
@@ -1114,17 +1114,16 @@
 h Broker
- 
 %0A * that
@@ -1189,17 +1189,16 @@
  content
- 
 %0A * of a
@@ -1267,17 +1267,16 @@
 path the
- 
 %0A * mess
@@ -1352,17 +1352,16 @@
 erty.%0A *
- 
 %0A * @org
@@ -1418,18 +1418,43 @@
 %22%0A *
- %0A * 
 %0A */%0A
+@SuppressWarnings(%22unchecked%22)
 %0Apub
@@ -1516,17 +1516,20 @@
 port %7B%0A%0A
-%09
+    
 private 
@@ -1661,11 +1661,13 @@
 s);%0A
-%09%0A%09
+%0A    
 publ
@@ -1697,18 +1697,24 @@
 rty() %7B%0A
-%09%09
+        
 return s
@@ -1719,37 +1719,43 @@
  stampProperty;%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 public void setS
@@ -1791,18 +1791,93 @@
 erty) %7B%0A
-%09%09
+        if (stampProperty != null && !stampProperty.isEmpty()) %7B%0A            
 this.sta
@@ -1908,13 +1908,29 @@
 ty;%0A
-%09%7D%0A%0A%09
+        %7D%0A    %7D%0A%0A    
 publ
@@ -1991,18 +1991,24 @@
 atch) %7B%0A
-%09%09
+        
 try %7B%0A  
@@ -2009,18 +2009,111 @@
 y %7B%0A    
+ 
-%09%09
+       if (messageDispatch != null && messageDispatch.getMessage() != null) %7B%0A                
 String b
@@ -2199,18 +2199,28 @@
 ));%0A    
-%09%09
+            
 if (brok
@@ -2238,27 +2238,40 @@
 null) %7B%0A    
-%09%09%09
+                
 brokerStamp 
@@ -2293,18 +2293,28 @@
 ();%0A    
-%09%09
+            
 %7D else %7B
@@ -2318,19 +2318,32 @@
 e %7B%0A    
-%09%09%09
+                
 brokerSt
@@ -2380,18 +2380,38 @@
     
-%09%09%7D%0A    %09%09
+            %7D%0A                
 mess
@@ -2481,18 +2481,38 @@
 Stamp);%0A
-%09%09
+            %7D%0A        
 %7D catch 
@@ -2535,11 +2535,20 @@
 ) %7B%0A
-%09%09%09
+            
 LOG.
@@ -2603,14 +2603,26 @@
 e);%0A
-%09%09%7D%0A%09%09
+        %7D%0A        
 supe
@@ -2664,9 +2664,12 @@
 h);%0A
-%09
+    
 %7D%0A%7D%0A

fix for: https://issues.apache.org/jira/browse/AMQ-3524