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: 749201 $
042 */
043 public class RecipientList extends ServiceSupport implements Processor {
044 private Expression expression;
045 private ProducerCache producerCache = new ProducerCache();
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);
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 = producerCache.getProducer(endpoint);
075 processors.add(producer);
076 }
077 MulticastProcessor mp = new MulticastProcessor(processors, new UseLatestAggregationStrategy());
078 mp.process(exchange);
079 }
080
081 protected Endpoint resolveEndpoint(Exchange exchange, Object recipient) {
082 // trim strings as end users might have added spaces between separators
083 if (recipient instanceof String) {
084 recipient = ((String)recipient).trim();
085 }
086 return ExchangeHelper.resolveEndpoint(exchange, recipient);
087 }
088
089 protected void doStop() throws Exception {
090 producerCache.stop();
091 }
092
093 protected void doStart() throws Exception {
094 }
095 }