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