1 /************************************************************************
2 * Copyright (c) 2000-2006 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;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
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 * TODO Please note that default configuration uses JamesUsersJdbcRepository
35 * instead of this class. So we could also delete this implementation.
36 *
37 */
38 public class DefaultUsersJdbcRepository extends AbstractJdbcUsersRepository
39 {
40 /***
41 * Reads properties for a User from an open ResultSet.
42 *
43 * @param rsUsers A ResultSet with a User record in the current row.
44 * @return A User instance
45 * @throws SQLException
46 * if an exception occurs reading from the ResultSet
47 */
48 protected User readUserFromResultSet(ResultSet rsUsers) throws SQLException
49 {
50
51 String username = rsUsers.getString(1);
52 String passwordHash = rsUsers.getString(2);
53 String passwordAlg = rsUsers.getString(3);
54 DefaultUser user = new DefaultUser(username, passwordHash, passwordAlg);
55 return user;
56 }
57
58 /***
59 * Set parameters of a PreparedStatement object with
60 * property values from a User instance.
61 *
62 * @param user a User instance, which should be an implementation class which
63 * is handled by this Repostory implementation.
64 * @param userInsert a PreparedStatement initialised with SQL taken from the "insert" SQL definition.
65 * @throws SQLException
66 * if an exception occurs while setting parameter values.
67 */
68 protected void setUserForInsertStatement(User user,
69 PreparedStatement userInsert)
70 throws SQLException
71 {
72 DefaultUser defUser = (DefaultUser)user;
73 userInsert.setString(1, defUser.getUserName());
74 userInsert.setString(2, defUser.getHashAlgorithm());
75 userInsert.setString(3, defUser.getHashedPassword());
76 }
77
78 /***
79 * Set parameters of a PreparedStatement object with
80 * property values from a User instance.
81 *
82 * @param user a User instance, which should be an implementation class which
83 * is handled by this Repostory implementation.
84 * @param userUpdate a PreparedStatement initialised with SQL taken from the "update" SQL definition.
85 * @throws SQLException
86 * if an exception occurs while setting parameter values.
87 */
88 protected void setUserForUpdateStatement(User user,
89 PreparedStatement userUpdate)
90 throws SQLException
91 {
92 DefaultUser defUser = (DefaultUser)user;
93 userUpdate.setString(3, defUser.getUserName());
94 userUpdate.setString(1, defUser.getHashAlgorithm());
95 userUpdate.setString(2, defUser.getHashedPassword());
96 }
97
98 /***
99 * @see org.apache.james.services.UsersRepository#addUser(java.lang.String, java.lang.String)
100 */
101 public boolean addUser(String username, String password) {
102 User newbie = new DefaultUser(username, "SHA");
103 newbie.setPassword(password);
104 return addUser(newbie);
105 }
106
107
108 }
109