usermanager.java
来自「基于Jabber协议的即时消息服务器」· Java 代码 · 共 418 行 · 第 1/2 页
JAVA
418 行
/**
* $RCSfile$
* $Revision: 1217 $
* $Date: 2005-04-11 18:11:06 -0300 (Mon, 11 Apr 2005) $
*
* Copyright (C) 2004 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.wildfire.user;
import org.dom4j.Element;
import org.jivesoftware.stringprep.Stringprep;
import org.jivesoftware.stringprep.StringprepException;
import org.jivesoftware.util.*;
import org.jivesoftware.wildfire.IQResultListener;
import org.jivesoftware.wildfire.XMPPServer;
import org.jivesoftware.wildfire.event.UserEventDispatcher;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import java.util.*;
/**
* Manages users, including loading, creating and deleting.
*
* @author Matt Tucker
* @see User
*/
public class UserManager implements IQResultListener {
/**
* Cache of local users.
*/
private static Cache<String, User> userCache;
/**
* Cache if a local or remote user exists.
*/
private static Cache<String, Boolean> remoteUsersCache;
private static UserProvider provider;
private static UserManager instance = new UserManager();
static {
// Initialize caches.
userCache = CacheManager.initializeCache("User", "userCache", 512 * 1024,
JiveConstants.MINUTE*30);
remoteUsersCache = CacheManager.initializeCache("Remote Users Existence", "remoteUsersCache",
512 * 1024, JiveConstants.MINUTE*30);
CacheManager.initializeCache("Roster", "username2roster", 512 * 1024);
// Load a user provider.
initProvider();
// Detect when a new auth provider class is set
PropertyEventListener propListener = new PropertyEventListener() {
public void propertySet(String property, Map params) {
//Ignore
}
public void propertyDeleted(String property, Map params) {
//Ignore
}
public void xmlPropertySet(String property, Map params) {
if ("provider.user.className".equals(property)) {
initProvider();
}
}
public void xmlPropertyDeleted(String property, Map params) {
//Ignore
}
};
PropertyEventDispatcher.addListener(propListener);
}
/**
* Returns the currently-installed UserProvider. <b>Warning:</b> in virtually all
* cases the user provider should not be used directly. Instead, the appropriate
* methods in UserManager should be called. Direct access to the user provider is
* only provided for special-case logic.
*
* @return the current UserProvider.
*/
public static UserProvider getUserProvider() {
return provider;
}
/**
* Returns a singleton UserManager instance.
*
* @return a UserManager instance.
*/
public static UserManager getInstance() {
return instance;
}
private UserManager() {
}
/**
* Creates a new User. Required values are username and password. The email address
* can optionally be <tt>null</tt>.
*
* @param username the new and unique username for the account.
* @param password the password for the account (plain text).
* @param name the name of the user.
* @param email the email address to associate with the new account, which can
* be <tt>null</tt>.
* @return a new User.
* @throws UserAlreadyExistsException if the username already exists in the system.
* @throws UnsupportedOperationException if the provider does not support the
* operation.
*/
public User createUser(String username, String password, String name, String email)
throws UserAlreadyExistsException
{
if (provider.isReadOnly()) {
throw new UnsupportedOperationException("User provider is read-only.");
}
// Make sure that the username is valid.
try {
username = Stringprep.nodeprep(username);
}
catch (StringprepException se) {
throw new IllegalArgumentException("Invalid username: " + username, se);
}
User user = provider.createUser(username, password, name, email);
userCache.put(username, user);
// Fire event.
UserEventDispatcher.dispatchEvent(user, UserEventDispatcher.EventType.user_created,
Collections.emptyMap());
return user;
}
/**
* Deletes a user (optional operation).
*
* @param user the user to delete.
*/
public void deleteUser(User user) {
if (provider.isReadOnly()) {
throw new UnsupportedOperationException("User provider is read-only.");
}
String username = user.getUsername();
// Make sure that the username is valid.
try {
username = Stringprep.nodeprep(username);
}
catch (StringprepException se) {
throw new IllegalArgumentException("Invalid username: " + username, se);
}
// Fire event.
UserEventDispatcher.dispatchEvent(user, UserEventDispatcher.EventType.user_deleting,
Collections.emptyMap());
provider.deleteUser(user.getUsername());
// Remove the user from cache.
userCache.remove(user.getUsername());
}
/**
* 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.
*/
public User getUser(String username) throws UserNotFoundException {
if (username == null) {
throw new UserNotFoundException("Username cannot be null");
}
// Make sure that the username is valid.
username = username.trim().toLowerCase();
User user = userCache.get(username);
if (user == null) {
synchronized (username.intern()) {
user = userCache.get(username);
if (user == null) {
user = provider.loadUser(username);
userCache.put(username, user);
}
}
}
return user;
}
/**
* Returns the total number of users in the system.
*
* @return the total number of users.
*/
public int getUserCount() {
return provider.getUserCount();
}
/**
* Returns an unmodifiable Collection of all users in the system.
*
* @return an unmodifiable Collection of all users.
*/
public Collection<User> getUsers() {
return provider.getUsers();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?