KickJava   Java API By Example, From Geeks To Geeks.

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


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

7
8 package org.jboss.security;
9
10 import java.security.Principal JavaDoc;
11 import java.security.acl.Group JavaDoc;
12 import java.util.Collection JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.HashMap JavaDoc;
17
18 /** An implementation of Group that manages a collection of Principal
19 objects based on their hashCode() and equals() methods. This class
20 is not thread safe.
21
22 @author Scott.Stark@jboss.org
23 @version $Revision: 1.5 $
24 */

25 public class SimpleGroup extends SimplePrincipal implements Group JavaDoc
26 {
27     private HashMap JavaDoc members;
28
29     public SimpleGroup(String JavaDoc groupName)
30     {
31         super(groupName);
32         members = new HashMap JavaDoc(3);
33     }
34
35     /** Adds the specified member to the group.
36      @param user the principal to add to this group.
37      @return true if the member was successfully added,
38          false if the principal was already a member.
39      */

40     public boolean addMember(Principal JavaDoc user)
41     {
42         boolean isMember = members.containsKey(user);
43         if( isMember == false )
44             members.put(user, user);
45         return isMember == false;
46     }
47     /** Returns true if the passed principal is a member of the group.
48         This method does a recursive search, so if a principal belongs to a
49         group which is a member of this group, true is returned.
50
51         A special check is made to see if the member is an instance of
52         org.jboss.security.AnybodyPrincipal or org.jboss.security.NobodyPrincipal
53         since these classes do not hash to meaningful values.
54     @param member the principal whose membership is to be checked.
55     @return true if the principal is a member of this group,
56         false otherwise.
57     */

58     public boolean isMember(Principal JavaDoc member)
59     {
60         // First see if there is a key with the member name
61
boolean isMember = members.containsKey(member);
62         if( isMember == false )
63         { // Check the AnybodyPrincipal & NobodyPrincipal special cases
64
isMember = (member instanceof org.jboss.security.AnybodyPrincipal);
65             if( isMember == false )
66             {
67                 if( member instanceof org.jboss.security.NobodyPrincipal )
68                 return false;
69             }
70         }
71         if( isMember == false )
72         { // Check any Groups for membership
73
Collection JavaDoc values = members.values();
74             Iterator JavaDoc iter = values.iterator();
75             while( isMember == false && iter.hasNext() )
76             {
77                 Object JavaDoc next = iter.next();
78                 if( next instanceof Group JavaDoc )
79                 {
80                     Group JavaDoc group = (Group JavaDoc) next;
81                     isMember = group.isMember(member);
82                 }
83             }
84         }
85         return isMember;
86     }
87
88     /** Returns an enumeration of the members in the group.
89         The returned objects can be instances of either Principal
90         or Group (which is a subinterface of Principal).
91     @return an enumeration of the group members.
92     */

93     public Enumeration JavaDoc members()
94     {
95         return Collections.enumeration(members.values());
96     }
97
98     /** Removes the specified member from the group.
99     @param user the principal to remove from this group.
100     @return true if the principal was removed, or
101         false if the principal was not a member.
102     */

103     public boolean removeMember(Principal JavaDoc user)
104     {
105         Object JavaDoc prev = members.remove(user);
106         return prev != null;
107     }
108
109    public String JavaDoc toString()
110    {
111       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc(getName());
112       tmp.append("(members:");
113       Iterator JavaDoc iter = members.keySet().iterator();
114       while( iter.hasNext() )
115       {
116          tmp.append(iter.next());
117          tmp.append(',');
118       }
119       tmp.setCharAt(tmp.length()-1, ')');
120       return tmp.toString();
121    }
122 }
123
Popular Tags