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