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