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 java.util.ArrayList; 020 import java.util.List; 021 022 import org.apache.camel.Exchange; 023 import org.apache.camel.impl.DefaultExchange; 024 025 /** 026 * Aggregate all exchanges into a {@link List} of values defined by the {@link #getValue(Exchange)} call. 027 * The combined Exchange will hold all the aggregated exchanges in a {@link java.util.List} 028 * as a exchange property with the key {@link org.apache.camel.Exchange#GROUPED_EXCHANGE}. 029 * <p/> 030 * The method {@link #isStoreAsBodyOnCompletion()} determines if the aggregated {@link List} should 031 * be stored on the {@link org.apache.camel.Message#setBody(Object)} or be kept as a property 032 * on the exchange. 033 * <br/> 034 * The default behavior to store as message body, allows to more easily group together a list of values 035 * and have its result stored as a {@link List} on the completed {@link Exchange}. 036 * 037 * @since 2.11 038 */ 039 public abstract class AbstractListAggregationStrategy<V> implements CompletionAwareAggregationStrategy { 040 041 /** 042 * This method is implemented by the sub-class and is called to retrieve 043 * an instance of the value that will be aggregated and forwarded to the 044 * receiving end point. 045 * <p/> 046 * If <tt>null</tt> is returned, then the value is <b>not</b> added to the {@link List}. 047 * 048 * @param exchange The exchange that is used to retrieve the value from 049 * @return An instance of V that is the associated value of the passed exchange 050 */ 051 public abstract V getValue(Exchange exchange); 052 053 /** 054 * Whether to store the completed aggregated {@link List} as message body, or to keep as property on the exchange. 055 * <p/> 056 * The default behavior is <tt>true</tt> to store as message body. 057 * 058 * @return <tt>true</tt> to store as message body, <tt>false</tt> to keep as property on the exchange. 059 */ 060 public boolean isStoreAsBodyOnCompletion() { 061 return true; 062 } 063 064 @SuppressWarnings("unchecked") 065 public void onCompletion(Exchange exchange) { 066 if (isStoreAsBodyOnCompletion()) { 067 List<V> list = (List<V>) exchange.removeProperty(Exchange.GROUPED_EXCHANGE); 068 if (list != null) { 069 exchange.getIn().setBody(list); 070 } 071 } 072 } 073 074 /** 075 * This method will aggregate the old and new exchange and return the result. 076 * 077 * @param oldExchange The oldest exchange, can be null 078 * @param newExchange The newest exchange, can be null 079 * @return a composite exchange of the old and/or new exchanges 080 */ 081 public Exchange aggregate(Exchange oldExchange, Exchange newExchange) { 082 List<V> list; 083 084 if (oldExchange == null) { 085 list = getList(newExchange); 086 } else { 087 list = getList(oldExchange); 088 } 089 090 if (newExchange != null) { 091 V value = getValue(newExchange); 092 if (value != null) { 093 list.add(value); 094 } 095 } 096 097 return oldExchange != null ? oldExchange : newExchange; 098 } 099 100 @SuppressWarnings("unchecked") 101 private List<V> getList(Exchange exchange) { 102 List<V> list = exchange.getProperty(Exchange.GROUPED_EXCHANGE, List.class); 103 if (list == null) { 104 list = new ArrayList<V>(); 105 exchange.setProperty(Exchange.GROUPED_EXCHANGE, list); 106 } 107 return list; 108 } 109 110 }