| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
package org.apache.camel.processor; |
| 19 |
|
|
| 20 |
|
import org.apache.camel.Predicate; |
| 21 |
|
import org.apache.camel.Processor; |
| 22 |
|
import org.apache.camel.Exchange; |
| 23 |
|
import org.apache.camel.util.ServiceHelper; |
| 24 |
|
import org.apache.camel.impl.ServiceSupport; |
| 25 |
|
|
| 26 |
|
import java.util.ArrayList; |
| 27 |
|
import java.util.List; |
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
public class ChoiceProcessor extends ServiceSupport implements Processor { |
| 36 |
5 |
private List<FilterProcessor> filters = new ArrayList<FilterProcessor>(); |
| 37 |
|
private Processor otherwise; |
| 38 |
|
|
| 39 |
5 |
public ChoiceProcessor(List<FilterProcessor> filters, Processor otherwise) { |
| 40 |
5 |
this.filters = filters; |
| 41 |
5 |
this.otherwise = otherwise; |
| 42 |
5 |
} |
| 43 |
|
|
| 44 |
|
public void process(Exchange exchange) throws Exception { |
| 45 |
6 |
for (FilterProcessor filterProcessor : filters) { |
| 46 |
10 |
Predicate<Exchange> predicate = filterProcessor.getPredicate(); |
| 47 |
10 |
if (predicate != null && predicate.matches(exchange)) { |
| 48 |
4 |
filterProcessor.getProcessor().process(exchange); |
| 49 |
4 |
return; |
| 50 |
|
} |
| 51 |
6 |
} |
| 52 |
2 |
if (otherwise != null) { |
| 53 |
2 |
otherwise.process(exchange); |
| 54 |
|
} |
| 55 |
2 |
} |
| 56 |
|
|
| 57 |
|
@Override |
| 58 |
|
public String toString() { |
| 59 |
8 |
StringBuilder builder = new StringBuilder("choice{"); |
| 60 |
8 |
boolean first = true; |
| 61 |
8 |
for (FilterProcessor processor : filters) { |
| 62 |
16 |
if (first) { |
| 63 |
8 |
first = false; |
| 64 |
8 |
} |
| 65 |
|
else { |
| 66 |
8 |
builder.append(", "); |
| 67 |
|
} |
| 68 |
16 |
builder.append("when "); |
| 69 |
16 |
builder.append(processor.getPredicate().toString()); |
| 70 |
16 |
builder.append(": "); |
| 71 |
16 |
builder.append(processor.getProcessor()); |
| 72 |
16 |
} |
| 73 |
8 |
if (otherwise != null) { |
| 74 |
8 |
builder.append(", otherwise: "); |
| 75 |
8 |
builder.append(otherwise); |
| 76 |
|
} |
| 77 |
8 |
builder.append("}"); |
| 78 |
8 |
return builder.toString(); |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
public List<FilterProcessor> getFilters() { |
| 82 |
1 |
return filters; |
| 83 |
|
} |
| 84 |
|
|
| 85 |
|
public Processor getOtherwise() { |
| 86 |
1 |
return otherwise; |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
protected void doStart() throws Exception { |
| 91 |
4 |
ServiceHelper.startServices(filters); |
| 92 |
4 |
ServiceHelper.startServices(otherwise); |
| 93 |
4 |
} |
| 94 |
|
|
| 95 |
|
protected void doStop() throws Exception { |
| 96 |
4 |
ServiceHelper.stopServices(otherwise); |
| 97 |
4 |
ServiceHelper.stopServices(filters); |
| 98 |
4 |
} |
| 99 |
|
} |