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