markt 2005/02/03 14:47:07 Modified: catalina/src/share/org/apache/catalina/realm DataSourceRealm.java LocalStrings.properties Log: Port fix for bug 33357 from TC5. - Fixes connection leaks - Improves efficiency - Submitted by Dominik Drzewiecki Revision Changes Path 1.5 +100 -88 jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/realm/DataSourceRealm.java Index: DataSourceRealm.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/realm/DataSourceRealm.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- DataSourceRealm.java 27 Nov 2004 18:29:44 -0000 1.4 +++ DataSourceRealm.java 3 Feb 2005 22:47:07 -0000 1.5 @@ -245,6 +245,11 @@ */ public Principal authenticate(String username, String credentials) { + // No user - can't possibly authenticate, don't bother the database then + if (username == null) { + return null; + } + Connection dbConnection = null; try { @@ -257,32 +262,17 @@ } // Acquire a Principal object for this user - Principal principal = authenticate(dbConnection, - username, credentials); - - if( !dbConnection.getAutoCommit() ) { - dbConnection.commit(); - } - - // Release the database connection we just used - close(dbConnection); - dbConnection = null; - - // Return the Principal (if any) - return (principal); + return authenticate(dbConnection, username, credentials); } catch (SQLException e) { - // Log the problem for posterity log(sm.getString("dataSourceRealm.exception"), e); - // Close the connection so that it gets reopened next time - if (dbConnection != null) - close(dbConnection); - // Return "not authenticated" for this request return (null); + } finally { + close(dbConnection); } } @@ -305,17 +295,11 @@ * * @exception SQLException if a database error occurs */ - private Principal authenticate(Connection dbConnection, - String username, - String credentials) { - + protected Principal authenticate(Connection dbConnection, + String username, + String credentials) throws SQLException { - // No user - can't possibly authenticate - if (username == null) { - return (null); - } - - String dbCredentials = getPassword(username); + String dbCredentials = getPassword(dbConnection, username); // Validate the user's credentials boolean validated = false; @@ -336,7 +320,7 @@ return (null); } - ArrayList list = getRoles(username); + ArrayList list = getRoles(dbConnection, username); // Create and return a suitable Principal for this user return (new GenericPrincipal(this, username, credentials, list)); @@ -357,6 +341,9 @@ // Close this database connection, and log any errors try { + if (!dbConnection.getAutoCommit()) { + dbConnection.commit(); + } dbConnection.close(); } catch (SQLException e) { log(sm.getString("dataSourceRealm.close"), e); // Just log it here @@ -386,28 +373,6 @@ /** - * Return a PreparedStatement configured to perform the SELECT required - * to retrieve user credentials for the specified username. - * - * @param dbConnection The database connection to be used - * @param username Username for which credentials should be retrieved - * - * @exception SQLException if a database error occurs - */ - private PreparedStatement credentials(Connection dbConnection, - String username) - throws SQLException { - - PreparedStatement credentials = - dbConnection.prepareStatement(preparedCredentials.toString()); - - credentials.setString(1, username); - return (credentials); - - } - - - /** * Return a short name for this Realm implementation. */ protected String getName() { @@ -422,9 +387,6 @@ */ protected String getPassword(String username) { - ResultSet rs = null; - PreparedStatement stmt = null; - ArrayList list = null; Connection dbConnection = null; // Ensure that we have an open database connection @@ -434,26 +396,36 @@ } try { + return getPassword(dbConnection, username); + } finally { + close(dbConnection); + } + } + + /** + * Return the password associated with the given principal's user name. + * @param dbConnection The database connection to be used + * @param username Username for which password should be retrieved + */ + protected String getPassword(Connection dbConnection, + String username) { + + ResultSet rs = null; + PreparedStatement stmt = null; + String dbCredentials = null; + + try { // Look up the user's credentials - String dbCredentials = null; stmt = credentials(dbConnection, username); rs = stmt.executeQuery(); if (rs.next()) { dbCredentials = rs.getString(1); } - rs.close(); - rs = null; - stmt.close(); - stmt = null; - if (dbCredentials == null) { - return (null); - } - dbCredentials = dbCredentials.trim(); - return (dbCredentials); - + return (dbCredentials != null) ? dbCredentials.trim() : null; + } catch(SQLException e) { - log(sm.getString("datasourceRealm.getPassword.exception", + log(sm.getString("dataSourceRealm.getPassword.exception", username)); } finally { try { @@ -463,21 +435,14 @@ if (stmt != null) { stmt.close(); } - if( !dbConnection.getAutoCommit() ) { - dbConnection.commit(); - } } catch (SQLException e) { - log(sm.getString("datasourceRealm.getPassword.exception", + log(sm.getString("dataSourceRealm.getPassword.exception", username)); } - // Release the database connection we just used - close(dbConnection); - dbConnection = null; - } - return (null); + return (null); } @@ -486,22 +451,29 @@ */ protected Principal getPrincipal(String username) { - return (new GenericPrincipal(this, - username, - getPassword(username), - getRoles(username))); + Connection dbConnection = open(); + if (dbConnection == null) { + return new GenericPrincipal(this,username, null, null); + } + try { + return (new GenericPrincipal(this, + username, + getPassword(dbConnection, username), + getRoles(dbConnection, username))); + } finally { + close(dbConnection); + } } /** - * Return the roles associated with the gven user name. + * Return the roles associated with the given user name. + * @param username Username for which roles should be retrieved */ protected ArrayList getRoles(String username) { - ResultSet rs = null; - PreparedStatement stmt = null; Connection dbConnection = null; // Ensure that we have an open database connection @@ -511,20 +483,38 @@ } try { - // Accumulate the user's roles - ArrayList list = new ArrayList(); + return getRoles(dbConnection, username); + } finally { + close(dbConnection); + } + } + + /** + * Return the roles associated with the given user name + * @param dbConnection The database connection to be used + * @param username Username for which roles should be retrieved + */ + protected ArrayList getRoles(Connection dbConnection, + String username) { + + ResultSet rs = null; + PreparedStatement stmt = null; + ArrayList list = null; + + try { stmt = roles(dbConnection, username); rs = stmt.executeQuery(); + list = new ArrayList(); + while (rs.next()) { String role = rs.getString(1); if (role != null) { list.add(role.trim()); } } - return (list); } catch(SQLException e) { - log(sm.getString("datasourceRealm.getRoles.exception", username)); + log(sm.getString("dataSourceRealm.getRoles.exception", username)); } finally { try { if (rs != null) { @@ -534,7 +524,7 @@ stmt.close(); } } catch(SQLException e) { - log(sm.getString("datasourceRealm.getRoles.exception", + log(sm.getString("dataSourceRealm.getRoles.exception", username)); } } @@ -545,6 +535,28 @@ /** * Return a PreparedStatement configured to perform the SELECT required + * to retrieve user credentials for the specified username. + * + * @param dbConnection The database connection to be used + * @param username Username for which credentials should be retrieved + * + * @exception SQLException if a database error occurs + */ + private PreparedStatement credentials(Connection dbConnection, + String username) + throws SQLException { + + PreparedStatement credentials = + dbConnection.prepareStatement(preparedCredentials.toString()); + + credentials.setString(1, username); + return (credentials); + + } + + + /** + * Return a PreparedStatement configured to perform the SELECT required * to retrieve user roles for the specified username. * * @param dbConnection The database connection to be used 1.11 +3 -3 jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/realm/LocalStrings.properties Index: LocalStrings.properties =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/realm/LocalStrings.properties,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- LocalStrings.properties 27 Nov 2004 18:29:44 -0000 1.10 +++ LocalStrings.properties 3 Feb 2005 22:47:07 -0000 1.11 @@ -43,6 +43,6 @@ dataSourceRealm.authenticateSuccess=Username {0} successfully authenticated dataSourceRealm.close=Exception closing database connection dataSourceRealm.exception=Exception performing authentication -datasourceRealm.getPassword.exception=Exception retrieving password for "{0}" -datasourceRealm.getRoles.exception=Exception retrieving roles for "{0}" +dataSourceRealm.getPassword.exception=Exception retrieving password for "{0}" +dataSourceRealm.getRoles.exception=Exception retrieving roles for "{0}" dataSourceRealm.open=Exception opening database connection
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]