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 org.apache.camel.AsyncCallback;
020 import org.apache.camel.CamelContext;
021 import org.apache.camel.Exchange;
022 import org.apache.camel.Processor;
023 import org.apache.camel.model.ProcessorDefinition;
024 import org.apache.camel.processor.DelegateAsyncProcessor;
025 import org.apache.camel.spi.Debugger;
026 import org.apache.camel.spi.InterceptStrategy;
027 import 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 */
034 public 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 debugger.beforeProcess(exchange, target, definition);
048 final StopWatch watch = new StopWatch();
049
050 return super.process(exchange, new AsyncCallback() {
051 public void done(boolean doneSync) {
052 long diff = watch.stop();
053 debugger.afterProcess(exchange, processor, definition, diff);
054
055 // must notify original callback
056 callback.done(doneSync);
057 }
058 });
059 }
060
061 @Override
062 public String toString() {
063 return "Debug[" + target + "]";
064 }
065 };
066 }
067 }