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