📄 abstractjdbcusersrepository.java
字号:
String tableName = m_tableName; if ( dbMetaData.storesLowerCaseIdentifiers() ) { tableName = tableName.toLowerCase(Locale.US); } else if ( dbMetaData.storesUpperCaseIdentifiers() ) { tableName = tableName.toUpperCase(Locale.US); } */ // Try UPPER, lower, and MixedCase, to see if the table is there. if (! theJDBCUtil.tableExists(dbMetaData, tableName)) { // Users table doesn't exist - create it. PreparedStatement createStatement = null; try { createStatement = conn.prepareStatement(m_createUserTableSql); createStatement.execute(); } finally { theJDBCUtil.closeJDBCStatement(createStatement); } logBuffer = new StringBuffer(128) .append(this.getClass().getName()) .append(": Created table \'") .append(tableName) .append("\'."); getLogger().info(logBuffer.toString()); } else { if (getLogger().isDebugEnabled()) { getLogger().debug("Using table: " + tableName); } } } finally { theJDBCUtil.closeJDBCConnection( conn ); } } /** * Produces the complete list of User names, with correct case. * @return a <code>List</code> of <code>String</code>s representing * user names. */ protected List listUserNames() { Collection users = getAllUsers(); List userNames = new ArrayList(users.size()); for (Iterator it = users.iterator(); it.hasNext(); ) { userNames.add(((User)it.next()).getUserName()); } users.clear(); return userNames; } // // Superclass methods - overridden from AbstractUsersRepository // /** * Returns a list populated with all of the Users in the repository. * @return an <code>Iterator</code> of <code>JamesUser</code>s. */ protected Iterator listAllUsers() { return getAllUsers().iterator(); } /** * Returns a list populated with all of the Users in the repository. * @return a <code>Collection</code> of <code>JamesUser</code>s. */ private Collection getAllUsers() { List userList = new ArrayList(); // Build the users into this list. Connection conn = openConnection(); PreparedStatement getUsersStatement = null; ResultSet rsUsers = null; try { // Get a ResultSet containing all users. getUsersStatement = conn.prepareStatement(m_getUsersSql); rsUsers = getUsersStatement.executeQuery(); // Loop through and build a User for every row. while ( rsUsers.next() ) { User user = readUserFromResultSet(rsUsers); userList.add(user); } } catch ( SQLException sqlExc) { throw new CascadingRuntimeException("Error accessing database", sqlExc); } finally { theJDBCUtil.closeJDBCResultSet(rsUsers); theJDBCUtil.closeJDBCStatement(getUsersStatement); theJDBCUtil.closeJDBCConnection(conn); } return userList; } /** * Adds a user to the underlying Repository. * The user name must not clash with an existing user. * * @param user the user to be added */ protected void doAddUser(User user) { Connection conn = openConnection(); PreparedStatement addUserStatement = null; // Insert into the database. try { // Get a PreparedStatement for the insert. addUserStatement = conn.prepareStatement(m_insertUserSql); setUserForInsertStatement(user, addUserStatement); addUserStatement.execute(); } catch ( SQLException sqlExc) { throw new CascadingRuntimeException("Error accessing database", sqlExc); } finally { theJDBCUtil.closeJDBCStatement(addUserStatement); theJDBCUtil.closeJDBCConnection(conn); } } /** * Removes a user from the underlying repository. * If the user doesn't exist this method doesn't throw * an exception. * * @param user the user to be removed */ protected void doRemoveUser(User user) { String username = user.getUserName(); Connection conn = openConnection(); PreparedStatement removeUserStatement = null; // Delete from the database. try { removeUserStatement = conn.prepareStatement(m_deleteUserSql); removeUserStatement.setString(1, username); removeUserStatement.execute(); } catch ( SQLException sqlExc ) { throw new CascadingRuntimeException("Error accessing database", sqlExc); } finally { theJDBCUtil.closeJDBCStatement(removeUserStatement); theJDBCUtil.closeJDBCConnection(conn); } } /** * Updates a user record to match the supplied User. * * @param user the updated user record */ protected void doUpdateUser(User user) { Connection conn = openConnection(); PreparedStatement updateUserStatement = null; // Update the database. try { updateUserStatement = conn.prepareStatement(m_updateUserSql); setUserForUpdateStatement(user, updateUserStatement); updateUserStatement.execute(); } catch ( SQLException sqlExc ) { throw new CascadingRuntimeException("Error accessing database", sqlExc); } finally { theJDBCUtil.closeJDBCStatement(updateUserStatement); theJDBCUtil.closeJDBCConnection(conn); } } /** * Gets a user by name, ignoring case if specified. * If the specified SQL statement has been defined, this method * overrides the basic implementation in AbstractUsersRepository * to increase performance. * * @param name the name of the user being retrieved * @param ignoreCase whether the name is regarded as case-insensitive * * @return the user being retrieved, null if the user doesn't exist */ protected User getUserByName(String name, boolean ignoreCase) { // See if this statement has been set, if not, use // simple superclass method. if ( m_userByNameCaseInsensitiveSql == null ) { return super.getUserByName(name, ignoreCase); } // Always get the user via case-insensitive SQL, // then check case if necessary. Connection conn = openConnection(); PreparedStatement getUsersStatement = null; ResultSet rsUsers = null; try { // Get a ResultSet containing all users. String sql = m_userByNameCaseInsensitiveSql; getUsersStatement = conn.prepareStatement(sql); getUsersStatement.setString(1, name.toLowerCase(Locale.US)); rsUsers = getUsersStatement.executeQuery(); // For case-insensitive matching, the first matching user will be returned. User user = null; while ( rsUsers.next() ) { User rowUser = readUserFromResultSet(rsUsers); String actualName = rowUser.getUserName(); // Check case before we assume it's the right one. if ( ignoreCase || actualName.equals(name) ) { user = rowUser; break; } } return user; } catch ( SQLException sqlExc ) { throw new CascadingRuntimeException("Error accessing database", sqlExc); } finally { theJDBCUtil.closeJDBCResultSet(rsUsers); theJDBCUtil.closeJDBCStatement(getUsersStatement); theJDBCUtil.closeJDBCConnection(conn); } } /** * Reads properties for a User from an open ResultSet. * Subclass implementations of this method must have knowledge of the fields * presented by the "select" and "selectByLowercaseName" SQL statements. * These implemenations may generate a subclass-specific User instance. * * @param rsUsers A ResultSet with a User record in the current row. * @return A User instance * @throws SQLException * if an exception occurs reading from the ResultSet */ protected abstract User readUserFromResultSet(ResultSet rsUsers) throws SQLException; /** * Set parameters of a PreparedStatement object with * property values from a User instance. * Implementations of this method have knowledge of the parameter * ordering of the "insert" SQL statement definition. * * @param user a User instance, which should be an implementation class which * is handled by this Repostory implementation. * @param userInsert a PreparedStatement initialised with SQL taken from the "insert" SQL definition. * @throws SQLException * if an exception occurs while setting parameter values. */ protected abstract void setUserForInsertStatement(User user, PreparedStatement userInsert) throws SQLException; /** * Set parameters of a PreparedStatement object with * property values from a User instance. * Implementations of this method have knowledge of the parameter * ordering of the "update" SQL statement definition. * * @param user a User instance, which should be an implementation class which * is handled by this Repostory implementation. * @param userUpdate a PreparedStatement initialised with SQL taken from the "update" SQL definition. * @throws SQLException * if an exception occurs while setting parameter values. */ protected abstract void setUserForUpdateStatement(User user, PreparedStatement userUpdate) throws SQLException; /** * Opens a connection, throwing a runtime exception if a SQLException is * encountered in the process. * * @return the new connection */ private Connection openConnection() { try { return m_datasource.getConnection(); } catch (SQLException sqle) { throw new CascadingRuntimeException( "An exception occurred getting a database connection.", sqle); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -