📄 jdbcgroupdatabase.java
字号:
if( conn != null ) conn.close(); } catch( Exception e ) { } } return groups.toArray( new Group[groups.size()] ); } /** * Saves a Group to the group database. Note that this method <em>must</em> * fail, and throw an <code>IllegalArgumentException</code>, if the * proposed group is the same name as one of the built-in Roles: e.g., * Admin, Authenticated, etc. The database is responsible for setting * create/modify timestamps, upon a successful save, to the Group. The * method commits the results of the delete to persistent storage. * * @param group the Group to save * @param modifier the user who saved the Group * @throws WikiSecurityException if the Group could not be saved * successfully */ public void save( Group group, Principal modifier ) throws WikiSecurityException { if( group == null || modifier == null ) { throw new IllegalArgumentException( "Group or modifier cannot be null." ); } boolean exists = exists( group ); Connection conn = null; try { // Open the database connection conn = m_ds.getConnection(); if( m_supportsCommits ) { conn.setAutoCommit( false ); } PreparedStatement ps; Timestamp ts = new Timestamp( System.currentTimeMillis() ); Date modDate = new Date( ts.getTime() ); if( !exists ) { // Group is new: insert new group record ps = conn.prepareStatement( m_insertGroup ); ps.setString( 1, group.getName() ); ps.setTimestamp( 2, ts ); ps.setString( 3, modifier.getName() ); ps.setTimestamp( 4, ts ); ps.setString( 5, modifier.getName() ); ps.execute(); // Set the group creation time group.setCreated( modDate ); group.setCreator( modifier.getName() ); ps.close(); } else { // Modify existing group record ps = conn.prepareStatement( m_updateGroup ); ps.setTimestamp( 1, ts ); ps.setString( 2, modifier.getName() ); ps.setString( 3, group.getName() ); ps.execute(); ps.close(); } // Set the group modified time group.setLastModified( modDate ); group.setModifier( modifier.getName() ); // Now, update the group member list // First, delete all existing member records ps = conn.prepareStatement( m_deleteGroupMembers ); ps.setString( 1, group.getName() ); ps.execute(); ps.close(); // Insert group member records ps = conn.prepareStatement( m_insertGroupMembers ); Principal[] members = group.members(); for( int i = 0; i < members.length; i++ ) { Principal member = members[i]; ps.setString( 1, group.getName() ); ps.setString( 2, member.getName() ); ps.execute(); } ps.close(); // 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 ) { } } } /** * Initializes the group database based on values from a Properties object. * * @param engine the wiki engine * @param props the properties used to initialize the group database * @throws WikiSecurityException if the database could not be initialized * successfully * @throws NoRequiredPropertyException if a required property is not present */ public void initialize( WikiEngine engine, Properties props ) throws NoRequiredPropertyException, WikiSecurityException { m_engine = engine; String jndiName = props.getProperty( PROP_GROUPDB_DATASOURCE, DEFAULT_GROUPDB_DATASOURCE ); try { Context initCtx = new InitialContext(); Context ctx = (Context) initCtx.lookup( "java:comp/env" ); m_ds = (DataSource) ctx.lookup( jndiName ); // Prepare the SQL selectors m_table = props.getProperty( PROP_GROUPDB_TABLE, DEFAULT_GROUPDB_TABLE ); m_memberTable = props.getProperty( PROP_GROUPDB_MEMBER_TABLE, DEFAULT_GROUPDB_MEMBER_TABLE ); m_name = props.getProperty( PROP_GROUPDB_NAME, DEFAULT_GROUPDB_NAME ); m_created = props.getProperty( PROP_GROUPDB_CREATED, DEFAULT_GROUPDB_CREATED ); m_creator = props.getProperty( PROP_GROUPDB_CREATOR, DEFAULT_GROUPDB_CREATOR ); m_modifier = props.getProperty( PROP_GROUPDB_MODIFIER, DEFAULT_GROUPDB_MODIFIER ); m_modified = props.getProperty( PROP_GROUPDB_MODIFIED, DEFAULT_GROUPDB_MODIFIED ); m_member = props.getProperty( PROP_GROUPDB_MEMBER, DEFAULT_GROUPDB_MEMBER ); m_findAll = "SELECT DISTINCT * FROM " + m_table; m_findGroup = "SELECT DISTINCT * FROM " + m_table + " WHERE " + m_name + "=?"; m_findMembers = "SELECT * FROM " + m_memberTable + " WHERE " + m_name + "=?"; // Prepare the group insert/update SQL m_insertGroup = "INSERT INTO " + m_table + " (" + m_name + "," + m_modified + "," + m_modifier + "," + m_created + "," + m_creator + ") VALUES (?,?,?,?,?)"; m_updateGroup = "UPDATE " + m_table + " SET " + m_modified + "=?," + m_modifier + "=? WHERE " + m_name + "=?"; // Prepare the group member insert SQL m_insertGroupMembers = "INSERT INTO " + m_memberTable + " (" + m_name + "," + m_member + ") VALUES (?,?)"; // Prepare the group delete SQL m_deleteGroup = "DELETE FROM " + m_table + " WHERE " + m_name + "=?"; m_deleteGroupMembers = "DELETE FROM " + m_memberTable + " WHERE " + m_name + "=?"; } catch( NamingException e ) { log.error( "JDBCGroupDatabase initialization error: " + e.getMessage() ); throw new NoRequiredPropertyException( PROP_GROUPDB_DATASOURCE, "JDBCGroupDatabase initialization error: " + e.getMessage() ); } // Test connection by doing a quickie select Connection conn = null; try { conn = m_ds.getConnection(); PreparedStatement ps = conn.prepareStatement( m_findAll ); ps.executeQuery(); ps.close(); } catch( SQLException e ) { log.error( "JDBCGroupDatabase initialization error: " + e.getMessage() ); throw new NoRequiredPropertyException( PROP_GROUPDB_DATASOURCE, "JDBCGroupDatabase initialization error: " + e.getMessage() ); } finally { try { if( conn != null ) conn.close(); } catch( Exception e ) { } } log.info( "JDBCGroupDatabase initialized from JNDI DataSource: " + jndiName ); // Determine if the datasource supports commits try { conn = m_ds.getConnection(); DatabaseMetaData dmd = conn.getMetaData(); if( dmd.supportsTransactions() ) { m_supportsCommits = true; conn.setAutoCommit( false ); log.info( "JDBCGroupDatabase supports transactions. Good; we will use them." ); } } catch( SQLException e ) { log.warn( "JDBCGroupDatabase warning: user database doesn't seem to support transactions. Reason: " + e.getMessage() ); throw new NoRequiredPropertyException( PROP_GROUPDB_DATASOURCE, "JDBCGroupDatabase initialization error: " + e.getMessage() ); } finally { try { if( conn != null ) conn.close(); } catch( Exception e ) { } } } /** * Returns <code>true</code> if the Group exists in back-end storage. * * @param group the Group to look for * @return the result of the search */ private boolean exists( Group group ) { String index = group.getName(); try { findGroup( index ); return true; } catch( NoSuchPrincipalException e ) { return false; } } /** * Loads and returns a Group from the back-end database matching a supplied * name. * * @param index the name of the Group to find * @return the populated Group * @throws NoSuchPrincipalException if the Group cannot be found * @throws SQLException if the database query returns an error */ private Group findGroup( String index ) throws NoSuchPrincipalException { Group group = null; boolean found = false; boolean unique = true; Connection conn = null; try { // Open the database connection conn = m_ds.getConnection(); PreparedStatement ps = conn.prepareStatement( m_findGroup ); ps.setString( 1, index ); ResultSet rs = ps.executeQuery(); while ( rs.next() ) { if( group != null ) { unique = false; break; } group = new Group( index, m_engine.getApplicationName() ); group.setCreated( rs.getTimestamp( m_created ) ); group.setCreator( rs.getString( m_creator ) ); group.setLastModified( rs.getTimestamp( m_modified ) ); group.setModifier( rs.getString( m_modifier ) ); populateGroup( group ); 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 group in database!" ); } if( !unique ) { throw new NoSuchPrincipalException( "More than one group in database!" ); } return group; } /** * Fills a Group with members. * * @param group the group to populate * @return the populated Group */ private Group populateGroup( Group group ) { Connection conn = null; try { // Open the database connection conn = m_ds.getConnection(); PreparedStatement ps = conn.prepareStatement( m_findMembers ); ps.setString( 1, group.getName() ); ResultSet rs = ps.executeQuery(); while ( rs.next() ) { String memberName = rs.getString( m_member ); if( memberName != null ) { WikiPrincipal principal = new WikiPrincipal( memberName, WikiPrincipal.UNSPECIFIED ); group.add( principal ); } } ps.close(); } catch( SQLException e ) { // I guess that means there aren't any principals... } finally { try { if( conn != null ) conn.close(); } catch( Exception e ) { } } return group; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -