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
020
021 import java.util.ArrayList;
022 import java.util.Iterator;
023 import java.util.List;
024
025 import org.apache.camel.Endpoint;
026 import org.apache.camel.Exchange;
027 import org.apache.camel.Expression;
028 import org.apache.camel.Processor;
029 import org.apache.camel.Producer;
030 import org.apache.camel.converter.ObjectConverter;
031 import org.apache.camel.impl.ProducerCache;
032 import org.apache.camel.impl.ServiceSupport;
033 import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
034 import org.apache.camel.util.ExchangeHelper;
035 import static org.apache.camel.util.ObjectHelper.notNull;
036
037 /**
038 * Implements a dynamic <a
039 * href="http://activemq.apache.org/camel/recipient-list.html">Recipient List</a>
040 * pattern where the list of actual endpoints to send a message exchange to are
041 * dependent on some dynamic expression.
042 *
043 * @version $Revision: 659638 $
044 */
045 public class RecipientList extends ServiceSupport implements Processor {
046 private final Expression<Exchange> expression;
047 private ProducerCache<Exchange> producerCache = new ProducerCache<Exchange>();
048
049 public RecipientList(Expression<Exchange> expression) {
050 notNull(expression, "expression");
051 this.expression = expression;
052 }
053
054 @Override
055 public String toString() {
056 return "RecipientList[" + expression + "]";
057 }
058
059 public void process(Exchange exchange) throws Exception {
060 Object receipientList = expression.evaluate(exchange);
061 Iterator iter = ObjectConverter.iterator(receipientList);
062 List<Processor> processors = new ArrayList<Processor>();
063 while (iter.hasNext()) {
064 Object recipient = iter.next();
065 Endpoint<Exchange> endpoint = resolveEndpoint(exchange, recipient);
066 Producer<Exchange> producer = producerCache.getProducer(endpoint);
067 processors.add(producer);
068 }
069 MulticastProcessor mp = new MulticastProcessor(processors, new UseLatestAggregationStrategy());
070 mp.process(exchange);
071 }
072
073 protected Endpoint<Exchange> resolveEndpoint(Exchange exchange, Object recipient) {
074 return ExchangeHelper.resolveEndpoint(exchange, recipient);
075 }
076
077 protected void doStop() throws Exception {
078 producerCache.stop();
079 }
080
081 protected void doStart() throws Exception {
082 }
083 }