KickJava   Java API By Example, From Geeks To Geeks.

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


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 Principals
15 with a single Principal Group member active at any time.
16 When one adds a Principal to a NestablePrincipal the Principal is pushed onto
17 the active Princpal stack and any of the Group methods operate as though the
18 Group contains only the Principal. When removing the Principal that corresponds
19 to the active Principal, the active Principal is popped from the stack and
20 the new active Principal is effectively 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 CallerPrincipal identity
24 without destroying the current CallerPrincipal identity and roles.
25
26 @author Scott.Stark@jboss.org
27 @version $Revision: 1.2.26.1 $
28 */

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

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

38     public NestablePrincipal(String JavaDoc name)
39     {
40         super(name);
41         principalStack = 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 principalStack.remove(user);
62     }
63
64     /** Pushes the user onto the Principal stack and makes it the active
65         Principal.
66     @return true always.
67     */

68     public boolean addMember(Principal JavaDoc user)
69     {
70         principalStack.addFirst(user);
71         return true;
72     }
73
74     /**
75      * Returns true if the passed principal is a member of the group.
76      * This method does a recursive search, so if a principal belongs to a
77      * group which is a member of this group, true is returned.
78      *
79      * @param member the principal whose membership is to be checked.
80      *
81      * @return true if the principal is a member of this group,
82      * false otherwise.
83      */

84     public boolean isMember(Principal JavaDoc member)
85     {
86         if( principalStack.size() == 0 )
87             return false;
88
89         Object JavaDoc activePrincipal = principalStack.getFirst();
90         return member.equals(activePrincipal);
91     }
92 // --- End Group interface methods
93

94     private class IndexEnumeration implements Enumeration JavaDoc
95     {
96         private boolean hasMoreElements;
97
98         IndexEnumeration()
99         {
100             hasMoreElements = principalStack.size() > 0;
101         }
102         public boolean hasMoreElements()
103         {
104             return hasMoreElements;
105         }
106         public Object JavaDoc nextElement()
107         {
108             Object JavaDoc next = principalStack.getFirst();
109             hasMoreElements = false;
110             return next;
111         }
112     }
113 }
114
Popular Tags