KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > user > UserManager


1 /**
2  * $RCSfile: UserManager.java,v $
3  * $Revision: 1.13 $
4  * $Date: 2005/04/11 21:11:06 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.user;
13
14 import org.jivesoftware.messenger.event.UserEventDispatcher;
15 import org.jivesoftware.stringprep.Stringprep;
16 import org.jivesoftware.stringprep.StringprepException;
17 import org.jivesoftware.util.*;
18
19 import java.util.Collection JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Set JavaDoc;
22
23 /**
24  * Manages users, including loading, creating and deleting.
25  *
26  * @author Matt Tucker
27  * @see User
28  */

29 public class UserManager {
30
31     private static Cache userCache;
32     private static UserProvider provider;
33     private static UserManager instance = new UserManager();
34
35     static {
36         // Initialize caches.
37
CacheManager.initializeCache("userCache", 512 * 1024);
38         CacheManager.initializeCache("username2roster", 512 * 1024);
39         userCache = CacheManager.getCache("userCache");
40         // Load a user provider.
41
String JavaDoc className = JiveGlobals.getXMLProperty("provider.user.className",
42                 "org.jivesoftware.messenger.user.DefaultUserProvider");
43         try {
44             Class JavaDoc c = ClassUtils.forName(className);
45             provider = (UserProvider)c.newInstance();
46         }
47         catch (Exception JavaDoc e) {
48             Log.error("Error loading user provider: " + className, e);
49             provider = new DefaultUserProvider();
50         }
51     }
52
53     /**
54      * Returns the currently-installed UserProvider. <b>Warning:</b> in virtually all
55      * cases the user provider should not be used directly. Instead, the appropriate
56      * methods in UserManager should be called. Direct access to the user provider is
57      * only provided for special-case logic.
58      *
59      * @return the current UserProvider.
60      */

61     public static UserProvider getUserProvider() {
62         return provider;
63     }
64
65     /**
66      * Returns a singleton UserManager instance.
67      *
68      * @return a UserManager instance.
69      */

70     public static UserManager getInstance() {
71         return instance;
72     }
73
74     private UserManager() {
75
76     }
77
78     /**
79      * Creates a new User. Required values are username and password. The email address
80      * can optionally be <tt>null</tt>.
81      *
82      * @param username the new and unique username for the account.
83      * @param password the password for the account (plain text).
84      * @param email the email address to associate with the new account, which can
85      * be <tt>null</tt>.
86      * @return a new User.
87      * @throws UserAlreadyExistsException if the username already exists in the system.
88      * @throws UnsupportedOperationException if the provider does not support the
89      * operation.
90      */

91     public User createUser(String JavaDoc username, String JavaDoc password, String JavaDoc name, String JavaDoc email)
92             throws UserAlreadyExistsException
93     {
94         if (provider.isReadOnly()) {
95             throw new UnsupportedOperationException JavaDoc("User provider is read-only.");
96         }
97         // Make sure that the username is valid.
98
try {
99             username = Stringprep.nodeprep(username);
100         }
101         catch (StringprepException se) {
102             throw new IllegalArgumentException JavaDoc("Invalid username: " + username, se);
103         }
104         User user = provider.createUser(username, password, name, email);
105         userCache.put(username, user);
106
107         // Fire event.
108
UserEventDispatcher.dispatchEvent(user, UserEventDispatcher.EventType.user_created,
109                 Collections.emptyMap());
110
111         return user;
112     }
113
114     /**
115      * Deletes a user (optional operation).
116      *
117      * @param user the user to delete.
118      */

119     public void deleteUser(User user) {
120         if (provider.isReadOnly()) {
121             throw new UnsupportedOperationException JavaDoc("User provider is read-only.");
122         }
123
124         String JavaDoc username = user.getUsername();
125         // Make sure that the username is valid.
126
try {
127             username = Stringprep.nodeprep(username);
128         }
129         catch (StringprepException se) {
130             throw new IllegalArgumentException JavaDoc("Invalid username: " + username, se);
131         }
132
133         // Fire event.
134
UserEventDispatcher.dispatchEvent(user, UserEventDispatcher.EventType.user_deleting,
135                 Collections.emptyMap());
136
137         provider.deleteUser(user.getUsername());
138         // Remove the user from cache.
139
userCache.remove(user.getUsername());
140     }
141
142     /**
143      * Returns the User specified by username.
144      *
145      * @param username the username of the user.
146      * @return the User that matches <tt>username</tt>.
147      * @throws UserNotFoundException if the user does not exist.
148      */

149     public User getUser(String JavaDoc username) throws UserNotFoundException {
150         // Make sure that the username is valid.
151
username = username.trim().toLowerCase();
152         User user = (User) userCache.get(username);
153         if (user == null) {
154             synchronized(username.intern()) {
155                 user = (User) userCache.get(username);
156                 if (user == null) {
157                     user = provider.loadUser(username);
158                     userCache.put(username, user);
159                 }
160             }
161         }
162         return user;
163     }
164
165     /**
166      * Returns the total number of users in the system.
167      *
168      * @return the total number of users.
169      */

170     public int getUserCount() {
171         return provider.getUserCount();
172     }
173
174     /**
175      * Returns an unmodifiable Collection of all users in the system.
176      *
177      * @return an unmodifiable Collection of all users.
178      */

179     public Collection JavaDoc<User> getUsers() {
180         return provider.getUsers();
181     }
182
183     /**
184      * Returns an unmodifiable Collection of all users starting at <tt>startIndex</tt>
185      * with the given number of results. This is useful to support pagination in a GUI
186      * where you may only want to display a certain number of results per page. It is
187      * possible that the number of results returned will be less than that specified
188      * by <tt>numResults</tt> if <tt>numResults</tt> is greater than the number of
189      * records left to display.
190      *
191      * @param startIndex the beginning index to start the results at.
192      * @param numResults the total number of results to return.
193      * @return a Collection of users in the specified range.
194      */

195     public Collection JavaDoc<User> getUsers(int startIndex, int numResults) {
196         return provider.getUsers(startIndex, numResults);
197     }
198
199     /**
200      * Returns the set of fields that can be used for searching for users. Each field
201      * returned must support wild-card and keyword searching. For example, an
202      * implementation might send back the set {"Username", "Name", "Email"}. Any of
203      * those three fields can then be used in a search with the
204      * {@link #findUsers(Set,String)} method.<p>
205      *
206      * This method should throw an UnsupportedOperationException if this
207      * operation is not supported by the backend user store.
208      *
209      * @return the valid search fields.
210      * @throws UnsupportedOperationException if the provider does not
211      * support the operation (this is an optional operation).
212      */

213     public Set JavaDoc<String JavaDoc> getSearchFields() throws UnsupportedOperationException JavaDoc {
214         return provider.getSearchFields();
215     }
216
217     /**
218      * earches for users based on a set of fields and a query string. The fields must
219      * be taken from the values returned by {@link #getSearchFields()}. The query can
220      * include wildcards. For example, a search on the field "Name" with a query of "Ma*"
221      * might return user's with the name "Matt", "Martha" and "Madeline".<p>
222      *
223      * This method should throw an UnsupportedOperationException if this
224      * operation is not supported by the backend user store.
225      *
226      * @param fields the fields to search on.
227      * @param query the query string.
228      * @return a Collection of users that match the search.
229      * @throws UnsupportedOperationException if the provider does not
230      * support the operation (this is an optional operation).
231      */

232     public Collection JavaDoc<User> findUsers(Set JavaDoc<String JavaDoc> fields, String JavaDoc query)
233             throws UnsupportedOperationException JavaDoc
234     {
235         return provider.findUsers(fields, query);
236     }
237 }
Popular Tags