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
021import static org.apache.camel.util.ExchangeHelper.hasExceptionBeenHandledByErrorHandler;
022
023/**
024 * An {@link AggregationStrategy} which just uses the latest exchange which is useful
025 * for status messages where old status messages have no real value. Another example is things
026 * like market data prices, where old stock prices are not that relevant, only the current price is.
027 *
028 * @version 
029 */
030public class UseLatestAggregationStrategy implements AggregationStrategy {
031
032    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
033        if (newExchange == null) {
034            return oldExchange;
035        }
036        if (oldExchange == null) {
037            return newExchange;
038        }
039
040        Exchange answer = null;
041
042        // propagate exception first
043        propagateException(oldExchange, newExchange);
044        if (newExchange.getException() != null) {
045            answer = newExchange;
046        }
047
048        if (answer == null) {
049            // the propagate failures
050            answer = propagateFailure(oldExchange, newExchange);
051        }
052
053        return answer;
054    }
055    
056    protected void propagateException(Exchange oldExchange, Exchange newExchange) {
057        if (oldExchange == null) {
058            return;
059        }
060
061        // propagate exception from old exchange if there isn't already an exception
062        if (newExchange.getException() == null) {
063            newExchange.setException(oldExchange.getException());
064            newExchange.setProperty(Exchange.FAILURE_ENDPOINT, oldExchange.getProperty(Exchange.FAILURE_ENDPOINT));
065        }
066    }
067
068    protected Exchange propagateFailure(Exchange oldExchange, Exchange newExchange) {
069        if (oldExchange == null) {
070            return newExchange;
071        }
072
073        // propagate exception from old exchange if there isn't already an exception
074        boolean exceptionHandled = hasExceptionBeenHandledByErrorHandler(oldExchange);
075        if (oldExchange.isFailed() || oldExchange.isRollbackOnly() || exceptionHandled) {
076            // propagate failure by using old exchange as the answer
077            return oldExchange;
078        }
079
080        return newExchange;
081    }
082
083    @Override
084    public String toString() {
085        return "UseLatestAggregationStrategy";
086    }
087}