1 28 29 package org.objectweb.util.explorer.core.role.lib; 30 31 import java.util.Arrays ; 32 import java.util.Comparator ; 33 import java.util.HashMap ; 34 import java.util.List ; 35 import java.util.Map ; 36 import java.util.Vector ; 37 38 import org.objectweb.util.explorer.ExplorerUtils; 39 import org.objectweb.util.explorer.core.common.lib.BindingFeature; 40 import org.objectweb.util.explorer.core.role.api.Role; 41 import org.objectweb.util.explorer.core.role.api.RoleDescription; 42 import org.objectweb.util.explorer.core.role.api.RoleFeeder; 43 import org.objectweb.util.explorer.core.role.api.RoleManager; 44 import org.objectweb.util.explorer.core.role.api.RoleProvider; 45 46 85 public class RoleProperty 86 extends BindingFeature 87 implements RoleFeeder, RoleProvider, RoleManager 88 { 89 90 96 97 protected Map roleMap_ = null; 98 99 100 protected Map inheritedRolesMap_ = null; 101 102 103 protected String [] currentRoleIds_ = new String []{RoleProvider.DEFAULT_ROLE}; 104 105 111 114 public RoleProperty(){ 115 roleMap_ = new HashMap (); 116 inheritedRolesMap_ = new HashMap (); 117 } 118 119 125 131 protected void buildInheritedRolesList(String [] roleIds, Vector roleIdsList){ 132 133 for(int i=0 ; i<roleIds.length ; i++){ 135 if(!roleIdsList.contains(roleIds[i])){ 136 roleIdsList.add(roleIds[i]); 137 } 138 } 139 140 Vector allExtendedRoleIds = new Vector (); 142 for(int i=0 ; i<roleIds.length ; i++){ 143 String [] inheritedRoles = (String [])inheritedRolesMap_.get(roleIds[i]); 144 String roleId = null; 145 if(inheritedRoles!=null){ 147 for(int j=0 ; j<inheritedRoles.length ; j++){ 148 roleId = inheritedRoles[j]; 149 if(roleMap_.containsKey(roleId) && !roleIdsList.contains(roleId)) { 150 allExtendedRoleIds.add(roleId); 151 } 152 } 153 } 154 } 155 156 if(allExtendedRoleIds.size()!=0){ 158 buildInheritedRolesList((String [])allExtendedRoleIds.toArray(new String [allExtendedRoleIds.size()]), roleIdsList); 159 } 160 161 } 162 163 169 protected String [] provideInheritedRoleIds(String [] roleIds){ 170 Vector roleList = new Vector (); 171 buildInheritedRolesList(roleIds, roleList); 172 if(!roleList.contains(RoleProvider.DEFAULT_ROLE)){ 173 roleList.add(RoleProvider.DEFAULT_ROLE); 174 } 175 return (String [])roleList.toArray(new String [roleList.size()]); 176 } 177 178 184 protected void mergeInheritedRoles(String roleId, RoleDescription roleDesc){ 185 String [] currentInheritedRoles = (String [])inheritedRolesMap_.get(roleId); 186 String [] inheritedRolesToAdd = roleDesc.getInheritance(); 187 inheritedRolesMap_.put(roleId, ExplorerUtils.mergeArrays(currentInheritedRoles, inheritedRolesToAdd)); 188 } 189 190 196 protected void mergeRole(Role currentRole, RoleDescription roleDesc){ 197 if(!roleDesc.isConcrete()) 198 currentRole.setConcrete(false); 199 } 200 201 204 protected void storeRoleDesc(RoleDescription roleDesc){ 205 Role theRole = null; 206 String roleId = roleDesc.getId(); 207 if(roleMap_.containsKey(roleId)){ 208 mergeRole((Role)roleMap_.get(roleId), roleDesc); 209 mergeInheritedRoles(roleId, roleDesc); 210 } else { 211 theRole = new DefaultRole(roleId); 212 theRole.setConcrete(roleDesc.isConcrete()); 213 roleMap_.put(roleId, theRole); 214 inheritedRolesMap_.put(roleId, roleDesc.getInheritance()!=null?roleDesc.getInheritance():new String []{}); 215 } 216 } 217 218 protected void fixCurrentRoleIds(String [] roleIds){ 219 if(roleIds!=null) { 220 currentRoleIds_ = roleIds; 221 } else { 222 currentRoleIds_ = new String [0]; 223 } 224 } 225 226 232 235 public String [] clientFc() { 236 return new String []{RoleFeeder.ROLE_FEEDER, RoleProvider.ROLE_PROVIDER}; 237 } 238 239 245 248 public void feed(RoleDescription roleDesc) { 249 storeRoleDesc(roleDesc); 250 } 251 252 258 261 public Role getRole(String id) { 262 return (Role)roleMap_.get(id); 263 } 264 265 268 public String [] getRoleIds(Role[] roles) { 269 String [] roleIds = new String [roles.length]; 270 for (int i = 0; i < roles.length; i++) { 271 roleIds[i] = roles[i].getId(); 272 } 273 return roleIds; 274 } 275 276 279 public Role[] getRoles(String [] roleIds) { 280 Vector v = new Vector (); 281 Role r = null; 282 for (int i = 0; i < roleIds.length; i++) { 283 r = getRole(roleIds[i]); 284 if(r!=null){ 285 v.add(r); 286 } 287 } 288 return (Role[])v.toArray(new Role[v.size()]); 289 } 290 291 294 public String [] getInheritedRoleIds(String [] roleIds) { 295 return provideInheritedRoleIds(roleIds); 296 } 297 298 301 public Role getDefaultRole() { 302 return (Role)roleMap_.get(RoleProvider.DEFAULT_ROLE); 303 } 304 305 308 public String [] getRoleList() { 309 return getRoleList(false); 310 } 311 312 315 public String [] getRoleList(boolean alphabeticOrder) { 316 return getRoleList(false, false); 317 } 318 319 322 public String [] getRoleList(boolean alphabeticOrder, boolean excludedAbstractRoles) { 323 String [] roleIds = (String [])roleMap_.keySet().toArray(new String [roleMap_.size()]); 324 if(alphabeticOrder){ 325 Arrays.sort(roleIds, new RoleComparator()); 326 } 327 if(excludedAbstractRoles){ 328 List l = new Vector (); 329 for(int i=0;i<roleIds.length;i++){ 330 if(getRole(roleIds[i]).isConcrete()){ 331 l.add(roleIds[i]); 332 } 333 } 334 return (String [])l.toArray(new String [l.size()]); 335 } 336 return roleIds; 337 } 338 339 345 348 public String [] getCurrentRoleIds() { 349 return currentRoleIds_; 350 } 351 352 355 public void setCurrentRoleIds(String [] roleIds) { 356 fixCurrentRoleIds(roleIds); 357 } 358 359 365 373 private final class RoleComparator implements Comparator { 374 375 protected int compare(String id1, String id2) { 376 return id1.compareTo(id2); 377 } 378 379 public int compare(Object o1, Object o2) { 380 return compare((String )o1,(String )o2); 381 } 382 383 } 384 } 385 386 | Popular Tags |