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.processor.interceptor;
018    
019    import java.text.SimpleDateFormat;
020    import java.util.Date;
021    
022    import org.apache.camel.api.management.mbean.BacklogTracerEventMessage;
023    
024    /**
025     * An event message holding the traced message by the {@link BacklogTracer}.
026     */
027    public final class DefaultBacklogTracerEventMessage implements BacklogTracerEventMessage {
028    
029        private static final long serialVersionUID = 1L;
030    
031        private final long uid;
032        private final Date timestamp;
033        private final String routeId;
034        private final String toNode;
035        private final String exchangeId;
036        private final String messageAsXml;
037    
038        public DefaultBacklogTracerEventMessage(long uid, Date timestamp, String routeId, String toNode, String exchangeId, String messageAsXml) {
039            this.uid = uid;
040            this.timestamp = timestamp;
041            this.routeId = routeId;
042            this.toNode = toNode;
043            this.exchangeId = exchangeId;
044            this.messageAsXml = messageAsXml;
045        }
046    
047        public long getUid() {
048            return uid;
049        }
050    
051        public Date getTimestamp() {
052            return timestamp;
053        }
054    
055        public String getRouteId() {
056            return routeId;
057        }
058    
059        public String getToNode() {
060            return toNode;
061        }
062    
063        public String getExchangeId() {
064            return exchangeId;
065        }
066    
067        public String getMessageAsXml() {
068            return messageAsXml;
069        }
070    
071        @Override
072        public String toString() {
073            return "DefaultBacklogTracerEventMessage[" + exchangeId + " at " + toNode + "]";
074        }
075    
076        /**
077         * Dumps the event message as XML using the {@link #ROOT_TAG} as root tag.
078         * <p/>
079         * The <tt>timestamp</tt> tag is formatted in the format defined by {@link #TIMESTAMP_FORMAT}
080         *
081         * @return xml representation of this event
082         */
083        public String toXml(int indent) {
084            StringBuilder prefix = new StringBuilder();
085            for (int i = 0; i < indent; i++) {
086                prefix.append(" ");
087            }
088    
089            StringBuilder sb = new StringBuilder();
090            sb.append(prefix).append("<").append(ROOT_TAG).append(">\n");
091            sb.append(prefix).append("  <uid>").append(uid).append("</uid>\n");
092            String ts = new SimpleDateFormat(TIMESTAMP_FORMAT).format(timestamp);
093            sb.append(prefix).append("  <timestamp>").append(ts).append("</timestamp>\n");
094            sb.append(prefix).append("  <routeId>").append(routeId).append("</routeId>\n");
095            if (toNode != null) {
096                sb.append(prefix).append("  <toNode>").append(toNode).append("</toNode>\n");
097            } else {
098                // if first message the use routeId as toNode
099                sb.append(prefix).append("  <toNode>").append(routeId).append("</toNode>\n");
100            }
101            sb.append(prefix).append("  <exchangeId>").append(exchangeId).append("</exchangeId>\n");
102            sb.append(prefix).append(messageAsXml).append("\n");
103            sb.append(prefix).append("</").append(ROOT_TAG).append(">");
104            return sb.toString();
105        }
106    }
107