📄 customusermanager.java
字号:
import com.jivesoftware.base.*;import com.jivesoftware.base.database.ConnectionManager;import com.jivesoftware.base.database.UserIterator;import com.jivesoftware.util.LongList;import javax.sql.DataSource;import javax.naming.Context;import javax.naming.InitialContext;import java.util.Map;import java.util.Iterator;import java.sql.*;/** * Sample UserManager implementation, which creates and manages CustomUser * objects. */public class CustomUserManager implements UserManager { protected DataSource dataSource = null; // Database queries for the sample database schema this class uses. See // the CustomUser class for more information. private static final String USER_COUNT = "SELECT count(*) FROM person"; private static final String ALL_USERS = "SELECT id FROM person"; public CustomUserManager() { try { // Lookup the database source at a hard-coded name. You'd probably want // to make this configurable for a real implementation. Context context = new InitialContext(); dataSource = (DataSource)context.lookup("java:comp/env/jdbc/database"); } catch (Exception e) { Log.error(e); } } public User createUser(String username, String password, String email) throws UserAlreadyExistsException { throw new UnsupportedOperationException("Unable to create users in external table"); } public User createUser(String username, String password, String name, String email, boolean nameVisible, boolean emailVisible, Map properties) throws UserAlreadyExistsException { throw new UnsupportedOperationException("Unable to create users in external table"); } public User getUser(long userID) throws UserNotFoundException { User user = (User) UserManagerFactory.userCache.get(new Long(userID)); if (user == null) { user = new CustomUser(userID, dataSource); UserManagerFactory.userCache.put(new Long(userID), user); } return user; } public User getUser(String username) throws UserNotFoundException { if (username == null) { throw new UserNotFoundException("Username with null value is not valid."); } return getUser(getUserID(username)); } public long getUserID(String username) throws UserNotFoundException { Long userIDLong = (Long) UserManagerFactory.userIDCache.get(username); // If ID wan't found in cache, load it up and put it there. if (userIDLong == null) { User user = new CustomUser(username, dataSource); UserManagerFactory.userIDCache.put(username, new Long(user.getID())); return user.getID(); } return userIDLong.longValue(); } public void deleteUser(User user) throws UnauthorizedException { throw new UnsupportedOperationException("Unable to delete users in external table"); } public int getUserCount() { // If your external user store doesn't support this operation, return 0. 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() { // If your external user store doesn't support this operation, return // Collections.EMPTY_LIST.iterator(); LongList users = new LongList(500); Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(ALL_USERS); ResultSet rs = pstmt.executeQuery(); // Set the fetch size. This will prevent some JDBC drivers from trying // to load the entire result set into memory. ConnectionManager.setFetchSize(rs, 500); while (rs.next()) { users.add(rs.getLong(1)); } } catch (SQLException e) { Log.error(e); } finally { try { if (pstmt != null) { pstmt.close(); } } catch (Exception e) { Log.error(e); } try { if (con != null) { con.close(); } } catch (Exception e) { Log.error(e); } } return new UserIterator(users.toArray()); } public Iterator users(int startIndex, int numResults) { // If your external user store doesn't support this operation, return // Collections.EMPTY_LIST.iterator(); LongList users = new LongList(); Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(ALL_USERS); ResultSet rs = pstmt.executeQuery(); ConnectionManager.setFetchSize(rs, startIndex+numResults); // 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 e) { Log.error(e); } finally { try { if (pstmt != null) { pstmt.close(); } } catch (Exception e) { Log.error(e); } try { if (con != null) { con.close(); } } catch (Exception e) { Log.error(e); } } return new UserIterator(users.toArray()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -