KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > NestableGroup


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.security;
8
9 import java.security.Principal JavaDoc;
10 import java.security.acl.Group JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.LinkedList JavaDoc;
13
14 /** An implementation of Group that allows that acts as a stack of Groups
15 with a single Group member active at any time.
16 When one adds a Group to a NestableGroup the Group is pushed onto
17 the active Group stack and any of the Group methods operate as though the
18 NestableGroup contains only the Group. When removing the Group that
19 corresponds to the active Group, the active Group is popped from the stack and
20 the new active Group is set to the new top of the stack.
21
22 The typical usage of this class is when doing a JAAS LoginContext login
23 to runAs a new Principal with a new set of roles that should be added
24 without destroying the current identity and roles.
25
26 @author Scott.Stark@jboss.org
27 @version $Revision: 1.7.6.1 $
28 */

29 public class NestableGroup extends SimplePrincipal implements Group JavaDoc
30 {
31     /** The stack of the Groups. Elements are pushed/poped by
32         inserting/removing element 0.
33     */

34     private LinkedList JavaDoc rolesStack;
35
36     /** Creates new NestableGroup with the given name
37     */

38     public NestableGroup(String JavaDoc name)
39     {
40         super(name);
41         rolesStack = new LinkedList JavaDoc();
42     }
43
44 // --- Begin Group interface methods
45
/** Returns an enumeration that contains the single active Principal.
46     @return an Enumeration of the single active Principal.
47     */

48     public Enumeration JavaDoc members()
49     {
50         return new IndexEnumeration();
51     }
52
53     /** Removes the first occurence of user from the Principal stack.
54
55     @param user the principal to remove from this group.
56     @return true if the principal was removed, or
57      * false if the principal was not a member.
58     */

59     public boolean removeMember(Principal JavaDoc user)
60     {
61         return rolesStack.remove(user);
62     }
63
64     /** Pushes the group onto the Group stack and makes it the active
65         Group.
66     @param group the instance of Group that contains the roles to set as the
67         active Group.
68     @exception IllegalArgumentException thrown if group is not an instance of Group.
69     @return true always.
70     */

71     public boolean addMember(Principal JavaDoc group) throws IllegalArgumentException JavaDoc
72     {
73         if( (group instanceof Group JavaDoc) == false )
74             throw new IllegalArgumentException JavaDoc("The addMember argument must be a Group");
75
76         rolesStack.addFirst(group);
77         return true;
78     }
79
80     /** Returns true if the passed principal is a member of the active group.
81         This method does a recursive search, so if a principal belongs to a
82         group which is a member of this group, true is returned.
83
84      @param member the principal whose membership is to be checked.
85
86      @return true if the principal is a member of this group, false otherwise.
87     */

88     public boolean isMember(Principal JavaDoc member)
89     {
90         if( rolesStack.size() == 0 )
91             return false;
92         Group JavaDoc activeGroup = (Group JavaDoc) rolesStack.getFirst();
93         boolean isMember = activeGroup.isMember(member);
94         return isMember;
95     }
96
97    public String JavaDoc toString()
98    {
99       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc(getName());
100       tmp.append("(members:");
101       Enumeration JavaDoc 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 // --- End Group interface methods
111

112     private class IndexEnumeration implements Enumeration JavaDoc
113     {
114         private Enumeration JavaDoc iter;
115
116         IndexEnumeration()
117         {
118             if( rolesStack.size() > 0 )
119             {
120                 Group JavaDoc grp = (Group JavaDoc) 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 JavaDoc nextElement()
130         {
131             Object JavaDoc next = null;
132             if( iter != null )
133                 next = iter.nextElement();
134             return next;
135         }
136     }
137 }
138
Popular Tags