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: 671751 $ 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 TraceFormatter formatter = new TraceFormatter(); 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 if (context instanceof DefaultCamelContext) { 055 DefaultCamelContext defaultCamelContext = (DefaultCamelContext) context; 056 List<InterceptStrategy> list = defaultCamelContext.getInterceptStrategies(); 057 for (InterceptStrategy interceptStrategy : list) { 058 if (interceptStrategy instanceof Debugger) { 059 return (Debugger)interceptStrategy; 060 } 061 } 062 } 063 return null; 064 } 065 066 public DebugInterceptor getInterceptor(String id) { 067 return interceptors.get(id); 068 } 069 070 /** 071 * Returns the list of exchanges sent to the given node in the DSL 072 */ 073 public List<Exchange> getExchanges(String id) { 074 DebugInterceptor interceptor = getInterceptor(id); 075 if (interceptor == null) { 076 return null; 077 } else { 078 return interceptor.getExchanges(); 079 } 080 } 081 082 /** 083 * Returns the breakpoint object for the given node in the DSL 084 */ 085 public Breakpoint getBreakpoint(String id) { 086 DebugInterceptor interceptor = getInterceptor(id); 087 if (interceptor == null) { 088 return null; 089 } else { 090 return interceptor.getBreakpoint(); 091 } 092 } 093 094 095 public Processor wrapProcessorInInterceptors(ProcessorType processorType, Processor target) throws Exception { 096 String id = processorType.idOrCreate(); 097 if (logExchanges) { 098 target = new TraceInterceptor(processorType, target, formatter); 099 } 100 DebugInterceptor interceptor = new DebugInterceptor(processorType, target, createExchangeList(), createExceptionsList()); 101 interceptors.put(id, interceptor); 102 if (LOG.isDebugEnabled()) { 103 LOG.debug("Adding " + id + " interceptor: " + interceptor); 104 } 105 return interceptor; 106 } 107 108 protected List<Exchange> createExchangeList() { 109 if (exchangeBufferSize == 0) { 110 return null; 111 } else if (exchangeBufferSize > 0) { 112 // TODO lets create a non blocking fixed size queue 113 return new ArrayList<Exchange>(); 114 } else { 115 return new ArrayList<Exchange>(); 116 } 117 } 118 119 protected List<ExceptionEvent> createExceptionsList() { 120 // TODO allow some kinda LRU based fixed size list to be used? 121 return new ArrayList<ExceptionEvent>(); 122 } 123 }