KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > security > struct > JGroup


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@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 any 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  * --------------------------------------------------------------------------
22  * $Id: JGroup.java 1121 2006-09-27 08:51:06Z benoitf $
23  * --------------------------------------------------------------------------
24  */

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

39 public class JGroup implements Group JavaDoc, Serializable JavaDoc {
40
41     /**
42      * UID for serialization.
43      */

44     private static final long serialVersionUID = 7035827226889396034L;
45
46     /**
47      * Name of this group.
48      */

49     private String JavaDoc groupName = null;
50
51     /**
52      * Members of this group.
53      */

54     private Vector JavaDoc<Principal JavaDoc> members = null;
55
56     /**
57      * Build a new group with the following name.
58      * @param groupName name of the group
59      */

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

73     @Override JavaDoc
74     public boolean equals(final Object JavaDoc another) {
75         if (!(another instanceof Group JavaDoc)) {
76             return false;
77         }
78         // else
79
return groupName.equals(((Group JavaDoc) another).getName());
80     }
81
82     /**
83      * Returns a string representation of this principal.
84      * @return a string representation of this principal.
85      */

86     @Override JavaDoc
87     public String JavaDoc toString() {
88         return "Principal[" + groupName + "]";
89     }
90
91     /**
92      * Returns a hashcode for this principal.
93      * @return a hashcode for this principal.
94      */

95     @Override JavaDoc
96     public int hashCode() {
97         return groupName.hashCode();
98     }
99
100     /**
101      * Returns the name of this principal.
102      * @return the name of this principal.
103      */

104     public String JavaDoc getName() {
105         return groupName;
106     }
107
108     /**
109      * Adds the specified member to the group.
110      * @param user the principal to add to this group.
111      * @return true if the member was successfully added, false if the principal
112      * was already a member.
113      */

114     public boolean addMember(final Principal JavaDoc user) {
115         if (isMember(user)) {
116             return false;
117         }
118         // else
119
members.add(user);
120         return true;
121     }
122
123     /**
124      * Removes the specified member from the group.
125      * @param user the principal to remove from this group.
126      * @return true if the principal was removed, or false if the principal was
127      * not a member.
128      */

129     public boolean removeMember(final Principal JavaDoc user) {
130         if (!isMember(user)) {
131             return false;
132         }
133         // else
134
members.remove(user);
135         return true;
136     }
137
138     /**
139      * Returns true if the passed principal is a member of the group. This
140      * method does a recursive search, so if a principal belongs to a group
141      * which is a member of this group, true is returned.
142      * @param member the principal whose membership is to be checked.
143      * @return true if the principal is a member of this group, false otherwise.
144      */

145     public boolean isMember(final Principal JavaDoc member) {
146         return members.contains(member);
147     }
148
149     /**
150      * Returns an enumeration of the members in the group. The returned objects
151      * can be instances of either Principal or Group (which is a subclass of
152      * Principal).
153      * @return an enumeration of the group members.
154      */

155     public Enumeration JavaDoc<? extends Principal JavaDoc> members() {
156         return members.elements();
157     }
158
159 }
160
Popular Tags