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: 53289 $
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 Exchange oldExchange = map.get(correlationKey);
060 Exchange newExchange = exchange;
061
062 if (oldExchange != null) {
063 Integer count = oldExchange.getProperty(Exchange.AGGREGATED_COUNT, Integer.class);
064 if (count == null) {
065 count = 1;
066 }
067 count++;
068 newExchange = aggregationStrategy.aggregate(oldExchange, newExchange);
069 newExchange.setProperty(Exchange.AGGREGATED_COUNT, count);
070 }
071
072 // the strategy may just update the old exchange and return it
073 if (newExchange != oldExchange) {
074 if (LOG.isDebugEnabled()) {
075 LOG.debug("put exchange:" + newExchange + " for key:" + correlationKey);
076 }
077 if (oldExchange == null) {
078 newExchange.setProperty(Exchange.AGGREGATED_COUNT, Integer.valueOf(1));
079 }
080 map.put(correlationKey, newExchange);
081 }
082
083 onAggregation(correlationKey, newExchange);
084
085 return true;
086 }
087
088 public Iterator<Exchange> iterator() {
089 return map.values().iterator();
090 }
091
092 public int size() {
093 return map.size();
094 }
095
096 @Override
097 public void clear() {
098 map.clear();
099 }
100
101 public void onAggregation(Object correlationKey, Exchange newExchange) {
102 }
103
104 public Expression<Exchange> getCorrelationExpression() {
105 return correlationExpression;
106 }
107
108 public void setCorrelationExpression(Expression<Exchange> correlationExpression) {
109 this.correlationExpression = correlationExpression;
110 }
111
112 public AggregationStrategy getAggregationStrategy() {
113 return aggregationStrategy;
114 }
115
116 public void setAggregationStrategy(AggregationStrategy aggregationStrategy) {
117 this.aggregationStrategy = aggregationStrategy;
118 }
119 }