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.Iterator;
021    import java.util.List;
022    import java.util.concurrent.ExecutorService;
023    
024    import org.apache.camel.Endpoint;
025    import org.apache.camel.Exchange;
026    import org.apache.camel.Expression;
027    import org.apache.camel.Processor;
028    import org.apache.camel.Producer;
029    import org.apache.camel.impl.ProducerCache;
030    import org.apache.camel.impl.ServiceSupport;
031    import org.apache.camel.processor.aggregate.AggregationStrategy;
032    import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
033    import org.apache.camel.util.ExchangeHelper;
034    import org.apache.camel.util.ObjectHelper;
035    
036    /**
037     * Implements a dynamic <a
038     * href="http://camel.apache.org/recipient-list.html">Recipient List</a>
039     * pattern where the list of actual endpoints to send a message exchange to are
040     * dependent on some dynamic expression.
041     *
042     * @version $Revision: 887262 $
043     */
044    public class RecipientList extends ServiceSupport implements Processor {
045        private ProducerCache producerCache;
046        private Expression expression;
047        private final String delimiter;
048        private boolean parallelProcessing;
049        private boolean stopOnException;
050        private ExecutorService executorService;
051        private AggregationStrategy aggregationStrategy = new UseLatestAggregationStrategy();
052    
053        public RecipientList() {
054            // use comma by default as delimiter
055            this.delimiter = ",";
056        }
057    
058        public RecipientList(String delimiter) {
059            this.delimiter = delimiter;
060        }
061    
062        public RecipientList(Expression expression) {
063            // use comma by default as delimiter
064            this(expression, ",");
065        }
066    
067        public RecipientList(Expression expression, String delimiter) {
068            ObjectHelper.notNull(expression, "expression");
069            ObjectHelper.notEmpty(delimiter, "delimiter");
070            this.expression = expression;
071            this.delimiter = delimiter;
072        }
073    
074        @Override
075        public String toString() {
076            return "RecipientList[" + (expression != null ? expression : "") + "]";
077        }
078    
079        public void process(Exchange exchange) throws Exception {
080            Object receipientList = expression.evaluate(exchange, Object.class);
081            sendToRecipientList(exchange, receipientList);
082        }
083    
084        /**
085         * Sends the given exchange to the recipient list
086         */
087        public void sendToRecipientList(Exchange exchange, Object receipientList) throws Exception {
088            Iterator<Object> iter = ObjectHelper.createIterator(receipientList, delimiter);
089    
090            List<Processor> processors = new ArrayList<Processor>();
091            while (iter.hasNext()) {
092                Object recipient = iter.next();
093                Endpoint endpoint = resolveEndpoint(exchange, recipient);
094                Producer producer = getProducerCache(exchange).getProducer(endpoint);
095                processors.add(producer);
096            }
097    
098            MulticastProcessor mp = new MulticastProcessor(processors, getAggregationStrategy(), isParallelProcessing(),
099                                                           getExecutorService(), false, isStopOnException());
100    
101            // now let the multicast process the exchange
102            mp.process(exchange);
103        }
104    
105        protected ProducerCache getProducerCache(Exchange exchange) throws Exception {
106            // setup producer cache as we need to use the pluggable service pool defined on camel context
107            if (producerCache == null) {
108                this.producerCache = new ProducerCache(exchange.getContext());
109                this.producerCache.start();
110            }
111            return this.producerCache;
112        }
113    
114        protected Endpoint resolveEndpoint(Exchange exchange, Object recipient) {
115            // trim strings as end users might have added spaces between separators
116            if (recipient instanceof String) {
117                recipient = ((String)recipient).trim();
118            }
119            return ExchangeHelper.resolveEndpoint(exchange, recipient);
120        }
121    
122        protected void doStart() throws Exception {
123            if (producerCache != null) {
124                producerCache.start();
125            }
126        }
127    
128        protected void doStop() throws Exception {
129            if (producerCache != null) {
130                producerCache.stop();
131            }
132        }
133    
134        public boolean isParallelProcessing() {
135            return parallelProcessing;
136        }
137    
138        public void setParallelProcessing(boolean parallelProcessing) {
139            this.parallelProcessing = parallelProcessing;
140        }
141    
142        public boolean isStopOnException() {
143            return stopOnException;
144        }
145    
146        public void setStopOnException(boolean stopOnException) {
147            this.stopOnException = stopOnException;
148        }
149    
150        public ExecutorService getExecutorService() {
151            return executorService;
152        }
153    
154        public void setExecutorService(ExecutorService executorService) {
155            this.executorService = executorService;
156        }
157    
158        public AggregationStrategy getAggregationStrategy() {
159            return aggregationStrategy;
160        }
161    
162        public void setAggregationStrategy(AggregationStrategy aggregationStrategy) {
163            this.aggregationStrategy = aggregationStrategy;
164        }
165    }