1 23 24 package com.sun.gjc.util; 25 26 import javax.security.auth.Subject ; 27 import java.security.AccessController ; 28 import java.security.PrivilegedAction ; 29 import javax.resource.spi.security.PasswordCredential ; 30 import javax.resource.ResourceException ; 31 import javax.resource.spi.*; 32 import com.sun.gjc.spi.ConnectionRequestInfo; 33 import com.sun.gjc.common.DataSourceObjectBuilder; 34 import java.util.Set ; 35 import java.util.Iterator ; 36 import com.sun.enterprise.util.i18n.StringManager; 37 43 public class SecurityUtils { 44 45 static private StringManager sm = StringManager.getManager( 46 DataSourceObjectBuilder.class ); 47 48 69 public static PasswordCredential getPasswordCredential(final ManagedConnectionFactory mcf, 70 final Subject subject, javax.resource.spi.ConnectionRequestInfo info) throws ResourceException { 71 72 if (info == null) { 73 if (subject == null) { 74 return null; 75 } else { 76 PasswordCredential pc = (PasswordCredential ) AccessController.doPrivileged 77 (new PrivilegedAction () { 78 public Object run() { 79 Set passwdCredentialSet = subject.getPrivateCredentials(PasswordCredential .class); 80 Iterator iter = passwdCredentialSet.iterator(); 81 while (iter.hasNext()) { 82 PasswordCredential temp = (PasswordCredential ) iter.next(); 83 if (temp.getManagedConnectionFactory().equals(mcf)) { 84 return temp; 85 } 86 } 87 return null; 88 } 89 }); 90 if (pc == null) { 91 String msg = sm.getString( "su.no_passwd_cred"); 92 throw new javax.resource.spi.SecurityException (msg); 93 } else { 94 return pc; 95 } 96 } 97 } else { 98 com.sun.gjc.spi.ConnectionRequestInfo cxReqInfo = (com.sun.gjc.spi.ConnectionRequestInfo) info; 99 PasswordCredential pc = new PasswordCredential (cxReqInfo.getUser(), cxReqInfo.getPassword().toCharArray()); 100 pc.setManagedConnectionFactory(mcf); 101 return pc; 102 } 103 } 104 105 113 static private boolean isEqual(String str1, String str2) { 114 if (str1 == null) { 115 return (str2 == null); 116 } else { 117 return str1.equals(str2); 118 } 119 } 120 121 129 static public boolean isPasswordCredentialEqual(PasswordCredential pC1, PasswordCredential pC2) { 130 if (pC1 == pC2) 131 return true; 132 if(pC1 == null || pC2 == null) 133 return (pC1 == pC2); 134 if (!isEqual(pC1.getUserName(), pC2.getUserName())) { 135 return false; 136 } 137 String p1 = null; 138 String p2 = null; 139 if (pC1.getPassword() != null) { 140 p1 = new String (pC1.getPassword()); 141 } 142 if (pC2.getPassword() != null) { 143 p2 = new String (pC2.getPassword()); 144 } 145 return (isEqual(p1, p2)); 146 } 147 } 148 | Popular Tags |