1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport; |
12 | |
|
13 | |
import org.mule.DefaultMuleEvent; |
14 | |
import org.mule.RegistryContext; |
15 | |
import org.mule.api.MuleEvent; |
16 | |
import org.mule.api.MuleException; |
17 | |
import org.mule.api.MuleMessage; |
18 | |
import org.mule.api.config.MuleProperties; |
19 | |
import org.mule.api.endpoint.EndpointBuilder; |
20 | |
import org.mule.api.endpoint.EndpointFactory; |
21 | |
import org.mule.api.endpoint.OutboundEndpoint; |
22 | |
import org.mule.api.transport.DispatchException; |
23 | |
import org.mule.api.transport.ReplyToHandler; |
24 | |
import org.mule.config.i18n.CoreMessages; |
25 | |
import org.mule.service.AbstractService; |
26 | |
|
27 | |
import java.util.HashMap; |
28 | |
import java.util.List; |
29 | |
import java.util.Map; |
30 | |
|
31 | |
import org.apache.commons.logging.Log; |
32 | |
import org.apache.commons.logging.LogFactory; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class DefaultReplyToHandler implements ReplyToHandler |
40 | |
{ |
41 | |
|
42 | |
|
43 | |
|
44 | 0 | protected static final Log logger = LogFactory.getLog(DefaultReplyToHandler.class); |
45 | |
|
46 | |
private volatile List transformers; |
47 | 0 | private final Map endpointCache = new HashMap(); |
48 | |
|
49 | |
public DefaultReplyToHandler(List transformers) |
50 | 0 | { |
51 | 0 | this.transformers = transformers; |
52 | 0 | } |
53 | |
|
54 | |
public void processReplyTo(MuleEvent event, MuleMessage returnMessage, Object replyTo) throws MuleException |
55 | |
{ |
56 | 0 | if (logger.isDebugEnabled()) |
57 | |
{ |
58 | 0 | logger.debug("sending reply to: " + returnMessage.getReplyTo()); |
59 | |
} |
60 | |
|
61 | 0 | String replyToEndpoint = replyTo.toString(); |
62 | |
|
63 | |
|
64 | 0 | OutboundEndpoint endpoint = getEndpoint(event, replyToEndpoint); |
65 | |
|
66 | |
|
67 | |
|
68 | 0 | returnMessage.removeProperty(MuleProperties.MULE_REPLY_TO_PROPERTY); |
69 | |
|
70 | |
|
71 | 0 | MuleEvent replyToEvent = new DefaultMuleEvent(returnMessage, endpoint, event.getSession(), false); |
72 | |
|
73 | |
|
74 | |
try |
75 | |
{ |
76 | 0 | endpoint.dispatch(replyToEvent); |
77 | 0 | if (logger.isInfoEnabled()) |
78 | |
{ |
79 | 0 | logger.info("reply to sent: " + endpoint); |
80 | |
} |
81 | 0 | ((AbstractService) event.getService()).getStatistics().incSentReplyToEvent(); |
82 | |
} |
83 | 0 | catch (Exception e) |
84 | |
{ |
85 | 0 | throw new DispatchException( |
86 | |
CoreMessages.failedToDispatchToReplyto(endpoint), |
87 | |
replyToEvent.getMessage(), replyToEvent.getEndpoint(), e); |
88 | 0 | } |
89 | |
|
90 | 0 | } |
91 | |
|
92 | |
protected synchronized OutboundEndpoint getEndpoint(MuleEvent event, String endpointUri) throws MuleException |
93 | |
{ |
94 | 0 | OutboundEndpoint endpoint = (OutboundEndpoint) endpointCache.get(endpointUri); |
95 | 0 | if (endpoint == null) |
96 | |
{ |
97 | 0 | EndpointFactory endpointFactory = RegistryContext.getRegistry().lookupEndpointFactory(); |
98 | 0 | EndpointBuilder endpointBuilder = endpointFactory.getEndpointBuilder(endpointUri); |
99 | 0 | if (transformers == null) |
100 | |
{ |
101 | 0 | endpointBuilder.setTransformers(event.getEndpoint().getResponseTransformers()); |
102 | |
} |
103 | 0 | endpoint = endpointFactory.getOutboundEndpoint(endpointBuilder); |
104 | 0 | endpointCache.put(endpointUri, endpoint); |
105 | |
} |
106 | 0 | return endpoint; |
107 | |
} |
108 | |
|
109 | |
public List getTransformers() |
110 | |
{ |
111 | 0 | return transformers; |
112 | |
} |
113 | |
|
114 | |
public void setTransformers(List transformers) |
115 | |
{ |
116 | 0 | this.transformers = transformers; |
117 | 0 | } |
118 | |
|
119 | |
} |