⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbcuserdatabase.java

📁 jspwiki source code,jspwiki source code
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            {                conn.setAutoCommit( false );            }            Timestamp ts = new Timestamp( System.currentTimeMillis() );            Date modDate = new Date( ts.getTime() );            // Change the login ID for the user record            PreparedStatement ps = conn.prepareStatement( m_renameProfile );            ps.setString( 1, newName );            ps.setTimestamp( 2, ts );            ps.setString( 3, loginName );            ps.execute();            ps.close();            // Change the login ID for the role records            ps = conn.prepareStatement( m_renameRoles );            ps.setString( 1, newName );            ps.setString( 2, loginName );            ps.execute();            ps.close();            // Set the profile name and mod time            profile.setLoginName( newName );            profile.setLastModified( modDate );            // Commit and close connection            if( m_supportsCommits )            {                conn.commit();            }        }        catch( SQLException e )        {            throw new WikiSecurityException( e.getMessage() );        }        finally        {            try            {                if( conn != null ) conn.close();            }            catch( Exception e )            {            }        }    }    /**     * @see com.ecyrd.jspwiki.auth.user.UserDatabase#save(com.ecyrd.jspwiki.auth.user.UserProfile)     */    public void save( UserProfile profile ) throws WikiSecurityException    {        // Figure out which prepared statement to use & execute it        String loginName = profile.getLoginName();        PreparedStatement ps = null;        UserProfile existingProfile = null;        try        {            existingProfile = findByLoginName( loginName );        }        catch( NoSuchPrincipalException e )        {            // Existing profile will be null        }        // Get a clean password from the passed profile.        // Blank password is the same as null, which means we re-use the        // existing one.        String password = profile.getPassword();        String existingPassword = (existingProfile == null) ? null : existingProfile.getPassword();        if( NOTHING.equals( password ) )        {            password = null;        }        if( password == null )        {            password = existingPassword;        }        // If password changed, hash it before we save        if( !password.equals( existingPassword ) )        {            password = getHash( password );        }        Connection conn = null;        try        {            // Open the database connection            conn = m_ds.getConnection();            if( m_supportsCommits )            {                conn.setAutoCommit( false );            }            Timestamp ts = new Timestamp( System.currentTimeMillis() );            Date modDate = new Date( ts.getTime() );            java.sql.Date lockExpiry = profile.getLockExpiry() == null ? null : new java.sql.Date( profile.getLockExpiry().getTime() );            if( existingProfile == null )            {                // User is new: insert new user record                ps = conn.prepareStatement( m_insertProfile );                ps.setString( 1, profile.getUid() );                ps.setString( 2, profile.getEmail() );                ps.setString( 3, profile.getFullname() );                ps.setString( 4, password );                ps.setString( 5, profile.getWikiName() );                ps.setTimestamp( 6, ts );                ps.setString( 7, profile.getLoginName() );                try                {                    ps.setString( 8, Serializer.serializeToBase64( profile.getAttributes() ) );                }                catch ( IOException e )                {                    throw new WikiSecurityException( "Could not save user profile attribute. Reason: " + e.getMessage() );                }                ps.setTimestamp( 9, ts );                ps.execute();                ps.close();                // Insert new role record                ps = conn.prepareStatement( m_findRoles );                ps.setString( 1, profile.getLoginName() );                ResultSet rs = ps.executeQuery();                int roles = 0;                while ( rs.next() )                {                    roles++;                }                ps.close();                if( roles == 0 )                {                    ps = conn.prepareStatement( m_insertRole );                    ps.setString( 1, profile.getLoginName() );                    ps.setString( 2, m_initialRole );                    ps.execute();                    ps.close();                }                // Set the profile creation time                profile.setCreated( modDate );            }            else            {                // User exists: modify existing record                ps = conn.prepareStatement( m_updateProfile );                ps.setString( 1, profile.getUid() );                ps.setString( 2, profile.getEmail() );                ps.setString( 3, profile.getFullname() );                ps.setString( 4, password );                ps.setString( 5, profile.getWikiName() );                ps.setTimestamp( 6, ts );                ps.setString( 7, profile.getLoginName() );                try                {                    ps.setString( 8, Serializer.serializeToBase64( profile.getAttributes() ) );                }                catch ( IOException e )                {                    throw new WikiSecurityException( "Could not save user profile attribute. Reason: " + e.getMessage() );                }                ps.setDate( 9, lockExpiry );                ps.setString( 10, profile.getLoginName() );                ps.execute();                ps.close();            }            // Set the profile mod time            profile.setLastModified( modDate );            // Commit and close connection            if( m_supportsCommits )            {                conn.commit();            }        }        catch( SQLException e )        {            throw new WikiSecurityException( e.getMessage() );        }        finally        {            try            {                if( conn != null ) conn.close();            }            catch( Exception e )            {            }        }    }    /**     * Private method that returns the first {@link UserProfile} matching a     * named column's value. This method will also set the UID if it has not yet been set.          * @param sql the SQL statement that should be prepared; it must have one parameter     * to set (either a String or a Long)     * @param index the value to match     * @return the resolved UserProfile     * @throws SQLException     */    private UserProfile findByPreparedStatement( String sql, Object index ) throws NoSuchPrincipalException    {        UserProfile profile = null;        boolean found = false;        boolean unique = true;        Connection conn = null;        try        {            // Open the database connection            conn = m_ds.getConnection();            if( m_supportsCommits )            {                conn.setAutoCommit( false );            }            PreparedStatement ps = conn.prepareStatement( sql );                        // Set the parameter to search by            if ( index instanceof String )            {                ps.setString( 1, (String)index );            }            else if ( index instanceof Long )            {                ps.setLong( 1, ( (Long)index).longValue() );            }            else             {                throw new IllegalArgumentException( "Index type not recognized!" );            }                        // Go and get the record!            ResultSet rs = ps.executeQuery();            while ( rs.next() )            {                if( profile != null )                {                    unique = false;                    break;                }                profile = newProfile();                                // Fetch the basic user attributes                profile.setUid( rs.getString( m_uid ) );                if ( profile.getUid() == null )                {                    profile.setUid( generateUid( this ) );                }                profile.setCreated( rs.getTimestamp( m_created ) );                profile.setEmail( rs.getString( m_email ) );                profile.setFullname( rs.getString( m_fullName ) );                profile.setLastModified( rs.getTimestamp( m_modified ) );                Date lockExpiry = rs.getDate( m_lockExpiry );                profile.setLockExpiry( rs.wasNull() ? null : lockExpiry );                profile.setLoginName( rs.getString( m_loginName ) );                profile.setPassword( rs.getString( m_password ) );                                // Fetch the user attributes                String rawAttributes = rs.getString( m_attributes );                if ( rawAttributes != null )                {                    try                    {                        Map<String,? extends Serializable> attributes = Serializer.deserializeFromBase64( rawAttributes );                        profile.getAttributes().putAll( attributes );                    }                    catch ( IOException e )                    {                        log.error( "Could not parse user profile attributes!", e );                    }                }                found = true;            }            ps.close();        }        catch( SQLException e )        {            throw new NoSuchPrincipalException( e.getMessage() );        }        finally        {            try            {                if( conn != null ) conn.close();            }            catch( Exception e )            {            }        }        if( !found )        {            throw new NoSuchPrincipalException( "Could not find profile in database!" );        }        if( !unique )        {            throw new NoSuchPrincipalException( "More than one profile in database!" );        }        return profile;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -