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.aggregate;
018
019import org.apache.camel.Exchange;
020
021/**
022 * An {@link org.apache.camel.processor.aggregate.AggregationStrategy} which just uses the original exchange
023 * which can be needed when you want to preserve the original Exchange. For example when splitting an {@link Exchange}
024 * and then you may want to keep routing using the original {@link Exchange}.
025 *
026 * @see org.apache.camel.processor.Splitter
027 * @version 
028 */
029public class UseOriginalAggregationStrategy implements AggregationStrategy {
030
031    private Exchange original;
032    private final boolean propagateException;
033
034    public UseOriginalAggregationStrategy() {
035        this(null, true);
036    }
037
038    public UseOriginalAggregationStrategy(boolean propagateException) {
039        this.propagateException = propagateException;
040    }
041
042    public UseOriginalAggregationStrategy(Exchange original, boolean propagateException) {
043        this.original = original;
044        this.propagateException = propagateException;
045    }
046
047    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
048        if (propagateException) {
049            Exception exception = checkException(oldExchange, newExchange);
050            if (exception != null) {
051                if (original != null) {
052                    original.setException(exception);
053                } else {
054                    oldExchange.setException(exception);
055                }
056            }
057        }
058        return original != null ? original : oldExchange;
059    }
060
061    protected Exception checkException(Exchange oldExchange, Exchange newExchange) {
062        if (oldExchange == null) {
063            return newExchange.getException();
064        } else {
065            return (newExchange != null && newExchange.getException() != null)
066                ? newExchange.getException()
067                : oldExchange.getException();
068        }
069    }
070
071    public Exchange getOriginal() {
072        return original;
073    }
074
075    public void setOriginal(Exchange original) {
076        this.original = original;
077    }
078
079    @Override
080    public String toString() {
081        return "UseOriginalAggregationStrategy";
082    }
083}