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