KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > users > MemoryUser


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.users;
20
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.catalina.Group;
26 import org.apache.catalina.Role;
27 import org.apache.catalina.UserDatabase;
28 import org.apache.catalina.util.RequestUtil;
29
30 /**
31  * <p>Concrete implementation of {@link org.apache.catalina.User} for the
32  * {@link MemoryUserDatabase} implementation of {@link UserDatabase}.</p>
33  *
34  * @author Craig R. McClanahan
35  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
36  * @since 4.1
37  */

38
39 public class MemoryUser extends AbstractUser {
40
41
42     // ----------------------------------------------------------- Constructors
43

44
45     /**
46      * Package-private constructor used by the factory method in
47      * {@link MemoryUserDatabase}.
48      *
49      * @param database The {@link MemoryUserDatabase} that owns this user
50      * @param username Logon username of the new user
51      * @param password Logon password of the new user
52      * @param fullName Full name of the new user
53      */

54     MemoryUser(MemoryUserDatabase database, String JavaDoc username,
55                String JavaDoc password, String JavaDoc fullName) {
56
57         super();
58         this.database = database;
59         setUsername(username);
60         setPassword(password);
61         setFullName(fullName);
62
63     }
64
65
66     // ----------------------------------------------------- Instance Variables
67

68
69     /**
70      * The {@link MemoryUserDatabase} that owns this user.
71      */

72     protected MemoryUserDatabase database = null;
73
74
75     /**
76      * The set of {@link Group}s that this user is a member of.
77      */

78     protected ArrayList JavaDoc groups = new ArrayList JavaDoc();
79
80
81     /**
82      * The set of {@link Role}s associated with this user.
83      */

84     protected ArrayList JavaDoc roles = new ArrayList JavaDoc();
85
86
87     // ------------------------------------------------------------- Properties
88

89
90     /**
91      * Return the set of {@link Group}s to which this user belongs.
92      */

93     public Iterator JavaDoc getGroups() {
94
95         synchronized (groups) {
96             return (groups.iterator());
97         }
98
99     }
100
101
102     /**
103      * Return the set of {@link Role}s assigned specifically to this user.
104      */

105     public Iterator JavaDoc getRoles() {
106
107         synchronized (roles) {
108             return (roles.iterator());
109         }
110
111     }
112
113
114     /**
115      * Return the {@link UserDatabase} within which this User is defined.
116      */

117     public UserDatabase getUserDatabase() {
118
119         return (this.database);
120
121     }
122
123
124     // --------------------------------------------------------- Public Methods
125

126
127     /**
128      * Add a new {@link Group} to those this user belongs to.
129      *
130      * @param group The new group
131      */

132     public void addGroup(Group group) {
133
134         synchronized (groups) {
135             if (!groups.contains(group)) {
136                 groups.add(group);
137             }
138         }
139
140     }
141
142
143     /**
144      * Add a new {@link Role} to those assigned specifically to this user.
145      *
146      * @param role The new role
147      */

148     public void addRole(Role role) {
149
150         synchronized (roles) {
151             if (!roles.contains(role)) {
152                 roles.add(role);
153             }
154         }
155
156     }
157
158
159     /**
160      * Is this user in the specified group?
161      *
162      * @param group The group to check
163      */

164     public boolean isInGroup(Group group) {
165
166         synchronized (groups) {
167             return (groups.contains(group));
168         }
169
170     }
171
172
173     /**
174      * Is this user specifically assigned the specified {@link Role}? This
175      * method does <strong>NOT</strong> check for roles inherited based on
176      * {@link Group} membership.
177      *
178      * @param role The role to check
179      */

180     public boolean isInRole(Role role) {
181
182         synchronized (roles) {
183             return (roles.contains(role));
184         }
185
186     }
187
188
189     /**
190      * Remove a {@link Group} from those this user belongs to.
191      *
192      * @param group The old group
193      */

194     public void removeGroup(Group group) {
195
196         synchronized (groups) {
197             groups.remove(group);
198         }
199
200     }
201
202
203     /**
204      * Remove all {@link Group}s from those this user belongs to.
205      */

206     public void removeGroups() {
207
208         synchronized (groups) {
209             groups.clear();
210         }
211
212     }
213
214
215     /**
216      * Remove a {@link Role} from those assigned to this user.
217      *
218      * @param role The old role
219      */

220     public void removeRole(Role role) {
221
222         synchronized (roles) {
223             roles.remove(role);
224         }
225
226     }
227
228
229     /**
230      * Remove all {@link Role}s from those assigned to this user.
231      */

232     public void removeRoles() {
233
234         synchronized (roles) {
235             roles.clear();
236         }
237
238     }
239
240
241     /**
242      * <p>Return a String representation of this user in XML format.</p>
243      *
244      * <p><strong>IMPLEMENTATION NOTE</strong> - For backwards compatibility,
245      * the reader that processes this entry will accept either
246      * <code>username</code> or </code>name</code> for the username
247      * property.</p>
248      */

249     public String JavaDoc toString() {
250
251         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<user username=\"");
252         sb.append(RequestUtil.filter(username));
253         sb.append("\" password=\"");
254         sb.append(RequestUtil.filter(password));
255         sb.append("\"");
256         if (fullName != null) {
257             sb.append(" fullName=\"");
258             sb.append(RequestUtil.filter(fullName));
259             sb.append("\"");
260         }
261         synchronized (groups) {
262             if (groups.size() > 0) {
263                 sb.append(" groups=\"");
264                 int n = 0;
265                 Iterator JavaDoc values = groups.iterator();
266                 while (values.hasNext()) {
267                     if (n > 0) {
268                         sb.append(',');
269                     }
270                     n++;
271                     sb.append(RequestUtil.filter(((Group) values.next()).getGroupname()));
272                 }
273                 sb.append("\"");
274             }
275         }
276         synchronized (roles) {
277             if (roles.size() > 0) {
278                 sb.append(" roles=\"");
279                 int n = 0;
280                 Iterator JavaDoc values = roles.iterator();
281                 while (values.hasNext()) {
282                     if (n > 0) {
283                         sb.append(',');
284                     }
285                     n++;
286                     sb.append(RequestUtil.filter(((Role) values.next()).getRolename()));
287                 }
288                 sb.append("\"");
289             }
290         }
291         sb.append("/>");
292         return (sb.toString());
293
294     }
295
296
297 }
298
Popular Tags