1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.ra; |
12 | |
|
13 | |
import org.mule.impl.security.MuleCredentials; |
14 | |
import org.mule.ra.i18n.JcaMessages; |
15 | |
|
16 | |
import java.io.PrintWriter; |
17 | |
import java.util.ArrayList; |
18 | |
import java.util.HashSet; |
19 | |
import java.util.Iterator; |
20 | |
import java.util.List; |
21 | |
import java.util.Set; |
22 | |
|
23 | |
import javax.resource.NotSupportedException; |
24 | |
import javax.resource.ResourceException; |
25 | |
import javax.resource.spi.ConnectionEvent; |
26 | |
import javax.resource.spi.ConnectionEventListener; |
27 | |
import javax.resource.spi.ConnectionRequestInfo; |
28 | |
import javax.resource.spi.ManagedConnection; |
29 | |
import javax.resource.spi.ManagedConnectionMetaData; |
30 | |
import javax.resource.spi.security.PasswordCredential; |
31 | |
import javax.security.auth.Subject; |
32 | |
import javax.transaction.xa.XAResource; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class MuleManagedConnection implements ManagedConnection |
38 | |
{ |
39 | |
private MuleManagedConnectionFactory mcf; |
40 | 0 | private List listeners = new ArrayList(); |
41 | |
private Set connectionSet; |
42 | |
private PrintWriter logWriter; |
43 | |
private boolean destroyed; |
44 | |
|
45 | |
private PasswordCredential passCred; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
MuleManagedConnection(MuleManagedConnectionFactory mcf, |
57 | |
Subject subject, |
58 | |
ConnectionRequestInfo cxRequestInfo) throws ResourceException |
59 | 0 | { |
60 | 0 | this.mcf = mcf; |
61 | |
|
62 | |
|
63 | |
|
64 | 0 | this.passCred = RaHelper.getPasswordCredential(mcf, subject, cxRequestInfo); |
65 | |
|
66 | 0 | connectionSet = new HashSet(); |
67 | 0 | } |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public Object getConnection(Subject subject, ConnectionRequestInfo connectionRequestInfo) |
81 | |
throws ResourceException |
82 | |
{ |
83 | |
|
84 | 0 | checkIfDestroyed(); |
85 | |
|
86 | 0 | PasswordCredential pc = RaHelper.getPasswordCredential(mcf, subject, connectionRequestInfo); |
87 | |
|
88 | 0 | if (!passCred.equals(pc)) |
89 | |
{ |
90 | |
|
91 | 0 | throw new javax.resource.spi.SecurityException( |
92 | |
JcaMessages.authDeniedOnEndpoint(this).getMessage()); |
93 | |
} |
94 | |
|
95 | |
String user; |
96 | |
String password; |
97 | 0 | MuleConnectionRequestInfo info = (MuleConnectionRequestInfo)connectionRequestInfo; |
98 | |
|
99 | 0 | user = info.getUserName(); |
100 | 0 | password = info.getPassword(); |
101 | 0 | if (user == null) |
102 | |
{ |
103 | |
|
104 | 0 | user = mcf.getUsername(); |
105 | 0 | password = mcf.getPassword(); |
106 | |
} |
107 | 0 | MuleCredentials creds = null; |
108 | 0 | if (user != null) |
109 | |
{ |
110 | 0 | if (password == null) |
111 | |
{ |
112 | 0 | password = ""; |
113 | |
} |
114 | 0 | creds = new MuleCredentials(user, password.toCharArray()); |
115 | |
} |
116 | |
|
117 | 0 | MuleConnection connection = new DefaultMuleConnection(this, info.getManager(), creds); |
118 | 0 | addConnection(connection); |
119 | 0 | return connection; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public void destroy() throws ResourceException |
129 | |
{ |
130 | 0 | if (destroyed) |
131 | |
{ |
132 | 0 | return; |
133 | |
} |
134 | 0 | destroyed = true; |
135 | |
|
136 | 0 | invalidateConnections(); |
137 | 0 | } |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public void cleanup() throws ResourceException |
148 | |
{ |
149 | 0 | checkIfDestroyed(); |
150 | |
|
151 | 0 | invalidateConnections(); |
152 | 0 | } |
153 | |
|
154 | |
private void invalidateConnections() |
155 | |
{ |
156 | 0 | Iterator it = connectionSet.iterator(); |
157 | 0 | while (it.hasNext()) |
158 | |
{ |
159 | 0 | DefaultMuleConnection connection = (DefaultMuleConnection)it.next(); |
160 | 0 | connection.invalidate(); |
161 | |
} |
162 | 0 | connectionSet.clear(); |
163 | 0 | } |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
public void associateConnection(Object connection) throws ResourceException |
175 | |
{ |
176 | 0 | checkIfDestroyed(); |
177 | |
|
178 | 0 | if (connection instanceof MuleConnection) |
179 | |
{ |
180 | 0 | MuleConnection cnn = (MuleConnection)connection; |
181 | 0 | cnn.associateConnection(this); |
182 | |
} |
183 | |
else |
184 | |
{ |
185 | 0 | throw new IllegalStateException( |
186 | 0 | JcaMessages.objectMarkedInvalid(DefaultMuleConnection.class.getName() + ": " |
187 | |
+ (connection == null ? "null" : connection.getClass().getName())).toString()); |
188 | |
} |
189 | 0 | } |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
public void addConnectionEventListener(ConnectionEventListener listener) |
201 | |
{ |
202 | 0 | listeners.add(listener); |
203 | 0 | } |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
public void removeConnectionEventListener(ConnectionEventListener listener) |
213 | |
{ |
214 | 0 | listeners.remove(listener); |
215 | 0 | } |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
public XAResource getXAResource() throws ResourceException |
229 | |
{ |
230 | 0 | throw new NotSupportedException("getXAResource"); |
231 | |
} |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
public javax.resource.spi.LocalTransaction getLocalTransaction() throws ResourceException |
244 | |
{ |
245 | 0 | throw new NotSupportedException("getLocalTransaction"); |
246 | |
} |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
public ManagedConnectionMetaData getMetaData() throws ResourceException |
259 | |
{ |
260 | 0 | checkIfDestroyed(); |
261 | 0 | return new MuleManagedConnectionMetaData(this); |
262 | |
} |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
public void setLogWriter(PrintWriter out) throws ResourceException |
274 | |
{ |
275 | 0 | this.logWriter = out; |
276 | 0 | } |
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
public PrintWriter getLogWriter() throws ResourceException |
287 | |
{ |
288 | 0 | return logWriter; |
289 | |
} |
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
public String getUsername() |
298 | |
{ |
299 | 0 | if (passCred != null) |
300 | |
{ |
301 | 0 | return passCred.getUserName(); |
302 | |
} |
303 | |
else |
304 | |
{ |
305 | 0 | return null; |
306 | |
} |
307 | |
} |
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
public PasswordCredential getPasswordCredential() |
316 | |
{ |
317 | 0 | return passCred; |
318 | |
} |
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
public void addConnection(MuleConnection connection) |
327 | |
{ |
328 | 0 | connectionSet.add(connection); |
329 | 0 | } |
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
private void checkIfDestroyed() throws ResourceException |
338 | |
{ |
339 | 0 | if (destroyed) |
340 | |
{ |
341 | 0 | throw new ResourceException( |
342 | |
JcaMessages.objectIsDisposed("MuleManagedConnection").toString()); |
343 | |
} |
344 | 0 | } |
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
public void removeConnection(MuleConnection connection) |
354 | |
{ |
355 | 0 | connectionSet.remove(connection); |
356 | 0 | } |
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
boolean isDestroyed() |
365 | |
{ |
366 | 0 | return destroyed; |
367 | |
} |
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
public MuleManagedConnectionFactory getManagedConnectionFactory() |
377 | |
{ |
378 | 0 | return this.mcf; |
379 | |
} |
380 | |
|
381 | |
void fireBeginEvent() |
382 | |
{ |
383 | 0 | ConnectionEvent event = new ConnectionEvent(MuleManagedConnection.this, |
384 | |
ConnectionEvent.LOCAL_TRANSACTION_STARTED); |
385 | 0 | Iterator iterator = listeners.iterator(); |
386 | 0 | while (iterator.hasNext()) |
387 | |
{ |
388 | 0 | ConnectionEventListener l = (ConnectionEventListener)iterator.next(); |
389 | 0 | l.localTransactionStarted(event); |
390 | |
} |
391 | 0 | } |
392 | |
|
393 | |
void fireCommitEvent() |
394 | |
{ |
395 | 0 | ConnectionEvent event = new ConnectionEvent(MuleManagedConnection.this, |
396 | |
ConnectionEvent.LOCAL_TRANSACTION_COMMITTED); |
397 | 0 | Iterator iterator = listeners.iterator(); |
398 | 0 | while (iterator.hasNext()) |
399 | |
{ |
400 | 0 | ConnectionEventListener l = (ConnectionEventListener)iterator.next(); |
401 | 0 | l.localTransactionCommitted(event); |
402 | |
} |
403 | 0 | } |
404 | |
|
405 | |
void fireRollbackEvent() |
406 | |
{ |
407 | 0 | ConnectionEvent event = new ConnectionEvent(MuleManagedConnection.this, |
408 | |
ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK); |
409 | 0 | Iterator iterator = listeners.iterator(); |
410 | 0 | while (iterator.hasNext()) |
411 | |
{ |
412 | 0 | ConnectionEventListener l = (ConnectionEventListener)iterator.next(); |
413 | 0 | l.localTransactionRolledback(event); |
414 | |
} |
415 | 0 | } |
416 | |
|
417 | |
void fireCloseEvent(MuleConnection connection) |
418 | |
{ |
419 | 0 | ConnectionEvent event = new ConnectionEvent(MuleManagedConnection.this, |
420 | |
ConnectionEvent.CONNECTION_CLOSED); |
421 | 0 | event.setConnectionHandle(connection); |
422 | |
|
423 | 0 | Iterator iterator = listeners.iterator(); |
424 | 0 | while (iterator.hasNext()) |
425 | |
{ |
426 | 0 | ConnectionEventListener l = (ConnectionEventListener)iterator.next(); |
427 | 0 | l.connectionClosed(event); |
428 | |
} |
429 | 0 | } |
430 | |
|
431 | |
void fireErrorOccurredEvent(Exception error) |
432 | |
{ |
433 | 0 | ConnectionEvent event = new ConnectionEvent(MuleManagedConnection.this, |
434 | |
ConnectionEvent.CONNECTION_ERROR_OCCURRED, error); |
435 | 0 | Iterator iterator = listeners.iterator(); |
436 | 0 | while (iterator.hasNext()) |
437 | |
{ |
438 | 0 | ConnectionEventListener l = (ConnectionEventListener)iterator.next(); |
439 | 0 | l.connectionErrorOccurred(event); |
440 | |
} |
441 | 0 | } |
442 | |
|
443 | |
} |