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;
018    
019    import java.util.ArrayList;
020    import java.util.Collections;
021    import java.util.Iterator;
022    import java.util.List;
023    import java.util.concurrent.ThreadPoolExecutor;
024    
025    import org.apache.camel.Exchange;
026    import org.apache.camel.Expression;
027    import org.apache.camel.Message;
028    import org.apache.camel.Processor;
029    import org.apache.camel.converter.ObjectConverter;
030    import org.apache.camel.processor.aggregate.AggregationStrategy;
031    import org.apache.camel.util.CollectionHelper;
032    
033    import static org.apache.camel.util.ObjectHelper.notNull;
034    /**
035     * Implements a dynamic <a
036     * href="http://activemq.apache.org/camel/splitter.html">Splitter</a> pattern
037     * where an expression is evaluated to iterate through each of the parts of a
038     * message and then each part is then send to some endpoint.
039     *
040     * @version $Revision: 37863 $
041     */
042    public class Splitter extends MulticastProcessor implements Processor {
043        public static final String SPLIT_SIZE = "org.apache.camel.splitSize";
044        public static final String SPLIT_COUNTER = "org.apache.camel.splitCounter";
045    
046        private final Expression expression;
047    
048        public Splitter(Expression expression, Processor destination, AggregationStrategy aggregationStrategy) {
049            this(expression, destination, aggregationStrategy, false, null);
050        }
051    
052        public Splitter(Expression expression, Processor destination,
053                AggregationStrategy aggregationStrategy,
054                boolean parallelProcessing, ThreadPoolExecutor threadPoolExecutor) {
055            super(Collections.singleton(destination), aggregationStrategy, parallelProcessing, threadPoolExecutor);
056    
057            this.expression = expression;
058            notNull(expression, "expression");
059            notNull(destination, "destination");
060        }
061    
062        @Override
063        public String toString() {
064            return "Splitter[on: " + expression + " to: " + getProcessors().iterator().next() + " aggregate: " + getAggregationStrategy() + "]";
065        }
066    
067        @Override
068        protected List<ProcessorExchangePair> createProcessorExchangePairs(
069            Exchange exchange) {
070            Object value = expression.evaluate(exchange);
071            Integer collectionSize = CollectionHelper.size(value);
072            List<ProcessorExchangePair> result;
073            if (collectionSize != null) {
074                result = new ArrayList<ProcessorExchangePair>(collectionSize);
075            } else {
076                result = new ArrayList<ProcessorExchangePair>();
077            }
078            Iterator<Object> iter = ObjectConverter.iterator(value);
079            while (iter.hasNext()) {
080                Object part = iter.next();
081                Exchange newExchange = exchange.copy();
082                Message in = newExchange.getIn();
083                in.setBody(part);
084                result.add(new ProcessorExchangePair(getProcessors().iterator().next(), newExchange));
085            }
086            return result;
087        }
088    
089        @Override
090        protected void updateNewExchange(Exchange exchange, int i, List<ProcessorExchangePair> allPairs) {
091            exchange.getIn().setHeader(SPLIT_COUNTER, i);
092            exchange.getIn().setHeader(SPLIT_SIZE, allPairs.size());
093        }
094    }