KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > userrepository > DefaultUsersJdbcRepository


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.userrepository;
19
20 import org.apache.james.services.User;
21
22 import java.sql.PreparedStatement JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25
26
27 /**
28  * A Jdbc-backed UserRepository which handles User instances
29  * of the <CODE>DefaultUser</CODE> class.
30  * Although this repository can handle subclasses of DefaultUser,
31  * like <CODE>DefaultJamesUser</CODE>, only properties from
32  * the DefaultUser class are persisted.
33  *
34  */

35 public class DefaultUsersJdbcRepository extends AbstractJdbcUsersRepository
36 {
37     /**
38      * Reads properties for a User from an open ResultSet.
39      *
40      * @param rsUsers A ResultSet with a User record in the current row.
41      * @return A User instance
42      * @throws SQLException
43      * if an exception occurs reading from the ResultSet
44      */

45     protected User readUserFromResultSet(ResultSet JavaDoc rsUsers) throws SQLException JavaDoc
46     {
47         // Get the username, and build a DefaultUser with it.
48
String JavaDoc username = rsUsers.getString(1);
49         String JavaDoc passwordAlg = rsUsers.getString(2);
50         String JavaDoc passwordHash = rsUsers.getString(3);
51         DefaultUser user = new DefaultUser(username, passwordHash, passwordAlg);
52         return user;
53     }
54
55     /**
56      * Set parameters of a PreparedStatement object with
57      * property values from a User instance.
58      *
59      * @param user a User instance, which should be an implementation class which
60      * is handled by this Repostory implementation.
61      * @param userInsert a PreparedStatement initialised with SQL taken from the "insert" SQL definition.
62      * @throws SQLException
63      * if an exception occurs while setting parameter values.
64      */

65     protected void setUserForInsertStatement(User user,
66                                              PreparedStatement JavaDoc userInsert)
67         throws SQLException JavaDoc
68     {
69         DefaultUser defUser = (DefaultUser)user;
70         userInsert.setString(1, defUser.getUserName());
71         userInsert.setString(2, defUser.getHashAlgorithm());
72         userInsert.setString(3, defUser.getHashedPassword());
73     }
74
75     /**
76      * Set parameters of a PreparedStatement object with
77      * property values from a User instance.
78      *
79      * @param user a User instance, which should be an implementation class which
80      * is handled by this Repostory implementation.
81      * @param userUpdate a PreparedStatement initialised with SQL taken from the "update" SQL definition.
82      * @throws SQLException
83      * if an exception occurs while setting parameter values.
84      */

85     protected void setUserForUpdateStatement(User user,
86                                              PreparedStatement JavaDoc userUpdate)
87         throws SQLException JavaDoc
88     {
89         DefaultUser defUser = (DefaultUser)user;
90         userUpdate.setString(3, defUser.getUserName());
91         userUpdate.setString(1, defUser.getHashAlgorithm());
92         userUpdate.setString(2, defUser.getHashedPassword());
93     }
94 }
95
96
Popular Tags