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.model;
018    
019    import java.util.Collection;
020    
021    import javax.xml.bind.annotation.XmlAccessType;
022    import javax.xml.bind.annotation.XmlAccessorType;
023    import javax.xml.bind.annotation.XmlRootElement;
024    import javax.xml.bind.annotation.XmlTransient;
025    
026    import org.apache.camel.Endpoint;
027    import org.apache.camel.Exchange;
028    import org.apache.camel.Expression;
029    import org.apache.camel.Processor;
030    import org.apache.camel.Route;
031    import org.apache.camel.impl.RouteContext;
032    import org.apache.camel.model.language.ExpressionType;
033    import org.apache.camel.processor.Aggregator;
034    import org.apache.camel.processor.aggregate.AggregationStrategy;
035    import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
036    
037    /**
038     * @version $Revision: 1.1 $
039     */
040    @XmlRootElement(name = "aggregator")
041    @XmlAccessorType(XmlAccessType.FIELD)
042    public class AggregatorType extends ExpressionNode {
043        @XmlTransient
044        private AggregationStrategy aggregationStrategy = new UseLatestAggregationStrategy();
045    
046        public AggregatorType() {
047        }
048    
049        public AggregatorType(Expression correlationExpression) {
050            super(correlationExpression);
051        }
052    
053        public AggregatorType(ExpressionType correlationExpression) {
054            super(correlationExpression);
055        }
056    
057        public AggregatorType(Expression correlationExpression, AggregationStrategy aggregationStrategy) {
058            super(correlationExpression);
059            this.aggregationStrategy = aggregationStrategy;
060        }
061    
062        @Override
063        public String toString() {
064            return "Aggregator[ " + getExpression() + " -> " + getOutputs() + "]";
065        }
066    
067        @Override
068        public void addRoutes(RouteContext routeContext, Collection<Route> routes) throws Exception {
069            Endpoint from = routeContext.getEndpoint();
070            final Processor processor = routeContext.createProcessor(this);
071            final Aggregator service = new Aggregator(from, processor, getExpression()
072                .createExpression(routeContext), aggregationStrategy);
073    
074            Route route = new Route<Exchange>(from, service) {
075                @Override
076                public String toString() {
077                    return "AggregatorRoute[" + getEndpoint() + " -> " + processor + "]";
078                }
079            };
080    
081            routes.add(route);
082        }
083    
084        public AggregationStrategy getAggregationStrategy() {
085            return aggregationStrategy;
086        }
087    
088        public void setAggregationStrategy(AggregationStrategy aggregationStrategy) {
089            this.aggregationStrategy = aggregationStrategy;
090        }
091    }