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.Iterator;
021    import java.util.LinkedHashMap;
022    import java.util.Map;
023    
024    import org.apache.camel.Exchange;
025    import org.apache.camel.Expression;
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    
029    /**
030     * A {@link java.util.Collection} which aggregates exchanges together using a correlation
031     * expression so that there is only a single message exchange sent for a single
032     * correlation key.
033     *
034     * @version $Revision: 752464 $
035     */
036    public class DefaultAggregationCollection extends AbstractCollection<Exchange> implements AggregationCollection {
037    
038        private static final transient Log LOG = LogFactory.getLog(DefaultAggregationCollection.class);
039        private Expression correlationExpression;
040        private AggregationStrategy aggregationStrategy;
041        private final Map<Object, Exchange> aggregated = new LinkedHashMap<Object, Exchange>();
042    
043        public DefaultAggregationCollection() {
044        }
045    
046        public DefaultAggregationCollection(Expression correlationExpression, AggregationStrategy aggregationStrategy) {
047            this.correlationExpression = correlationExpression;
048            this.aggregationStrategy = aggregationStrategy;
049        }
050    
051        protected Map<Object, Exchange> getAggregated() {
052            return aggregated;
053        }
054    
055        @Override
056        public boolean add(Exchange exchange) {
057            Object correlationKey = correlationExpression.evaluate(exchange);
058            if (LOG.isDebugEnabled()) {
059                LOG.debug("Evaluated expression: " + correlationExpression + " as CorrelationKey: " + correlationKey);
060            }
061            Exchange oldExchange = aggregated.get(correlationKey);
062            Exchange newExchange = exchange;
063    
064            if (oldExchange != null) {
065                Integer count = oldExchange.getProperty(Exchange.AGGREGATED_SIZE, Integer.class);
066                if (count == null) {
067                    count = 1;
068                }
069                count++;
070                newExchange = aggregationStrategy.aggregate(oldExchange, newExchange);
071                newExchange.setProperty(Exchange.AGGREGATED_SIZE, count);
072            }
073    
074            // the strategy may just update the old exchange and return it
075            if (!newExchange.equals(oldExchange)) {
076                if (LOG.isDebugEnabled()) {
077                    LOG.debug("Put exchange:" + newExchange + " with coorelation key:"  + correlationKey);
078                }
079                if (oldExchange == null) {
080                    newExchange.setProperty(Exchange.AGGREGATED_SIZE, Integer.valueOf(1));
081                }
082                aggregated.put(correlationKey, newExchange);
083            }
084    
085            onAggregation(correlationKey, newExchange);
086    
087            return true;
088        }
089    
090        public Iterator<Exchange> iterator() {
091            return aggregated.values().iterator();
092        }
093    
094        public int size() {
095            return aggregated.size();
096        }
097    
098        @Override
099        public void clear() {
100            aggregated.clear();
101        }
102    
103        public void onAggregation(Object correlationKey, Exchange newExchange) {
104        }
105    
106        public Expression getCorrelationExpression() {
107            return correlationExpression;
108        }
109    
110        public void setCorrelationExpression(Expression correlationExpression) {
111            this.correlationExpression = correlationExpression;
112        }
113    
114        public AggregationStrategy getAggregationStrategy() {
115            return aggregationStrategy;
116        }
117    
118        public void setAggregationStrategy(AggregationStrategy aggregationStrategy) {
119            this.aggregationStrategy = aggregationStrategy;
120        }
121    }