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 public class RaHelper
32 {
33 public static PasswordCredential getPasswordCredential(final ManagedConnectionFactory mcf,
34 final Subject subject,
35 ConnectionRequestInfo info)
36 throws ResourceException
37 {
38 if (subject == null)
39 {
40 if (info == null)
41 {
42 return null;
43 }
44 else
45 {
46 MuleConnectionRequestInfo muleInfo = (MuleConnectionRequestInfo)info;
47
48
49 if (muleInfo.getUserName() == null || muleInfo.getPassword() == null)
50 {
51
52
53 return null;
54 }
55
56 char[] password = muleInfo.getPassword().toCharArray();
57 PasswordCredential pc = new PasswordCredential(muleInfo.getUserName(), password);
58 pc.setManagedConnectionFactory(mcf);
59 return pc;
60 }
61 }
62 else
63 {
64 PasswordCredential pc = (PasswordCredential)AccessController.doPrivileged(new PrivilegedAction()
65 {
66 public Object run()
67 {
68 Set creds = subject.getPrivateCredentials(PasswordCredential.class);
69 Iterator iter = creds.iterator();
70 while (iter.hasNext())
71 {
72 PasswordCredential candidate = (PasswordCredential)iter.next();
73 if (candidate != null)
74 {
75 ManagedConnectionFactory candidatemcf = candidate.getManagedConnectionFactory();
76 if (candidatemcf != null && candidatemcf.equals(mcf))
77 {
78 return candidate;
79 }
80 }
81 }
82 return null;
83 }
84 });
85 if (pc == null)
86 {
87 throw new java.lang.SecurityException(CoreMessages.authNoCredentials().getMessage());
88 }
89 else
90 {
91 return pc;
92 }
93 }
94 }
95 }