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 */ 017 package org.apache.camel.management.mbean; 018 019 import java.util.Date; 020 import java.util.HashMap; 021 import java.util.Map; 022 import java.util.concurrent.atomic.AtomicLong; 023 import javax.management.Notification; 024 025 import org.apache.camel.Exchange; 026 import org.apache.camel.Message; 027 import org.apache.camel.Processor; 028 import org.apache.camel.Traceable; 029 import org.apache.camel.api.management.NotificationSender; 030 import org.apache.camel.api.management.NotificationSenderAware; 031 import org.apache.camel.model.ProcessorDefinition; 032 import org.apache.camel.processor.interceptor.TraceEventHandler; 033 import org.apache.camel.processor.interceptor.TraceInterceptor; 034 import org.apache.camel.processor.interceptor.Tracer; 035 import org.apache.camel.util.MessageHelper; 036 037 public final class JMXNotificationTraceEventHandler implements TraceEventHandler, NotificationSenderAware { 038 private static final int MAX_MESSAGE_LENGTH = 60; 039 private final AtomicLong num = new AtomicLong(); 040 private final Tracer tracer; 041 private NotificationSender notificationSender; 042 043 public JMXNotificationTraceEventHandler(Tracer tracer) { 044 this.tracer = tracer; 045 } 046 047 public void traceExchangeOut(ProcessorDefinition<?> node, Processor target, TraceInterceptor traceInterceptor, Exchange exchange, Object traceState) throws Exception { 048 } 049 050 public Object traceExchangeIn(ProcessorDefinition<?> node, Processor target, TraceInterceptor traceInterceptor, Exchange exchange) throws Exception { 051 return null; 052 } 053 054 public void traceExchange(ProcessorDefinition<?> node, Processor target, TraceInterceptor traceInterceptor, Exchange exchange) throws Exception { 055 if (notificationSender != null && tracer.isJmxTraceNotifications()) { 056 String body = MessageHelper.extractBodyForLogging(exchange.getIn(), "", false, true, tracer.getTraceBodySize()); 057 058 if (body == null) { 059 body = ""; 060 } 061 String message = body.substring(0, Math.min(body.length(), MAX_MESSAGE_LENGTH)); 062 Map<String, Object> tm = createTraceMessage(node, exchange, body); 063 064 Notification notification = new Notification("TraceNotification", exchange.toString(), num.getAndIncrement(), System.currentTimeMillis(), message); 065 notification.setUserData(tm); 066 067 notificationSender.sendNotification(notification); 068 } 069 070 } 071 072 private Map<String, Object> createTraceMessage(ProcessorDefinition<?> node, Exchange exchange, String body) { 073 Map<String, Object> mi = new HashMap<String, Object>(); 074 mi.put("ExchangeId", exchange.getExchangeId()); 075 mi.put("EndpointURI", getEndpointUri(node)); 076 mi.put("TimeStamp", new Date(System.currentTimeMillis())); 077 mi.put("Body", body); 078 079 Message message = exchange.getIn(); 080 Map<String, Object> sHeaders = message.getHeaders(); 081 Map<String, Object> sProperties = exchange.getProperties(); 082 083 Map<String, String> headers = new HashMap<String, String>(); 084 for (String key : sHeaders.keySet()) { 085 headers.put(key, message.getHeader(key, String.class)); 086 } 087 mi.put("Headers", headers); 088 089 Map<String, String> properties = new HashMap<String, String>(); 090 for (String key : sProperties.keySet()) { 091 properties.put(key, exchange.getProperty(key, String.class)); 092 } 093 mi.put("Properties", properties); 094 return mi; 095 } 096 097 private String getEndpointUri(ProcessorDefinition<?> node) { 098 if (node instanceof Traceable) { 099 Traceable tr = (Traceable)node; 100 return tr.getTraceLabel(); 101 } else { 102 return node.getLabel(); 103 } 104 } 105 106 @Override 107 public void setNotificationSender(NotificationSender sender) { 108 this.notificationSender = sender; 109 } 110 111 }