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 private ConcurrentMap inboundEndpointCache = new ConcurrentHashMap();
43 private ConcurrentMap outboundEndpointCache = new ConcurrentHashMap();
44
45 public DefaultLocalMuleClient(MuleContext muleContext)
46 {
47 this.muleContext = muleContext;
48 }
49
50 public MuleMessage process(OutboundEndpoint endpoint,
51 Object payload,
52 Map<String, Object> messageProperties) throws MuleException
53 {
54 return process(endpoint, new DefaultMuleMessage(payload, messageProperties, muleContext));
55
56 }
57
58 public MuleMessage process(OutboundEndpoint endpoint, MuleMessage message) throws MuleException
59 {
60 return returnMessage(endpoint.process(createMuleEvent(message, endpoint)));
61 }
62
63 public MuleMessage request(InboundEndpoint endpoint, long timeout) throws MuleException
64 {
65 try
66 {
67 return endpoint.request(timeout);
68 }
69 catch (Exception e)
70 {
71 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 dispatch(url, new DefaultMuleMessage(payload, messageProperties, muleContext));
79 }
80
81 public MuleMessage send(String url, Object payload, Map<String, Object> messageProperties)
82 throws MuleException
83 {
84 return send(url, new DefaultMuleMessage(payload, messageProperties, muleContext));
85 }
86
87 public MuleMessage send(String url, MuleMessage message) throws MuleException
88 {
89 OutboundEndpoint endpoint = getOutboundEndpoint(url, MessageExchangePattern.REQUEST_RESPONSE, null);
90 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 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 OutboundEndpoint endpoint = getOutboundEndpoint(url, MessageExchangePattern.REQUEST_RESPONSE, timeout);
103 return returnMessage(endpoint.process(createMuleEvent(message, endpoint)));
104 }
105
106 public void dispatch(String url, MuleMessage message) throws MuleException
107 {
108 OutboundEndpoint endpoint = getOutboundEndpoint(url, MessageExchangePattern.ONE_WAY, null);
109 endpoint.process(createMuleEvent(message, endpoint));
110 }
111
112 public MuleMessage request(String url, long timeout) throws MuleException
113 {
114 InboundEndpoint endpoint = getInboundEndpoint(url, MessageExchangePattern.ONE_WAY);
115 try
116 {
117 return endpoint.request(timeout);
118 }
119 catch (Exception e)
120 {
121 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 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 OutboundEndpoint endpoint = getOutboundEndpoint(uri, mep, null);
137 return returnMessage(endpoint.process(createMuleEvent(message, endpoint)));
138 }
139
140 protected MuleEvent createMuleEvent(MuleMessage message, OutboundEndpoint endpoint)
141 {
142 DefaultMuleSession session = new DefaultMuleSession(new MuleClientFlowConstruct(muleContext), muleContext);
143 return new DefaultMuleEvent(message, endpoint, session);
144 }
145
146 protected MuleMessage returnMessage(MuleEvent event)
147 {
148 if (event != null)
149 {
150 return event.getMessage();
151 }
152 else
153 {
154 return null;
155 }
156 }
157
158 protected OutboundEndpoint getOutboundEndpoint(String uri,
159 MessageExchangePattern mep,
160 Long responseTimeout) throws MuleException
161 {
162 OutboundEndpoint endpoint = (OutboundEndpoint) outboundEndpointCache.get(uri + ":" + mep.toString()
163 + ":" + responseTimeout);
164 if (endpoint == null)
165 {
166 EndpointBuilder endpointBuilder = muleContext.getRegistry()
167 .lookupEndpointFactory()
168 .getEndpointBuilder(uri);
169 endpointBuilder.setExchangePattern(mep);
170 if (responseTimeout != null && responseTimeout > 0)
171 {
172 endpointBuilder.setResponseTimeout(responseTimeout.intValue());
173 }
174 endpoint = muleContext.getRegistry().lookupEndpointFactory().getOutboundEndpoint(endpointBuilder);
175 OutboundEndpoint concurrentlyAddedEndpoint = (OutboundEndpoint) outboundEndpointCache.putIfAbsent(
176 uri + ":" + mep.toString() + ":" + responseTimeout, endpoint);
177 if (concurrentlyAddedEndpoint != null)
178 {
179 return concurrentlyAddedEndpoint;
180 }
181 }
182 return endpoint;
183 }
184
185 protected InboundEndpoint getInboundEndpoint(String uri, MessageExchangePattern mep) throws MuleException
186 {
187 InboundEndpoint endpoint = (InboundEndpoint) inboundEndpointCache.get(uri + ":" + mep.toString());
188 if (endpoint == null)
189 {
190 EndpointBuilder endpointBuilder = muleContext.getRegistry()
191 .lookupEndpointFactory()
192 .getEndpointBuilder(uri);
193 endpointBuilder.setExchangePattern(mep);
194 endpoint = muleContext.getRegistry().lookupEndpointFactory().getInboundEndpoint(endpointBuilder);
195 InboundEndpoint concurrentlyAddedEndpoint = (InboundEndpoint) inboundEndpointCache.putIfAbsent(
196 uri + ":" + mep.toString(), endpoint);
197 if (concurrentlyAddedEndpoint != null)
198 {
199 return concurrentlyAddedEndpoint;
200 }
201 }
202 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 {
214 this.muleContext = muleContext;
215 }
216
217 public String getName()
218 {
219 return "MuleClient";
220 }
221
222 public MessagingExceptionHandler getExceptionListener()
223 {
224 return new DefaultServiceExceptionStrategy(muleContext);
225 }
226
227 public LifecycleState getLifecycleState()
228 {
229 return null;
230 }
231
232 public FlowConstructStatistics getStatistics()
233 {
234 return null;
235 }
236
237 public MuleContext getMuleContext()
238 {
239 return muleContext;
240 }
241
242 public MessageInfoMapping getMessageInfoMapping()
243 {
244 return null;
245 }
246 };
247 }