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     * @deprecated will be removed in Camel 2.0
037     * @version $Revision: 14069 $
038     */
039    public class Debugger implements InterceptStrategy {
040        private static final transient Log LOG = LogFactory.getLog(Debugger.class);
041    
042        private int exchangeBufferSize = -1;
043        private Map<String, DebugInterceptor> interceptors = new HashMap<String, DebugInterceptor>();
044        private boolean logExchanges = true;
045        private boolean enabled = true;
046        private Tracer tracer = new Tracer();
047    
048    
049        /**
050         * A helper method to return the debugger instance for a given {@link CamelContext} if one is enabled
051         *
052         * @param context the camel context the debugger is connected to
053         * @return the debugger or null if none can be found
054         */
055        public static Debugger getDebugger(CamelContext context) {
056            if (context instanceof DefaultCamelContext) {
057                DefaultCamelContext defaultCamelContext = (DefaultCamelContext) context;
058                List<InterceptStrategy> list = defaultCamelContext.getInterceptStrategies();
059                for (InterceptStrategy interceptStrategy : list) {
060                    if (interceptStrategy instanceof Debugger) {
061                        return (Debugger)interceptStrategy;
062                    }
063                }
064            }
065            return null;
066        }
067    
068    
069        public DebugInterceptor getInterceptor(String id) {
070            return interceptors.get(id);
071        }
072    
073    
074        /**
075         * Returns the list of exchanges sent to the given node in the DSL
076         */
077        public List<Exchange> getExchanges(String id) {
078            DebugInterceptor interceptor = getInterceptor(id);
079            if (interceptor == null) {
080                return null;
081            } else {
082                return interceptor.getExchanges();
083            }
084        }
085    
086        public void setEnable(boolean flag) {
087            enabled = flag;
088            tracer.setEnabled(flag);
089            for (DebugInterceptor interceptor : interceptors.values()) {
090                interceptor.setEnabled(flag);
091            }
092        }
093    
094        public boolean isEnabled() {
095            return enabled;
096        }
097    
098        /**
099         * Returns the breakpoint object for the given node in the DSL
100         */
101        public Breakpoint getBreakpoint(String id) {
102            DebugInterceptor interceptor = getInterceptor(id);
103            if (interceptor == null) {
104                return null;
105            } else {
106                return interceptor.getBreakpoint();
107            }
108        }
109    
110        public TraceFormatter getTraceFormatter() {
111            return tracer.getFormatter();
112        }
113    
114        public void setTraceFormatter(TraceFormatter formatter) {
115            tracer.setFormatter(formatter);
116        }
117    
118        public void setLogExchanges(boolean flag) {
119            logExchanges = flag;
120        }
121    
122    
123        public Processor wrapProcessorInInterceptors(ProcessorType processorType, Processor target) throws Exception {
124            String id = processorType.idOrCreate();
125            if (logExchanges) {
126                TraceInterceptor  traceInterceptor = new TraceInterceptor(processorType, target, tracer);
127                target = traceInterceptor;
128            }
129            DebugInterceptor interceptor = new DebugInterceptor(processorType, target, createExchangeList(), createExceptionsList());
130            interceptors.put(id, interceptor);
131            if (LOG.isDebugEnabled()) {
132                LOG.debug("Adding " + id + " interceptor: " + interceptor);
133            }
134            return interceptor;
135        }
136    
137        protected List<Exchange> createExchangeList() {
138            if (exchangeBufferSize == 0) {
139                return null;
140            } else if (exchangeBufferSize > 0) {
141                // TODO lets create a non blocking fixed size queue
142                return new ArrayList<Exchange>();
143            } else {
144                return new ArrayList<Exchange>();
145            }
146        }
147    
148        protected List<ExceptionEvent> createExceptionsList() {
149            // TODO allow some kinda LRU based fixed size list to be used?
150            return new ArrayList<ExceptionEvent>();
151        }
152    
153    
154    }