KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > userrepository > AbstractUsersRepository


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * 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 *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.userrepository;
19
20 import org.apache.avalon.framework.component.Component;
21 import org.apache.avalon.framework.logger.AbstractLogEnabled;
22 import org.apache.james.services.User;
23 import org.apache.james.services.UsersRepository;
24
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * A partial implementation of a Repository to store users.
31  * <p>This implements common functionality found in different UsersRespository
32  * implementations, and makes it easier to create new User repositories.</p>
33  *
34  */

35 public abstract class AbstractUsersRepository
36     extends AbstractLogEnabled
37     implements UsersRepository, Component {
38
39     //
40
// Core Abstract methods - override these for a functional UserRepository.
41
//
42

43     /**
44      * Returns a list populated with all of the Users in the repository.
45      * @return an <code>Iterator</code> of <code>User</code>s.
46      */

47     protected abstract Iterator JavaDoc listAllUsers();
48
49     /**
50      * Adds a user to the underlying Repository.
51      * The user name must not clash with an existing user.
52      */

53     protected abstract void doAddUser(User user);
54
55     /**
56      * Removes a user from the underlying repository.
57      * If the user doesn't exist, returns ok.
58      */

59     protected abstract void doRemoveUser(User user);
60
61     /**
62      * Updates a user record to match the supplied User.
63      */

64     protected abstract void doUpdateUser(User user);
65
66     //
67
// Extended protected methods.
68
// These provide very basic default implementations, which will work,
69
// but may need to be overridden by subclasses for performance reasons.
70
//
71
/**
72      * Produces the complete list of User names, with correct case.
73      * @return a <code>List</code> of <code>String</code>s representing
74      * user names.
75      */

76     protected List JavaDoc listUserNames() {
77         Iterator JavaDoc users = listAllUsers();
78         List JavaDoc userNames = new LinkedList JavaDoc();
79         while ( users.hasNext() ) {
80             User user = (User)users.next();
81             userNames.add(user.getUserName());
82         }
83
84         return userNames;
85     }
86
87     /**
88      * Gets a user by name, ignoring case if specified.
89      * This implementation gets the entire set of users,
90      * and scrolls through searching for one matching <code>name</code>.
91      *
92      * @param name the name of the user being retrieved
93      * @param ignoreCase whether the name is regarded as case-insensitive
94      *
95      * @return the user being retrieved, null if the user doesn't exist
96      */

97     protected User getUserByName(String JavaDoc name, boolean ignoreCase) {
98         // Just iterate through all of the users until we find one matching.
99
Iterator JavaDoc users = listAllUsers();
100         while ( users.hasNext() ) {
101             User user = (User)users.next();
102             String JavaDoc username = user.getUserName();
103             if (( !ignoreCase && username.equals(name) ) ||
104                 ( ignoreCase && username.equalsIgnoreCase(name) )) {
105                 return user;
106             }
107         }
108         // Not found - return null
109
return null;
110     }
111
112     //
113
// UsersRepository interface implementation.
114
//
115
/**
116      * Adds a user to the repository with the specified User object.
117      * Users names must be unique-case-insensitive in the repository.
118      *
119      * @param user the user to be added
120      *
121      * @return true if succesful, false otherwise
122      * @since James 1.2.2
123      */

124     public boolean addUser(User user) {
125         String JavaDoc username = user.getUserName();
126
127         if ( containsCaseInsensitive(username) ) {
128             return false;
129         }
130         
131         doAddUser(user);
132         return true;
133     }
134
135     /**
136      * Adds a user to the repository with the specified attributes. In current
137      * implementations, the Object attributes is generally a String password.
138      *
139      * @param name the name of the user to be added
140      * @param attributes the password value as a String
141      */

142     public void addUser(String JavaDoc name, Object JavaDoc attributes) {
143         if (attributes instanceof String JavaDoc) {
144             User newbie = new DefaultUser(name, "SHA");
145             newbie.setPassword( (String JavaDoc) attributes );
146             addUser(newbie);
147         } else {
148             throw new RuntimeException JavaDoc("Improper use of deprecated method"
149                                        + " - use addUser(User user)");
150         }
151     }
152
153     /**
154      * Update the repository with the specified user object. A user object
155      * with this username must already exist.
156      *
157      * @param user the user to be updated
158      *
159      * @return true if successful.
160      */

161     public boolean updateUser(User user) {
162         // Return false if it's not found.
163
if ( ! contains(user.getUserName()) ) {
164             return false;
165         }
166         else {
167             doUpdateUser(user);
168             return true;
169         }
170     }
171
172     /**
173      * Removes a user from the repository
174      *
175      * @param user the user to be removed
176      */

177     public void removeUser(String JavaDoc name) {
178         User user = getUserByName(name);
179         if ( user != null ) {
180             doRemoveUser(user);
181         }
182     }
183
184     /**
185      * Gets the attribute for a user. Not clear on behavior.
186      *
187      * @deprecated As of James 1.2.2 . Use the {@link #getUserByName(String) getUserByName} method.
188      */

189     public Object JavaDoc getAttributes(String JavaDoc name) {
190         throw new RuntimeException JavaDoc("Improper use of deprecated method - read javadocs");
191     }
192
193     /**
194      * Get the user object with the specified user name. Return null if no
195      * such user.
196      *
197      * @param name the name of the user to retrieve
198      *
199      * @return the user if found, null otherwise
200      *
201      * @since James 1.2.2
202      */

203     public User getUserByName(String JavaDoc name) {
204         return getUserByName(name, false);
205     }
206
207     /**
208      * Get the user object with the specified user name. Match user naems on
209      * a case insensitive basis. Return null if no such user.
210      *
211      * @param name the name of the user to retrieve
212      *
213      * @return the user if found, null otherwise
214      *
215      * @since James 1.2.2
216      */

217     public User getUserByNameCaseInsensitive(String JavaDoc name) {
218         return getUserByName(name, true);
219     }
220
221     /**
222      * Returns the user name of the user matching name on an equalsIgnoreCase
223      * basis. Returns null if no match.
224      *
225      * @param name the name of the user to retrieve
226      *
227      * @return the correct case sensitive name of the user
228      */

229     public String JavaDoc getRealName(String JavaDoc name) {
230         // Get the user by name, ignoring case, and return the correct name.
231
User user = getUserByName(name, true);
232         if ( user == null ) {
233             return null;
234         } else {
235             return user.getUserName();
236         }
237     }
238
239     /**
240      * Returns whether or not this user is in the repository
241      */

242     public boolean contains(String JavaDoc name) {
243         User user = getUserByName(name, false);
244         return ( user != null );
245     }
246
247     /**
248      * Returns whether or not this user is in the repository. Names are
249      * matched on a case insensitive basis.
250      */

251     public boolean containsCaseInsensitive(String JavaDoc name) {
252         User user = getUserByName( name, true );
253         return ( user != null );
254     }
255
256     /**
257      * Tests a user with the appropriate attributes. In current implementations,
258      * this typically means "check the password" where a String password is passed
259      * as the Object attributes.
260      *
261      * @param name the name of the user to be tested
262      * @param attributes the password to be tested
263      *
264      * @throws UnsupportedOperationException always, as this method should not be used
265      *
266      * @deprecated As of James 1.2.2, use {@link #test(String, String) test(String name, String password)}
267      */

268     public boolean test(String JavaDoc name, Object JavaDoc attributes) {
269         throw new UnsupportedOperationException JavaDoc("Improper use of deprecated method - read javadocs");
270     }
271
272     /**
273      * Test if user with name 'name' has password 'password'.
274      *
275      * @param name the name of the user to be tested
276      * @param password the password to be tested
277      *
278      * @return true if the test is successful, false if the
279      * password is incorrect or the user doesn't
280      * exist
281      * @since James 1.2.2
282      */

283     public boolean test(String JavaDoc name, String JavaDoc password) {
284         User user = getUserByName(name, false);
285         if ( user == null ) {
286             return false;
287         } else {
288             return user.verifyPassword(password);
289         }
290     }
291
292     /**
293      * Returns a count of the users in the repository.
294      *
295      * @return the number of users in the repository
296      */

297     public int countUsers() {
298         List JavaDoc usernames = listUserNames();
299         return usernames.size();
300     }
301
302     /**
303      * List users in repository.
304      *
305      * @return Iterator over a collection of Strings, each being one user in the repository.
306      */

307     public Iterator JavaDoc list() {
308         return listUserNames().iterator();
309     }
310 }
311
Popular Tags