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