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.List;
020    
021    import org.apache.camel.CamelContext;
022    import org.apache.camel.Exchange;
023    import org.apache.camel.Predicate;
024    import org.apache.camel.Processor;
025    import org.apache.camel.impl.DefaultCamelContext;
026    import org.apache.camel.model.ProcessorType;
027    import org.apache.camel.processor.LoggingLevel;
028    import org.apache.camel.spi.InterceptStrategy;
029    
030    /**
031     * An interceptor strategy for tracing routes
032     *
033     * @version $Revision: 700185 $
034     */
035    public class Tracer implements InterceptStrategy {
036    
037        private TraceFormatter formatter = new TraceFormatter();
038        private boolean enabled = true;
039        private String logName;
040        private LoggingLevel logLevel;
041        private Predicate<Exchange> traceFilter;
042        private boolean traceInterceptors;
043        private boolean traceExceptions = true;
044    
045        /**
046         * A helper method to return the Tracer instance for a given {@link CamelContext} if one is enabled
047         *
048         * @param context the camel context the tracer is connected to
049         * @return the tracer or null if none can be found
050         */
051        public static Tracer getTracer(CamelContext context) {
052            if (context instanceof DefaultCamelContext) {
053                DefaultCamelContext defaultCamelContext = (DefaultCamelContext) context;
054                List<InterceptStrategy> list = defaultCamelContext.getInterceptStrategies();
055                for (InterceptStrategy interceptStrategy : list) {
056                    if (interceptStrategy instanceof Tracer) {
057                        return (Tracer)interceptStrategy;
058                    }
059                }
060            }
061            return null;
062        }
063    
064        public Processor wrapProcessorInInterceptors(ProcessorType processorType, Processor target) throws Exception {
065            // Force the creation of an id, otherwise the id is not available when the trace formatter is
066            // outputting trace information
067            String id = processorType.idOrCreate();
068            return new TraceInterceptor(processorType, target, this);
069        }
070    
071        public TraceFormatter getFormatter() {
072            return formatter;
073        }
074    
075        public void setFormatter(TraceFormatter formatter) {
076            this.formatter = formatter;
077        }
078    
079        public void setEnabled(boolean flag) {
080            enabled = flag;
081        }
082    
083        public boolean isEnabled() {
084            return enabled;
085        }
086    
087        public boolean isTraceInterceptors() {
088            return traceInterceptors;
089        }
090    
091        /**
092         * Sets wether interceptors should be traced or not
093         */
094        public void setTraceInterceptors(boolean traceInterceptors) {
095            this.traceInterceptors = traceInterceptors;
096        }
097    
098        public Predicate getTraceFilter() {
099            return traceFilter;
100        }
101    
102        /**
103         * Sets a predicate to be used as filter when tracing
104         */
105        public void setTraceFilter(Predicate traceFilter) {
106            this.traceFilter = traceFilter;
107        }
108    
109        public LoggingLevel getLogLevel() {
110            return logLevel;
111        }
112    
113        /**
114         * Sets the logging level to ouput tracing. Will default use <tt>INFO</tt> level.
115         */
116        public void setLogLevel(LoggingLevel logLevel) {
117            this.logLevel = logLevel;
118        }
119    
120        public boolean isTraceExceptions() {
121            return traceExceptions;
122        }
123    
124        /**
125         * Sets wether thrown exceptions should be traced
126         */
127        public void setTraceExceptions(boolean traceExceptions) {
128            this.traceExceptions = traceExceptions;
129        }
130    
131        public String getLogName() {
132            return logName;
133        }
134    
135        /**
136         * Sets the logging name to use.
137         * Will default use <tt>org.apache.camel.processor.interceptor.TraceInterceptor<tt>.
138         */
139        public void setLogName(String logName) {
140            this.logName = logName;
141        }
142    }