Commits

Anonymous committed 65cf54e

Patch from USER-65

Comments (0)

Files changed (5)

src/java/com/opensymphony/user/provider/jdbc/BaseJDBCProvider.java

 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
 import java.util.Properties;
 
 import javax.naming.InitialContext;
  * DOCUMENT ME!
  *
  * @author $author$
- * @version $Revision: 1.1.1.1 $
+ * @version $Revision: 1.2 $
  */
 public abstract class BaseJDBCProvider implements UserProvider {
     //~ Static fields/initializers /////////////////////////////////////////////
     protected String userName;
     protected String userPassword;
     protected String userTable;
+    protected boolean closeConnWhenDone = false;
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
 
         String jndi = (String) props.get("datasource");
 
-        try {
-            ds = (DataSource) new InitialContext().lookup(jndi);
-        } catch (NamingException e) {
-            log.fatal("Could not look up DataSource using JNDI location: " + jndi, e);
+        if (jndi != null) {
+            try {
+                ds = (DataSource) new InitialContext().lookup(jndi);
+            } catch (NamingException e) {
+                log.fatal("Could not look up DataSource using JNDI location: " + jndi, e);
 
-            return false;
+                return false;
+            }
         }
 
         return true;
     public boolean store(String name, Entity.Accessor accessor) {
         return true;
     }
+
+    protected Connection getConnection() throws SQLException {
+        closeConnWhenDone = true;
+
+        return ds.getConnection();
+    }
+
+    protected void cleanup(Connection connection, Statement statement, ResultSet result) {
+        if (result != null) {
+            try {
+                result.close();
+            } catch (SQLException ex) {
+                log.error("Error closing resultset", ex);
+            }
+        }
+
+        if (statement != null) {
+            try {
+                statement.close();
+            } catch (SQLException ex) {
+                log.error("Error closing statement", ex);
+            }
+        }
+
+        if ((connection != null) && closeConnWhenDone) {
+            try {
+                connection.close();
+            } catch (SQLException ex) {
+                log.error("Error closing connection", ex);
+            }
+        }
+    }
 }

src/java/com/opensymphony/user/provider/jdbc/JDBCAccessProvider.java

  * DOCUMENT ME!
  *
  * @author $author$
- * @version $Revision: 1.1.1.1 $
+ * @version $Revision: 1.2 $
  */
 public class JDBCAccessProvider extends BaseJDBCProvider implements AccessProvider {
     //~ Static fields/initializers /////////////////////////////////////////////
 
     public boolean addToGroup(String username, String groupname) {
         boolean addedToGroup = false;
+        Connection conn = null;
+        PreparedStatement ps = null;
 
         try {
-            Connection conn = ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement("INSERT INTO " + membershipTable + " (" + userName + ", " + groupName + ") VALUES (?, ?)");
+            conn = getConnection();
+            ps = conn.prepareStatement("INSERT INTO " + membershipTable + " (" + userName + ", " + groupName + ") VALUES (?, ?)");
             ps.setString(1, username);
             ps.setString(2, groupname);
             ps.executeUpdate();
             addedToGroup = true;
-            ps.close();
-            conn.close();
         } catch (SQLException e) {
             log.fatal("Could not add user [" + username + "] to group [" + groupname + "]", e);
+        } finally {
+            cleanup(conn, ps, null);
         }
 
         return addedToGroup;
 
     public boolean create(String name) {
         boolean created = false;
+        Connection conn = null;
+        PreparedStatement ps = null;
 
         try {
-            Connection conn = ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement("INSERT INTO " + groupTable + " (" + groupName + ") VALUES (?)");
+            conn = getConnection();
+            ps = conn.prepareStatement("INSERT INTO " + groupTable + " (" + groupName + ") VALUES (?)");
             ps.setString(1, name);
 
             try {
             } catch (SQLException e) {
                 log.warn("Group [" + name + "] must already exist", e);
             }
-
-            ps.close();
-            conn.close();
         } catch (SQLException e) {
             log.fatal("Could not insert [" + name + "] into groups", e);
+        } finally {
+            cleanup(conn, ps, null);
         }
 
         return created;
 
     public boolean handles(String name) {
         boolean handles = false;
+        Connection conn = null;
+        PreparedStatement ps = null;
+        ResultSet rs = null;
 
         try {
             // first try if the user exists
-            Connection conn = ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement("SELECT " + userName + " FROM " + userTable + " WHERE " + userName + " = ?");
+            conn = getConnection();
+            ps = conn.prepareStatement("SELECT " + userName + " FROM " + userTable + " WHERE " + userName + " = ?");
             ps.setString(1, name);
 
-            ResultSet rs = ps.executeQuery();
+            rs = ps.executeQuery();
 
             if (rs.next()) {
                 handles = true;
                 ps.setString(1, name);
                 rs = ps.executeQuery();
                 handles = rs.next();
-                rs.close();
-                ps.close();
+
+                // ps and rs closed in cleanup
             }
-
-            conn.close();
         } catch (SQLException e) {
             log.fatal("Could not see if [" + name + "] is handled", e);
+        } finally {
+            cleanup(conn, ps, rs);
         }
 
         return handles;
 
     public boolean inGroup(String username, String groupname) {
         boolean inGroup = false;
+        Connection conn = null;
+        PreparedStatement ps = null;
+        ResultSet rs = null;
 
         try {
-            Connection conn = ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement("SELECT * FROM " + membershipTable + " WHERE " + userName + " = ? AND " + groupName + " = ?");
+            conn = getConnection();
+            ps = conn.prepareStatement("SELECT * FROM " + membershipTable + " WHERE " + userName + " = ? AND " + groupName + " = ?");
             ps.setString(1, username);
             ps.setString(2, groupname);
 
-            ResultSet rs = ps.executeQuery();
+            rs = ps.executeQuery();
             inGroup = rs.next();
-            rs.close();
-            ps.close();
-            conn.close();
         } catch (SQLException e) {
             log.fatal("Could not determine if user [" + username + "] is in group [" + groupname + "]", e);
+        } finally {
+            cleanup(conn, ps, rs);
         }
 
         return inGroup;
 
     public List list() {
         ArrayList groups = new ArrayList();
+        Connection conn = null;
+        PreparedStatement ps = null;
+        ResultSet rs = null;
 
         try {
-            Connection conn = ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement("SELECT " + groupName + " FROM " + groupTable + " ORDER BY " + groupName);
-            ResultSet rs = ps.executeQuery();
+            conn = getConnection();
+            ps = conn.prepareStatement("SELECT " + groupName + " FROM " + groupTable + " ORDER BY " + groupName);
+            rs = ps.executeQuery();
 
             while (rs.next()) {
                 groups.add(rs.getString(1));
             }
-
-            rs.close();
-            ps.close();
-            conn.close();
         } catch (SQLException e) {
             log.fatal("Could not list groups", e);
+        } finally {
+            cleanup(conn, ps, rs);
         }
 
         return groups;
 
     public List listGroupsContainingUser(String username) {
         ArrayList groups = new ArrayList();
+        Connection conn = null;
+        PreparedStatement ps = null;
+        ResultSet rs = null;
 
         try {
-            Connection conn = ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement("SELECT " + membershipGroupName + " FROM " + membershipTable + " WHERE " + membershipUserName + " = ?");
+            conn = getConnection();
+            ps = conn.prepareStatement("SELECT " + membershipGroupName + " FROM " + membershipTable + " WHERE " + membershipUserName + " = ?");
             ps.setString(1, username);
 
-            ResultSet rs = ps.executeQuery();
+            rs = ps.executeQuery();
 
             while (rs.next()) {
                 groups.add(rs.getString(1));
             }
-
-            rs.close();
-            ps.close();
-            conn.close();
         } catch (SQLException e) {
             log.fatal("Could not list groups containing user [" + username + "]", e);
+        } finally {
+            cleanup(conn, ps, rs);
         }
 
         return groups;
 
     public List listUsersInGroup(String groupname) {
         ArrayList users = new ArrayList();
+        Connection conn = null;
+        PreparedStatement ps = null;
+        ResultSet rs = null;
 
         try {
-            Connection conn = ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement("SELECT " + membershipUserName + " FROM " + membershipTable + " WHERE " + membershipGroupName + " = ?");
+            conn = getConnection();
+            ps = conn.prepareStatement("SELECT " + membershipUserName + " FROM " + membershipTable + " WHERE " + membershipGroupName + " = ?");
             ps.setString(1, groupname);
 
-            ResultSet rs = ps.executeQuery();
+            rs = ps.executeQuery();
 
             while (rs.next()) {
                 users.add(rs.getString(1));
             }
-
-            rs.close();
-            ps.close();
-            conn.close();
         } catch (SQLException e) {
             log.fatal("Could not list users in group [" + groupname + "]", e);
+        } finally {
+            cleanup(conn, ps, rs);
         }
 
         return users;
 
     public boolean remove(String name) {
         boolean removed = false;
+        Connection conn = null;
+        PreparedStatement ps = null;
 
         try {
-            Connection conn = ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement("DELETE FROM " + membershipTable + " WHERE " + membershipGroupName + " = ?");
+            conn = getConnection();
+            ps = conn.prepareStatement("DELETE FROM " + membershipTable + " WHERE " + membershipGroupName + " = ?");
             ps.setString(1, name);
             ps.executeUpdate();
             ps.close();
             conn.close();
         } catch (SQLException e) {
             log.fatal("Could not remove group [" + name + "]", e);
+        } finally {
+            cleanup(conn, ps, null);
         }
 
         return removed;
 
     public boolean removeFromGroup(String username, String groupname) {
         boolean removedFromGroup = false;
+        Connection conn = null;
+        PreparedStatement ps = null;
 
         try {
-            Connection conn = ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement("DELETE FROM " + membershipTable + " WHERE " + userName + " = ? AND " + groupName + " =?");
+            conn = getConnection();
+            ps = conn.prepareStatement("DELETE FROM " + membershipTable + " WHERE " + userName + " = ? AND " + groupName + " =?");
             ps.setString(1, username);
             ps.setString(2, groupname);
 
             if (rows == 1) {
                 removedFromGroup = true;
             }
-
-            ps.close();
-            conn.close();
         } catch (SQLException e) {
             log.fatal("Could not detele user [" + username + "] from group [" + groupname + "]", e);
+        } finally {
+            cleanup(conn, ps, null);
         }
 
         return removedFromGroup;

src/java/com/opensymphony/user/provider/jdbc/JDBCCredentialsProvider.java

  * Credentials provider backed by a JDBC datastore.  Allows modification of
  * user data.
  *
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
  */
 public class JDBCCredentialsProvider extends BaseJDBCProvider implements CredentialsProvider {
     //~ Static fields/initializers /////////////////////////////////////////////
      */
     public boolean authenticate(String name, String password) {
         boolean authenticated = false;
+        Connection conn = null;
+        PreparedStatement ps = null;
+        ResultSet rs = null;
 
         try {
-            Connection conn = ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement("SELECT " + userPassword + " FROM " + userTable + " WHERE " + userName + " = ?");
+            conn = getConnection();
+            ps = conn.prepareStatement("SELECT " + userPassword + " FROM " + userTable + " WHERE " + userName + " = ?");
             ps.setString(1, name);
 
-            ResultSet rs = ps.executeQuery();
+            rs = ps.executeQuery();
 
             if (rs.next()) {
                 authenticated = compareHash(rs.getString(1), password);
             }
-
-            rs.close();
-            ps.close();
-            conn.close();
         } catch (SQLException e) {
             log.fatal("Could not authenticate user [" + name + "]", e);
+        } finally {
+            cleanup(conn, ps, rs);
         }
 
         return authenticated;
      */
     public boolean changePassword(String name, String password) {
         boolean changedPassword = false;
+        Connection conn = null;
+        PreparedStatement ps = null;
 
         try {
-            Connection conn = ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement("UPDATE " + userTable + " SET " + userPassword + " = ? WHERE " + userName + " = ?");
+            conn = getConnection();
+            ps = conn.prepareStatement("UPDATE " + userTable + " SET " + userPassword + " = ? WHERE " + userName + " = ?");
             ps.setString(1, createHash(password));
             ps.setString(2, name);
             ps.executeUpdate();
             changedPassword = true;
-            ps.close();
-            conn.close();
         } catch (SQLException e) {
             log.fatal("Could not change password for user [" + name + "]", e);
+        } finally {
+            cleanup(conn, ps, null);
         }
 
         return changedPassword;
      */
     public boolean create(String name) {
         boolean created = false;
+        Connection conn = null;
+        PreparedStatement ps = null;
 
         try {
-            Connection conn = ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement("INSERT INTO " + userTable + " (" + userName + ") VALUES (?)");
+            conn = getConnection();
+            ps = conn.prepareStatement("INSERT INTO " + userTable + " (" + userName + ") VALUES (?)");
             ps.setString(1, name);
 
             try {
             } catch (SQLException e) {
                 log.warn("User [" + name + "] must already exist", e);
             }
-
-            ps.close();
-            conn.close();
         } catch (SQLException e) {
             log.fatal("Could not create user [" + name + "]", e);
+        } finally {
+            cleanup(conn, ps, null);
         }
 
         return created;
      */
     public boolean handles(String name) {
         boolean handles = false;
+        Connection conn = null;
+        PreparedStatement ps = null;
+        ResultSet rs = null;
 
         try {
-            Connection conn = ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement("SELECT * FROM " + userTable + " WHERE " + userName + " = ?");
+            conn = getConnection();
+            ps = conn.prepareStatement("SELECT * FROM " + userTable + " WHERE " + userName + " = ?");
             ps.setString(1, name);
 
-            ResultSet rs = ps.executeQuery();
+            rs = ps.executeQuery();
             handles = rs.next();
-            rs.close();
-            ps.close();
-            conn.close();
         } catch (SQLException e) {
             log.fatal("Could not see if [" + name + "] is handled", e);
+        } finally {
+            cleanup(conn, ps, rs);
         }
 
         return handles;
      */
     public List list() {
         ArrayList users = new ArrayList();
+        Connection conn = null;
+        PreparedStatement ps = null;
+        ResultSet rs = null;
 
         try {
-            Connection conn = ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement("SELECT " + userName + " FROM " + userTable + " ORDER BY " + userName);
-            ResultSet rs = ps.executeQuery();
+            conn = getConnection();
+            ps = conn.prepareStatement("SELECT " + userName + " FROM " + userTable + " ORDER BY " + userName);
+            rs = ps.executeQuery();
 
             while (rs.next()) {
                 users.add(rs.getString(1));
             }
-
-            rs.close();
-            ps.close();
-            conn.close();
         } catch (SQLException e) {
             log.fatal("Could not list users", e);
+        } finally {
+            cleanup(conn, ps, rs);
         }
 
         return users;
      */
     public boolean remove(String name) {
         boolean removed = false;
+        Connection conn = null;
+        PreparedStatement ps = null;
 
         try {
-            Connection conn = ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement("DELETE FROM " + membershipTable + " WHERE " + membershipUserName + " = ?");
+            conn = getConnection();
+            ps = conn.prepareStatement("DELETE FROM " + membershipTable + " WHERE " + membershipUserName + " = ?");
             ps.setString(1, name);
             ps.executeUpdate();
             ps.close();
             if (rows == 1) {
                 removed = true;
             }
-
-            ps.close();
-            conn.close();
         } catch (SQLException e) {
             log.fatal("Unable to remove user [" + name + "]", e);
+        } finally {
+            cleanup(conn, ps, null);
         }
 
         return removed;

src/java/com/opensymphony/user/provider/jdbc/JDBCProfileProvider.java

  * Profile provider that is back by a JDBC datasource.
  *
  * @author $author$
- * @version $Revision: 1.3 $
+ * @version $Revision: 1.4 $
  */
 public class JDBCProfileProvider extends BaseJDBCProvider implements ProfileProvider {
     //~ Static fields/initializers /////////////////////////////////////////////
      */
     public boolean handles(String name) {
         boolean handles = false;
+        Connection conn = null;
+        PreparedStatement ps = null;
+        ResultSet rs = null;
 
         try {
-            Connection conn = ds.getConnection();
-            PreparedStatement ps = conn.prepareStatement("SELECT * FROM " + userTable + " WHERE " + userName + " = ?");
+            conn = getConnection();
+            ps = conn.prepareStatement("SELECT * FROM " + userTable + " WHERE " + userName + " = ?");
             ps.setString(1, name);
 
-            ResultSet rs = ps.executeQuery();
+            rs = ps.executeQuery();
             handles = rs.next();
-            rs.close();
-            ps.close();
-            conn.close();
         } catch (SQLException e) {
             log.fatal("Could not see if [" + name + "] is handled", e);
+        } finally {
+            cleanup(conn, ps, rs);
         }
 
         return handles;

src/java/com/opensymphony/user/provider/ldap/LDAPCredentialsProvider.java

  *   <li>If the user exists in LDAP but the password was incorrect, the module fails without consulting other CredentialsProviders.
  *   <li>Turning logging up to DEBUG will reveal details on authentication attempts.
  * </ul>
- *  
- * @author <a href="mailto:jeff@atlassian.com">Jeff Turner</a> 
+ *
+ * @author <a href="mailto:jeff@atlassian.com">Jeff Turner</a>
  */
 public class LDAPCredentialsProvider implements CredentialsProvider {
     //~ Static fields/initializers /////////////////////////////////////////////
 
     public boolean init(Properties properties) {
         if (log.isDebugEnabled()) {
-            log.debug("LDAPCredentialsProvider $Revision: 1.3 $ initializing");
+            log.debug("LDAPCredentialsProvider $Revision: 1.4 $ initializing");
         }
 
         env = new Hashtable(properties);