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