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 final Exchange original;
032    private final boolean propagateException;
033
034    public UseOriginalAggregationStrategy() {
035        this(null, true);
036    }
037
038    public UseOriginalAggregationStrategy(boolean propagateException) {
039        this(null, propagateException);
040    }
041
042    public UseOriginalAggregationStrategy(Exchange original, boolean propagateException) {
043        this.original = original;
044        this.propagateException = propagateException;
045    }
046
047    /**
048     * Creates a new instance as a clone of this strategy with the new given original.
049     */
050    public UseOriginalAggregationStrategy newInstance(Exchange original) {
051        return new UseOriginalAggregationStrategy(original, propagateException);
052    }
053
054    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
055        if (propagateException) {
056            Exception exception = checkException(oldExchange, newExchange);
057            if (exception != null) {
058                if (original != null) {
059                    original.setException(exception);
060                } else {
061                    oldExchange.setException(exception);
062                }
063            }
064        }
065        return original != null ? original : oldExchange;
066    }
067
068    protected Exception checkException(Exchange oldExchange, Exchange newExchange) {
069        if (oldExchange == null) {
070            return newExchange.getException();
071        } else {
072            return (newExchange != null && newExchange.getException() != null)
073                ? newExchange.getException()
074                : oldExchange.getException();
075        }
076    }
077
078    public Exchange getOriginal() {
079        return original;
080    }
081
082    @Deprecated
083    public void setOriginal(Exchange original) {
084        throw new UnsupportedOperationException("This method is deprecated");
085    }
086
087    @Override
088    public String toString() {
089        return "UseOriginalAggregationStrategy";
090    }
091}