KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > security > auth > JGroup


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 1any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: JGroup.java,v 1.4 2004/07/04 19:35:33 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.security.auth;
28
29 import java.io.Serializable JavaDoc;
30 import java.security.Principal JavaDoc;
31 import java.security.acl.Group JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 /**
36  * This class represents all the roles of a principal (group of principals).
37  * These roles are added to the Subject
38  * @author Florent Benoit
39  */

40 public class JGroup implements Group JavaDoc, Serializable JavaDoc {
41
42
43     /**
44      * Name of this group
45      */

46     private String JavaDoc name = null;
47
48     /**
49      * Members of this group
50      */

51     private Vector JavaDoc members = null;
52
53
54     /**
55      * Build a new group with the following name
56      * @param name name of the group
57      */

58     public JGroup(String JavaDoc name) {
59         this.name = name;
60         this.members = new Vector JavaDoc();
61
62     }
63
64     /**
65      * Compares this principal to the specified object. Returns true if the object passed in matches the principal represented by the implementation of this interface.
66      * @param another principal to compare with.
67      * @return true if the principal passed in is the same as that encapsulated by this principal, and false otherwise.
68      */

69     public boolean equals(Object JavaDoc another) {
70         if (!(another instanceof Group JavaDoc)) {
71             return false;
72         }
73         // else
74
return name.equals(((Group JavaDoc) another).getName());
75     }
76
77     /**
78      * Returns a string representation of this principal.
79      * @return a string representation of this principal.
80      */

81     public String JavaDoc toString() {
82         return "Principal[" + name + "]";
83     }
84
85
86     /**
87      * Returns a hashcode for this principal.
88      * @return a hashcode for this principal.
89      */

90     public int hashCode() {
91         return name.hashCode();
92     }
93
94     /**
95      * Returns the name of this principal.
96      * @return the name of this principal.
97      */

98     public String JavaDoc getName() {
99         return name;
100     }
101
102     /**
103      * Adds the specified member to the group.
104      * @param user the principal to add to this group.
105      * @return true if the member was successfully added, false if the principal was already a member.
106      */

107     public boolean addMember(Principal JavaDoc user) {
108         if (isMember(user)) {
109             return false;
110         }
111         // else
112
members.add(user);
113         return true;
114     }
115
116     /**
117      * Removes the specified member from the group.
118      * @param user the principal to remove from this group.
119      * @return true if the principal was removed, or false if the principal was not a member.
120      */

121     public boolean removeMember(Principal JavaDoc user) {
122         if (!isMember(user)) {
123             return false;
124         }
125         // else
126
members.remove(user);
127         return true;
128     }
129
130
131     /**
132      * Returns true if the passed principal is a member of the group. This method does a recursive search, so if a principal belongs to a group which is a member of this group, true is returned.
133      * @param member the principal whose membership is to be checked.
134      * @return true if the principal is a member of this group, false otherwise.
135      */

136     public boolean isMember(Principal JavaDoc member) {
137         return members.contains(member);
138     }
139
140
141     /**
142      * Returns an enumeration of the members in the group. The returned objects can be instances of either Principal or Group (which is a subclass of Principal).
143      * @return an enumeration of the group members.
144      */

145     public Enumeration JavaDoc members() {
146         return members.elements();
147     }
148
149 }
150
Popular Tags