1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.config.MuleProperties; |
15 | |
import org.mule.config.i18n.CoreMessages; |
16 | |
import org.mule.impl.MuleEvent; |
17 | |
import org.mule.impl.endpoint.MuleEndpoint; |
18 | |
import org.mule.impl.endpoint.MuleEndpointURI; |
19 | |
import org.mule.impl.model.AbstractComponent; |
20 | |
import org.mule.umo.UMOEvent; |
21 | |
import org.mule.umo.UMOException; |
22 | |
import org.mule.umo.UMOMessage; |
23 | |
import org.mule.umo.endpoint.UMOEndpoint; |
24 | |
import org.mule.umo.endpoint.UMOEndpointURI; |
25 | |
import org.mule.umo.provider.DispatchException; |
26 | |
import org.mule.umo.transformer.UMOTransformer; |
27 | |
|
28 | |
import java.util.HashMap; |
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 UMOTransformer transformer; |
47 | 0 | private final Map endpointCache = new HashMap(); |
48 | |
|
49 | |
public DefaultReplyToHandler(UMOTransformer transformer) |
50 | 0 | { |
51 | 0 | this.transformer = transformer; |
52 | 0 | } |
53 | |
|
54 | |
public void processReplyTo(UMOEvent event, UMOMessage returnMessage, Object replyTo) throws UMOException |
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 | UMOEndpoint endpoint = getEndpoint(event, replyToEndpoint); |
65 | |
|
66 | 0 | if (transformer == null) |
67 | |
{ |
68 | 0 | transformer = event.getEndpoint().getResponseTransformer(); |
69 | |
} |
70 | |
|
71 | 0 | if (transformer != null) |
72 | |
{ |
73 | 0 | endpoint.setTransformer(transformer); |
74 | |
} |
75 | |
|
76 | |
|
77 | |
|
78 | 0 | returnMessage.removeProperty(MuleProperties.MULE_REPLY_TO_PROPERTY); |
79 | |
|
80 | |
|
81 | 0 | UMOEvent replyToEvent = new MuleEvent(returnMessage, endpoint, event.getSession(), false); |
82 | |
|
83 | |
|
84 | |
try |
85 | |
{ |
86 | 0 | endpoint.dispatch(replyToEvent); |
87 | 0 | if (logger.isInfoEnabled()) |
88 | |
{ |
89 | 0 | logger.info("reply to sent: " + endpoint); |
90 | |
} |
91 | 0 | ((AbstractComponent) event.getComponent()).getStatistics().incSentReplyToEvent(); |
92 | |
} |
93 | 0 | catch (Exception e) |
94 | |
{ |
95 | 0 | throw new DispatchException( |
96 | |
CoreMessages.failedToDispatchToReplyto(endpoint), |
97 | |
replyToEvent.getMessage(), replyToEvent.getEndpoint(), e); |
98 | 0 | } |
99 | |
|
100 | 0 | } |
101 | |
|
102 | |
protected synchronized UMOEndpoint getEndpoint(UMOEvent event, String endpointUri) throws UMOException |
103 | |
{ |
104 | 0 | UMOEndpoint endpoint = (UMOEndpoint) endpointCache.get(endpointUri); |
105 | 0 | if (endpoint == null) |
106 | |
{ |
107 | 0 | endpoint = MuleManager.getInstance().lookupEndpoint(endpointUri); |
108 | 0 | if (endpoint == null) |
109 | |
{ |
110 | 0 | UMOEndpointURI ep = new MuleEndpointURI(endpointUri); |
111 | 0 | endpoint = MuleEndpoint.getOrCreateEndpointForUri(ep, UMOEndpoint.ENDPOINT_TYPE_SENDER); |
112 | 0 | endpointCache.put(endpointUri, endpoint); |
113 | |
} |
114 | |
} |
115 | 0 | return endpoint; |
116 | |
} |
117 | |
|
118 | |
public UMOTransformer getTransformer() |
119 | |
{ |
120 | 0 | return transformer; |
121 | |
} |
122 | |
|
123 | |
public void setTransformer(UMOTransformer transformer) |
124 | |
{ |
125 | 0 | this.transformer = transformer; |
126 | 0 | } |
127 | |
|
128 | |
} |