| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| MulticastProcessor |
|
| 0.0;0 |
| 1 | /** |
|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
| 3 | * contributor license agreements. See the NOTICE file distributed with |
|
| 4 | * this work for additional information regarding copyright ownership. |
|
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
| 6 | * (the "License"); you may not use this file except in compliance with |
|
| 7 | * the License. You may obtain a copy of the License at |
|
| 8 | * |
|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 10 | * |
|
| 11 | * Unless required by applicable law or agreed to in writing, software |
|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 14 | * See the License for the specific language governing permissions and |
|
| 15 | * limitations under the License. |
|
| 16 | */ |
|
| 17 | package org.apache.camel.processor; |
|
| 18 | ||
| 19 | import java.util.ArrayList; |
|
| 20 | import java.util.Collection; |
|
| 21 | ||
| 22 | import org.apache.camel.Endpoint; |
|
| 23 | import org.apache.camel.Exchange; |
|
| 24 | import org.apache.camel.Processor; |
|
| 25 | import org.apache.camel.impl.ServiceSupport; |
|
| 26 | import org.apache.camel.util.ServiceHelper; |
|
| 27 | ||
| 28 | /** |
|
| 29 | * Implements the Multicast pattern to send a message exchange to a number of |
|
| 30 | * endpoints, each endpoint receiving a copy of the message exchange. |
|
| 31 | * |
|
| 32 | * @see Pipeline |
|
| 33 | * @version $Revision: 563607 $ |
|
| 34 | */ |
|
| 35 | public class MulticastProcessor extends ServiceSupport implements Processor { |
|
| 36 | private Collection<Processor> processors; |
|
| 37 | ||
| 38 | 48 | public MulticastProcessor(Collection<Processor> processors) { |
| 39 | 48 | this.processors = processors; |
| 40 | 48 | } |
| 41 | ||
| 42 | /** |
|
| 43 | * A helper method to convert a list of endpoints into a list of processors |
|
| 44 | */ |
|
| 45 | public static <E extends Exchange> Collection<Processor> toProducers(Collection<Endpoint> endpoints) |
|
| 46 | throws Exception { |
|
| 47 | 0 | Collection<Processor> answer = new ArrayList<Processor>(); |
| 48 | 0 | for (Endpoint endpoint : endpoints) { |
| 49 | 0 | answer.add(endpoint.createProducer()); |
| 50 | 0 | } |
| 51 | 0 | return answer; |
| 52 | } |
|
| 53 | ||
| 54 | @Override |
|
| 55 | public String toString() { |
|
| 56 | 3 | return "Multicast" + getProcessors(); |
| 57 | } |
|
| 58 | ||
| 59 | public void process(Exchange exchange) throws Exception { |
|
| 60 | 3 | for (Processor producer : processors) { |
| 61 | 9 | Exchange copy = copyExchangeStrategy(producer, exchange); |
| 62 | 9 | producer.process(copy); |
| 63 | 9 | } |
| 64 | 3 | } |
| 65 | ||
| 66 | protected void doStop() throws Exception { |
|
| 67 | 42 | ServiceHelper.stopServices(processors); |
| 68 | 42 | } |
| 69 | ||
| 70 | protected void doStart() throws Exception { |
|
| 71 | 42 | ServiceHelper.startServices(processors); |
| 72 | 42 | } |
| 73 | ||
| 74 | /** |
|
| 75 | * Returns the producers to multicast to |
|
| 76 | */ |
|
| 77 | public Collection<Processor> getProcessors() { |
|
| 78 | 87 | return processors; |
| 79 | } |
|
| 80 | ||
| 81 | /** |
|
| 82 | * Strategy method to copy the exchange before sending to another endpoint. |
|
| 83 | * Derived classes such as the {@link Pipeline} will not clone the exchange |
|
| 84 | * |
|
| 85 | * @param processor the processor that will send the exchange |
|
| 86 | * @param exchange |
|
| 87 | * @return the current exchange if no copying is required such as for a |
|
| 88 | * pipeline otherwise a new copy of the exchange is returned. |
|
| 89 | */ |
|
| 90 | protected Exchange copyExchangeStrategy(Processor processor, Exchange exchange) { |
|
| 91 | 9 | return exchange.copy(); |
| 92 | } |
|
| 93 | } |