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