1 22 package org.jboss.resource.adapter.jms; 23 24 import java.util.Set ; 25 import java.util.Iterator ; 26 import java.security.AccessController ; 27 import java.security.PrivilegedAction ; 28 29 import javax.security.auth.Subject ; 30 31 import javax.resource.spi.ManagedConnectionFactory ; 32 import javax.resource.spi.SecurityException ; 33 import javax.resource.spi.ConnectionRequestInfo ; 34 35 import javax.resource.spi.security.PasswordCredential ; 36 37 44 public class JmsCred 45 { 46 public String name; 47 48 public String pwd; 49 50 public JmsCred() 51 { 52 } 54 55 58 public static JmsCred getJmsCred(ManagedConnectionFactory mcf, Subject subject, ConnectionRequestInfo info) 59 throws SecurityException 60 { 61 JmsCred jc = new JmsCred(); 62 if (subject == null && info != null) 63 { 64 jc.name = ((JmsConnectionRequestInfo) info).getUserName(); 66 jc.pwd = ((JmsConnectionRequestInfo) info).getPassword(); 67 } 68 else if (subject != null) 69 { 70 PasswordCredential pwdc = GetCredentialAction.getCredential(subject, mcf); 72 if (pwdc == null) 73 { 74 throw new SecurityException ("No Password credentials found"); 76 } 77 jc.name = pwdc.getUserName(); 78 jc.pwd = new String (pwdc.getPassword()); 79 } 80 else 81 { 82 throw new SecurityException ("No Subject or ConnectionRequestInfo set, could not get credentials"); 83 } 84 return jc; 85 } 86 87 public String toString() 88 { 89 return super.toString() + "{ username=" + name + ", password=**** }"; 90 } 91 92 private static class GetCredentialAction implements PrivilegedAction 93 { 94 Subject subject; 95 ManagedConnectionFactory mcf; 96 GetCredentialAction(Subject subject, ManagedConnectionFactory mcf) 97 { 98 this.subject = subject; 99 this.mcf = mcf; 100 } 101 public Object run() 102 { 103 Set creds = subject.getPrivateCredentials(PasswordCredential .class); 104 PasswordCredential pwdc = null; 105 Iterator credentials = creds.iterator(); 106 while (credentials.hasNext()) 107 { 108 PasswordCredential curCred = (PasswordCredential ) credentials.next(); 109 if (curCred.getManagedConnectionFactory().equals(mcf)) 110 { 111 pwdc = curCred; 112 break; 113 } 114 } 115 return pwdc; 116 } 117 static PasswordCredential getCredential(Subject subject, ManagedConnectionFactory mcf) 118 { 119 GetCredentialAction action = new GetCredentialAction(subject, mcf); 120 PasswordCredential pc = (PasswordCredential ) AccessController.doPrivileged(action); 121 return pc; 122 } 123 } 124 } 125 | Popular Tags |