KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > security > realm > principals > Group


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: Group.java,v 1.3 2004/05/25 15:13:29 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.security.realm.principals;
28
29 import java.io.Serializable JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 import org.objectweb.jonas.security.realm.lib.XML;
35
36 /**
37  * This class define the Group class which represent a group with its associated
38  * roles
39  * @author Florent Benoit
40  */

41
42 public class Group implements Serializable JavaDoc, GroupMBean {
43
44     /**
45      * Separator of the roles
46      */

47     protected static final String JavaDoc SEPARATOR = ",";
48
49     /**
50      * Name of the user
51      */

52     private String JavaDoc name = null;
53
54     /**
55      * Roles
56      */

57     private Vector JavaDoc roles = new Vector JavaDoc();
58
59     /**
60      * Description of the role
61      */

62     private String JavaDoc description = null;
63
64     /**
65      * Default Constructor
66      */

67     public Group() {
68
69     }
70
71     /**
72      * Constructor with a given name
73      * @param name the name of this group
74      */

75     public Group(String JavaDoc name) {
76         setName(name);
77     }
78
79     /**
80      * Set the name of this user
81      * @param name Name of the user
82      */

83     public void setName(String JavaDoc name) {
84         this.name = name;
85     }
86
87     /**
88      * Get the name of this user
89      * @return the name of this user
90      */

91     public String JavaDoc getName() {
92         return name;
93     }
94
95     /**
96      * Set the description of this group
97      * @param description description of the group
98      */

99     public void setDescription(String JavaDoc description) {
100         this.description = description;
101     }
102
103     /**
104      * Get the description of this group
105      * @return the description of this group
106      */

107     public String JavaDoc getDescription() {
108         return description;
109     }
110
111     /**
112      * Set the roles of the group
113      * @param roles the list of the roles of the group
114      */

115     public void setRoles(String JavaDoc roles) {
116         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(roles, SEPARATOR);
117         String JavaDoc role = null;
118         while (st.hasMoreTokens()) {
119             role = st.nextToken().trim();
120             addRole(role);
121         }
122     }
123
124     /**
125      * Add a role to this group
126      * @param role the given role
127      */

128     public void addRole(String JavaDoc role) {
129         if (!roles.contains(role)) {
130             this.roles.addElement(role);
131         }
132     }
133
134     /**
135      * Remove a role from this group
136      * @param role the given role
137      */

138     public void removeRole(String JavaDoc role) {
139         if (roles.contains(role)) {
140             this.roles.removeElement(role);
141         }
142     }
143
144     /**
145      * Get the roles
146      * @return the array of the roles
147      */

148     public String JavaDoc getRoles() {
149         String JavaDoc rolesList = "";
150         Enumeration JavaDoc r = roles.elements();
151         int nb = 0;
152         String JavaDoc role = null;
153
154         while (r.hasMoreElements()) {
155             if (nb > 0) {
156                 rolesList += ", ";
157             }
158             role = (String JavaDoc) r.nextElement();
159             rolesList += role;
160         }
161         return rolesList;
162     }
163
164     /**
165      * Get the roles
166      * @return the array of the roles
167      */

168     public String JavaDoc[] getArrayRoles() {
169         return ((String JavaDoc[]) roles.toArray(new String JavaDoc[roles.size()]));
170     }
171
172     /**
173      * String representation of the group
174      * @return the xml representation of the group
175      */

176     public String JavaDoc toXML() {
177         StringBuffer JavaDoc xml = new StringBuffer JavaDoc("<group name=\"");
178         xml.append(name);
179         xml.append("\" description=\"");
180         if (description != null) {
181             xml.append(description);
182         }
183         xml.append("\"");
184         XML.appendVectorToBuffer("roles=", xml, roles);
185         xml.append(" />");
186         return xml.toString();
187     }
188
189     /**
190      * Use the XML representation of this object
191      * @return the XML representation of this object
192      */

193     public String JavaDoc toString() {
194         return this.toXML();
195     }
196
197 }
Popular Tags