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 {
52 this.manager = manager;
53 this.credentials = credentials;
54 this.managedConnection = managedConnection;
55 }
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 UMOEndpointURI muleEndpoint = new MuleEndpointURI(url);
72 UMOMessage message = new MuleMessage(payload, messageProperties);
73 UMOEvent event = getEvent(message, muleEndpoint, false);
74 try
75 {
76 event.getSession().dispatchEvent(event);
77 }
78 catch (UMOException e)
79 {
80 throw e;
81 }
82 catch (Exception e)
83 {
84 throw new DispatchException(
85 ClientMessages.failedToDispatchClientEvent(),
86 event.getMessage(), event.getEndpoint(), e);
87 }
88 }
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 UMOEndpointURI muleEndpoint = new MuleEndpointURI(url);
106 UMOMessage message = new MuleMessage(payload, messageProperties);
107 UMOEvent event = getEvent(message, muleEndpoint, true);
108
109 UMOMessage response;
110 try
111 {
112 response = event.getSession().sendEvent(event);
113 }
114 catch (UMOException e)
115 {
116 throw e;
117 }
118 catch (Exception e)
119 {
120 throw new DispatchException(
121 ClientMessages.failedToDispatchClientEvent(),
122 event.getMessage(), event.getEndpoint(), e);
123 }
124 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 MuleEndpointURI muleEndpoint = new MuleEndpointURI(url);
141
142 UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(muleEndpoint,
143 UMOEndpoint.ENDPOINT_TYPE_SENDER);
144
145 try
146 {
147 return endpoint.receive(timeout);
148 }
149 catch (Exception e)
150 {
151 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 UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(uri, UMOEndpoint.ENDPOINT_TYPE_SENDER);
168 UMOConnector connector = endpoint.getConnector();
169
170 if (!connector.isStarted() && manager.isStarted())
171 {
172 connector.startConnector();
173 }
174
175 try
176 {
177 UMOSession session = new MuleSession(message,
178 ((AbstractConnector)endpoint.getConnector()).getSessionHandler());
179
180 if (credentials != null)
181 {
182 message.setProperty(MuleProperties.MULE_USER_PROPERTY, "Plain " + credentials.getToken());
183 }
184
185 return new MuleEvent(message, endpoint, session, synchronous);
186 }
187 catch (Exception e)
188 {
189 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 return managedConnection;
204 }
205
206
207
208
209 public void close() throws ResourceException
210 {
211 if (managedConnection == null)
212 {
213 return;
214 }
215 managedConnection.removeConnection(this);
216
217
218 managedConnection.fireCloseEvent(this);
219 managedConnection = null;
220 }
221
222
223
224
225
226
227
228 public void associateConnection(MuleManagedConnection newMc) throws ResourceException
229 {
230 checkIfValid();
231
232 managedConnection.removeConnection(this);
233
234 newMc.addConnection(this);
235 managedConnection = newMc;
236 }
237
238
239
240
241
242
243
244 void checkIfValid() throws ResourceException
245 {
246 if (managedConnection == null)
247 {
248 throw new ResourceException(
249 JcaMessages.objectMarkedInvalid("muleManagedConnection").toString());
250 }
251 }
252
253
254
255
256
257
258 void invalidate()
259 {
260 managedConnection = null;
261 }
262 }