1 17 18 19 package org.apache.catalina.realm; 20 21 22 import java.security.Principal ; 23 import java.util.Arrays ; 24 import java.util.List ; 25 import org.apache.catalina.Realm; 26 27 28 35 36 public class GenericPrincipal implements Principal { 37 38 39 41 42 50 public GenericPrincipal(Realm realm, String name, String password) { 51 52 this(realm, name, password, null); 53 54 } 55 56 57 67 public GenericPrincipal(Realm realm, String name, String password, 68 List roles) { 69 this(realm, name, password, roles, null); 70 } 71 72 84 public GenericPrincipal(Realm realm, String name, String password, 85 List roles, Principal userPrincipal) { 86 87 super(); 88 this.realm = realm; 89 this.name = name; 90 this.password = password; 91 this.userPrincipal = userPrincipal; 92 if (roles != null) { 93 this.roles = new String [roles.size()]; 94 this.roles = (String []) roles.toArray(this.roles); 95 if (this.roles.length > 0) 96 Arrays.sort(this.roles); 97 } 98 } 99 100 101 103 104 107 protected String name = null; 108 109 public String getName() { 110 return (this.name); 111 } 112 113 114 118 protected String password = null; 119 120 public String getPassword() { 121 return (this.password); 122 } 123 124 125 128 protected Realm realm = null; 129 130 public Realm getRealm() { 131 return (this.realm); 132 } 133 134 void setRealm( Realm realm ) { 135 this.realm=realm; 136 } 137 138 139 142 protected String roles[] = new String [0]; 143 144 public String [] getRoles() { 145 return (this.roles); 146 } 147 148 149 152 protected Principal userPrincipal = null; 153 154 public Principal getUserPrincipal() { 155 if (userPrincipal != null) { 156 return userPrincipal; 157 } else { 158 return this; 159 } 160 } 161 162 163 165 166 171 public boolean hasRole(String role) { 172 173 if("*".equals(role)) return true; 175 if (role == null) 176 return (false); 177 return (Arrays.binarySearch(roles, role) >= 0); 178 179 } 180 181 182 186 public String toString() { 187 188 StringBuffer sb = new StringBuffer ("GenericPrincipal["); 189 sb.append(this.name); 190 sb.append("("); 191 for( int i=0;i<roles.length; i++ ) { 192 sb.append( roles[i]).append(","); 193 } 194 sb.append(")]"); 195 return (sb.toString()); 196 197 } 198 199 200 } 201 | Popular Tags |