KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > signon > dao > PointbaseUserDAO


1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any
21 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
22 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
24 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
25 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
27 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
28 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
29 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
30 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
31 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that Software is not designed, licensed or intended
34 * for use in the design, construction, operation or maintenance of
35 * any nuclear facility.
36 */

37
38 package com.sun.j2ee.blueprints.signon.dao;
39
40 import java.util.ArrayList JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.sql.Connection JavaDoc;
43 import javax.sql.DataSource JavaDoc;
44 import java.sql.ResultSet JavaDoc;
45 import java.sql.SQLException JavaDoc;
46 import java.sql.PreparedStatement JavaDoc;
47 import javax.naming.InitialContext JavaDoc;
48 import javax.naming.NamingException JavaDoc;
49 import javax.naming.Context JavaDoc;
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 /**
57  * This class encapsulates all the database access. It follows
58  * the Data Access Object pattern.
59  **/

60 public class PointbaseUserDAO implements UserDAO {
61     
62     private final static String JavaDoc MATCH_PASSWORD_QUERY =
63     "SELECT password FROM " + DatabaseNames.SIGNON_TABLE +
64     " WHERE username = ?";
65     private final static String JavaDoc CREATE_USER_QUERY = "INSERT INTO " +
66     DatabaseNames.SIGNON_TABLE + " VALUES(?, ?)";
67     
68     public PointbaseUserDAO() {}
69     
70     
71     /**
72      * @throws SignOnDAODupKeyException if userName already exists in database
73      **/

74     public void createUser(String JavaDoc userName, String JavaDoc password)
75     throws SignOnDAODupKeyException {
76         
77         Connection JavaDoc conn = null;
78         PreparedStatement JavaDoc 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 JavaDoc se) {
90             throw new DAOSystemException(se);
91         } finally {
92             DAOUtils.closeStatement(ps);
93             DAOUtils.closeConnection(conn);
94         }
95     }
96     
97     
98     /**
99      * @return true if userName already exists in database AND the
100      * corresponding password in the database matches
101      * the password parameter
102      **/

103     public boolean matchPassword(String JavaDoc userName, String JavaDoc password)
104     throws SignOnDAOFinderException,
105     InvalidPasswordException{
106         
107         Connection JavaDoc conn = null;
108         PreparedStatement JavaDoc ps = null;
109         ResultSet JavaDoc 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 JavaDoc 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