1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.jca; |
8 | |
|
9 | |
import org.mule.config.i18n.CoreMessages; |
10 | |
|
11 | |
import java.security.AccessController; |
12 | |
import java.security.PrivilegedAction; |
13 | |
import java.util.Iterator; |
14 | |
import java.util.Set; |
15 | |
|
16 | |
import javax.resource.ResourceException; |
17 | |
import javax.resource.spi.ConnectionRequestInfo; |
18 | |
import javax.resource.spi.ManagedConnectionFactory; |
19 | |
import javax.resource.spi.security.PasswordCredential; |
20 | |
import javax.security.auth.Subject; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | 0 | public class RaHelper |
28 | |
{ |
29 | |
public static PasswordCredential getPasswordCredential(final ManagedConnectionFactory mcf, |
30 | |
final Subject subject, |
31 | |
ConnectionRequestInfo info) |
32 | |
throws ResourceException |
33 | |
{ |
34 | 0 | if (subject == null) |
35 | |
{ |
36 | 0 | if (info == null) |
37 | |
{ |
38 | 0 | return null; |
39 | |
} |
40 | |
else |
41 | |
{ |
42 | 0 | MuleConnectionRequestInfo muleInfo = (MuleConnectionRequestInfo)info; |
43 | |
|
44 | |
|
45 | 0 | if (muleInfo.getUserName() == null || muleInfo.getPassword() == null) |
46 | |
{ |
47 | |
|
48 | |
|
49 | 0 | return null; |
50 | |
} |
51 | |
|
52 | 0 | char[] password = muleInfo.getPassword().toCharArray(); |
53 | 0 | PasswordCredential pc = new PasswordCredential(muleInfo.getUserName(), password); |
54 | 0 | pc.setManagedConnectionFactory(mcf); |
55 | 0 | return pc; |
56 | |
} |
57 | |
} |
58 | |
else |
59 | |
{ |
60 | 0 | PasswordCredential pc = (PasswordCredential)AccessController.doPrivileged(new PrivilegedAction() |
61 | 0 | { |
62 | |
public Object run() |
63 | |
{ |
64 | 0 | Set creds = subject.getPrivateCredentials(PasswordCredential.class); |
65 | 0 | Iterator iter = creds.iterator(); |
66 | 0 | while (iter.hasNext()) |
67 | |
{ |
68 | 0 | PasswordCredential candidate = (PasswordCredential)iter.next(); |
69 | 0 | if (candidate != null) |
70 | |
{ |
71 | 0 | ManagedConnectionFactory candidatemcf = candidate.getManagedConnectionFactory(); |
72 | 0 | if (candidatemcf != null && candidatemcf.equals(mcf)) |
73 | |
{ |
74 | 0 | return candidate; |
75 | |
} |
76 | |
} |
77 | 0 | } |
78 | 0 | return null; |
79 | |
} |
80 | |
}); |
81 | 0 | if (pc == null) |
82 | |
{ |
83 | 0 | throw new java.lang.SecurityException(CoreMessages.authNoCredentials().getMessage()); |
84 | |
} |
85 | |
else |
86 | |
{ |
87 | 0 | return pc; |
88 | |
} |
89 | |
} |
90 | |
} |
91 | |
} |