1 37 38 package com.sun.j2ee.blueprints.signon.dao; 39 40 import java.util.ArrayList ; 41 import java.util.Iterator ; 42 import java.sql.Connection ; 43 import javax.sql.DataSource ; 44 import java.sql.ResultSet ; 45 import java.sql.SQLException ; 46 import java.sql.PreparedStatement ; 47 import javax.naming.InitialContext ; 48 import javax.naming.NamingException ; 49 import javax.naming.Context ; 50 51 import com.sun.j2ee.blueprints.util.tracer.Debug; 52 import com.sun.j2ee.blueprints.util.dao.DAOSystemException; 53 import com.sun.j2ee.blueprints.util.dao.DAOUtils; 54 55 56 60 public class PointbaseUserDAO implements UserDAO { 61 62 private final static String MATCH_PASSWORD_QUERY = 63 "SELECT password FROM " + DatabaseNames.SIGNON_TABLE + 64 " WHERE username = ?"; 65 private final static String CREATE_USER_QUERY = "INSERT INTO " + 66 DatabaseNames.SIGNON_TABLE + " VALUES(?, ?)"; 67 68 public PointbaseUserDAO() {} 69 70 71 74 public void createUser(String userName, String password) 75 throws SignOnDAODupKeyException { 76 77 Connection conn = null; 78 PreparedStatement ps = null; 79 try { 80 conn = DAOUtils.getDBConnection(JNDINames.SIGNON_DATASOURCE); 81 ps = conn.prepareStatement(CREATE_USER_QUERY); 82 ps.setString(1, userName.trim()); 83 ps.setString(2, password.trim()); 84 int result = ps.executeUpdate(); 85 if(result != 1) { 86 throw new SignOnDAODupKeyException("Unable to create user. " + 87 "Duplicate Key : " + userName); 88 } 89 } catch (SQLException se) { 90 throw new DAOSystemException(se); 91 } finally { 92 DAOUtils.closeStatement(ps); 93 DAOUtils.closeConnection(conn); 94 } 95 } 96 97 98 103 public boolean matchPassword(String userName, String password) 104 throws SignOnDAOFinderException, 105 InvalidPasswordException{ 106 107 Connection conn = null; 108 PreparedStatement ps = null; 109 ResultSet rs = null; 110 try { 111 conn = DAOUtils.getDBConnection(JNDINames.SIGNON_DATASOURCE); 112 ps = conn.prepareStatement(MATCH_PASSWORD_QUERY); 113 ps.setString(1, userName.trim()); 114 rs = ps.executeQuery(); 115 if(rs.next()) { 116 if(!rs.getString(1).equals(password)) { 117 throw new InvalidPasswordException("Password does not match"); 118 } 119 } else { 120 throw new SignOnDAOFinderException("Unable to find user " + 121 userName); 122 } 123 } catch (SQLException se) { 124 throw new DAOSystemException(se); 125 } finally { 126 DAOUtils.closeResultSet(rs); 127 DAOUtils.closeStatement(ps); 128 DAOUtils.closeConnection(conn); 129 } 130 return(true); 131 } 132 } 133 | Popular Tags |