1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.ra; |
12 | |
|
13 | |
import org.mule.config.MuleProperties; |
14 | |
import org.mule.config.i18n.CoreMessages; |
15 | |
import org.mule.extras.client.i18n.ClientMessages; |
16 | |
import org.mule.impl.MuleEvent; |
17 | |
import org.mule.impl.MuleMessage; |
18 | |
import org.mule.impl.MuleSession; |
19 | |
import org.mule.impl.endpoint.MuleEndpoint; |
20 | |
import org.mule.impl.endpoint.MuleEndpointURI; |
21 | |
import org.mule.impl.security.MuleCredentials; |
22 | |
import org.mule.providers.AbstractConnector; |
23 | |
import org.mule.ra.i18n.JcaMessages; |
24 | |
import org.mule.umo.UMOEvent; |
25 | |
import org.mule.umo.UMOException; |
26 | |
import org.mule.umo.UMOMessage; |
27 | |
import org.mule.umo.UMOSession; |
28 | |
import org.mule.umo.endpoint.UMOEndpoint; |
29 | |
import org.mule.umo.endpoint.UMOEndpointURI; |
30 | |
import org.mule.umo.manager.UMOManager; |
31 | |
import org.mule.umo.provider.DispatchException; |
32 | |
import org.mule.umo.provider.ReceiveException; |
33 | |
import org.mule.umo.provider.UMOConnector; |
34 | |
|
35 | |
import java.util.Map; |
36 | |
|
37 | |
import javax.resource.ResourceException; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public class DefaultMuleConnection implements MuleConnection |
43 | |
{ |
44 | |
private final MuleCredentials credentials; |
45 | |
private final UMOManager manager; |
46 | |
private MuleManagedConnection managedConnection; |
47 | |
|
48 | |
public DefaultMuleConnection(MuleManagedConnection managedConnection, |
49 | |
UMOManager manager, |
50 | |
MuleCredentials credentials) |
51 | 0 | { |
52 | 0 | this.manager = manager; |
53 | 0 | this.credentials = credentials; |
54 | 0 | this.managedConnection = managedConnection; |
55 | 0 | } |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public void dispatch(String url, Object payload, Map messageProperties) throws UMOException |
70 | |
{ |
71 | 0 | UMOEndpointURI muleEndpoint = new MuleEndpointURI(url); |
72 | 0 | UMOMessage message = new MuleMessage(payload, messageProperties); |
73 | 0 | UMOEvent event = getEvent(message, muleEndpoint, false); |
74 | |
try |
75 | |
{ |
76 | 0 | event.getSession().dispatchEvent(event); |
77 | |
} |
78 | 0 | catch (UMOException e) |
79 | |
{ |
80 | 0 | throw e; |
81 | |
} |
82 | 0 | catch (Exception e) |
83 | |
{ |
84 | 0 | throw new DispatchException( |
85 | |
ClientMessages.failedToDispatchClientEvent(), |
86 | |
event.getMessage(), event.getEndpoint(), e); |
87 | 0 | } |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
public UMOMessage send(String url, Object payload, Map messageProperties) throws UMOException |
104 | |
{ |
105 | 0 | UMOEndpointURI muleEndpoint = new MuleEndpointURI(url); |
106 | 0 | UMOMessage message = new MuleMessage(payload, messageProperties); |
107 | 0 | UMOEvent event = getEvent(message, muleEndpoint, true); |
108 | |
|
109 | |
UMOMessage response; |
110 | |
try |
111 | |
{ |
112 | 0 | response = event.getSession().sendEvent(event); |
113 | |
} |
114 | 0 | catch (UMOException e) |
115 | |
{ |
116 | 0 | throw e; |
117 | |
} |
118 | 0 | catch (Exception e) |
119 | |
{ |
120 | 0 | throw new DispatchException( |
121 | |
ClientMessages.failedToDispatchClientEvent(), |
122 | |
event.getMessage(), event.getEndpoint(), e); |
123 | 0 | } |
124 | 0 | return response; |
125 | |
} |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public UMOMessage receive(String url, long timeout) throws UMOException |
139 | |
{ |
140 | 0 | MuleEndpointURI muleEndpoint = new MuleEndpointURI(url); |
141 | |
|
142 | 0 | UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(muleEndpoint, |
143 | |
UMOEndpoint.ENDPOINT_TYPE_SENDER); |
144 | |
|
145 | |
try |
146 | |
{ |
147 | 0 | return endpoint.receive(timeout); |
148 | |
} |
149 | 0 | catch (Exception e) |
150 | |
{ |
151 | 0 | throw new ReceiveException(endpoint, timeout, e); |
152 | |
} |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
protected UMOEvent getEvent(UMOMessage message, UMOEndpointURI uri, boolean synchronous) |
165 | |
throws UMOException |
166 | |
{ |
167 | 0 | UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(uri, UMOEndpoint.ENDPOINT_TYPE_SENDER); |
168 | 0 | UMOConnector connector = endpoint.getConnector(); |
169 | |
|
170 | 0 | if (!connector.isStarted() && manager.isStarted()) |
171 | |
{ |
172 | 0 | connector.startConnector(); |
173 | |
} |
174 | |
|
175 | |
try |
176 | |
{ |
177 | 0 | UMOSession session = new MuleSession(message, |
178 | |
((AbstractConnector)endpoint.getConnector()).getSessionHandler()); |
179 | |
|
180 | 0 | if (credentials != null) |
181 | |
{ |
182 | 0 | message.setProperty(MuleProperties.MULE_USER_PROPERTY, "Plain " + credentials.getToken()); |
183 | |
} |
184 | |
|
185 | 0 | return new MuleEvent(message, endpoint, session, synchronous); |
186 | |
} |
187 | 0 | catch (Exception e) |
188 | |
{ |
189 | 0 | throw new DispatchException( |
190 | |
CoreMessages.failedToCreate("Client event"), message, endpoint, e); |
191 | |
} |
192 | |
} |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
public MuleManagedConnection getManagedConnection() |
202 | |
{ |
203 | 0 | return managedConnection; |
204 | |
} |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
public void close() throws ResourceException |
210 | |
{ |
211 | 0 | if (managedConnection == null) |
212 | |
{ |
213 | 0 | return; |
214 | |
} |
215 | 0 | managedConnection.removeConnection(this); |
216 | |
|
217 | |
|
218 | 0 | managedConnection.fireCloseEvent(this); |
219 | 0 | managedConnection = null; |
220 | 0 | } |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
public void associateConnection(MuleManagedConnection newMc) throws ResourceException |
229 | |
{ |
230 | 0 | checkIfValid(); |
231 | |
|
232 | 0 | managedConnection.removeConnection(this); |
233 | |
|
234 | 0 | newMc.addConnection(this); |
235 | 0 | managedConnection = newMc; |
236 | 0 | } |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
void checkIfValid() throws ResourceException |
245 | |
{ |
246 | 0 | if (managedConnection == null) |
247 | |
{ |
248 | 0 | throw new ResourceException( |
249 | |
JcaMessages.objectMarkedInvalid("muleManagedConnection").toString()); |
250 | |
} |
251 | 0 | } |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
void invalidate() |
259 | |
{ |
260 | 0 | managedConnection = null; |
261 | 0 | } |
262 | |
} |