UserManager.java 5.49 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13
package org.jivesoftware.messenger.user;

Matt Tucker's avatar
Matt Tucker committed
14 15 16 17 18 19 20 21 22
import org.jivesoftware.messenger.JiveGlobals;
import org.jivesoftware.util.Cache;
import org.jivesoftware.util.CacheManager;
import org.jivesoftware.util.ClassUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.stringprep.Stringprep;
import org.jivesoftware.stringprep.StringprepException;

import java.util.Collection;
Matt Tucker's avatar
Matt Tucker committed
23 24

/**
Matt Tucker's avatar
Matt Tucker committed
25
 * Manages users, including loading, creating and deleting.
Matt Tucker's avatar
Matt Tucker committed
26
 *
Matt Tucker's avatar
Matt Tucker committed
27
 * @author Matt Tucker
Matt Tucker's avatar
Matt Tucker committed
28 29
 * @see User
 */
Matt Tucker's avatar
Matt Tucker committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
public class UserManager {

    private static Cache userCache;
    private static UserProvider provider;
    private static UserManager instance = new UserManager();

    static {
        // Initialize caches.
        CacheManager.initializeCache("userCache", 512 * 1024);
        CacheManager.initializeCache("username2roster", 512 * 1024);
        userCache = CacheManager.getCache("userCache");
        // Load a group provider.
        String className = JiveGlobals.getXMLProperty("provider.user.className",
                "org.jivesoftware.messenger.user.DefaultUserProvider");
        try {
            Class c = ClassUtils.forName(className);
            provider = (UserProvider)c.newInstance();
        }
        catch (Exception e) {
            Log.error("Error loading user provider: " + className, e);
            provider = new DefaultUserProvider();
        }
    }

    /**
     * Returns the currently-installed UserProvider.
     *
     * @return the current UserProvider.
     */
    static UserProvider getUserProvider() {
        return provider;
    }

    public static UserManager getInstance() {
        return instance;
    }

    private UserManager() {

    }
Matt Tucker's avatar
Matt Tucker committed
70 71

    /**
Matt Tucker's avatar
Matt Tucker committed
72 73
     * Creates a new User. Required values are username and password. The email address
     * can optionally be <tt>null</tt>.
Matt Tucker's avatar
Matt Tucker committed
74 75
     *
     * @param username the new and unique username for the account.
Matt Tucker's avatar
Matt Tucker committed
76 77 78
     * @param password the password for the account (plain text).
     * @param email the email address to associate with the new account, which can
     *      be <tt>null</tt>.
Matt Tucker's avatar
Matt Tucker committed
79
     * @return a new User.
Matt Tucker's avatar
Matt Tucker committed
80 81 82
     * @throws UserAlreadyExistsException if the username already exists in the system.
     * @throws UnsupportedOperationException if the provider does not support the
     *      operation.
Matt Tucker's avatar
Matt Tucker committed
83
     */
Matt Tucker's avatar
Matt Tucker committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97
    public User createUser(String username, String password, String name, String email)
            throws UserAlreadyExistsException
    {
        // Make sure that the username is valid.
        try {
            username = Stringprep.nameprep(username, true);
        }
        catch (StringprepException se) {
            throw new IllegalArgumentException(se);
        }
        User user = provider.createUser(username, password, name, email);
        userCache.put(username, user);
        return user;
    }
Matt Tucker's avatar
Matt Tucker committed
98 99 100 101 102 103

    /**
     * Deletes a user (optional operation).
     *
     * @param user the user to delete.
     */
Matt Tucker's avatar
Matt Tucker committed
104 105 106 107 108 109 110 111 112 113 114 115 116
    public void deleteUser(User user) {
        String username = user.getUsername();
        // Make sure that the username is valid.
        try {
            username = Stringprep.nameprep(username, true);
        }
        catch (StringprepException se) {
            throw new IllegalArgumentException(se);
        }
        provider.deleteUser(user.getUsername());
        // Remove the user from cache.
        userCache.remove(user.getUsername());
    }
Matt Tucker's avatar
Matt Tucker committed
117 118 119 120 121 122 123 124

    /**
     * Returns the User specified by username.
     *
     * @param username the username of the user.
     * @return the User that matches <tt>username</tt>.
     * @throws UserNotFoundException if the user does not exist.
     */
Matt Tucker's avatar
Matt Tucker committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    public User getUser(String username) throws UserNotFoundException {
        // Make sure that the username is valid.
        try {
            username = Stringprep.nameprep(username, true);
        }
        catch (StringprepException se) {
            throw new IllegalArgumentException(se);
        }
        User user = (User)userCache.get(username);
        if (user == null) {
            user = provider.loadUser(username);
            userCache.put(username, user);
        }
        return user;
    }
Matt Tucker's avatar
Matt Tucker committed
140 141

    /**
Matt Tucker's avatar
Matt Tucker committed
142
     * Returns the total number of users in the system.
Matt Tucker's avatar
Matt Tucker committed
143 144 145
     *
     * @return the total number of users.
     */
Matt Tucker's avatar
Matt Tucker committed
146 147 148
    public int getUserCount() {
        return provider.getUserCount();
    }
Matt Tucker's avatar
Matt Tucker committed
149 150

    /**
Matt Tucker's avatar
Matt Tucker committed
151
     * Returns an unmodifiable Collection of all users in the system.
Matt Tucker's avatar
Matt Tucker committed
152
     *
Matt Tucker's avatar
Matt Tucker committed
153
     * @return an unmodifiable Collection of all users.
Matt Tucker's avatar
Matt Tucker committed
154
     */
Matt Tucker's avatar
Matt Tucker committed
155 156 157
    public Collection<User> getUsers() {
        return provider.getUsers();
    }
Matt Tucker's avatar
Matt Tucker committed
158 159

    /**
Matt Tucker's avatar
Matt Tucker committed
160 161 162 163 164 165
     * Returns an unmodifiable Collection of all users starting at <tt>startIndex</tt>
     * with the given number of results. This is useful to support pagination in a GUI
     * where you may only want to display a certain number of results per page. It is
     * possible that the number of results returned will be less than that specified
     * by <tt>numResults</tt> if <tt>numResults</tt> is greater than the number of
     * records left to display.
Matt Tucker's avatar
Matt Tucker committed
166 167 168
     *
     * @param startIndex the beginning index to start the results at.
     * @param numResults the total number of results to return.
Matt Tucker's avatar
Matt Tucker committed
169
     * @return a Collection of users in the specified range.
Matt Tucker's avatar
Matt Tucker committed
170
     */
Matt Tucker's avatar
Matt Tucker committed
171 172 173
    public Collection<User> getUsers(int startIndex, int numResults) {
        return provider.getUsers(startIndex, numResults);
    }
Matt Tucker's avatar
Matt Tucker committed
174
}