📄 dbusermanager.java
字号:
/**
* $RCSfile: DbUserManager.java,v $
* $Revision: 1.1.1.1 $
* $Date: 2002/09/09 13:50:59 $
*
* New Jive from Jdon.com.Mofified by Frank Chu
*
* This software is the proprietary information of CoolServlets, Inc.
* Use is subject to license terms.
*/
package com.airinbox.member.forum.database;
import java.util.*;
import java.sql.*;
import com.airinbox.member.util.*;
import com.airinbox.member.forum.*;
import com.airinbox.component.authorize.UnauthorizedException;
/**
* Database implementation of the UserManager interface. It uses the DbUser
* class along with the memberUser database table to store and manipulate user
* information.<p>
*
* In some cases, you may wish to plug in your own user system implementation.
* In that case, you should set the Jive property <tt>UserManager.className</tt>
* with the name of your UserManager class. Your class must have a public
* constructor that takes a DbForumFactory object as an argument. The class
* must also create and return User object implementations as necessary.
*
* This UserManager implementation uses two global caches to vastly improve speed:<ul>
* <li> DbForumFactory.userMessageCountCache
* <li> DbForumFactory.cacheManager.userCache </ul><p>
*
* If making your own UserManager implementation, it's highly recommended that
* you also use these caches. However, if you do, your User implementation
* must implement Cacheable to be put into cache. Other classes inside of the
* database implementation know about the caches and expire them as necessary.
*/
public class DbUserManager implements UserManager {
/** DATABASE QUERIES **/
private static final String USER_MESSAGE_COUNT =
"SELECT count(1) FROM "+Globals.getConfig(Globals._TABLE_MESSAGE)+" WHERE "+Globals.getConfig(Globals._TABLE_MESSAGE)+".userID=?";
private static final String USER_COUNT = "SELECT count(1) FROM "+Globals.getConfig(Globals._TABLE_USER);
private static final String ALL_USER_MESSAGES =
"SELECT messageID FROM "+Globals.getConfig(Globals._TABLE_MESSAGE)+" WHERE userID=? ORDER BY modifiedDate DESC";
private static final String DELETE_USER_MESSAGES =
"UPDATE "+Globals.getConfig(Globals._TABLE_MESSAGE)+" set userID=NULL WHERE userID=?";
private static final String DELETE_USER_PERMS =
"DELETE FROM "+Globals.getConfig(Globals._TABLE_USER_PERM)+" WHERE userID=?";
private static final String DELETE_USER_GROUPS =
"DELETE FROM "+Globals.getConfig(Globals._TABLE_GROUP_USER)+" WHERE userID=?";
private static final String DELETE_USER_PROPS =
"DELETE FROM "+Globals.getConfig(Globals._TABLE_USER_PROP)+" WHERE userID=?";
private static final String DELETE_USER_WATCHES =
"DELETE FROM "+Globals.getConfig(Globals._TABLE_WATCH)+" WHERE userID=?";
private static final String DELETE_USER =
"DELETE FROM "+Globals.getConfig(Globals._TABLE_USER)+" WHERE userID=?";
private static final String ALL_USERS =
"SELECT userID from "+Globals.getConfig(Globals._TABLE_USER)+"";
private DbForumFactory factory;
private UserCache userCache;
/**
* Creates a new UserManager.
*/
public DbUserManager(DbForumFactory factory) {
this.factory = factory;
this.userCache = factory.cacheManager.userCache;
}
public User createUser(com.airinbox.component.authorize.User origuser,
boolean nameVisible,
boolean emailVisible,
Map properties) throws UserAlreadyExistsException
{
User newUser = null;
try {
User existingUser = getUser(origuser.getId());
//The user already exists since now exception, so:
throw new UserAlreadyExistsException();
}
catch (UserNotFoundException unfe) {
//The user doesn't already exist so we can create a new user
newUser = new DbUser(origuser,nameVisible, emailVisible, properties, factory);
}
return newUser;
}
public com.airinbox.component.authorize.User getOriginalUser(long id) {
com.airinbox.component.authorize.UserManager um = com.airinbox.component.authorize.concrete.XmlUserManager.getInstance();
com.airinbox.component.authorize.User user = null;
try {
user = um.getUser(new Long(id).intValue());
} catch(Exception e) {
e.printStackTrace();
}
return user;
}
public com.airinbox.component.authorize.User getOriginalUser(String username) {
com.airinbox.component.authorize.UserManager um = com.airinbox.component.authorize.concrete.XmlUserManager.getInstance();
com.airinbox.component.authorize.User user = null;
try {
user = um.getUser(username);
} catch(Exception e) {
e.printStackTrace();
}
return user;
}
public User getUser(long userID) throws UserNotFoundException {
if (!userCache.isEnabled()) {
return new DbUser(userID, factory);
}
//Cache is enabled.
User user = (User)userCache.get(userID);
if (user == null) {
user = new DbUser(userID, factory);
userCache.add(userID, user);
}
return user;
}
public User getUser(String username) throws UserNotFoundException {
// Do a special check to see if chache is disabled. This will let us
// avoid more db hits than necessary if it is.
if (!userCache.isEnabled()) {
com.airinbox.component.authorize.User user = getOriginalUser(username);
return new DbUser(user.getId(), factory);
}
//Cache is enabled.
com.airinbox.component.authorize.User ouser = getOriginalUser(username);
User user = (User)userCache.get(ouser.getId());
if (user == null) {
user = new DbUser(ouser.getId(), factory);
userCache.add(ouser.getId(), user);
}
return user;
}
public void deleteUser(User user) throws UnauthorizedException {
long userID = user.getID();
// Get array of all user's messages in the system so that
// we can expire them from cache.
LongList messages = new LongList();
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(ALL_USER_MESSAGES);
pstmt.setLong(1, user.getID());
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
messages.add(rs.getLong(1));
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
con = null;
pstmt = null;
boolean abortTransaction = false;
try {
con = ConnectionManager.getTransactionConnection();
// Mark all message by user as anonymous
pstmt = con.prepareStatement(DELETE_USER_MESSAGES);
pstmt.setLong(1,userID);
pstmt.execute();
pstmt.close();
// Remove all permissions given to user
pstmt = con.prepareStatement(DELETE_USER_PERMS);
pstmt.setLong(1,userID);
pstmt.execute();
pstmt.close();
// Remove user from all groups
pstmt = con.prepareStatement(DELETE_USER_GROUPS);
pstmt.setLong(1,userID);
pstmt.execute();
pstmt.close();
// Delete all of the users's extended properties
pstmt = con.prepareStatement(DELETE_USER_PROPS);
pstmt.setLong(1,userID);
pstmt.execute();
pstmt.close();
// Delete all of user's watches
pstmt = con.prepareStatement(DELETE_USER_WATCHES);
pstmt.setLong(1, userID);
pstmt.execute();
pstmt.close();
// Delete the actual user entry
pstmt = con.prepareStatement(DELETE_USER);
pstmt.setLong(1,userID);
pstmt.execute();
}
catch (Exception e) {
e.printStackTrace();
abortTransaction = true;
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
ConnectionManager.closeTransactionConnection(con, abortTransaction);
}
// Expire user's messages from cache
long [] messagesArray = messages.toArray();
for (int i=0; i<messagesArray.length; i++) {
factory.cacheManager.messageCache.remove(messagesArray[i]);
}
// Expire user perms cache
factory.cacheManager.userPermsCache.removeUserPerm(userID);
// Expire user cache
factory.cacheManager.userCache.remove(userID);
}
public int getUserCount() {
int count = 0;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(USER_COUNT);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
count = rs.getInt(1);
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
return count;
}
public Iterator users() {
int userCount = getUserCount();
long [] users = new long[userCount];
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(ALL_USERS);
ResultSet rs = pstmt.executeQuery();
for (int i=0; i<userCount; i++) {
rs.next();
users[i] = rs.getLong(1);
}
}
catch(SQLException sqle) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
return new DatabaseObjectIterator(Globals.USER, users, this);
}
public Iterator users(int startIndex, int numResults) {
LongList users = new LongList();
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(ALL_USERS);
ResultSet rs = pstmt.executeQuery();
// Move to start of index
for (int i=0; i<startIndex; i++) {
rs.next();
}
// Now read in desired number of results (or stop if we run out
// of results).
for (int i=0; i<numResults; i++) {
if (rs.next()) {
users.add(rs.getLong(1));
}
else {
break;
}
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
return new DatabaseObjectIterator(Globals.USER, users.toArray(), this);
}
public int userMessageCount(User user) {
// Construct key for the cache entry which is "forumID-userID".
long key = user.getID();
// See if the value is in cache.
CacheableInt msgCount = (CacheableInt)factory.userMessageCountCache.get(key);
if (msgCount != null) {
return msgCount.getInt();
}
// Not cached, so load from database.
int count = 0;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(USER_MESSAGE_COUNT);
pstmt.setLong(1, user.getID());
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
count = rs.getInt(1);
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
// Add to cache.
msgCount = new CacheableInt(count);
factory.userMessageCountCache.add(key, msgCount);
return count;
}
public Iterator userMessages(User user,int start, int count) {
LongList messages = new LongList();
Connection con = null;
PreparedStatement pstmt = null;
int total =userMessageCount(user) ;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(ALL_USER_MESSAGES);
pstmt.setLong(1, user.getID());
ResultSet rs = pstmt.executeQuery();
if (start >= 0 && start < total) {
rs.absolute(start+1);
do {
messages.add(rs.getLong(1));
} while (rs.next() && (--count > 0));
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
return new DatabaseObjectIterator(Globals.MESSAGE, messages.toArray(),
factory);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -