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.api.MuleEvent; |
15 | |
import org.mule.api.MuleException; |
16 | |
import org.mule.api.MuleMessage; |
17 | |
import org.mule.api.config.MuleProperties; |
18 | |
import org.mule.api.context.WorkManager; |
19 | |
import org.mule.api.endpoint.OutboundEndpoint; |
20 | |
import org.mule.api.service.Service; |
21 | |
import org.mule.api.transformer.Transformer; |
22 | |
import org.mule.api.transport.DispatchException; |
23 | |
import org.mule.api.transport.MessageDispatcher; |
24 | |
import org.mule.service.ServiceAsyncReplyCompositeMessageSource; |
25 | |
|
26 | |
import java.util.List; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 0 | public abstract class AbstractMessageDispatcher extends AbstractConnectable implements MessageDispatcher |
34 | |
{ |
35 | |
|
36 | |
protected List<Transformer> defaultOutboundTransformers; |
37 | |
protected List<Transformer> defaultResponseTransformers; |
38 | |
|
39 | |
public AbstractMessageDispatcher(OutboundEndpoint endpoint) |
40 | |
{ |
41 | 0 | super(endpoint); |
42 | 0 | } |
43 | |
|
44 | |
@Override |
45 | |
protected ConnectableLifecycleManager createLifecycleManager() |
46 | |
{ |
47 | 0 | defaultOutboundTransformers = connector.getDefaultOutboundTransformers(endpoint); |
48 | 0 | defaultResponseTransformers = connector.getDefaultResponseTransformers(endpoint); |
49 | 0 | return new ConnectableLifecycleManager<MessageDispatcher>(getDispatcherName(), this); |
50 | |
} |
51 | |
|
52 | |
protected String getDispatcherName() |
53 | |
{ |
54 | 0 | return getConnector().getName() + ".dispatcher." + System.identityHashCode(this); |
55 | |
} |
56 | |
|
57 | |
|
58 | |
|
59 | |
public MuleEvent process(MuleEvent event) throws MuleException |
60 | |
{ |
61 | 0 | MuleEvent resultEvent = null; |
62 | |
try |
63 | |
{ |
64 | 0 | connect(); |
65 | |
|
66 | 0 | String prop = (String) event.getMessage().getProperty(MuleProperties.MULE_DISABLE_TRANSPORT_TRANSFORMER_PROPERTY); |
67 | 0 | boolean disableTransportTransformer = (prop != null && Boolean.parseBoolean(prop)) || endpoint.isDisableTransportTransformer(); |
68 | |
|
69 | 0 | if (!disableTransportTransformer) |
70 | |
{ |
71 | 0 | applyOutboundTransformers(event); |
72 | |
} |
73 | 0 | if (endpoint.getExchangePattern().hasResponse()) |
74 | |
{ |
75 | 0 | MuleMessage resultMessage = doSend(event); |
76 | 0 | if (resultMessage != null) |
77 | |
{ |
78 | 0 | resultEvent = new DefaultMuleEvent(resultMessage, event); |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
} |
85 | 0 | } |
86 | |
else |
87 | |
{ |
88 | 0 | doDispatch(event); |
89 | |
} |
90 | |
} |
91 | 0 | catch (MuleException muleException) |
92 | |
{ |
93 | 0 | throw muleException; |
94 | |
} |
95 | 0 | catch (Exception e) |
96 | |
{ |
97 | 0 | throw new DispatchException(event, (OutboundEndpoint) endpoint, e); |
98 | 0 | } |
99 | 0 | return resultEvent; |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
@Deprecated |
106 | |
protected boolean returnResponse(MuleEvent event) |
107 | |
{ |
108 | |
|
109 | |
|
110 | 0 | return returnResponse(event, false); |
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
protected boolean returnResponse(MuleEvent event, boolean doSend) |
137 | |
{ |
138 | 0 | boolean remoteSync = false; |
139 | 0 | if (event.getEndpoint().getConnector().isResponseEnabled()) |
140 | |
{ |
141 | 0 | boolean hasResponse = event.getEndpoint().getExchangePattern().hasResponse(); |
142 | 0 | remoteSync = hasResponse || doSend; |
143 | 0 | if (remoteSync) |
144 | |
{ |
145 | |
|
146 | 0 | if (event.getFlowConstruct() != null && event.getFlowConstruct() instanceof Service) |
147 | |
{ |
148 | 0 | ServiceAsyncReplyCompositeMessageSource responseRouters = ((Service) event.getFlowConstruct()).getAsyncReplyMessageSource(); |
149 | 0 | if (responseRouters != null && responseRouters.getEndpoints().size() > 0) |
150 | |
{ |
151 | 0 | remoteSync = false; |
152 | |
} |
153 | |
else |
154 | |
{ |
155 | 0 | remoteSync = true; |
156 | |
} |
157 | |
} |
158 | |
} |
159 | |
} |
160 | 0 | if (!remoteSync) |
161 | |
{ |
162 | 0 | event.getMessage().removeProperty(MuleProperties.MULE_REMOTE_SYNC_PROPERTY); |
163 | |
} |
164 | 0 | return remoteSync; |
165 | |
} |
166 | |
|
167 | |
@Override |
168 | |
protected WorkManager getWorkManager() |
169 | |
{ |
170 | |
try |
171 | |
{ |
172 | 0 | return connector.getDispatcherWorkManager(); |
173 | |
} |
174 | 0 | catch (MuleException e) |
175 | |
{ |
176 | 0 | logger.error(e); |
177 | 0 | return null; |
178 | |
} |
179 | |
} |
180 | |
|
181 | |
@Override |
182 | |
public OutboundEndpoint getEndpoint() |
183 | |
{ |
184 | 0 | return (OutboundEndpoint) super.getEndpoint(); |
185 | |
} |
186 | |
|
187 | |
protected void applyOutboundTransformers(MuleEvent event) throws MuleException |
188 | |
{ |
189 | 0 | event.getMessage().applyTransformers(event, defaultOutboundTransformers); |
190 | 0 | } |
191 | |
|
192 | |
protected void applyResponseTransformers(MuleEvent event) throws MuleException |
193 | |
{ |
194 | 0 | event.getMessage().applyTransformers(event, defaultResponseTransformers); |
195 | 0 | } |
196 | |
|
197 | |
protected abstract void doDispatch(MuleEvent event) throws Exception; |
198 | |
|
199 | |
protected abstract MuleMessage doSend(MuleEvent event) throws Exception; |
200 | |
} |