1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.service; |
12 | |
|
13 | |
import org.mule.MuleSessionHandler; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.api.config.MuleProperties; |
16 | |
import org.mule.api.endpoint.EndpointURIBuilder; |
17 | |
import org.mule.api.endpoint.InboundEndpoint; |
18 | |
import org.mule.api.registry.AbstractServiceDescriptor; |
19 | |
import org.mule.api.registry.Registry; |
20 | |
import org.mule.api.service.Service; |
21 | |
import org.mule.api.transaction.TransactionConfig; |
22 | |
import org.mule.api.transaction.TransactionFactory; |
23 | |
import org.mule.api.transformer.Transformer; |
24 | |
import org.mule.api.transport.Connector; |
25 | |
import org.mule.api.transport.MessageAdapter; |
26 | |
import org.mule.api.transport.MessageDispatcherFactory; |
27 | |
import org.mule.api.transport.MessageReceiver; |
28 | |
import org.mule.api.transport.MessageRequesterFactory; |
29 | |
import org.mule.api.transport.SessionHandler; |
30 | |
import org.mule.config.i18n.CoreMessages; |
31 | |
import org.mule.endpoint.UrlEndpointURIBuilder; |
32 | |
import org.mule.transaction.XaTransactionFactory; |
33 | |
import org.mule.transport.NullPayload; |
34 | |
import org.mule.util.ClassUtils; |
35 | |
import org.mule.util.CollectionUtils; |
36 | |
|
37 | |
import java.util.List; |
38 | |
import java.util.Properties; |
39 | |
|
40 | |
|
41 | |
public class DefaultTransportServiceDescriptor extends AbstractServiceDescriptor implements TransportServiceDescriptor |
42 | |
{ |
43 | |
private String connector; |
44 | |
private String dispatcherFactory; |
45 | |
private String requesterFactory; |
46 | |
private String transactionFactory; |
47 | |
private String messageAdapter; |
48 | |
private String messageReceiver; |
49 | |
private String transactedMessageReceiver; |
50 | |
private String xaTransactedMessageReceiver; |
51 | |
private String endpointBuilder; |
52 | |
private String sessionHandler; |
53 | |
private String defaultInboundTransformer; |
54 | |
private String defaultOutboundTransformer; |
55 | |
private String defaultResponseTransformer; |
56 | |
|
57 | |
private Transformer inboundTransformer; |
58 | |
private Transformer outboundTransformer; |
59 | |
private Transformer responseTransformer; |
60 | |
|
61 | |
|
62 | 390 | private Properties exceptionMappings = new Properties(); |
63 | |
|
64 | |
public DefaultTransportServiceDescriptor(String service, Properties props, Registry registry) |
65 | |
{ |
66 | 390 | super(service); |
67 | |
|
68 | 390 | connector = removeProperty(MuleProperties.CONNECTOR_CLASS, props); |
69 | 390 | dispatcherFactory = removeProperty(MuleProperties.CONNECTOR_DISPATCHER_FACTORY, props); |
70 | 390 | requesterFactory = removeProperty(MuleProperties.CONNECTOR_REQUESTER_FACTORY, props); |
71 | 390 | transactionFactory = removeProperty(MuleProperties.CONNECTOR_DISPATCHER_FACTORY, props); |
72 | 390 | messageReceiver = removeProperty(MuleProperties.CONNECTOR_MESSAGE_RECEIVER_CLASS, props); |
73 | 390 | transactedMessageReceiver = removeProperty(MuleProperties.CONNECTOR_TRANSACTED_MESSAGE_RECEIVER_CLASS, props); |
74 | 390 | xaTransactedMessageReceiver = removeProperty(MuleProperties.CONNECTOR_XA_TRANSACTED_MESSAGE_RECEIVER_CLASS, props); |
75 | 390 | messageAdapter = removeProperty(MuleProperties.CONNECTOR_MESSAGE_ADAPTER, props); |
76 | 390 | defaultInboundTransformer = removeProperty(MuleProperties.CONNECTOR_INBOUND_TRANSFORMER, props); |
77 | 390 | defaultOutboundTransformer = removeProperty(MuleProperties.CONNECTOR_OUTBOUND_TRANSFORMER, props); |
78 | 390 | defaultResponseTransformer = removeProperty(MuleProperties.CONNECTOR_RESPONSE_TRANSFORMER, props); |
79 | 390 | endpointBuilder = removeProperty(MuleProperties.CONNECTOR_ENDPOINT_BUILDER, props); |
80 | 390 | sessionHandler = removeProperty(MuleProperties.CONNECTOR_SESSION_HANDLER, props); |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | 390 | } |
91 | |
|
92 | |
|
93 | |
public void setOverrides(Properties props) |
94 | |
{ |
95 | 0 | if (props == null || props.size() == 0) |
96 | |
{ |
97 | 0 | return; |
98 | |
} |
99 | |
|
100 | 0 | connector = props.getProperty(MuleProperties.CONNECTOR_CLASS, connector); |
101 | 0 | dispatcherFactory = props.getProperty(MuleProperties.CONNECTOR_DISPATCHER_FACTORY, dispatcherFactory); |
102 | 0 | requesterFactory = props.getProperty(MuleProperties.CONNECTOR_REQUESTER_FACTORY, requesterFactory); |
103 | 0 | messageReceiver = props.getProperty(MuleProperties.CONNECTOR_MESSAGE_RECEIVER_CLASS, messageReceiver); |
104 | 0 | transactedMessageReceiver = props.getProperty( |
105 | |
MuleProperties.CONNECTOR_TRANSACTED_MESSAGE_RECEIVER_CLASS, transactedMessageReceiver); |
106 | 0 | xaTransactedMessageReceiver = props.getProperty( |
107 | |
MuleProperties.CONNECTOR_XA_TRANSACTED_MESSAGE_RECEIVER_CLASS, xaTransactedMessageReceiver); |
108 | 0 | messageAdapter = props.getProperty(MuleProperties.CONNECTOR_MESSAGE_ADAPTER, messageAdapter); |
109 | |
|
110 | 0 | String temp = props.getProperty(MuleProperties.CONNECTOR_INBOUND_TRANSFORMER); |
111 | 0 | if (temp != null) |
112 | |
{ |
113 | 0 | defaultInboundTransformer = temp; |
114 | 0 | inboundTransformer = null; |
115 | |
} |
116 | |
|
117 | 0 | temp = props.getProperty(MuleProperties.CONNECTOR_OUTBOUND_TRANSFORMER); |
118 | 0 | if (temp != null) |
119 | |
{ |
120 | 0 | defaultOutboundTransformer = temp; |
121 | 0 | outboundTransformer = null; |
122 | |
} |
123 | |
|
124 | 0 | temp = props.getProperty(MuleProperties.CONNECTOR_RESPONSE_TRANSFORMER); |
125 | 0 | if (temp != null) |
126 | |
{ |
127 | 0 | defaultResponseTransformer = temp; |
128 | 0 | responseTransformer = null; |
129 | |
} |
130 | |
|
131 | 0 | temp = props.getProperty(MuleProperties.CONNECTOR_ENDPOINT_BUILDER); |
132 | 0 | if (temp != null) |
133 | |
{ |
134 | 0 | endpointBuilder = temp; |
135 | |
} |
136 | 0 | } |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public MessageAdapter createMessageAdapter(Object message) throws TransportServiceException |
142 | |
{ |
143 | 0 | return createMessageAdapter(message, messageAdapter); |
144 | |
} |
145 | |
|
146 | |
protected MessageAdapter createMessageAdapter(Object message, String clazz) |
147 | |
throws TransportServiceException |
148 | |
{ |
149 | 0 | if (message == null) |
150 | |
{ |
151 | 0 | message = NullPayload.getInstance(); |
152 | |
} |
153 | 0 | if (messageAdapter != null) |
154 | |
{ |
155 | |
try |
156 | |
{ |
157 | 0 | return (MessageAdapter) ClassUtils.instanciateClass(clazz, new Object[]{message}); |
158 | |
} |
159 | 0 | catch (Exception e) |
160 | |
{ |
161 | 0 | throw new TransportServiceException(CoreMessages.failedToCreateObjectWith("Message Adapter", clazz), e); |
162 | |
} |
163 | |
} |
164 | |
else |
165 | |
{ |
166 | 0 | throw new TransportServiceException(CoreMessages.objectNotSetInService("Message Adapter", getService())); |
167 | |
} |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
public SessionHandler createSessionHandler() throws TransportServiceException |
174 | |
{ |
175 | 472 | if (sessionHandler == null) |
176 | |
{ |
177 | 390 | sessionHandler = MuleSessionHandler.class.getName(); |
178 | 390 | if (logger.isDebugEnabled()) |
179 | |
{ |
180 | 0 | logger.debug("No session.handler set in service description, defaulting to: " |
181 | |
+ sessionHandler); |
182 | |
} |
183 | |
} |
184 | |
try |
185 | |
{ |
186 | 472 | return (SessionHandler) ClassUtils.instanciateClass(sessionHandler, ClassUtils.NO_ARGS, |
187 | |
getClass()); |
188 | |
} |
189 | 0 | catch (Throwable e) |
190 | |
{ |
191 | 0 | throw new TransportServiceException(CoreMessages.failedToCreateObjectWith("SessionHandler", sessionHandler), e); |
192 | |
} |
193 | |
} |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
public MessageReceiver createMessageReceiver(Connector connector, |
199 | |
Service service, |
200 | |
InboundEndpoint endpoint) throws MuleException |
201 | |
{ |
202 | |
|
203 | 0 | return createMessageReceiver(connector, service, endpoint, null); |
204 | |
} |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
public MessageReceiver createMessageReceiver(Connector connector, |
210 | |
Service service, |
211 | |
InboundEndpoint endpoint, |
212 | |
Object[] args) throws MuleException |
213 | |
{ |
214 | 0 | String receiverClass = messageReceiver; |
215 | |
|
216 | 0 | if (endpoint.getTransactionConfig() != null |
217 | |
&& endpoint.getTransactionConfig().getAction() != TransactionConfig.ACTION_NONE) |
218 | |
{ |
219 | 0 | boolean xaTx = endpoint.getTransactionConfig().getFactory() instanceof XaTransactionFactory; |
220 | 0 | if (transactedMessageReceiver != null && !xaTx) |
221 | |
{ |
222 | 0 | receiverClass = transactedMessageReceiver; |
223 | |
} |
224 | 0 | else if (xaTransactedMessageReceiver != null && xaTx) |
225 | |
{ |
226 | 0 | receiverClass = xaTransactedMessageReceiver; |
227 | |
} |
228 | |
|
229 | |
} |
230 | |
|
231 | 0 | if (receiverClass != null) |
232 | |
{ |
233 | |
Object[] newArgs; |
234 | |
|
235 | 0 | if (args != null && args.length != 0) |
236 | |
{ |
237 | 0 | newArgs = new Object[3 + args.length]; |
238 | |
} |
239 | |
else |
240 | |
{ |
241 | 0 | newArgs = new Object[3]; |
242 | |
} |
243 | |
|
244 | 0 | newArgs[0] = connector; |
245 | 0 | newArgs[1] = service; |
246 | 0 | newArgs[2] = endpoint; |
247 | |
|
248 | 0 | if (args != null && args.length != 0) |
249 | |
{ |
250 | 0 | System.arraycopy(args, 0, newArgs, 3, newArgs.length - 3); |
251 | |
} |
252 | |
|
253 | |
try |
254 | |
{ |
255 | 0 | return (MessageReceiver) ClassUtils.instanciateClass(receiverClass, newArgs); |
256 | |
} |
257 | 0 | catch (Exception e) |
258 | |
{ |
259 | 0 | throw new TransportServiceException(CoreMessages.failedToCreateObjectWith("Message Receiver", getService()), e); |
260 | |
} |
261 | |
|
262 | |
} |
263 | |
else |
264 | |
{ |
265 | 0 | throw new TransportServiceException(CoreMessages.objectNotSetInService("Message Receiver", getService())); |
266 | |
} |
267 | |
} |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
public MessageDispatcherFactory createDispatcherFactory() throws TransportServiceException |
273 | |
{ |
274 | 472 | if (dispatcherFactory != null) |
275 | |
{ |
276 | |
try |
277 | |
{ |
278 | 472 | return (MessageDispatcherFactory) ClassUtils.instanciateClass(dispatcherFactory, |
279 | |
ClassUtils.NO_ARGS); |
280 | |
} |
281 | 0 | catch (Exception e) |
282 | |
{ |
283 | 0 | throw new TransportServiceException(CoreMessages.failedToCreateObjectWith("Message Dispatcher Factory", dispatcherFactory), e); |
284 | |
} |
285 | |
} |
286 | |
else |
287 | |
{ |
288 | |
|
289 | 0 | return null; |
290 | |
} |
291 | |
} |
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
public MessageRequesterFactory createRequesterFactory() throws TransportServiceException |
297 | |
{ |
298 | 472 | if (requesterFactory != null) |
299 | |
{ |
300 | |
try |
301 | |
{ |
302 | 0 | return (MessageRequesterFactory) ClassUtils.instanciateClass(requesterFactory, |
303 | |
ClassUtils.NO_ARGS); |
304 | |
} |
305 | 0 | catch (Exception e) |
306 | |
{ |
307 | 0 | throw new TransportServiceException( |
308 | |
CoreMessages.failedToCreateObjectWith("Message Requester Factory", requesterFactory), e); |
309 | |
} |
310 | |
} |
311 | |
else |
312 | |
{ |
313 | |
|
314 | 472 | return null; |
315 | |
} |
316 | |
} |
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
public TransactionFactory createTransactionFactory() throws TransportServiceException |
322 | |
{ |
323 | 0 | if (transactionFactory != null) |
324 | |
{ |
325 | |
try |
326 | |
{ |
327 | 0 | return (TransactionFactory) ClassUtils.instanciateClass(transactionFactory, |
328 | |
ClassUtils.NO_ARGS); |
329 | |
} |
330 | 0 | catch (Exception e) |
331 | |
{ |
332 | 0 | throw new TransportServiceException(CoreMessages.failedToCreateObjectWith("Transaction Factory", transactionFactory), e); |
333 | |
} |
334 | |
} |
335 | |
else |
336 | |
{ |
337 | 0 | return null; |
338 | |
} |
339 | |
} |
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
public Connector createConnector() throws TransportServiceException |
345 | |
{ |
346 | |
Connector newConnector; |
347 | |
|
348 | |
try |
349 | |
{ |
350 | 34 | if (connector != null) |
351 | |
{ |
352 | 34 | newConnector = (Connector) ClassUtils.loadClass(connector, TransportFactory.class) |
353 | |
.newInstance(); |
354 | |
} |
355 | |
else |
356 | |
{ |
357 | 0 | throw new TransportServiceException(CoreMessages.objectNotSetInService("Connector", getService())); |
358 | |
} |
359 | |
} |
360 | 0 | catch (TransportServiceException e) |
361 | |
{ |
362 | 0 | throw e; |
363 | |
} |
364 | 0 | catch (Exception e) |
365 | |
{ |
366 | 0 | throw new TransportServiceException(CoreMessages.failedToCreateObjectWith("Connector", connector), e); |
367 | 34 | } |
368 | |
|
369 | 34 | if (newConnector.getName() == null) |
370 | |
{ |
371 | 34 | newConnector.setName("_" + newConnector.getProtocol() + "Connector#" + connector.hashCode()); |
372 | |
} |
373 | 34 | return newConnector; |
374 | |
} |
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
public List createInboundTransformers() throws TransportFactoryException |
380 | |
{ |
381 | 68 | if (inboundTransformer != null) |
382 | |
{ |
383 | 20 | return CollectionUtils.singletonList(inboundTransformer); |
384 | |
} |
385 | 48 | if (defaultInboundTransformer != null) |
386 | |
{ |
387 | 48 | logger.info("Loading default inbound transformer: " + defaultInboundTransformer); |
388 | |
try |
389 | |
{ |
390 | 48 | inboundTransformer = (Transformer) ClassUtils.instanciateClass( |
391 | |
defaultInboundTransformer, ClassUtils.NO_ARGS); |
392 | 48 | return CollectionUtils.singletonList(inboundTransformer); |
393 | |
} |
394 | 0 | catch (Exception e) |
395 | |
{ |
396 | 0 | throw new TransportFactoryException(CoreMessages.failedToLoadTransformer("inbound", defaultInboundTransformer), e); |
397 | |
} |
398 | |
} |
399 | 0 | return null; |
400 | |
} |
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
public List createOutboundTransformers() throws TransportFactoryException |
406 | |
{ |
407 | 414 | if (outboundTransformer != null) |
408 | |
{ |
409 | 70 | return CollectionUtils.singletonList(outboundTransformer); |
410 | |
} |
411 | 344 | if (defaultOutboundTransformer != null) |
412 | |
{ |
413 | 344 | logger.info("Loading default outbound transformer: " + defaultOutboundTransformer); |
414 | |
try |
415 | |
{ |
416 | 344 | outboundTransformer = (Transformer) ClassUtils.instanciateClass( |
417 | |
defaultOutboundTransformer, ClassUtils.NO_ARGS); |
418 | 344 | return CollectionUtils.singletonList(outboundTransformer); |
419 | |
} |
420 | 0 | catch (Exception e) |
421 | |
{ |
422 | 0 | throw new TransportFactoryException(CoreMessages.failedToLoadTransformer("outbound", defaultOutboundTransformer), e); |
423 | |
} |
424 | |
} |
425 | 0 | return null; |
426 | |
} |
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
public List createResponseTransformers() throws TransportFactoryException |
432 | |
{ |
433 | 68 | if (responseTransformer != null) |
434 | |
{ |
435 | 20 | return CollectionUtils.singletonList(responseTransformer); |
436 | |
} |
437 | 48 | if (defaultResponseTransformer != null) |
438 | |
{ |
439 | 48 | logger.info("Loading default response transformer: " + defaultResponseTransformer); |
440 | |
try |
441 | |
{ |
442 | 48 | responseTransformer = (Transformer) ClassUtils.instanciateClass( |
443 | |
defaultResponseTransformer, ClassUtils.NO_ARGS); |
444 | 48 | return CollectionUtils.singletonList(responseTransformer); |
445 | |
} |
446 | 0 | catch (Exception e) |
447 | |
{ |
448 | 0 | throw new TransportFactoryException(CoreMessages.failedToLoadTransformer("response", defaultResponseTransformer), e); |
449 | |
} |
450 | |
} |
451 | 0 | return null; |
452 | |
} |
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
public EndpointURIBuilder createEndpointBuilder() throws TransportFactoryException |
458 | |
{ |
459 | 486 | if (endpointBuilder == null) |
460 | |
{ |
461 | 0 | logger.debug("Endpoint resolver not set, Loading default resolver: " |
462 | |
+ UrlEndpointURIBuilder.class.getName()); |
463 | 0 | return new UrlEndpointURIBuilder(); |
464 | |
} |
465 | |
else |
466 | |
{ |
467 | 486 | logger.debug("Loading endpointUri resolver: " + endpointBuilder); |
468 | |
try |
469 | |
{ |
470 | 486 | return (EndpointURIBuilder) ClassUtils.instanciateClass(endpointBuilder, ClassUtils.NO_ARGS); |
471 | |
} |
472 | 0 | catch (Exception e) |
473 | |
{ |
474 | 0 | throw new TransportFactoryException(CoreMessages.failedToLoad("Endpoint Builder: " + endpointBuilder), e); |
475 | |
} |
476 | |
} |
477 | |
} |
478 | |
|
479 | |
public void setExceptionMappings(Properties props) |
480 | |
{ |
481 | 390 | this.exceptionMappings = props; |
482 | 390 | } |
483 | |
|
484 | |
public Properties getExceptionMappings() |
485 | |
{ |
486 | 0 | return this.exceptionMappings; |
487 | |
} |
488 | |
} |