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