1 28 package net.sf.jguard.core.principals; 29 import java.security.Principal ; 30 import java.util.Collection ; 31 import java.util.HashMap ; 32 import java.util.HashSet ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 import java.util.Set ; 36 import java.util.logging.Logger ; 37 38 import javax.security.auth.Subject ; 39 40 import net.sf.jguard.core.authentication.credentials.JGuardCredential; 41 42 47 public class UserPrincipal implements BasePrincipal{ 48 49 50 private static final String NO_NAME_FOUND = "NO NAME FOUND"; 51 private static final long serialVersionUID = 9075426017744650798L; 52 private Logger logger = Logger.getLogger(UserPrincipal.class.getName()); 53 private String name = "NO NAME DEFINED"; 54 private Subject subject; 55 56 public UserPrincipal(Subject subject){ 57 this.subject = subject; 58 } 59 60 public void setName(String name) { 61 this.name = name; 62 63 } 64 65 public Map getPrivateCredentials() { 66 67 Set privateCredentials = subject.getPrivateCredentials(); 68 Map pCredentials = transformCredentialSetIntoMap(privateCredentials); 69 return pCredentials; 70 } 71 72 private Map transformCredentialSetIntoMap(Set credentials) { 73 Map pCredentials = new HashMap (); 74 Iterator privateCIterator = credentials.iterator(); 75 while(privateCIterator.hasNext()){ 76 Object credential = privateCIterator.next(); 77 if(credential instanceof JGuardCredential){ 78 JGuardCredential jcred = (JGuardCredential)credential; 79 if(!pCredentials.containsKey(jcred.getId())){ 80 Collection values = new HashSet (); 81 values.add(jcred.getValue()); 82 pCredentials.put(jcred.getId(),values); 83 }else{ 84 Collection valuesStored = (Collection ) pCredentials.get(jcred.getId()); 85 valuesStored.add(jcred.getValue()); 86 } 87 88 } 89 } 90 return pCredentials; 91 } 92 93 public Map getPublicCredentials() { 94 Set publicCredentials = subject.getPublicCredentials(); 95 Map pCredentials = transformCredentialSetIntoMap(publicCredentials); 96 return pCredentials; 97 } 98 99 104 public String getName() { 105 return NO_NAME_FOUND; 107 } 108 109 115 public boolean equals (Object object){ 116 UserPrincipal userPrincipal; 117 if (object instanceof UserPrincipal) { 118 userPrincipal = (UserPrincipal) object; 119 if(getPrincipals().equals(userPrincipal.getPrincipals())){ 120 return true; 121 } 122 } 124 return false; 125 126 } 127 128 133 protected Map getPrincipals(){ 134 135 Set principals = subject.getPrincipals(); 137 Set filteredSet = new HashSet (); 138 Iterator it = principals.iterator(); 139 while(it.hasNext()){ 140 Principal principal = (Principal )it.next(); 141 if (!(principal instanceof UserPrincipal)) { 142 filteredSet.add(principal); 143 } 144 } 145 146 Map ppals = new HashMap (); 148 149 Iterator itFiletedPrincipals = filteredSet.iterator(); 150 while (itFiletedPrincipals.hasNext()){ 151 Principal principal = (Principal )itFiletedPrincipals.next(); 152 ppals.put(principal.getName(), principal); 153 } 154 155 return ppals; 156 } 157 158 159 163 public Map getRoles(){ 164 return getSpecificPrincipals(RolePrincipal.class); 165 } 166 167 172 public Map getGroups(){ 173 return getSpecificPrincipals(GroupPrincipal.class); 174 } 175 176 private Map getSpecificPrincipals(Class principalSubclass){ 177 Set principals = subject.getPrincipals(principalSubclass); 178 179 Map ppals = new HashMap (); 181 182 Iterator itPrincipals = principals.iterator(); 183 while (itPrincipals.hasNext()){ 184 Principal principal = (Principal )itPrincipals.next(); 185 ppals.put(principal.getName(), principal); 186 } 187 return ppals; 188 } 189 190 public int compareTo(Object o) { 191 UserPrincipal principal = (UserPrincipal)o; 192 if (this.equals(o)){ 193 return 0; 194 } 195 196 return this.getName().compareTo(principal.getName()); 197 198 } 199 200 public String toString(){ 201 StringBuffer sb = new StringBuffer (); 202 sb.append("UserPrincipal "); 203 sb.append(name); 204 sb.append(this.hashcode()); 205 return sb.toString(); 206 } 207 208 public int hashcode(){ 209 return getRoles().hashCode() 210 +getPublicCredentials().hashCode() 211 +getPrivateCredentials().hashCode()+45; 212 } 213 } 214 215 216 217 | Popular Tags |