1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.jca; |
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.MuleSession; |
21 | |
import org.mule.api.config.MuleProperties; |
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.transport.DispatchException; |
26 | |
import org.mule.api.transport.ReceiveException; |
27 | |
import org.mule.module.client.i18n.ClientMessages; |
28 | |
import org.mule.module.jca.i18n.JcaMessages; |
29 | |
import org.mule.security.MuleCredentials; |
30 | |
import org.mule.session.DefaultMuleSession; |
31 | |
import org.mule.transport.AbstractConnector; |
32 | |
|
33 | |
import java.util.Map; |
34 | |
|
35 | |
import javax.resource.ResourceException; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class DefaultMuleConnection implements MuleConnection |
41 | |
{ |
42 | |
private final MuleCredentials credentials; |
43 | |
private final MuleContext muleContext; |
44 | |
private MuleManagedConnection managedConnection; |
45 | |
|
46 | |
public DefaultMuleConnection(MuleManagedConnection managedConnection, |
47 | |
MuleContext muleContext, |
48 | |
MuleCredentials credentials) |
49 | 0 | { |
50 | 0 | this.muleContext = muleContext; |
51 | 0 | this.credentials = credentials; |
52 | 0 | this.managedConnection = managedConnection; |
53 | 0 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public void dispatch(String url, Object payload, Map messageProperties) throws MuleException |
68 | |
{ |
69 | 0 | MuleMessage message = new DefaultMuleMessage(payload, messageProperties, muleContext); |
70 | 0 | OutboundEndpoint endpoint = muleContext.getRegistry().lookupEndpointFactory().getOutboundEndpoint(url); |
71 | 0 | MuleEvent event = getEvent(message,endpoint); |
72 | |
try |
73 | |
{ |
74 | 0 | endpoint.process(event); |
75 | |
} |
76 | 0 | catch (MuleException e) |
77 | |
{ |
78 | 0 | throw e; |
79 | |
} |
80 | 0 | catch (Exception e) |
81 | |
{ |
82 | 0 | throw new DispatchException(ClientMessages.failedToDispatchClientEvent(), event, |
83 | |
endpoint, e); |
84 | 0 | } |
85 | 0 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public MuleMessage send(String url, Object payload, Map messageProperties) throws MuleException |
101 | |
{ |
102 | 0 | MuleMessage message = new DefaultMuleMessage(payload, messageProperties, muleContext); |
103 | 0 | OutboundEndpoint endpoint = getOutboundEndpoint(url, MessageExchangePattern.REQUEST_RESPONSE); |
104 | 0 | MuleEvent event = getEvent(message, endpoint); |
105 | |
|
106 | |
try |
107 | |
{ |
108 | 0 | MuleEvent resultEvent = endpoint.process(event); |
109 | 0 | if (resultEvent != null) |
110 | |
{ |
111 | 0 | return resultEvent.getMessage(); |
112 | |
} |
113 | |
else |
114 | |
{ |
115 | 0 | return null; |
116 | |
} |
117 | |
} |
118 | 0 | catch (MuleException e) |
119 | |
{ |
120 | 0 | throw e; |
121 | |
} |
122 | 0 | catch (Exception e) |
123 | |
{ |
124 | 0 | throw new DispatchException(ClientMessages.failedToDispatchClientEvent(), event, |
125 | |
endpoint, e); |
126 | |
} |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
public MuleMessage request(String url, long timeout) throws MuleException |
141 | |
{ |
142 | 0 | InboundEndpoint endpoint = muleContext.getRegistry().lookupEndpointFactory().getInboundEndpoint(url); |
143 | |
|
144 | |
try |
145 | |
{ |
146 | 0 | return endpoint.request(timeout); |
147 | |
} |
148 | 0 | catch (Exception e) |
149 | |
{ |
150 | 0 | throw new ReceiveException(endpoint, timeout, e); |
151 | |
} |
152 | |
} |
153 | |
|
154 | |
protected OutboundEndpoint getOutboundEndpoint(String uri, MessageExchangePattern exchangePattern) |
155 | |
throws MuleException |
156 | |
{ |
157 | 0 | EndpointBuilder endpointBuilder = muleContext.getRegistry() |
158 | |
.lookupEndpointFactory() |
159 | |
.getEndpointBuilder(uri); |
160 | 0 | endpointBuilder.setExchangePattern(exchangePattern); |
161 | 0 | return muleContext.getRegistry().lookupEndpointFactory().getOutboundEndpoint(endpointBuilder); |
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
protected MuleEvent getEvent(MuleMessage message, OutboundEndpoint endpoint) |
174 | |
throws MuleException |
175 | |
{ |
176 | 0 | MuleSession session = new DefaultMuleSession(message, ((AbstractConnector)endpoint.getConnector()).getSessionHandler(), muleContext); |
177 | |
|
178 | 0 | if (credentials != null) |
179 | |
{ |
180 | 0 | message.setOutboundProperty(MuleProperties.MULE_USER_PROPERTY, "Plain " + credentials.getToken()); |
181 | |
} |
182 | |
|
183 | 0 | return new DefaultMuleEvent(message, endpoint, session); |
184 | |
} |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public MuleManagedConnection getManagedConnection() |
194 | |
{ |
195 | 0 | return managedConnection; |
196 | |
} |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
public void close() throws ResourceException |
202 | |
{ |
203 | 0 | if (managedConnection == null) |
204 | |
{ |
205 | 0 | return; |
206 | |
} |
207 | 0 | managedConnection.removeConnection(this); |
208 | |
|
209 | |
|
210 | 0 | managedConnection.fireCloseEvent(this); |
211 | 0 | managedConnection = null; |
212 | 0 | } |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
public void associateConnection(MuleManagedConnection newMc) throws ResourceException |
221 | |
{ |
222 | 0 | checkIfValid(); |
223 | |
|
224 | 0 | managedConnection.removeConnection(this); |
225 | |
|
226 | 0 | newMc.addConnection(this); |
227 | 0 | managedConnection = newMc; |
228 | 0 | } |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
void checkIfValid() throws ResourceException |
237 | |
{ |
238 | 0 | if (managedConnection == null) |
239 | |
{ |
240 | 0 | throw new ResourceException( |
241 | |
JcaMessages.objectMarkedInvalid("muleManagedConnection").toString()); |
242 | |
} |
243 | 0 | } |
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
void invalidate() |
251 | |
{ |
252 | 0 | managedConnection = null; |
253 | 0 | } |
254 | |
} |