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: 2288 $
036     */
037    public class DefaultAggregationCollection extends AbstractCollection<Exchange> implements AggregationCollection {
038    
039        private static final transient Log LOG = LogFactory.getLog(DefaultAggregationCollection.class);
040        private Expression<Exchange> correlationExpression;
041        private AggregationStrategy aggregationStrategy;
042        private Map<Object, Exchange> map = new LinkedHashMap<Object, Exchange>();
043    
044        public DefaultAggregationCollection() {
045        }
046    
047        public DefaultAggregationCollection(Expression<Exchange> correlationExpression, AggregationStrategy aggregationStrategy) {
048            this.correlationExpression = correlationExpression;
049            this.aggregationStrategy = aggregationStrategy;
050        }
051    
052        protected Map<Object, Exchange> getMap() {
053            return map;
054        }
055    
056        @Override
057        public boolean add(Exchange exchange) {
058            Object correlationKey = correlationExpression.evaluate(exchange);
059            if (LOG.isDebugEnabled()) {
060                LOG.debug("evaluated expression: " + correlationExpression + " as CorrelationKey: " + correlationKey);
061            }
062            Exchange oldExchange = map.get(correlationKey);
063            Exchange newExchange = exchange;
064    
065            if (oldExchange != null) {
066                Integer count = oldExchange.getProperty(Exchange.AGGREGATED_COUNT, Integer.class);
067                if (count == null) {
068                    count = 1;
069                }
070                count++;
071                newExchange = aggregationStrategy.aggregate(oldExchange, newExchange);
072                newExchange.setProperty(Exchange.AGGREGATED_COUNT, count);
073            }
074    
075            // the strategy may just update the old exchange and return it
076            if (newExchange != oldExchange) {
077                if (LOG.isDebugEnabled()) {
078                    LOG.debug("put exchange:" + newExchange + " for key:"  + correlationKey);
079                }
080                if (oldExchange == null) {
081                    newExchange.setProperty(Exchange.AGGREGATED_COUNT, Integer.valueOf(1));
082                }
083                map.put(correlationKey, newExchange);
084            }
085    
086            onAggregation(correlationKey, newExchange);
087    
088            return true;
089        }
090    
091        public Iterator<Exchange> iterator() {
092            return map.values().iterator();
093        }
094    
095        public int size() {
096            return map.size();
097        }
098    
099        @Override
100        public void clear() {
101            map.clear();
102        }
103    
104        public void onAggregation(Object correlationKey, Exchange newExchange) {
105        }
106    
107        public Expression<Exchange> getCorrelationExpression() {
108            return correlationExpression;
109        }
110    
111        public void setCorrelationExpression(Expression<Exchange> correlationExpression) {
112            this.correlationExpression = correlationExpression;
113        }
114    
115        public AggregationStrategy getAggregationStrategy() {
116            return aggregationStrategy;
117        }
118    
119        public void setAggregationStrategy(AggregationStrategy aggregationStrategy) {
120            this.aggregationStrategy = aggregationStrategy;
121        }
122    }