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.DefaultMuleSession; |
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.ImmutableEndpoint; |
23 | |
import org.mule.api.endpoint.InboundEndpoint; |
24 | |
import org.mule.api.transport.DispatchException; |
25 | |
import org.mule.api.transport.ReceiveException; |
26 | |
import org.mule.config.i18n.CoreMessages; |
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.transport.AbstractConnector; |
31 | |
|
32 | |
import java.util.Map; |
33 | |
|
34 | |
import javax.resource.ResourceException; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class DefaultMuleConnection implements MuleConnection |
40 | |
{ |
41 | |
private final MuleCredentials credentials; |
42 | |
private final MuleContext muleContext; |
43 | |
private MuleManagedConnection managedConnection; |
44 | |
|
45 | |
public DefaultMuleConnection(MuleManagedConnection managedConnection, |
46 | |
MuleContext muleContext, |
47 | |
MuleCredentials credentials) |
48 | 0 | { |
49 | 0 | this.muleContext = muleContext; |
50 | 0 | this.credentials = credentials; |
51 | 0 | this.managedConnection = managedConnection; |
52 | 0 | } |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public void dispatch(String url, Object payload, Map messageProperties) throws MuleException |
67 | |
{ |
68 | 0 | MuleMessage message = new DefaultMuleMessage(payload, messageProperties); |
69 | 0 | MuleEvent event = getEvent(message, url, false); |
70 | |
try |
71 | |
{ |
72 | 0 | event.getSession().dispatchEvent(event); |
73 | |
} |
74 | 0 | catch (MuleException e) |
75 | |
{ |
76 | 0 | throw e; |
77 | |
} |
78 | 0 | catch (Exception e) |
79 | |
{ |
80 | 0 | throw new DispatchException( |
81 | |
ClientMessages.failedToDispatchClientEvent(), |
82 | |
event.getMessage(), event.getEndpoint(), e); |
83 | 0 | } |
84 | 0 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public MuleMessage send(String url, Object payload, Map messageProperties) throws MuleException |
100 | |
{ |
101 | 0 | MuleMessage message = new DefaultMuleMessage(payload, messageProperties); |
102 | 0 | MuleEvent event = getEvent(message, url, true); |
103 | |
|
104 | |
MuleMessage response; |
105 | |
try |
106 | |
{ |
107 | 0 | response = event.getSession().sendEvent(event); |
108 | |
} |
109 | 0 | catch (MuleException e) |
110 | |
{ |
111 | 0 | throw e; |
112 | |
} |
113 | 0 | catch (Exception e) |
114 | |
{ |
115 | 0 | throw new DispatchException( |
116 | |
ClientMessages.failedToDispatchClientEvent(), |
117 | |
event.getMessage(), event.getEndpoint(), e); |
118 | 0 | } |
119 | 0 | return response; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
public MuleMessage request(String url, long timeout) throws MuleException |
134 | |
{ |
135 | 0 | InboundEndpoint endpoint = muleContext.getRegistry().lookupEndpointFactory().getInboundEndpoint(url); |
136 | |
|
137 | |
try |
138 | |
{ |
139 | 0 | return endpoint.request(timeout); |
140 | |
} |
141 | 0 | catch (Exception e) |
142 | |
{ |
143 | 0 | throw new ReceiveException(endpoint, timeout, e); |
144 | |
} |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
protected MuleEvent getEvent(MuleMessage message, String uri, boolean synchronous) |
157 | |
throws MuleException |
158 | |
{ |
159 | 0 | ImmutableEndpoint endpoint = muleContext.getRegistry().lookupEndpointFactory().getOutboundEndpoint(uri); |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
try |
168 | |
{ |
169 | 0 | MuleSession session = new DefaultMuleSession(message, |
170 | |
((AbstractConnector)endpoint.getConnector()).getSessionHandler(), muleContext); |
171 | |
|
172 | 0 | if (credentials != null) |
173 | |
{ |
174 | 0 | message.setProperty(MuleProperties.MULE_USER_PROPERTY, "Plain " + credentials.getToken()); |
175 | |
} |
176 | |
|
177 | 0 | return new DefaultMuleEvent(message, endpoint, session, synchronous); |
178 | |
} |
179 | 0 | catch (Exception e) |
180 | |
{ |
181 | 0 | throw new DispatchException( |
182 | |
CoreMessages.failedToCreate("Client event"), message, endpoint, e); |
183 | |
} |
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 | |
} |