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