1 7 package org.jboss.security; 8 9 import java.security.Principal ; 10 import java.security.acl.Group ; 11 import java.util.Enumeration ; 12 import java.util.LinkedList ; 13 14 29 public class NestableGroup extends SimplePrincipal implements Group 30 { 31 34 private LinkedList rolesStack; 35 36 38 public NestableGroup(String name) 39 { 40 super(name); 41 rolesStack = new LinkedList (); 42 } 43 44 48 public Enumeration members() 49 { 50 return new IndexEnumeration(); 51 } 52 53 59 public boolean removeMember(Principal user) 60 { 61 return rolesStack.remove(user); 62 } 63 64 71 public boolean addMember(Principal group) throws IllegalArgumentException 72 { 73 if( (group instanceof Group ) == false ) 74 throw new IllegalArgumentException ("The addMember argument must be a Group"); 75 76 rolesStack.addFirst(group); 77 return true; 78 } 79 80 88 public boolean isMember(Principal member) 89 { 90 if( rolesStack.size() == 0 ) 91 return false; 92 Group activeGroup = (Group ) rolesStack.getFirst(); 93 boolean isMember = activeGroup.isMember(member); 94 return isMember; 95 } 96 97 public String toString() 98 { 99 StringBuffer tmp = new StringBuffer (getName()); 100 tmp.append("(members:"); 101 Enumeration iter = members(); 102 while( iter.hasMoreElements() ) 103 { 104 tmp.append(iter.nextElement()); 105 tmp.append(','); 106 } 107 tmp.setCharAt(tmp.length()-1, ')'); 108 return tmp.toString(); 109 } 110 112 private class IndexEnumeration implements Enumeration 113 { 114 private Enumeration iter; 115 116 IndexEnumeration() 117 { 118 if( rolesStack.size() > 0 ) 119 { 120 Group grp = (Group ) rolesStack.get(0); 121 iter = grp.members(); 122 } 123 } 124 public boolean hasMoreElements() 125 { 126 boolean hasMore = iter != null && iter.hasMoreElements(); 127 return hasMore; 128 } 129 public Object nextElement() 130 { 131 Object next = null; 132 if( iter != null ) 133 next = iter.nextElement(); 134 return next; 135 } 136 } 137 } 138 | Popular Tags |