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