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.Exchange; |
21 |
|
import org.apache.camel.Expression; |
22 |
|
import org.apache.camel.Processor; |
23 |
|
import org.apache.camel.converter.ObjectConverter; |
24 |
|
import org.apache.camel.impl.ServiceSupport; |
25 |
|
import static org.apache.camel.util.ObjectHelper.notNull; |
26 |
|
import org.apache.camel.util.ServiceHelper; |
27 |
|
|
28 |
|
import java.util.Iterator; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
public class Splitter extends ServiceSupport implements Processor { |
37 |
|
private final Processor processor; |
38 |
|
private final Expression expression; |
39 |
|
|
40 |
2 |
public Splitter(Processor destination, Expression expression) { |
41 |
2 |
this.processor = destination; |
42 |
2 |
this.expression = expression; |
43 |
2 |
notNull(destination, "destination"); |
44 |
2 |
notNull(expression, "expression"); |
45 |
2 |
} |
46 |
|
|
47 |
|
@Override |
48 |
|
public String toString() { |
49 |
5 |
return "Splitter[on: " + expression + " to: " + processor + "]"; |
50 |
|
} |
51 |
|
|
52 |
|
public void process(Exchange exchange) throws Exception { |
53 |
1 |
Object value = expression.evaluate(exchange); |
54 |
1 |
Iterator iter = ObjectConverter.iterator(value); |
55 |
5 |
while (iter.hasNext()) { |
56 |
4 |
Object part = iter.next(); |
57 |
4 |
Exchange newExchange = exchange.copy(); |
58 |
4 |
newExchange.getIn().setBody(part); |
59 |
4 |
processor.process(newExchange); |
60 |
4 |
} |
61 |
1 |
} |
62 |
|
|
63 |
|
protected void doStart() throws Exception { |
64 |
1 |
ServiceHelper.startServices(processor); |
65 |
1 |
} |
66 |
|
|
67 |
|
protected void doStop() throws Exception { |
68 |
1 |
ServiceHelper.stopServices(processor); |
69 |
1 |
} |
70 |
|
} |