/**
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.processor.interceptor;

import java.util.List;

import org.apache.camel.model.ProcessorType;
import org.apache.camel.Processor;
import org.apache.camel.Exchange;
import org.apache.camel.Predicate;

/**
 * An interceptor for debugging and tracing routes
 * 
 * @version $Revision: 1.1 $
 */
public class DebugInterceptor implements Processor {
    private final ProcessorType node;
    private final Processor target;
    private final List<Exchange> exchanges;
    private Predicate traceFilter;
    private Breakpoint breakpoint = new Breakpoint();

    public DebugInterceptor(ProcessorType node, Processor target, List<Exchange> exchanges) {
        this.node = node;
        this.target = target;
        this.exchanges = exchanges;
    }

    @Override
    public String toString() {
        return "DebugInterceptor[" + node + "]";
    }

    public void process(Exchange exchange) throws Exception {
        checkForBreakpoint(exchange);        
        addTraceExchange(exchange);
        target.process(exchange);
    }

    public ProcessorType getNode() {
        return node;
    }

    public Processor getTarget() {
        return target;
    }

    public List<Exchange> getExchanges() {
        return exchanges;
    }

    public Breakpoint getBreakpoint() {
        return breakpoint;
    }

    public Predicate getTraceFilter() {
        return traceFilter;
    }

    public void setTraceFilter(Predicate traceFilter) {
        this.traceFilter = traceFilter;
    }

    /**
     * Stategy method to wait for a breakpoint if one is set
     */
    protected void checkForBreakpoint(Exchange exchange) {
        breakpoint.waitForBreakpoint(exchange);
    }

    /**
     * Strategy method to store the exchange in a trace log if it is enabled
     */
    protected void addTraceExchange(Exchange exchange) {
        if (shouldTraceExchange(exchange)) {
            exchanges.add(exchange);
        }
    }

    /**
     * Returns true if the given exchange should be logged in the trace list
     */
    protected boolean shouldTraceExchange(Exchange exchange) {
        return traceFilter == null || traceFilter.matches(exchange) ;
    }
}

@@ -1016,16 +1016,49 @@
 edicate;
+%0Aimport org.apache.camel.Message;
 %0A%0A/**%0A *
@@ -2753,16 +2753,29 @@
 ges.add(
+copyExchange(
 exchange
@@ -2767,32 +2767,33 @@
 change(exchange)
+)
 ;%0A        %7D%0A    
@@ -2787,32 +2787,520 @@
        %7D%0A    %7D%0A%0A
+    protected Exchange copyExchange(Exchange previousExchange) %7B%0A        Exchange answer = previousExchange.newInstance();%0A        answer.getProperties().putAll(previousExchange.getProperties());%0A        answer.getIn().copyFrom(previousExchange.getIn());%0A%0A        // only copy the out if its defined%0A        Message previousOut = previousExchange.getOut(false);%0A        if (previousOut != null) %7B%0A            answer.getOut().copyFrom(previousOut);%0A        %7D%0A        return answer;%0A    %7D%0A%0A
     /**%0A     * R

copy the exchanges stored in the watch lists