1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.client; |
12 | |
|
13 | |
import org.mule.DefaultMuleEvent; |
14 | |
import org.mule.DefaultMuleMessage; |
15 | |
import org.mule.MessageExchangePattern; |
16 | |
import org.mule.api.MuleContext; |
17 | |
import org.mule.api.MuleEvent; |
18 | |
import org.mule.api.MuleException; |
19 | |
import org.mule.api.MuleMessage; |
20 | |
import org.mule.api.client.LocalMuleClient; |
21 | |
import org.mule.api.construct.FlowConstruct; |
22 | |
import org.mule.api.endpoint.EndpointBuilder; |
23 | |
import org.mule.api.endpoint.InboundEndpoint; |
24 | |
import org.mule.api.endpoint.OutboundEndpoint; |
25 | |
import org.mule.api.exception.MessagingExceptionHandler; |
26 | |
import org.mule.api.lifecycle.LifecycleState; |
27 | |
import org.mule.api.routing.MessageInfoMapping; |
28 | |
import org.mule.api.transport.ReceiveException; |
29 | |
import org.mule.exception.DefaultServiceExceptionStrategy; |
30 | |
import org.mule.management.stats.FlowConstructStatistics; |
31 | |
import org.mule.session.DefaultMuleSession; |
32 | |
|
33 | |
import java.util.Map; |
34 | |
|
35 | |
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; |
36 | |
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentMap; |
37 | |
|
38 | |
public class DefaultLocalMuleClient implements LocalMuleClient |
39 | |
{ |
40 | |
|
41 | |
protected MuleContext muleContext; |
42 | 0 | private ConcurrentMap inboundEndpointCache = new ConcurrentHashMap(); |
43 | 0 | private ConcurrentMap outboundEndpointCache = new ConcurrentHashMap(); |
44 | |
|
45 | |
public DefaultLocalMuleClient(MuleContext muleContext) |
46 | 0 | { |
47 | 0 | this.muleContext = muleContext; |
48 | 0 | } |
49 | |
|
50 | |
public MuleMessage process(OutboundEndpoint endpoint, |
51 | |
Object payload, |
52 | |
Map<String, Object> messageProperties) throws MuleException |
53 | |
{ |
54 | 0 | return process(endpoint, new DefaultMuleMessage(payload, messageProperties, muleContext)); |
55 | |
|
56 | |
} |
57 | |
|
58 | |
public MuleMessage process(OutboundEndpoint endpoint, MuleMessage message) throws MuleException |
59 | |
{ |
60 | 0 | return returnMessage(endpoint.process(createMuleEvent(message, endpoint))); |
61 | |
} |
62 | |
|
63 | |
public MuleMessage request(InboundEndpoint endpoint, long timeout) throws MuleException |
64 | |
{ |
65 | |
try |
66 | |
{ |
67 | 0 | return endpoint.request(timeout); |
68 | |
} |
69 | 0 | catch (Exception e) |
70 | |
{ |
71 | 0 | throw new ReceiveException(endpoint, timeout, e); |
72 | |
} |
73 | |
} |
74 | |
|
75 | |
public void dispatch(String url, Object payload, Map<String, Object> messageProperties) |
76 | |
throws MuleException |
77 | |
{ |
78 | 0 | dispatch(url, new DefaultMuleMessage(payload, messageProperties, muleContext)); |
79 | 0 | } |
80 | |
|
81 | |
public MuleMessage send(String url, Object payload, Map<String, Object> messageProperties) |
82 | |
throws MuleException |
83 | |
{ |
84 | 0 | return send(url, new DefaultMuleMessage(payload, messageProperties, muleContext)); |
85 | |
} |
86 | |
|
87 | |
public MuleMessage send(String url, MuleMessage message) throws MuleException |
88 | |
{ |
89 | 0 | OutboundEndpoint endpoint = getOutboundEndpoint(url, MessageExchangePattern.REQUEST_RESPONSE, null); |
90 | 0 | return returnMessage(endpoint.process(createMuleEvent(message, endpoint))); |
91 | |
} |
92 | |
|
93 | |
public MuleMessage send(String url, Object payload, Map<String, Object> messageProperties, long timeout) |
94 | |
throws MuleException |
95 | |
{ |
96 | 0 | return send(url, new DefaultMuleMessage(payload, messageProperties, muleContext), timeout); |
97 | |
|
98 | |
} |
99 | |
|
100 | |
public MuleMessage send(String url, MuleMessage message, long timeout) throws MuleException |
101 | |
{ |
102 | 0 | OutboundEndpoint endpoint = getOutboundEndpoint(url, MessageExchangePattern.REQUEST_RESPONSE, timeout); |
103 | 0 | return returnMessage(endpoint.process(createMuleEvent(message, endpoint))); |
104 | |
} |
105 | |
|
106 | |
public void dispatch(String url, MuleMessage message) throws MuleException |
107 | |
{ |
108 | 0 | OutboundEndpoint endpoint = getOutboundEndpoint(url, MessageExchangePattern.ONE_WAY, null); |
109 | 0 | endpoint.process(createMuleEvent(message, endpoint)); |
110 | 0 | } |
111 | |
|
112 | |
public MuleMessage request(String url, long timeout) throws MuleException |
113 | |
{ |
114 | 0 | InboundEndpoint endpoint = getInboundEndpoint(url, MessageExchangePattern.ONE_WAY); |
115 | |
try |
116 | |
{ |
117 | 0 | return endpoint.request(timeout); |
118 | |
} |
119 | 0 | catch (Exception e) |
120 | |
{ |
121 | 0 | throw new ReceiveException(endpoint, timeout, e); |
122 | |
} |
123 | |
} |
124 | |
|
125 | |
public MuleMessage process(String uri, |
126 | |
MessageExchangePattern mep, |
127 | |
Object payload, |
128 | |
Map<String, Object> messageProperties) throws MuleException |
129 | |
{ |
130 | 0 | return process(uri, mep, new DefaultMuleMessage(payload, messageProperties, muleContext)); |
131 | |
} |
132 | |
|
133 | |
public MuleMessage process(String uri, MessageExchangePattern mep, MuleMessage message) |
134 | |
throws MuleException |
135 | |
{ |
136 | 0 | OutboundEndpoint endpoint = getOutboundEndpoint(uri, mep, null); |
137 | 0 | return returnMessage(endpoint.process(createMuleEvent(message, endpoint))); |
138 | |
} |
139 | |
|
140 | |
protected MuleEvent createMuleEvent(MuleMessage message, OutboundEndpoint endpoint) |
141 | |
{ |
142 | 0 | DefaultMuleSession session = new DefaultMuleSession(new MuleClientFlowConstruct(muleContext), muleContext); |
143 | 0 | return new DefaultMuleEvent(message, endpoint, session); |
144 | |
} |
145 | |
|
146 | |
protected MuleMessage returnMessage(MuleEvent event) |
147 | |
{ |
148 | 0 | if (event != null) |
149 | |
{ |
150 | 0 | return event.getMessage(); |
151 | |
} |
152 | |
else |
153 | |
{ |
154 | 0 | return null; |
155 | |
} |
156 | |
} |
157 | |
|
158 | |
protected OutboundEndpoint getOutboundEndpoint(String uri, |
159 | |
MessageExchangePattern mep, |
160 | |
Long responseTimeout) throws MuleException |
161 | |
{ |
162 | 0 | OutboundEndpoint endpoint = (OutboundEndpoint) outboundEndpointCache.get(uri + ":" + mep.toString() |
163 | |
+ ":" + responseTimeout); |
164 | 0 | if (endpoint == null) |
165 | |
{ |
166 | 0 | EndpointBuilder endpointBuilder = muleContext.getRegistry() |
167 | |
.lookupEndpointFactory() |
168 | |
.getEndpointBuilder(uri); |
169 | 0 | endpointBuilder.setExchangePattern(mep); |
170 | 0 | if (responseTimeout != null && responseTimeout > 0) |
171 | |
{ |
172 | 0 | endpointBuilder.setResponseTimeout(responseTimeout.intValue()); |
173 | |
} |
174 | 0 | endpoint = muleContext.getRegistry().lookupEndpointFactory().getOutboundEndpoint(endpointBuilder); |
175 | 0 | OutboundEndpoint concurrentlyAddedEndpoint = (OutboundEndpoint) outboundEndpointCache.putIfAbsent( |
176 | |
uri + ":" + mep.toString() + ":" + responseTimeout, endpoint); |
177 | 0 | if (concurrentlyAddedEndpoint != null) |
178 | |
{ |
179 | 0 | return concurrentlyAddedEndpoint; |
180 | |
} |
181 | |
} |
182 | 0 | return endpoint; |
183 | |
} |
184 | |
|
185 | |
protected InboundEndpoint getInboundEndpoint(String uri, MessageExchangePattern mep) throws MuleException |
186 | |
{ |
187 | 0 | InboundEndpoint endpoint = (InboundEndpoint) inboundEndpointCache.get(uri + ":" + mep.toString()); |
188 | 0 | if (endpoint == null) |
189 | |
{ |
190 | 0 | EndpointBuilder endpointBuilder = muleContext.getRegistry() |
191 | |
.lookupEndpointFactory() |
192 | |
.getEndpointBuilder(uri); |
193 | 0 | endpointBuilder.setExchangePattern(mep); |
194 | 0 | endpoint = muleContext.getRegistry().lookupEndpointFactory().getInboundEndpoint(endpointBuilder); |
195 | 0 | InboundEndpoint concurrentlyAddedEndpoint = (InboundEndpoint) inboundEndpointCache.putIfAbsent( |
196 | |
uri + ":" + mep.toString(), endpoint); |
197 | 0 | if (concurrentlyAddedEndpoint != null) |
198 | |
{ |
199 | 0 | return concurrentlyAddedEndpoint; |
200 | |
} |
201 | |
} |
202 | 0 | return endpoint; |
203 | |
} |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
static public class MuleClientFlowConstruct implements FlowConstruct |
209 | |
{ |
210 | |
MuleContext muleContext; |
211 | |
|
212 | |
public MuleClientFlowConstruct(MuleContext muleContext) |
213 | 0 | { |
214 | 0 | this.muleContext = muleContext; |
215 | 0 | } |
216 | |
|
217 | |
public String getName() |
218 | |
{ |
219 | 0 | return "MuleClient"; |
220 | |
} |
221 | |
|
222 | |
public MessagingExceptionHandler getExceptionListener() |
223 | |
{ |
224 | 0 | return new DefaultServiceExceptionStrategy(muleContext); |
225 | |
} |
226 | |
|
227 | |
public LifecycleState getLifecycleState() |
228 | |
{ |
229 | 0 | return null; |
230 | |
} |
231 | |
|
232 | |
public FlowConstructStatistics getStatistics() |
233 | |
{ |
234 | 0 | return null; |
235 | |
} |
236 | |
|
237 | |
public MuleContext getMuleContext() |
238 | |
{ |
239 | 0 | return muleContext; |
240 | |
} |
241 | |
|
242 | |
public MessageInfoMapping getMessageInfoMapping() |
243 | |
{ |
244 | 0 | return null; |
245 | |
} |
246 | |
}; |
247 | |
} |