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 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 MuleConnection connection = new DefaultMuleConnection(this, info.getManager(), creds);
118 addConnection(connection);
119 return connection;
120 }
121
122
123
124
125
126
127
128 public void destroy() throws ResourceException
129 {
130 if (destroyed)
131 {
132 return;
133 }
134 destroyed = true;
135
136 invalidateConnections();
137 }
138
139
140
141
142
143
144
145
146
147 public void cleanup() throws ResourceException
148 {
149 checkIfDestroyed();
150
151 invalidateConnections();
152 }
153
154 private void invalidateConnections()
155 {
156 Iterator it = connectionSet.iterator();
157 while (it.hasNext())
158 {
159 DefaultMuleConnection connection = (DefaultMuleConnection)it.next();
160 connection.invalidate();
161 }
162 connectionSet.clear();
163 }
164
165
166
167
168
169
170
171
172
173
174 public void associateConnection(Object connection) throws ResourceException
175 {
176 checkIfDestroyed();
177
178 if (connection instanceof MuleConnection)
179 {
180 MuleConnection cnn = (MuleConnection)connection;
181 cnn.associateConnection(this);
182 }
183 else
184 {
185 throw new IllegalStateException(
186 JcaMessages.objectMarkedInvalid(DefaultMuleConnection.class.getName() + ": "
187 + (connection == null ? "null" : connection.getClass().getName())).toString());
188 }
189 }
190
191
192
193
194
195
196
197
198
199
200 public void addConnectionEventListener(ConnectionEventListener listener)
201 {
202 listeners.add(listener);
203 }
204
205
206
207
208
209
210
211
212 public void removeConnectionEventListener(ConnectionEventListener listener)
213 {
214 listeners.remove(listener);
215 }
216
217
218
219
220
221
222
223
224
225
226
227
228 public XAResource getXAResource() throws ResourceException
229 {
230 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 throw new NotSupportedException("getLocalTransaction");
246 }
247
248
249
250
251
252
253
254
255
256
257
258 public ManagedConnectionMetaData getMetaData() throws ResourceException
259 {
260 checkIfDestroyed();
261 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 this.logWriter = out;
276 }
277
278
279
280
281
282
283
284
285
286 public PrintWriter getLogWriter() throws ResourceException
287 {
288 return logWriter;
289 }
290
291
292
293
294
295
296
297 public String getUsername()
298 {
299 if (passCred != null)
300 {
301 return passCred.getUserName();
302 }
303 else
304 {
305 return null;
306 }
307 }
308
309
310
311
312
313
314
315 public PasswordCredential getPasswordCredential()
316 {
317 return passCred;
318 }
319
320
321
322
323
324
325
326 public void addConnection(MuleConnection connection)
327 {
328 connectionSet.add(connection);
329 }
330
331
332
333
334
335
336
337 private void checkIfDestroyed() throws ResourceException
338 {
339 if (destroyed)
340 {
341 throw new ResourceException(
342 JcaMessages.objectIsDisposed("MuleManagedConnection").toString());
343 }
344 }
345
346
347
348
349
350
351
352
353 public void removeConnection(MuleConnection connection)
354 {
355 connectionSet.remove(connection);
356 }
357
358
359
360
361
362
363
364 boolean isDestroyed()
365 {
366 return destroyed;
367 }
368
369
370
371
372
373
374
375
376 public MuleManagedConnectionFactory getManagedConnectionFactory()
377 {
378 return this.mcf;
379 }
380
381 void fireBeginEvent()
382 {
383 ConnectionEvent event = new ConnectionEvent(MuleManagedConnection.this,
384 ConnectionEvent.LOCAL_TRANSACTION_STARTED);
385 Iterator iterator = listeners.iterator();
386 while (iterator.hasNext())
387 {
388 ConnectionEventListener l = (ConnectionEventListener)iterator.next();
389 l.localTransactionStarted(event);
390 }
391 }
392
393 void fireCommitEvent()
394 {
395 ConnectionEvent event = new ConnectionEvent(MuleManagedConnection.this,
396 ConnectionEvent.LOCAL_TRANSACTION_COMMITTED);
397 Iterator iterator = listeners.iterator();
398 while (iterator.hasNext())
399 {
400 ConnectionEventListener l = (ConnectionEventListener)iterator.next();
401 l.localTransactionCommitted(event);
402 }
403 }
404
405 void fireRollbackEvent()
406 {
407 ConnectionEvent event = new ConnectionEvent(MuleManagedConnection.this,
408 ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK);
409 Iterator iterator = listeners.iterator();
410 while (iterator.hasNext())
411 {
412 ConnectionEventListener l = (ConnectionEventListener)iterator.next();
413 l.localTransactionRolledback(event);
414 }
415 }
416
417 void fireCloseEvent(MuleConnection connection)
418 {
419 ConnectionEvent event = new ConnectionEvent(MuleManagedConnection.this,
420 ConnectionEvent.CONNECTION_CLOSED);
421 event.setConnectionHandle(connection);
422
423 Iterator iterator = listeners.iterator();
424 while (iterator.hasNext())
425 {
426 ConnectionEventListener l = (ConnectionEventListener)iterator.next();
427 l.connectionClosed(event);
428 }
429 }
430
431 void fireErrorOccurredEvent(Exception error)
432 {
433 ConnectionEvent event = new ConnectionEvent(MuleManagedConnection.this,
434 ConnectionEvent.CONNECTION_ERROR_OCCURRED, error);
435 Iterator iterator = listeners.iterator();
436 while (iterator.hasNext())
437 {
438 ConnectionEventListener l = (ConnectionEventListener)iterator.next();
439 l.connectionErrorOccurred(event);
440 }
441 }
442
443 }