KickJava   Java API By Example, From Geeks To Geeks.

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


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: User.java,v 1.5 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.security.Principal JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 import org.objectweb.jonas.security.realm.lib.HashHelper;
37 import org.objectweb.jonas.security.realm.lib.HashPassword;
38 import org.objectweb.jonas.security.realm.lib.XML;
39
40 /**
41  * This class define the User class which represent a user by its name,
42  * password, etc.
43  * @author Florent Benoit (initial developer)
44  * @author Alexandre Thaveau (add DN for the certificates in method setName)
45  * @author Marc-Antoine Bourgeot (add DN for the certificates in method setName)
46  */

47 public class User implements Principal JavaDoc, Serializable JavaDoc, UserMBean {
48
49     /**
50      * Separator of the groups/roles
51      */

52     protected static final String JavaDoc SEPARATOR = ",";
53
54     /**
55      * Name of the user
56      */

57     private String JavaDoc name = null;
58
59     /**
60      * Password of the user
61      */

62     private String JavaDoc password = null;
63
64     /**
65      * Hash password of the user
66      */

67     private HashPassword hashPassword = null;
68
69     /**
70      * Groups
71      */

72     private Vector JavaDoc groups = new Vector JavaDoc();
73
74     /**
75      * Roles
76      */

77     private Vector JavaDoc roles = new Vector JavaDoc();
78
79     /**
80      * Combined roles
81      */

82     private ArrayList JavaDoc combinedRoles = new ArrayList JavaDoc();
83
84     /**
85      * Constructor
86      */

87     public User() {
88     }
89
90     /**
91      * Constructor with a given login / password
92      * @param name the given name
93      * @param password the given password
94      */

95     public User(String JavaDoc name, String JavaDoc password) {
96         setName(name);
97         setPassword(password);
98     }
99
100     /**
101      * Set the name of this user
102      * @param name Name of the user
103      */

104     public void setName(String JavaDoc name) {
105         if (name.startsWith("##DN##")) {
106             //replace problematic caracters(for mbeans) by special caracters
107
this.name = name.replace('=', '#').replace(',', '%').replace(' ', '$');
108         } else {
109             this.name = name;
110         }
111     }
112
113     /**
114      * Get the name of this user
115      * @return the name of this user
116      */

117     public String JavaDoc getName() {
118         return name;
119     }
120
121     /**
122      * Get the password of this user
123      * @return the password of this user
124      */

125     public String JavaDoc getPassword() {
126         return password;
127     }
128
129     /**
130      * Set the password of this user
131      * @param password password of the user
132      */

133     public void setPassword(String JavaDoc password) {
134         this.password = password;
135         setHashPassword(HashHelper.getHashPassword(password));
136     }
137
138     /**
139      * Set the hashed password of this user
140      * @param hashPassword hashed password of this user
141      */

142     protected void setHashPassword(HashPassword hashPassword) {
143         this.hashPassword = hashPassword;
144     }
145
146     /**
147      * Set the hashed password of this user
148      * @return hashPassword hashed password of this user
149      */

150     public HashPassword getHashPassword() {
151         return hashPassword;
152     }
153
154     /**
155      * Set the groups of the user
156      * @param groups the comma separated list of the groups of the user
157      */

158     public void setGroups(String JavaDoc groups) {
159         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(groups, SEPARATOR);
160         String JavaDoc group = null;
161         while (st.hasMoreTokens()) {
162             group = st.nextToken().trim();
163             addGroup(group);
164         }
165     }
166
167     /**
168      * Get the groups
169      * @return the comma separated list of groups
170      */

171     public String JavaDoc getGroups() {
172         String JavaDoc groupsList = "";
173         Enumeration JavaDoc g = groups.elements();
174         int nb = 0;
175         String JavaDoc group = null;
176
177         while (g.hasMoreElements()) {
178             if (nb > 0) {
179                 groupsList += ", ";
180             }
181             group = (String JavaDoc) g.nextElement();
182             groupsList += group;
183         }
184         return groupsList;
185
186     }
187
188     /**
189      * Get the groups
190      * @return the array of the groups
191      */

192     public String JavaDoc[] getArrayGroups() {
193         return ((String JavaDoc[]) groups.toArray(new String JavaDoc[groups.size()]));
194     }
195
196     /**
197      * Set the roles of the user
198      * @param roles the comma separated list of the roles of the user
199      */

200     public void setRoles(String JavaDoc roles) {
201         if (roles != null) {
202             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(roles, SEPARATOR);
203             String JavaDoc role = null;
204             while (st.hasMoreTokens()) {
205                 role = st.nextToken().trim();
206                 addRole(role);
207             }
208         }
209     }
210
211     /**
212      * Add the specified group to this user
213      * @param group the group to add
214      */

215     public void addGroup(String JavaDoc group) {
216         if (!groups.contains(group)) {
217             this.groups.addElement(group);
218         }
219     }
220
221     /**
222      * Add a role to this user
223      * @param role the given role
224      */

225     public void addRole(String JavaDoc role) {
226         if (!roles.contains(role)) {
227             this.roles.addElement(role);
228         }
229     }
230
231     /**
232      * Remove a group from this user
233      * @param group the given group
234      */

235     public void removeGroup(String JavaDoc group) {
236         if (groups.contains(group)) {
237             this.groups.removeElement(group);
238         }
239     }
240
241     /**
242      * Remove a role from this user
243      * @param role the given role
244      */

245     public void removeRole(String JavaDoc role) {
246         if (roles.contains(role)) {
247             this.roles.removeElement(role);
248         }
249     }
250
251     /**
252      * Get the roles
253      * @return the array of the roles
254      */

255     public String JavaDoc getRoles() {
256         String JavaDoc rolesList = "";
257         Enumeration JavaDoc r = roles.elements();
258         int nb = 0;
259         String JavaDoc role = null;
260
261         while (r.hasMoreElements()) {
262             if (nb > 0) {
263                 rolesList += ", ";
264             }
265             role = (String JavaDoc) r.nextElement();
266             rolesList += role;
267         }
268         return rolesList;
269     }
270
271     /**
272      * Set the combined roles of this user
273      * @param combinedRoles combined of the user
274      */

275     public void setCombinedRoles(ArrayList JavaDoc combinedRoles) {
276         this.combinedRoles = combinedRoles;
277     }
278
279     /**
280      * Get the combined roles of this user
281      * @return the combined of the user
282      */

283     public ArrayList JavaDoc getCombinedRoles() {
284         return combinedRoles;
285     }
286
287     /**
288      * Get the roles
289      * @return the array of the roles
290      */

291     public String JavaDoc[] getArrayRoles() {
292         return ((String JavaDoc[]) roles.toArray(new String JavaDoc[roles.size()]));
293     }
294
295     /**
296      * String representation of the user
297      * @return the xml representation of the user
298      */

299     public String JavaDoc toXML() {
300         StringBuffer JavaDoc xml = new StringBuffer JavaDoc("<user name=\"");
301         xml.append(name);
302         xml.append("\" password=\"");
303         if (password != null) {
304             xml.append(password);
305         }
306         xml.append("\"");
307         XML.appendVectorToBuffer("groups=", xml, groups);
308         XML.appendVectorToBuffer("roles=", xml, roles);
309         xml.append(" />");
310         return xml.toString();
311     }
312
313     /**
314      * Use the XML representation of this object
315      * @return the XML representation of this object
316      */

317     public String JavaDoc toString() {
318         return this.toXML();
319     }
320
321 }
Popular Tags