SEC-1012: generification

This commit is contained in:
Luke Taylor 2008-11-21 12:39:30 +00:00
parent 05e753de61
commit dca0505d23
1 changed files with 17 additions and 20 deletions

View File

@ -72,19 +72,19 @@ public class RoleHierarchyImpl implements RoleHierarchy {
* rolesReachableInOneStepMap is a Map that under the key of a specific role name contains a set of all roles * rolesReachableInOneStepMap is a Map that under the key of a specific role name contains a set of all roles
* reachable from this role in 1 step * reachable from this role in 1 step
*/ */
private Map rolesReachableInOneStepMap = null; private Map<GrantedAuthority, Set<GrantedAuthority>> rolesReachableInOneStepMap = null;
/** /**
* rolesReachableInOneOrMoreStepsMap is a Map that under the key of a specific role name contains a set of all * rolesReachableInOneOrMoreStepsMap is a Map that under the key of a specific role name contains a set of all
* roles reachable from this role in 1 or more steps * roles reachable from this role in 1 or more steps
*/ */
private Map rolesReachableInOneOrMoreStepsMap = null; private Map<GrantedAuthority, Set<GrantedAuthority>> rolesReachableInOneOrMoreStepsMap = null;
/** /**
* Set the role hierarchy and precalculate for every role the set of all reachable roles, i. e. all roles lower in * Set the role hierarchy and pre-calculate for every role the set of all reachable roles, i.e. all roles lower in
* the hierarchy of every given role. Precalculation is done for performance reasons (reachable roles can then be * the hierarchy of every given role. Pre-calculation is done for performance reasons (reachable roles can then be
* calculated in O(1) time). * calculated in O(1) time).
* During precalculation cycles in role hierarchy are detected and will cause a * During pre-calculation, cycles in role hierarchy are detected and will cause a
* <tt>CycleInRoleHierarchyException</tt> to be thrown. * <tt>CycleInRoleHierarchyException</tt> to be thrown.
* *
* @param roleHierarchyStringRepresentation - String definition of the role hierarchy. * @param roleHierarchyStringRepresentation - String definition of the role hierarchy.
@ -107,7 +107,7 @@ public class RoleHierarchyImpl implements RoleHierarchy {
for (GrantedAuthority authority : authorities) { for (GrantedAuthority authority : authorities) {
reachableRoles.add(authority); reachableRoles.add(authority);
Set additionalReachableRoles = (Set) rolesReachableInOneOrMoreStepsMap.get(authority); Set<GrantedAuthority> additionalReachableRoles = rolesReachableInOneOrMoreStepsMap.get(authority);
if (additionalReachableRoles != null) { if (additionalReachableRoles != null) {
reachableRoles.addAll(additionalReachableRoles); reachableRoles.addAll(additionalReachableRoles);
} }
@ -129,22 +129,21 @@ public class RoleHierarchyImpl implements RoleHierarchy {
* references a set of the reachable lower roles. * references a set of the reachable lower roles.
*/ */
private void buildRolesReachableInOneStepMap() { private void buildRolesReachableInOneStepMap() {
String parsingRegex = "(\\s*([^\\s>]+)\\s*\\>\\s*([^\\s>]+))"; Pattern pattern = Pattern.compile("(\\s*([^\\s>]+)\\s*\\>\\s*([^\\s>]+))");
Pattern pattern = Pattern.compile(parsingRegex);
Matcher roleHierarchyMatcher = pattern.matcher(roleHierarchyStringRepresentation); Matcher roleHierarchyMatcher = pattern.matcher(roleHierarchyStringRepresentation);
rolesReachableInOneStepMap = new HashMap(); rolesReachableInOneStepMap = new HashMap<GrantedAuthority, Set<GrantedAuthority>>();
while (roleHierarchyMatcher.find()) { while (roleHierarchyMatcher.find()) {
GrantedAuthority higherRole = new GrantedAuthorityImpl(roleHierarchyMatcher.group(2)); GrantedAuthority higherRole = new GrantedAuthorityImpl(roleHierarchyMatcher.group(2));
GrantedAuthority lowerRole = new GrantedAuthorityImpl(roleHierarchyMatcher.group(3)); GrantedAuthority lowerRole = new GrantedAuthorityImpl(roleHierarchyMatcher.group(3));
Set rolesReachableInOneStepSet = null; Set<GrantedAuthority> rolesReachableInOneStepSet = null;
if (!rolesReachableInOneStepMap.containsKey(higherRole)) { if (!rolesReachableInOneStepMap.containsKey(higherRole)) {
rolesReachableInOneStepSet = new HashSet(); rolesReachableInOneStepSet = new HashSet<GrantedAuthority>();
rolesReachableInOneStepMap.put(higherRole, rolesReachableInOneStepSet); rolesReachableInOneStepMap.put(higherRole, rolesReachableInOneStepSet);
} else { } else {
rolesReachableInOneStepSet = (Set) rolesReachableInOneStepMap.get(higherRole); rolesReachableInOneStepSet = rolesReachableInOneStepMap.get(higherRole);
} }
rolesReachableInOneStepSet.add(lowerRole); rolesReachableInOneStepSet.add(lowerRole);
@ -159,19 +158,17 @@ public class RoleHierarchyImpl implements RoleHierarchy {
* hierarchy definition is detected) * hierarchy definition is detected)
*/ */
private void buildRolesReachableInOneOrMoreStepsMap() { private void buildRolesReachableInOneOrMoreStepsMap() {
rolesReachableInOneOrMoreStepsMap = new HashMap(); rolesReachableInOneOrMoreStepsMap = new HashMap<GrantedAuthority, Set<GrantedAuthority>>();
// iterate over all higher roles from rolesReachableInOneStepMap // iterate over all higher roles from rolesReachableInOneStepMap
Iterator roleIterator = rolesReachableInOneStepMap.keySet().iterator();
while (roleIterator.hasNext()) { for(GrantedAuthority role : rolesReachableInOneStepMap.keySet()) {
GrantedAuthority role = (GrantedAuthority) roleIterator.next(); Set<GrantedAuthority> rolesToVisitSet = new HashSet<GrantedAuthority>();
Set rolesToVisitSet = new HashSet();
if (rolesReachableInOneStepMap.containsKey(role)) { if (rolesReachableInOneStepMap.containsKey(role)) {
rolesToVisitSet.addAll((Set) rolesReachableInOneStepMap.get(role)); rolesToVisitSet.addAll(rolesReachableInOneStepMap.get(role));
} }
Set visitedRolesSet = new HashSet(); Set<GrantedAuthority> visitedRolesSet = new HashSet<GrantedAuthority>();
while (!rolesToVisitSet.isEmpty()) { while (!rolesToVisitSet.isEmpty()) {
// take a role from the rolesToVisit set // take a role from the rolesToVisit set
@ -179,7 +176,7 @@ public class RoleHierarchyImpl implements RoleHierarchy {
rolesToVisitSet.remove(aRole); rolesToVisitSet.remove(aRole);
visitedRolesSet.add(aRole); visitedRolesSet.add(aRole);
if (rolesReachableInOneStepMap.containsKey(aRole)) { if (rolesReachableInOneStepMap.containsKey(aRole)) {
Set newReachableRoles = (Set) rolesReachableInOneStepMap.get(aRole); Set<GrantedAuthority> newReachableRoles = rolesReachableInOneStepMap.get(aRole);
// definition of a cycle: you can reach the role you are starting from // definition of a cycle: you can reach the role you are starting from
if (rolesToVisitSet.contains(role) || visitedRolesSet.contains(role)) { if (rolesToVisitSet.contains(role) || visitedRolesSet.contains(role)) {