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 */ 017package org.apache.camel.processor.interceptor; 018 019import org.apache.camel.AsyncCallback; 020import org.apache.camel.CamelContext; 021import org.apache.camel.Exchange; 022import org.apache.camel.Processor; 023import org.apache.camel.model.ProcessorDefinition; 024import org.apache.camel.processor.DelegateAsyncProcessor; 025import org.apache.camel.spi.Debugger; 026import org.apache.camel.spi.InterceptStrategy; 027import org.apache.camel.util.StopWatch; 028 029/** 030 * A debug interceptor to notify {@link Debugger} with {@link Exchange}s being processed. 031 * 032 * @version 033 */ 034public class Debug implements InterceptStrategy { 035 036 private final Debugger debugger; 037 038 public Debug(Debugger debugger) { 039 this.debugger = debugger; 040 } 041 042 public Processor wrapProcessorInInterceptors(final CamelContext context, final ProcessorDefinition<?> definition, 043 final Processor target, final Processor nextTarget) throws Exception { 044 return new DelegateAsyncProcessor(target) { 045 @Override 046 public boolean process(final Exchange exchange, final AsyncCallback callback) { 047 try { 048 debugger.beforeProcess(exchange, target, definition); 049 } catch (Throwable e) { 050 exchange.setException(e); 051 callback.done(true); 052 return true; 053 } 054 055 final StopWatch watch = new StopWatch(); 056 057 return processor.process(exchange, new AsyncCallback() { 058 public void done(boolean doneSync) { 059 long diff = watch.taken(); 060 try { 061 debugger.afterProcess(exchange, processor, definition, diff); 062 } finally { 063 // must notify original callback 064 callback.done(doneSync); 065 } 066 } 067 }); 068 } 069 070 @Override 071 public String toString() { 072 return "Debug[" + target + "]"; 073 } 074 }; 075 } 076}