📄 usermanager.java
字号:
/**
* $RCSfile$
* $Revision: 1217 $
* $Date: 2005-04-11 18:11:06 -0300 (Mon, 11 Apr 2005) $
*
* Copyright (C) 2008 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, or a commercial license
* agreement with Jive.
*/
package org.jivesoftware.openfire.user;
import org.dom4j.Element;
import org.jivesoftware.openfire.IQResultListener;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.event.UserEventDispatcher;
import org.jivesoftware.openfire.event.UserEventListener;
import org.jivesoftware.stringprep.Stringprep;
import org.jivesoftware.stringprep.StringprepException;
import org.jivesoftware.util.*;
import org.jivesoftware.util.cache.Cache;
import org.jivesoftware.util.cache.CacheFactory;
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 {
// Wrap this guy up so we can mock out the UserManager class.
private static class UserManagerContainer {
private static UserManager instance = new UserManager();
}
/**
* 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 UserManagerContainer.instance.provider;
}
/**
* Returns a singleton UserManager instance.
*
* @return a UserManager instance.
*/
public static UserManager getInstance() {
return UserManagerContainer.instance;
}
/** Cache of local users. */
private Cache<String, User> userCache;
/** Cache if a local or remote user exists. */
private Cache<String, Boolean> remoteUsersCache;
private UserProvider provider;
private UserManager() {
// Initialize caches.
userCache = CacheFactory.createCache("User");
remoteUsersCache = CacheFactory.createCache("Remote Users Existence");
// 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);
UserEventListener userListener = new UserEventListener() {
public void userCreated(User user, Map<String, Object> params) {
// Do nothing
}
public void userDeleting(User user, Map<String, Object> params) {
// Do nothing
}
public void userModified(User user, Map<String, Object> params) {
// Set object again in cache. This is done so that other cluster nodes
// get refreshed with latest version of the user
userCache.put(user.getUsername(), user);
}
};
UserEventDispatcher.addListener(userListener);
}
/**
* Creates a new User. Required values are username and password. The email address
* and name can optionally be <tt>null</tt>, unless the UserProvider deems that
* either of them are required.
*
* @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, which can be <tt>null</tt> unless the UserProvider
* deems that it's required.
* @param email the email address to associate with the new account, which can
* be <tt>null</tt>, unless the UserProvider deems that it's required.
* @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);
}
if (provider.isNameRequired() && (name == null || name.equals(""))) {
throw new IllegalArgumentException("Invalid or empty name specified with provider that requires name. User: "
+ username + " Name: " + name);
}
if (provider.isEmailRequired() && !StringUtils.isValidEmailAddress(email)) {
throw new IllegalArgumentException("Invalid or empty email address specified with provider that requires email address. User: "
+ username + " Email: " + email);
}
User user = provider.createUser(username, password, name, email);
userCache.put(username, user);
// Fire event.
Map<String,Object> params = Collections.emptyMap();
UserEventDispatcher.dispatchEvent(user, UserEventDispatcher.EventType.user_created, params);
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.
Map<String,Object> params = Collections.emptyMap();
UserEventDispatcher.dispatchEvent(user, UserEventDispatcher.EventType.user_deleting, params);
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);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -