1 package org.apache.turbine.util.security; 2 3 18 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 22 import org.apache.commons.lang.StringUtils; 23 24 import org.apache.turbine.om.security.Role; 25 26 38 public class RoleSet 39 extends SecuritySet 40 { 41 44 public RoleSet() 45 { 46 super(); 47 } 48 49 57 public RoleSet(Collection roles) 58 { 59 super(); 60 add(roles); 61 } 62 63 70 public boolean add(Role role) 71 { 72 boolean res = contains(role); 73 nameMap.put(role.getName(), role); 74 idMap.put(role.getIdAsObj(), role); 75 return res; 76 } 77 78 86 public boolean add(Collection roles) 87 { 88 boolean res = false; 89 for (Iterator it = roles.iterator(); it.hasNext();) 90 { 91 Role r = (Role) it.next(); 92 res |= add(r); 93 } 94 return res; 95 } 96 97 105 public boolean add(RoleSet roleSet) 106 { 107 boolean res = false; 108 for( Iterator it = roleSet.iterator(); it.hasNext();) 109 { 110 Role r = (Role) it.next(); 111 res |= add(r); 112 } 113 return res; 114 } 115 116 123 public boolean remove(Role role) 124 { 125 boolean res = contains(role); 126 nameMap.remove(role.getName()); 127 idMap.remove(role.getIdAsObj()); 128 return res; 129 } 130 131 138 public boolean contains(Role role) 139 { 140 return nameMap.containsValue((Object ) role); 141 } 142 143 152 public Role getRole(String roleName) 153 { 154 return getRoleByName(roleName); 155 } 156 157 165 public Role getRoleByName(String roleName) 166 { 167 return (StringUtils.isNotEmpty(roleName)) 168 ? (Role) nameMap.get(roleName) : null; 169 } 170 171 179 public Role getRoleById(int roleId) 180 { 181 return (roleId != 0) 182 ? (Role) idMap.get(new Integer (roleId)) : null; 183 } 184 185 190 public Role[] getRolesArray() 191 { 192 return (Role[]) getSet().toArray(new Role[0]); 193 } 194 195 201 public String toString() 202 { 203 StringBuffer sb = new StringBuffer (); 204 sb.append("RoleSet: "); 205 206 for(Iterator it = iterator(); it.hasNext();) 207 { 208 Role r = (Role) it.next(); 209 sb.append('['); 210 sb.append(r.getName()); 211 sb.append(" -> "); 212 sb.append(r.getIdAsObj()); 213 sb.append(']'); 214 if (it.hasNext()) 215 { 216 sb.append(", "); 217 } 218 } 219 220 return sb.toString(); 221 } 222 } 223 | Popular Tags |