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.Exchange; 022 import org.apache.camel.Message; 023 import org.apache.camel.Predicate; 024 import org.apache.camel.Processor; 025 import org.apache.camel.model.ProcessorType; 026 import org.apache.camel.processor.DelegateProcessor; 027 028 /** 029 * An interceptor for debugging and tracing routes 030 * 031 * @version $Revision: 664245 $ 032 */ 033 public class DebugInterceptor extends DelegateProcessor { 034 private final ProcessorType node; 035 private final List<Exchange> exchanges; 036 private final List<ExceptionEvent> exceptions; 037 private Predicate traceFilter; 038 private Breakpoint breakpoint = new Breakpoint(); 039 private boolean traceExceptions = true; 040 041 public DebugInterceptor(ProcessorType node, Processor target, List<Exchange> exchanges, List<ExceptionEvent> exceptions) { 042 super(target); 043 this.node = node; 044 this.exchanges = exchanges; 045 this.exceptions = exceptions; 046 } 047 048 @Override 049 public String toString() { 050 return "DebugInterceptor[" + node + "]"; 051 } 052 053 public void process(Exchange exchange) throws Exception { 054 checkForBreakpoint(exchange); 055 addTraceExchange(exchange); 056 try { 057 super.proceed(exchange); 058 } catch (Exception e) { 059 onException(exchange, e); 060 throw e; 061 } catch (Error e) { 062 onException(exchange, e); 063 throw e; 064 } 065 } 066 067 public ProcessorType getNode() { 068 return node; 069 } 070 071 public List<Exchange> getExchanges() { 072 return exchanges; 073 } 074 075 public List<ExceptionEvent> getExceptions() { 076 return exceptions; 077 } 078 079 public Breakpoint getBreakpoint() { 080 return breakpoint; 081 } 082 083 public Predicate getTraceFilter() { 084 return traceFilter; 085 } 086 087 public void setTraceFilter(Predicate traceFilter) { 088 this.traceFilter = traceFilter; 089 } 090 091 public boolean isTraceExceptions() { 092 return traceExceptions; 093 } 094 095 public void setTraceExceptions(boolean traceExceptions) { 096 this.traceExceptions = traceExceptions; 097 } 098 099 /** 100 * Stategy method to wait for a breakpoint if one is set 101 */ 102 protected void checkForBreakpoint(Exchange exchange) { 103 breakpoint.waitForBreakpoint(exchange); 104 } 105 106 /** 107 * Fired when an exception is thrown when processing the underlying processor 108 */ 109 protected void onException(Exchange exchange, Throwable e) { 110 if (shouldTraceExceptionEvents(exchange, e)) { 111 exceptions.add(new ExceptionEvent(this, exchange, e)); 112 } 113 114 } 115 116 private boolean shouldTraceExceptionEvents(Exchange exchange, Throwable e) { 117 return isTraceExceptions(); 118 } 119 120 /** 121 * Strategy method to store the exchange in a trace log if it is enabled 122 */ 123 protected void addTraceExchange(Exchange exchange) { 124 if (shouldTraceExchange(exchange)) { 125 exchanges.add(copyExchange(exchange)); 126 } 127 } 128 129 protected Exchange copyExchange(Exchange previousExchange) { 130 Exchange answer = previousExchange.newInstance(); 131 answer.getProperties().putAll(previousExchange.getProperties()); 132 answer.getIn().copyFrom(previousExchange.getIn()); 133 134 // only copy the out if its defined 135 Message previousOut = previousExchange.getOut(false); 136 if (previousOut != null) { 137 answer.getOut().copyFrom(previousOut); 138 } 139 return answer; 140 } 141 142 /** 143 * Returns true if the given exchange should be logged in the trace list 144 */ 145 protected boolean shouldTraceExchange(Exchange exchange) { 146 return traceFilter == null || traceFilter.matches(exchange); 147 } 148 }