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    
023    import org.apache.camel.Endpoint;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.Expression;
026    import org.apache.camel.Processor;
027    import org.apache.camel.Producer;
028    import org.apache.camel.impl.ProducerCache;
029    import org.apache.camel.impl.ServiceSupport;
030    import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
031    import org.apache.camel.util.ExchangeHelper;
032    import org.apache.camel.util.ObjectHelper;
033    import static org.apache.camel.util.ObjectHelper.notNull;
034    
035    /**
036     * Implements a dynamic <a
037     * href="http://camel.apache.org/recipient-list.html">Recipient List</a>
038     * pattern where the list of actual endpoints to send a message exchange to are
039     * dependent on some dynamic expression.
040     *
041     * @version $Revision: 779038 $
042     */
043    public class RecipientList extends ServiceSupport implements Processor {
044        private ProducerCache producerCache;
045        private Expression expression;
046    
047        public RecipientList() {
048        }
049    
050        public RecipientList(Expression expression) {
051            notNull(expression, "expression");
052            this.expression = expression;
053        }
054    
055        @Override
056        public String toString() {
057            return "RecipientList[" + (expression != null ? expression : "") + "]";
058        }
059    
060        public void process(Exchange exchange) throws Exception {
061            Object receipientList = expression.evaluate(exchange, Object.class);
062            sendToRecipientList(exchange, receipientList);
063        }
064    
065        /**
066         * Sends the given exchange to the recipient list
067         */
068        public void sendToRecipientList(Exchange exchange, Object receipientList) throws Exception {
069            Iterator iter = ObjectHelper.createIterator(receipientList);
070            List<Processor> processors = new ArrayList<Processor>();
071            while (iter.hasNext()) {
072                Object recipient = iter.next();
073                Endpoint endpoint = resolveEndpoint(exchange, recipient);
074                Producer producer = getProducerCache(exchange).getProducer(endpoint);
075                processors.add(producer);
076            }
077            MulticastProcessor mp = new MulticastProcessor(processors, new UseLatestAggregationStrategy());
078            mp.process(exchange);
079        }
080    
081        protected ProducerCache getProducerCache(Exchange exchange) throws Exception {
082            // setup producer cache as we need to use the pluggable service pool defined on camel context
083            if (producerCache == null) {
084                this.producerCache = new ProducerCache(exchange.getContext().getProducerServicePool());
085                this.producerCache.start();
086            }
087            return this.producerCache;
088        }
089    
090        protected Endpoint resolveEndpoint(Exchange exchange, Object recipient) {
091            // trim strings as end users might have added spaces between separators
092            if (recipient instanceof String) {
093                recipient = ((String)recipient).trim();
094            }
095            return ExchangeHelper.resolveEndpoint(exchange, recipient);
096        }
097    
098        protected void doStart() throws Exception {
099            if (producerCache != null) {
100                producerCache.start();
101            }
102        }
103    
104        protected void doStop() throws Exception {
105            if (producerCache != null) {
106                producerCache.stop();
107            }
108        }
109    
110    }