1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.builder; |
19 |
|
|
20 |
|
import java.util.ArrayList; |
21 |
|
import java.util.List; |
22 |
|
|
23 |
|
import org.apache.camel.Exchange; |
24 |
|
import org.apache.camel.processor.DelegateProcessor; |
25 |
|
import org.apache.camel.Processor; |
26 |
|
import org.apache.camel.RuntimeCamelException; |
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
public class InterceptorBuilder implements ProcessorFactory { |
32 |
2 |
private final List<DelegateProcessor> intercepts = new ArrayList<DelegateProcessor>(); |
33 |
|
private final FromBuilder parent; |
34 |
|
private FromBuilder target; |
35 |
|
|
36 |
2 |
public InterceptorBuilder(FromBuilder parent) { |
37 |
2 |
this.parent = parent; |
38 |
2 |
} |
39 |
|
|
40 |
|
@Fluent("interceptor") |
41 |
|
public InterceptorBuilder add(@FluentArg("ref") DelegateProcessor interceptor) { |
42 |
4 |
intercepts.add(interceptor); |
43 |
4 |
return this; |
44 |
|
} |
45 |
|
|
46 |
|
@Fluent(callOnElementEnd=true) |
47 |
|
public FromBuilder target() { |
48 |
2 |
this.target = new FromBuilder(parent); |
49 |
2 |
return target; |
50 |
|
} |
51 |
|
|
52 |
|
public Processor createProcessor() throws Exception { |
53 |
|
|
54 |
|
|
55 |
2 |
if( target == null ) |
56 |
0 |
throw new RuntimeCamelException("target provided."); |
57 |
|
|
58 |
|
|
59 |
2 |
DelegateProcessor first=null; |
60 |
2 |
DelegateProcessor last=null; |
61 |
2 |
for (DelegateProcessor p : intercepts) { |
62 |
4 |
if( first == null ) { |
63 |
2 |
first = p; |
64 |
|
} |
65 |
4 |
if( last != null ) { |
66 |
2 |
last.setNext(p); |
67 |
|
} |
68 |
4 |
last = p; |
69 |
4 |
} |
70 |
|
|
71 |
2 |
Processor p = target.createProcessor(); |
72 |
2 |
if( last != null ) { |
73 |
2 |
last.setNext(p); |
74 |
|
} |
75 |
2 |
return first == null ? p : first; |
76 |
|
} |
77 |
|
} |