1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel; |
18 |
|
|
19 |
|
import java.util.HashMap; |
20 |
|
import java.util.Map; |
21 |
|
|
22 |
|
import org.apache.camel.impl.ServiceSupport; |
23 |
|
import org.apache.camel.util.ObjectHelper; |
24 |
|
import org.apache.camel.util.ProducerCache; |
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
public class CamelTemplate<E extends Exchange> extends ServiceSupport implements ProducerTemplate<E> { |
34 |
|
private CamelContext context; |
35 |
213 |
private ProducerCache<E> producerCache = new ProducerCache<E>(); |
36 |
213 |
private boolean useEndpointCache = true; |
37 |
213 |
private Map<String, Endpoint<E>> endpointCache = new HashMap<String, Endpoint<E>>(); |
38 |
|
private Endpoint<E> defaultEndpoint; |
39 |
|
|
40 |
213 |
public CamelTemplate(CamelContext context) { |
41 |
213 |
this.context = context; |
42 |
213 |
} |
43 |
|
|
44 |
|
public CamelTemplate(CamelContext context, Endpoint defaultEndpoint) { |
45 |
0 |
this(context); |
46 |
0 |
this.defaultEndpoint = defaultEndpoint; |
47 |
0 |
} |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
public E send(String endpointUri, E exchange) { |
56 |
9 |
Endpoint endpoint = resolveMandatoryEndpoint(endpointUri); |
57 |
9 |
send(endpoint, exchange); |
58 |
9 |
return exchange; |
59 |
|
} |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
public E send(String endpointUri, Processor processor) { |
70 |
102 |
Endpoint endpoint = resolveMandatoryEndpoint(endpointUri); |
71 |
96 |
return send(endpoint, processor); |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
public E send(Endpoint<E> endpoint, E exchange) { |
81 |
9 |
E convertedExchange = endpoint.toExchangeType(exchange); |
82 |
9 |
producerCache.send(endpoint, convertedExchange); |
83 |
9 |
return exchange; |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
public E send(Endpoint<E> endpoint, Processor processor) { |
95 |
585 |
return producerCache.send(endpoint, processor); |
96 |
|
} |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
public Object sendBody(Endpoint<E> endpoint, final Object body) { |
106 |
0 |
E result = send(endpoint, new Processor() { |
107 |
0 |
public void process(Exchange exchange) { |
108 |
0 |
Message in = exchange.getIn(); |
109 |
0 |
in.setBody(body); |
110 |
0 |
} |
111 |
|
}); |
112 |
0 |
return extractResultBody(result); |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
public Object sendBody(String endpointUri, final Object body) { |
123 |
48 |
E result = send(endpointUri, new Processor() { |
124 |
48 |
public void process(Exchange exchange) { |
125 |
42 |
Message in = exchange.getIn(); |
126 |
42 |
in.setBody(body); |
127 |
42 |
} |
128 |
|
}); |
129 |
42 |
return extractResultBody(result); |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
public Object sendBodyAndHeader(String endpointUri, final Object body, final String header, |
142 |
|
final Object headerValue) { |
143 |
462 |
return sendBodyAndHeader(resolveMandatoryEndpoint(endpointUri), body, header, headerValue); |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
public Object sendBodyAndHeader(Endpoint endpoint, final Object body, final String header, |
156 |
|
final Object headerValue) { |
157 |
462 |
E result = send(endpoint, new Processor() { |
158 |
462 |
public void process(Exchange exchange) { |
159 |
462 |
Message in = exchange.getIn(); |
160 |
462 |
in.setHeader(header, headerValue); |
161 |
462 |
in.setBody(body); |
162 |
462 |
} |
163 |
|
}); |
164 |
462 |
return extractResultBody(result); |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
public Object sendBodyAndHeaders(String endpointUri, final Object body, final Map<String, Object> headers) { |
176 |
0 |
return sendBodyAndHeaders(resolveMandatoryEndpoint(endpointUri), body, headers); |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
public Object sendBodyAndHeaders(Endpoint endpoint, final Object body, final Map<String, Object> headers) { |
188 |
0 |
E result = send(endpoint, new Processor() { |
189 |
0 |
public void process(Exchange exchange) { |
190 |
0 |
Message in = exchange.getIn(); |
191 |
0 |
for (Map.Entry<String, Object> header : headers.entrySet()) { |
192 |
0 |
in.setHeader(header.getKey(), header.getValue()); |
193 |
0 |
} |
194 |
0 |
in.setBody(body); |
195 |
0 |
} |
196 |
|
}); |
197 |
0 |
return extractResultBody(result); |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
public Object sendBody(Object body) { |
210 |
0 |
return sendBody(getMandatoryDefaultEndpoint(), body); |
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
public E send(E exchange) { |
219 |
0 |
return send(getMandatoryDefaultEndpoint(), exchange); |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
public E send(Processor processor) { |
230 |
0 |
return send(getMandatoryDefaultEndpoint(), processor); |
231 |
|
} |
232 |
|
|
233 |
|
public Object sendBodyAndHeader(Object body, String header, Object headerValue) { |
234 |
0 |
return sendBodyAndHeader(getMandatoryDefaultEndpoint(), body, header, headerValue); |
235 |
|
} |
236 |
|
|
237 |
|
public Object sendBodyAndHeaders(Object body, Map<String, Object> headers) { |
238 |
0 |
return sendBodyAndHeaders(getMandatoryDefaultEndpoint(), body, headers); |
239 |
|
} |
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
public Producer<E> getProducer(Endpoint<E> endpoint) { |
244 |
0 |
return producerCache.getProducer(endpoint); |
245 |
|
} |
246 |
|
|
247 |
|
public CamelContext getContext() { |
248 |
0 |
return context; |
249 |
|
} |
250 |
|
|
251 |
|
public Endpoint<E> getDefaultEndpoint() { |
252 |
0 |
return defaultEndpoint; |
253 |
|
} |
254 |
|
|
255 |
|
public void setDefaultEndpoint(Endpoint<E> defaultEndpoint) { |
256 |
0 |
this.defaultEndpoint = defaultEndpoint; |
257 |
0 |
} |
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
public void setDefaultEndpointUri(String endpointUri) { |
263 |
0 |
setDefaultEndpoint(getContext().getEndpoint(endpointUri)); |
264 |
0 |
} |
265 |
|
|
266 |
|
public boolean isUseEndpointCache() { |
267 |
573 |
return useEndpointCache; |
268 |
|
} |
269 |
|
|
270 |
|
public void setUseEndpointCache(boolean useEndpointCache) { |
271 |
0 |
this.useEndpointCache = useEndpointCache; |
272 |
0 |
} |
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
protected Endpoint resolveMandatoryEndpoint(String endpointUri) { |
278 |
573 |
Endpoint endpoint = null; |
279 |
|
|
280 |
573 |
if (isUseEndpointCache()) { |
281 |
573 |
synchronized (endpointCache) { |
282 |
573 |
endpoint = endpointCache.get(endpointUri); |
283 |
573 |
if (endpoint == null) { |
284 |
180 |
endpoint = context.getEndpoint(endpointUri); |
285 |
177 |
if (endpoint != null) { |
286 |
174 |
endpointCache.put(endpointUri, endpoint); |
287 |
|
} |
288 |
|
} |
289 |
570 |
} |
290 |
570 |
} else { |
291 |
0 |
endpoint = context.getEndpoint(endpointUri); |
292 |
|
} |
293 |
570 |
if (endpoint == null) { |
294 |
3 |
throw new NoSuchEndpointException(endpointUri); |
295 |
|
} |
296 |
567 |
return endpoint; |
297 |
|
} |
298 |
|
|
299 |
|
protected Endpoint<E> getMandatoryDefaultEndpoint() { |
300 |
0 |
Endpoint<E> answer = getDefaultEndpoint(); |
301 |
0 |
ObjectHelper.notNull(answer, "defaultEndpoint"); |
302 |
0 |
return answer; |
303 |
|
} |
304 |
|
|
305 |
|
protected void doStart() throws Exception { |
306 |
0 |
producerCache.start(); |
307 |
0 |
} |
308 |
|
|
309 |
|
protected void doStop() throws Exception { |
310 |
0 |
producerCache.stop(); |
311 |
0 |
} |
312 |
|
|
313 |
|
protected Object extractResultBody(E result) { |
314 |
504 |
Object answer = null; |
315 |
504 |
if (result != null) { |
316 |
504 |
answer = result.getOut().getBody(); |
317 |
504 |
if (answer == null) { |
318 |
504 |
answer = result.getIn().getBody(); |
319 |
|
} |
320 |
|
} |
321 |
504 |
return answer; |
322 |
|
} |
323 |
|
} |