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 import org.apache.mailet.MailAddress;
22
23 import java.sql.PreparedStatement;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26
27 /***
28 * A Jdbc-backed UserRepository which handles User instances
29 * of the <CODE>DefaultJamesUser</CODE> class, or any superclass.
30 *
31 */
32 public class JamesUsersJdbcRepository extends AbstractJdbcUsersRepository
33 {
34 /***
35 * Reads properties for a User from an open ResultSet.
36 *
37 * @param rsUsers A ResultSet with a User record in the current row.
38 * @return A User instance
39 * @throws SQLException
40 * if an exception occurs reading from the ResultSet
41 */
42 protected User readUserFromResultSet(ResultSet rsUsers) throws SQLException
43 {
44
45 String username = rsUsers.getString(1);
46 String pwdHash = rsUsers.getString(2);
47 String pwdAlgorithm = rsUsers.getString(3);
48 boolean useForwarding = rsUsers.getBoolean(4);
49 String forwardingDestination = rsUsers.getString(5);
50 boolean useAlias = rsUsers.getBoolean(6);
51 String alias = rsUsers.getString(7);
52
53 MailAddress forwardAddress = null;
54 if ( forwardingDestination != null ) {
55 try {
56 forwardAddress = new MailAddress(forwardingDestination);
57 }
58 catch (javax.mail.internet.ParseException pe) {
59 StringBuffer exceptionBuffer =
60 new StringBuffer(256)
61 .append("Invalid mail address in database: ")
62 .append(forwardingDestination)
63 .append(", for user ")
64 .append(username)
65 .append(".");
66 throw new RuntimeException(exceptionBuffer.toString());
67 }
68 }
69
70
71 DefaultJamesUser user = new DefaultJamesUser(username, pwdHash, pwdAlgorithm);
72 user.setForwarding(useForwarding);
73 user.setForwardingDestination(forwardAddress);
74 user.setAliasing(useAlias);
75 user.setAlias(alias);
76
77 return user;
78 }
79
80 /***
81 * Set parameters of a PreparedStatement object with
82 * property values from a User instance.
83 *
84 * @param user a User instance, which should be an implementation class which
85 * is handled by this Repostory implementation.
86 * @param userInsert a PreparedStatement initialised with SQL taken from the "insert" SQL definition.
87 * @throws SQLException
88 * if an exception occurs while setting parameter values.
89 */
90 protected void setUserForInsertStatement(User user,
91 PreparedStatement userInsert)
92 throws SQLException {
93 setUserForStatement(user, userInsert, false);
94 }
95
96 /***
97 * Set parameters of a PreparedStatement object with
98 * property values from a User instance.
99 *
100 * @param user a User instance, which should be an implementation class which
101 * is handled by this Repostory implementation.
102 * @param userUpdate a PreparedStatement initialised with SQL taken from the "update" SQL definition.
103 * @throws SQLException
104 * if an exception occurs while setting parameter values.
105 */
106 protected void setUserForUpdateStatement(User user,
107 PreparedStatement userUpdate)
108 throws SQLException {
109 setUserForStatement(user, userUpdate, true);
110 }
111
112 /***
113 * Sets the data for the prepared statement to match the information
114 * in the user object.
115 *
116 * @param user the user whose data is to be stored in the PreparedStatement.
117 * @param stmt the PreparedStatement to be modified.
118 * @param userNameLast whether the user id is the last or the first column
119 */
120 private void setUserForStatement(User user, PreparedStatement stmt,
121 boolean userNameLast) throws SQLException {
122
123 int nameIndex = 1;
124 int colOffset = 1;
125 if ( userNameLast ) {
126 nameIndex = 7;
127 colOffset = 0;
128 }
129
130
131 DefaultJamesUser jamesUser;
132 if (user instanceof DefaultJamesUser) {
133 jamesUser = (DefaultJamesUser)user;
134 }
135 else if ( user instanceof DefaultUser ) {
136 DefaultUser aUser = (DefaultUser)user;
137 jamesUser = new DefaultJamesUser(aUser.getUserName(),
138 aUser.getHashedPassword(),
139 aUser.getHashAlgorithm());
140 }
141
142 else {
143 throw new RuntimeException("An unknown implementation of User was " +
144 "found. This implementation cannot be " +
145 "persisted to a UsersJDBCRepsitory.");
146 }
147
148
149 stmt.setString(nameIndex, jamesUser.getUserName());
150 stmt.setString(1 + colOffset, jamesUser.getHashedPassword());
151 stmt.setString(2 + colOffset, jamesUser.getHashAlgorithm());
152 stmt.setInt(3 + colOffset, (jamesUser.getForwarding() ? 1 : 0));
153
154 MailAddress forwardAddress = jamesUser.getForwardingDestination();
155 String forwardDestination = null;
156 if ( forwardAddress != null ) {
157 forwardDestination = forwardAddress.toString();
158 }
159 stmt.setString(4 + colOffset, forwardDestination);
160 stmt.setInt(5 + colOffset, (jamesUser.getAliasing() ? 1 : 0));
161 stmt.setString(6 + colOffset, jamesUser.getAlias());
162 }
163
164
165
166 /***
167 * @see org.apache.james.services.UsersRepository#addUser(java.lang.String, java.lang.String)
168 */
169 public boolean addUser(String username, String password) {
170 User newbie = new DefaultJamesUser(username, "SHA");
171 newbie.setPassword(password);
172 return addUser(newbie);
173 }
174
175 }