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.impl;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024import java.util.Stack;
025import java.util.concurrent.atomic.AtomicInteger;
026
027import org.apache.camel.Exchange;
028import org.apache.camel.RouteNode;
029import org.apache.camel.model.ProcessorDefinition;
030import org.apache.camel.spi.TracedRouteNodes;
031
032/**
033 * Default {@link org.apache.camel.spi.TracedRouteNodes}
034 *
035 * @deprecated use {@link Exchange#MESSAGE_HISTORY} instead.
036 */
037@Deprecated
038public class DefaultTracedRouteNodes implements TracedRouteNodes {
039    private final Stack<List<RouteNode>> routeNodes = new Stack<>();
040    private final Map<ProcessorDefinition<?>, AtomicInteger> nodeCounter = new HashMap<>();
041
042    public DefaultTracedRouteNodes() {
043        // create an empty list to start with
044        routeNodes.push(new ArrayList<RouteNode>());
045    }
046
047    public void addTraced(RouteNode entry) {
048        List<RouteNode> list = routeNodes.isEmpty() ? null : routeNodes.peek();
049        if (list == null) {
050            list = new ArrayList<>();
051            routeNodes.push(list);
052        }
053        list.add(entry);
054    }
055
056    public RouteNode getLastNode() {
057        List<RouteNode> list = routeNodes.isEmpty() ? null : routeNodes.peek();
058        if (list == null || list.isEmpty()) {
059            return null;
060        }
061        return list.get(list.size() - 1);
062    }
063
064    public RouteNode getSecondLastNode() {
065        List<RouteNode> list = routeNodes.isEmpty() ? null : routeNodes.peek();
066        if (list == null || list.isEmpty() || list.size() == 1) {
067            return null;
068        }
069        return list.get(list.size() - 2);
070    }
071
072    public List<RouteNode> getNodes() {
073        List<RouteNode> answer = new ArrayList<>();
074        for (List<RouteNode> list : routeNodes) {
075            answer.addAll(list);
076        }
077        return Collections.unmodifiableList(answer);
078    }
079
080    public void popBlock() {
081        if (!routeNodes.isEmpty()) {
082            routeNodes.pop();
083        }
084    }
085
086    public void pushBlock() {
087        // push a new block and add the last node as starting point
088        RouteNode last = getLastNode();
089        routeNodes.push(new ArrayList<RouteNode>());
090        if (last != null) {
091            addTraced(last);
092        }
093    }
094
095    public void clear() {
096        routeNodes.clear();
097    }
098
099    public int getAndIncrementCounter(ProcessorDefinition<?> node) {
100        AtomicInteger count = nodeCounter.get(node);
101        if (count == null) {
102            count = new AtomicInteger();
103            nodeCounter.put(node, count);
104        }
105        return count.getAndIncrement();
106    }
107
108}