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