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.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.apache.camel.CamelContext;
025    import org.apache.camel.Exchange;
026    import org.apache.camel.Processor;
027    import org.apache.camel.impl.DefaultCamelContext;
028    import org.apache.camel.model.ProcessorType;
029    import org.apache.camel.spi.InterceptStrategy;
030    import org.apache.commons.logging.Log;
031    import org.apache.commons.logging.LogFactory;
032    
033    /**
034     * An interceptor strategy for debugging and tracing routes
035     *
036     * @version $Revision: 42411 $
037     */
038    public class Debugger implements InterceptStrategy {
039        private static final transient Log LOG = LogFactory.getLog(Debugger.class);
040    
041        private int exchangeBufferSize = -1;
042        private Map<String, DebugInterceptor> interceptors = new HashMap<String, DebugInterceptor>();
043    
044        /**
045         * A helper method to return the debugger instance for a given {@link CamelContext} if one is enabled
046         *
047         * @param context the camel context the debugger is connected to
048         * @return the debugger or null if none can be found
049         */
050        public static Debugger getDebugger(CamelContext context) {
051            if (context instanceof DefaultCamelContext) {
052                DefaultCamelContext defaultCamelContext = (DefaultCamelContext) context;
053                List<InterceptStrategy> list = defaultCamelContext.getInterceptStrategies();
054                for (InterceptStrategy interceptStrategy : list) {
055                    if (interceptStrategy instanceof Debugger) {
056                        return (Debugger)interceptStrategy;
057                    }
058                }
059            }
060            return null;
061        }
062    
063        public DebugInterceptor getInterceptor(String id) {
064            return interceptors.get(id);
065        }
066    
067        /**
068         * Returns the list of exchanges sent to the given node in the DSL
069         */
070        public List<Exchange> getExchanges(String id) {
071            DebugInterceptor interceptor = getInterceptor(id);
072            if (interceptor == null) {
073                return null;
074            } else {
075                return interceptor.getExchanges();
076            }
077        }
078    
079        /**
080         * Returns the breakpoint object for the given node in the DSL
081         */
082        public Breakpoint getBreakpoint(String id) {
083            DebugInterceptor interceptor = getInterceptor(id);
084            if (interceptor == null) {
085                return null;
086            } else {
087                return interceptor.getBreakpoint();
088            }
089        }
090    
091    
092        public Processor wrapProcessorInInterceptors(ProcessorType processorType, Processor target) throws Exception {
093            String id = processorType.idOrCreate();
094            DebugInterceptor interceptor = new DebugInterceptor(processorType, target, createExchangeList(), createExceptionsList());
095            interceptors.put(id, interceptor);
096            if (LOG.isDebugEnabled()) {
097                LOG.debug("adding interceptor: " + interceptor);
098            }
099            return interceptor;
100        }
101    
102        protected List<Exchange> createExchangeList() {
103            if (exchangeBufferSize == 0) {
104                return null;
105            } else if (exchangeBufferSize > 0) {
106                // TODO lets create a non blocking fixed size queue
107                return new ArrayList<Exchange>();
108            } else {
109                return new ArrayList<Exchange>();
110            }
111        }
112    
113        protected List<ExceptionEvent> createExceptionsList() {
114            // TODO allow some kinda LRU based fixed size list to be used?
115            return new ArrayList<ExceptionEvent>();
116        }
117    }