1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.client; |
8 | |
|
9 | |
import org.mule.DefaultMuleEvent; |
10 | |
import org.mule.DefaultMuleMessage; |
11 | |
import org.mule.MessageExchangePattern; |
12 | |
import org.mule.api.MuleContext; |
13 | |
import org.mule.api.MuleEvent; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.api.MuleMessage; |
16 | |
import org.mule.api.client.LocalMuleClient; |
17 | |
import org.mule.api.construct.FlowConstruct; |
18 | |
import org.mule.api.endpoint.EndpointCache; |
19 | |
import org.mule.api.endpoint.InboundEndpoint; |
20 | |
import org.mule.api.endpoint.OutboundEndpoint; |
21 | |
import org.mule.api.exception.MessagingExceptionHandler; |
22 | |
import org.mule.api.lifecycle.LifecycleState; |
23 | |
import org.mule.api.processor.MessageProcessorChain; |
24 | |
import org.mule.api.routing.MessageInfoMapping; |
25 | |
import org.mule.api.transport.ReceiveException; |
26 | |
import org.mule.endpoint.SimpleEndpointCache; |
27 | |
import org.mule.exception.DefaultServiceExceptionStrategy; |
28 | |
import org.mule.management.stats.FlowConstructStatistics; |
29 | |
import org.mule.session.DefaultMuleSession; |
30 | |
|
31 | |
import java.util.Map; |
32 | |
|
33 | |
public class DefaultLocalMuleClient implements LocalMuleClient |
34 | |
{ |
35 | |
protected final MuleContext muleContext; |
36 | |
private final EndpointCache endpointCache; |
37 | |
|
38 | |
public DefaultLocalMuleClient(MuleContext muleContext) |
39 | 0 | { |
40 | 0 | this.muleContext = muleContext; |
41 | 0 | this.endpointCache = new SimpleEndpointCache(muleContext); |
42 | 0 | } |
43 | |
|
44 | |
public MuleMessage process(OutboundEndpoint endpoint, |
45 | |
Object payload, |
46 | |
Map<String, Object> messageProperties) throws MuleException |
47 | |
{ |
48 | 0 | return process(endpoint, new DefaultMuleMessage(payload, messageProperties, muleContext)); |
49 | |
|
50 | |
} |
51 | |
|
52 | |
public MuleMessage process(OutboundEndpoint endpoint, MuleMessage message) throws MuleException |
53 | |
{ |
54 | 0 | return returnMessage(endpoint.process(createMuleEvent(message, endpoint))); |
55 | |
} |
56 | |
|
57 | |
public MuleMessage request(InboundEndpoint endpoint, long timeout) throws MuleException |
58 | |
{ |
59 | |
try |
60 | |
{ |
61 | 0 | return endpoint.request(timeout); |
62 | |
} |
63 | 0 | catch (Exception e) |
64 | |
{ |
65 | 0 | throw new ReceiveException(endpoint, timeout, e); |
66 | |
} |
67 | |
} |
68 | |
|
69 | |
public void dispatch(String url, Object payload, Map<String, Object> messageProperties) |
70 | |
throws MuleException |
71 | |
{ |
72 | 0 | dispatch(url, new DefaultMuleMessage(payload, messageProperties, muleContext)); |
73 | 0 | } |
74 | |
|
75 | |
public MuleMessage send(String url, Object payload, Map<String, Object> messageProperties) |
76 | |
throws MuleException |
77 | |
{ |
78 | 0 | return send(url, new DefaultMuleMessage(payload, messageProperties, muleContext)); |
79 | |
} |
80 | |
|
81 | |
public MuleMessage send(String url, MuleMessage message) throws MuleException |
82 | |
{ |
83 | 0 | OutboundEndpoint endpoint = endpointCache.getOutboundEndpoint(url, MessageExchangePattern.REQUEST_RESPONSE, null); |
84 | 0 | return returnMessage(endpoint.process(createMuleEvent(message, endpoint))); |
85 | |
} |
86 | |
|
87 | |
public MuleMessage send(String url, Object payload, Map<String, Object> messageProperties, long timeout) |
88 | |
throws MuleException |
89 | |
{ |
90 | 0 | return send(url, new DefaultMuleMessage(payload, messageProperties, muleContext), timeout); |
91 | |
|
92 | |
} |
93 | |
|
94 | |
public MuleMessage send(String url, MuleMessage message, long timeout) throws MuleException |
95 | |
{ |
96 | 0 | OutboundEndpoint endpoint = endpointCache.getOutboundEndpoint(url, MessageExchangePattern.REQUEST_RESPONSE, timeout); |
97 | 0 | return returnMessage(endpoint.process(createMuleEvent(message, endpoint))); |
98 | |
} |
99 | |
|
100 | |
public void dispatch(String url, MuleMessage message) throws MuleException |
101 | |
{ |
102 | 0 | OutboundEndpoint endpoint = endpointCache.getOutboundEndpoint(url, MessageExchangePattern.ONE_WAY, null); |
103 | 0 | endpoint.process(createMuleEvent(message, endpoint)); |
104 | 0 | } |
105 | |
|
106 | |
public MuleMessage request(String url, long timeout) throws MuleException |
107 | |
{ |
108 | 0 | InboundEndpoint endpoint = endpointCache.getInboundEndpoint(url, MessageExchangePattern.ONE_WAY); |
109 | |
try |
110 | |
{ |
111 | 0 | return endpoint.request(timeout); |
112 | |
} |
113 | 0 | catch (Exception e) |
114 | |
{ |
115 | 0 | throw new ReceiveException(endpoint, timeout, e); |
116 | |
} |
117 | |
} |
118 | |
|
119 | |
public MuleMessage process(String uri, |
120 | |
MessageExchangePattern mep, |
121 | |
Object payload, |
122 | |
Map<String, Object> messageProperties) throws MuleException |
123 | |
{ |
124 | 0 | return process(uri, mep, new DefaultMuleMessage(payload, messageProperties, muleContext)); |
125 | |
} |
126 | |
|
127 | |
public MuleMessage process(String uri, MessageExchangePattern mep, MuleMessage message) |
128 | |
throws MuleException |
129 | |
{ |
130 | 0 | OutboundEndpoint endpoint = endpointCache.getOutboundEndpoint(uri, mep, null); |
131 | 0 | return returnMessage(endpoint.process(createMuleEvent(message, endpoint))); |
132 | |
} |
133 | |
|
134 | |
protected MuleEvent createMuleEvent(MuleMessage message, OutboundEndpoint endpoint) |
135 | |
{ |
136 | 0 | DefaultMuleSession session = new DefaultMuleSession(new MuleClientFlowConstruct(muleContext), muleContext); |
137 | 0 | return new DefaultMuleEvent(message, endpoint, session); |
138 | |
} |
139 | |
|
140 | |
protected MuleMessage returnMessage(MuleEvent event) |
141 | |
{ |
142 | 0 | if (event != null) |
143 | |
{ |
144 | 0 | return event.getMessage(); |
145 | |
} |
146 | |
else |
147 | |
{ |
148 | 0 | return null; |
149 | |
} |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
static public class MuleClientFlowConstruct implements FlowConstruct |
156 | |
{ |
157 | |
MuleContext muleContext; |
158 | |
|
159 | |
public MuleClientFlowConstruct(MuleContext muleContext) |
160 | 0 | { |
161 | 0 | this.muleContext = muleContext; |
162 | 0 | } |
163 | |
|
164 | |
public String getName() |
165 | |
{ |
166 | 0 | return "MuleClient"; |
167 | |
} |
168 | |
|
169 | |
public MessagingExceptionHandler getExceptionListener() |
170 | |
{ |
171 | 0 | return new DefaultServiceExceptionStrategy(muleContext); |
172 | |
} |
173 | |
|
174 | |
public LifecycleState getLifecycleState() |
175 | |
{ |
176 | 0 | return null; |
177 | |
} |
178 | |
|
179 | |
public FlowConstructStatistics getStatistics() |
180 | |
{ |
181 | 0 | return null; |
182 | |
} |
183 | |
|
184 | |
public MuleContext getMuleContext() |
185 | |
{ |
186 | 0 | return muleContext; |
187 | |
} |
188 | |
|
189 | |
public MessageInfoMapping getMessageInfoMapping() |
190 | |
{ |
191 | 0 | return null; |
192 | |
} |
193 | |
|
194 | |
public MessageProcessorChain getMessageProcessorChain() |
195 | |
{ |
196 | 0 | return null; |
197 | |
} |
198 | |
}; |
199 | |
} |