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.DefaultMuleMessage; |
15 | |
import org.mule.api.MuleContext; |
16 | |
import org.mule.api.MuleEvent; |
17 | |
import org.mule.api.MuleException; |
18 | |
import org.mule.api.MuleMessage; |
19 | |
import org.mule.api.config.MuleProperties; |
20 | |
import org.mule.api.endpoint.EndpointBuilder; |
21 | |
import org.mule.api.endpoint.EndpointFactory; |
22 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
23 | |
import org.mule.api.endpoint.OutboundEndpoint; |
24 | |
import org.mule.api.transformer.Transformer; |
25 | |
import org.mule.api.transport.DispatchException; |
26 | |
import org.mule.api.transport.ReplyToHandler; |
27 | |
import org.mule.config.i18n.CoreMessages; |
28 | |
import org.mule.service.AbstractService; |
29 | |
|
30 | |
import java.util.HashMap; |
31 | |
import java.util.List; |
32 | |
import java.util.Map; |
33 | |
|
34 | |
import org.apache.commons.logging.Log; |
35 | |
import org.apache.commons.logging.LogFactory; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public class DefaultReplyToHandler implements ReplyToHandler |
43 | |
{ |
44 | |
|
45 | |
|
46 | |
|
47 | 0 | protected transient final Log logger = LogFactory.getLog(getClass()); |
48 | |
|
49 | |
private volatile List<Transformer> transformers; |
50 | 0 | private final Map<String, ImmutableEndpoint> endpointCache = new HashMap<String, ImmutableEndpoint>(); |
51 | |
protected MuleContext muleContext; |
52 | |
|
53 | |
public DefaultReplyToHandler(List<Transformer> transformers, MuleContext muleContext) |
54 | 0 | { |
55 | 0 | this.transformers = transformers; |
56 | 0 | this.muleContext = muleContext; |
57 | 0 | } |
58 | |
|
59 | |
public void processReplyTo(MuleEvent event, MuleMessage returnMessage, Object replyTo) throws MuleException |
60 | |
{ |
61 | 0 | if (logger.isDebugEnabled()) |
62 | |
{ |
63 | 0 | logger.debug("sending reply to: " + replyTo); |
64 | |
} |
65 | 0 | String replyToEndpoint = replyTo.toString(); |
66 | |
|
67 | |
|
68 | 0 | OutboundEndpoint endpoint = getEndpoint(event, replyToEndpoint); |
69 | |
|
70 | |
|
71 | |
|
72 | 0 | returnMessage.removeProperty(MuleProperties.MULE_REPLY_TO_PROPERTY); |
73 | |
|
74 | |
|
75 | |
|
76 | 0 | returnMessage.removeProperty(MuleProperties.MULE_REMOTE_SYNC_PROPERTY); |
77 | |
|
78 | |
|
79 | 0 | returnMessage = new DefaultMuleMessage(returnMessage.getPayload(), returnMessage, muleContext); |
80 | |
|
81 | |
|
82 | 0 | MuleEvent replyToEvent = new DefaultMuleEvent(returnMessage, endpoint, event.getSession()); |
83 | |
|
84 | |
|
85 | 0 | List<String> responseProperties = endpoint.getResponseProperties(); |
86 | 0 | for (String propertyName : responseProperties) |
87 | |
{ |
88 | 0 | Object propertyValue = event.getMessage().getInboundProperty(propertyName); |
89 | 0 | if (propertyValue != null) |
90 | |
{ |
91 | 0 | replyToEvent.getMessage().setOutboundProperty(propertyName, propertyValue); |
92 | |
} |
93 | 0 | } |
94 | |
|
95 | |
|
96 | |
try |
97 | |
{ |
98 | 0 | ((AbstractService) event.getFlowConstruct()).getStatistics().incSentReplyToEvent(); |
99 | 0 | endpoint.process(replyToEvent); |
100 | 0 | if (logger.isInfoEnabled()) |
101 | |
{ |
102 | 0 | logger.info("reply to sent: " + endpoint); |
103 | |
} |
104 | |
} |
105 | 0 | catch (Exception e) |
106 | |
{ |
107 | 0 | throw new DispatchException(CoreMessages.failedToDispatchToReplyto(endpoint), |
108 | |
replyToEvent, endpoint, e); |
109 | 0 | } |
110 | |
|
111 | 0 | } |
112 | |
|
113 | |
protected synchronized OutboundEndpoint getEndpoint(MuleEvent event, String endpointUri) throws MuleException |
114 | |
{ |
115 | 0 | OutboundEndpoint endpoint = (OutboundEndpoint) endpointCache.get(endpointUri); |
116 | 0 | if (endpoint == null) |
117 | |
{ |
118 | 0 | EndpointFactory endpointFactory = muleContext.getRegistry().lookupEndpointFactory(); |
119 | 0 | EndpointBuilder endpointBuilder = endpointFactory.getEndpointBuilder(endpointUri); |
120 | 0 | if (transformers == null) |
121 | |
{ |
122 | 0 | endpointBuilder.setTransformers(event.getEndpoint().getResponseTransformers()); |
123 | |
} |
124 | 0 | endpoint = endpointFactory.getOutboundEndpoint(endpointBuilder); |
125 | 0 | endpointCache.put(endpointUri, endpoint); |
126 | |
} |
127 | 0 | return endpoint; |
128 | |
} |
129 | |
|
130 | |
public List<Transformer> getTransformers() |
131 | |
{ |
132 | 0 | return transformers; |
133 | |
} |
134 | |
|
135 | |
public void setTransformers(List<Transformer> transformers) |
136 | |
{ |
137 | 0 | this.transformers = transformers; |
138 | 0 | } |
139 | |
} |