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 are used when the option <tt>shareUnitOfWork</tt> is enabled
025 * on EIPs such as multicast, splitter or recipientList.
026 * <p/>
027 * This strategy wraps the actual in use strategy to provide the logic needed for making shareUnitOfWork work.
028 * <p/>
029 * This strategy is <b>not</b> intended for end users to use.
030 */
031public final class ShareUnitOfWorkAggregationStrategy implements AggregationStrategy, DelegateAggregationStrategy {
032
033    private final AggregationStrategy strategy;
034
035    public ShareUnitOfWorkAggregationStrategy(AggregationStrategy strategy) {
036        this.strategy = strategy;
037    }
038
039    public AggregationStrategy getDelegate() {
040        return strategy;
041    }
042
043    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
044        // aggreagate using the actual strategy first
045        Exchange answer = strategy.aggregate(oldExchange, newExchange);
046        // ensure any errors is propagated from the new exchange to the answer
047        propagateFailure(answer, newExchange);
048
049        return answer;
050    }
051
052    protected void propagateFailure(Exchange answer, Exchange newExchange) {
053        // if new exchange failed then propagate all the error related properties to the answer
054        boolean exceptionHandled = hasExceptionBeenHandledByErrorHandler(newExchange);
055        if (newExchange.isFailed() || newExchange.isRollbackOnly() || exceptionHandled) {
056            if (newExchange.getException() != null) {
057                answer.setException(newExchange.getException());
058            }
059            if (newExchange.getProperty(Exchange.EXCEPTION_CAUGHT) != null) {
060                answer.setProperty(Exchange.EXCEPTION_CAUGHT, newExchange.getProperty(Exchange.EXCEPTION_CAUGHT));
061            }
062            if (newExchange.getProperty(Exchange.FAILURE_ENDPOINT) != null) {
063                answer.setProperty(Exchange.FAILURE_ENDPOINT, newExchange.getProperty(Exchange.FAILURE_ENDPOINT));
064            }
065            if (newExchange.getProperty(Exchange.FAILURE_ROUTE_ID) != null) {
066                answer.setProperty(Exchange.FAILURE_ROUTE_ID, newExchange.getProperty(Exchange.FAILURE_ROUTE_ID));
067            }
068            if (newExchange.getProperty(Exchange.ERRORHANDLER_HANDLED) != null) {
069                answer.setProperty(Exchange.ERRORHANDLER_HANDLED, newExchange.getProperty(Exchange.ERRORHANDLER_HANDLED));
070            }
071            if (newExchange.getProperty(Exchange.FAILURE_HANDLED) != null) {
072                answer.setProperty(Exchange.FAILURE_HANDLED, newExchange.getProperty(Exchange.FAILURE_HANDLED));
073            }
074        }
075    }
076
077    @Override
078    public String toString() {
079        return "ShareUnitOfWorkAggregationStrategy";
080    }
081}