1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.impl; |
18 |
|
|
19 |
|
import java.util.ArrayList; |
20 |
|
import java.util.Collection; |
21 |
|
import java.util.List; |
22 |
|
|
23 |
|
import org.apache.camel.CamelContext; |
24 |
|
import org.apache.camel.Endpoint; |
25 |
|
import org.apache.camel.NoSuchEndpointException; |
26 |
|
import org.apache.camel.Processor; |
27 |
|
import org.apache.camel.Route; |
28 |
|
import org.apache.camel.model.FromType; |
29 |
|
import org.apache.camel.model.ProcessorType; |
30 |
|
import org.apache.camel.model.RouteType; |
31 |
|
import org.apache.camel.processor.Interceptor; |
32 |
|
import org.apache.camel.processor.Pipeline; |
33 |
|
import org.apache.camel.processor.ProceedProcessor; |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
public class RouteContext { |
41 |
|
private final RouteType route; |
42 |
|
private final FromType from; |
43 |
|
private final Collection<Route> routes; |
44 |
|
private Endpoint endpoint; |
45 |
267 |
private List<Processor> eventDrivenProcessors = new ArrayList<Processor>(); |
46 |
|
private Interceptor lastInterceptor; |
47 |
|
|
48 |
267 |
public RouteContext(RouteType route, FromType from, Collection<Route> routes) { |
49 |
267 |
this.route = route; |
50 |
267 |
this.from = from; |
51 |
267 |
this.routes = routes; |
52 |
267 |
} |
53 |
|
|
54 |
|
public Endpoint getEndpoint() { |
55 |
531 |
if (endpoint == null) { |
56 |
267 |
endpoint = from.resolveEndpoint(this); |
57 |
|
} |
58 |
531 |
return endpoint; |
59 |
|
} |
60 |
|
|
61 |
|
public FromType getFrom() { |
62 |
0 |
return from; |
63 |
|
} |
64 |
|
|
65 |
|
public RouteType getRoute() { |
66 |
336 |
return route; |
67 |
|
} |
68 |
|
|
69 |
|
public CamelContext getCamelContext() { |
70 |
0 |
return getRoute().getCamelContext(); |
71 |
|
} |
72 |
|
|
73 |
|
public Processor createProcessor(ProcessorType node) throws Exception { |
74 |
198 |
return node.createOutputsProcessor(this); |
75 |
|
} |
76 |
|
|
77 |
|
public Endpoint resolveEndpoint(String uri) { |
78 |
579 |
return route.resolveEndpoint(uri); |
79 |
|
} |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
public Endpoint resolveEndpoint(String uri, String ref) { |
85 |
579 |
Endpoint endpoint = null; |
86 |
579 |
if (uri != null) { |
87 |
579 |
endpoint = resolveEndpoint(uri); |
88 |
576 |
if (endpoint == null) { |
89 |
0 |
throw new NoSuchEndpointException(uri); |
90 |
|
} |
91 |
|
} |
92 |
576 |
if (ref != null) { |
93 |
0 |
endpoint = lookup(ref, Endpoint.class); |
94 |
0 |
if (endpoint == null) { |
95 |
0 |
throw new NoSuchEndpointException("ref:" + ref); |
96 |
|
} |
97 |
|
} |
98 |
576 |
if (endpoint == null) { |
99 |
0 |
throw new IllegalArgumentException("Either 'uri' or 'ref' must be specified on: " + this); |
100 |
|
} else { |
101 |
576 |
return endpoint; |
102 |
|
} |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
public <T> T lookup(String name, Class<T> type) { |
109 |
0 |
return getCamelContext().getRegistry().lookup(name, type); |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
public void commit() { |
117 |
|
|
118 |
|
|
119 |
264 |
if (!eventDrivenProcessors.isEmpty()) { |
120 |
258 |
Processor processor = Pipeline.newInstance(eventDrivenProcessors); |
121 |
258 |
routes.add(new EventDrivenConsumerRoute(getEndpoint(), processor)); |
122 |
|
} |
123 |
264 |
} |
124 |
|
|
125 |
|
public void addEventDrivenProcessor(Processor processor) { |
126 |
294 |
eventDrivenProcessors.add(processor); |
127 |
294 |
} |
128 |
|
|
129 |
|
public void intercept(Interceptor interceptor) { |
130 |
24 |
getRoute().intercept(interceptor); |
131 |
24 |
lastInterceptor = interceptor; |
132 |
24 |
} |
133 |
|
|
134 |
|
public Processor createProceedProcessor() { |
135 |
24 |
if (lastInterceptor == null) { |
136 |
0 |
throw new IllegalArgumentException("Cannot proceed() from outside of an interceptor!"); |
137 |
|
} |
138 |
|
else { |
139 |
24 |
return new ProceedProcessor(lastInterceptor); |
140 |
|
} |
141 |
|
} |
142 |
|
} |