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://activemq.apache.org/camel/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: 66990 $ 042 */ 043 public class RecipientList extends ServiceSupport implements Processor { 044 private final Expression<Exchange> expression; 045 private ProducerCache<Exchange> producerCache = new ProducerCache<Exchange>(); 046 047 public RecipientList(Expression<Exchange> expression) { 048 notNull(expression, "expression"); 049 this.expression = expression; 050 } 051 052 @Override 053 public String toString() { 054 return "RecipientList[" + expression + "]"; 055 } 056 057 public void process(Exchange exchange) throws Exception { 058 Object receipientList = expression.evaluate(exchange); 059 sendToRecipientList(exchange, receipientList); 060 } 061 062 /** 063 * Sends the given exchange to the recipient list 064 */ 065 public void sendToRecipientList(Exchange exchange, Object receipientList) throws Exception { 066 Iterator iter = ObjectHelper.createIterator(receipientList); 067 List<Processor> processors = new ArrayList<Processor>(); 068 while (iter.hasNext()) { 069 Object recipient = iter.next(); 070 Endpoint<Exchange> endpoint = resolveEndpoint(exchange, recipient); 071 Producer<Exchange> producer = producerCache.getProducer(endpoint); 072 processors.add(producer); 073 } 074 MulticastProcessor mp = new MulticastProcessor(processors, new UseLatestAggregationStrategy()); 075 mp.process(exchange); 076 } 077 078 protected Endpoint<Exchange> resolveEndpoint(Exchange exchange, Object recipient) { 079 // trim strings as end users might have added spaces between separators 080 if (recipient instanceof String) { 081 recipient = ((String)recipient).trim(); 082 } 083 return ExchangeHelper.resolveEndpoint(exchange, recipient); 084 } 085 086 protected void doStop() throws Exception { 087 producerCache.stop(); 088 } 089 090 protected void doStart() throws Exception { 091 } 092 }