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.AbstractCollection;
020    import java.util.Collection;
021    import java.util.Iterator;
022    import java.util.LinkedHashMap;
023    import java.util.Map;
024    
025    import org.apache.camel.Exchange;
026    import org.apache.camel.Expression;
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    
030    /**
031     * A {@link Collection} which aggregates exchanges together using a correlation
032     * expression so that there is only a single message exchange sent for a single
033     * correlation key.
034     *
035     * @version $Revision: 36321 $
036     */
037    public class AggregationCollection extends AbstractCollection<Exchange> {
038        private static final transient Log LOG = LogFactory.getLog(AggregationCollection.class);
039        private final Expression<Exchange> correlationExpression;
040        private final AggregationStrategy aggregationStrategy;
041        private Map<Object, Exchange> map = new LinkedHashMap<Object, Exchange>();
042    
043        public AggregationCollection(Expression<Exchange> correlationExpression,
044                                     AggregationStrategy aggregationStrategy) {
045            this.correlationExpression = correlationExpression;
046            this.aggregationStrategy = aggregationStrategy;
047        }
048    
049        protected Map<Object, Exchange> getMap() {
050            return map;
051        }
052    
053        @Override
054        public boolean add(Exchange exchange) {
055            Object correlationKey = correlationExpression.evaluate(exchange);
056            Exchange oldExchange = map.get(correlationKey);
057            Exchange newExchange = exchange;
058            if (oldExchange != null) {
059                newExchange = aggregationStrategy.aggregate(oldExchange, newExchange);
060            }
061    
062            // the strategy may just update the old exchange and return it
063            if (newExchange != oldExchange) {
064                LOG.debug("put exchange:" + newExchange + " for key:"  + correlationKey);
065                map.put(correlationKey, newExchange);
066            }
067            onAggregation(correlationKey, newExchange);
068            return true;
069        }
070    
071        public Iterator<Exchange> iterator() {
072            return map.values().iterator();
073        }
074    
075        public int size() {
076            return map.size();
077        }
078    
079        @Override
080        public void clear() {
081            map.clear();
082        }
083    
084        /**
085         * A strategy method allowing derived classes such as {@link PredicateAggregationCollection}
086         * to check to see if the aggregation has completed
087         */
088        protected void onAggregation(Object correlationKey, Exchange newExchange) {
089        }
090    }