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 */
017 package org.apache.camel.processor.aggregate;
018
019 import org.apache.camel.Exchange;
020 import org.apache.camel.util.ObjectHelper;
021
022 /**
023 * An {@link org.apache.camel.processor.aggregate.AggregationStrategy} which just uses the original exchange
024 * which can be needed when you want to preserve the original Exchange. For example when splitting an {@link Exchange}
025 * and then you may want to keep routing using the original {@link Exchange}.
026 *
027 * @see org.apache.camel.processor.Splitter
028 * @version
029 */
030 public class UseOriginalAggregationStrategy implements AggregationStrategy {
031
032 private final Exchange original;
033 private final boolean propagateException;
034
035 public UseOriginalAggregationStrategy(Exchange original, boolean propagateException) {
036 ObjectHelper.notNull(original, "Original Exchange");
037 this.original = original;
038 this.propagateException = propagateException;
039 }
040
041 public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
042 if (propagateException) {
043 Exception exception = checkException(oldExchange, newExchange);
044 if (exception != null) {
045 original.setException(exception);
046 }
047 }
048 return original;
049 }
050
051 protected Exception checkException(Exchange oldExchange, Exchange newExchange) {
052 if (oldExchange == null) {
053 return newExchange.getException();
054 } else {
055 return (newExchange != null && newExchange.getException() != null)
056 ? newExchange.getException()
057 : oldExchange.getException();
058 }
059 }
060
061 @Override
062 public String toString() {
063 return "UseOriginalAggregationStrategy";
064 }
065 }