001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.userrepository;
019:
020: import org.apache.james.services.User;
021: import org.apache.mailet.MailAddress;
022:
023: import java.sql.PreparedStatement;
024: import java.sql.ResultSet;
025: import java.sql.SQLException;
026:
027: /**
028: * A Jdbc-backed UserRepository which handles User instances
029: * of the <CODE>DefaultJamesUser</CODE> class, or any superclass.
030: *
031: */
032: public class JamesUsersJdbcRepository extends
033: AbstractJdbcUsersRepository {
034: /**
035: * Reads properties for a User from an open ResultSet.
036: *
037: * @param rsUsers A ResultSet with a User record in the current row.
038: * @return A User instance
039: * @throws SQLException
040: * if an exception occurs reading from the ResultSet
041: */
042: protected User readUserFromResultSet(ResultSet rsUsers)
043: throws SQLException {
044: // Get the column values
045: String username = rsUsers.getString(1);
046: String pwdHash = rsUsers.getString(2);
047: String pwdAlgorithm = rsUsers.getString(3);
048: boolean useForwarding = rsUsers.getBoolean(4);
049: String forwardingDestination = rsUsers.getString(5);
050: boolean useAlias = rsUsers.getBoolean(6);
051: String alias = rsUsers.getString(7);
052:
053: MailAddress forwardAddress = null;
054: if (forwardingDestination != null) {
055: try {
056: forwardAddress = new MailAddress(forwardingDestination);
057: } catch (javax.mail.internet.ParseException pe) {
058: StringBuffer exceptionBuffer = new StringBuffer(256)
059: .append("Invalid mail address in database: ")
060: .append(forwardingDestination).append(
061: ", for user ").append(username).append(
062: ".");
063: throw new RuntimeException(exceptionBuffer.toString());
064: }
065: }
066:
067: // Build a DefaultJamesUser with these values, and add to the list.
068: DefaultJamesUser user = new DefaultJamesUser(username, pwdHash,
069: pwdAlgorithm);
070: user.setForwarding(useForwarding);
071: user.setForwardingDestination(forwardAddress);
072: user.setAliasing(useAlias);
073: user.setAlias(alias);
074:
075: return user;
076: }
077:
078: /**
079: * Set parameters of a PreparedStatement object with
080: * property values from a User instance.
081: *
082: * @param user a User instance, which should be an implementation class which
083: * is handled by this Repostory implementation.
084: * @param userInsert a PreparedStatement initialised with SQL taken from the "insert" SQL definition.
085: * @throws SQLException
086: * if an exception occurs while setting parameter values.
087: */
088: protected void setUserForInsertStatement(User user,
089: PreparedStatement userInsert) throws SQLException {
090: setUserForStatement(user, userInsert, false);
091: }
092:
093: /**
094: * Set parameters of a PreparedStatement object with
095: * property values from a User instance.
096: *
097: * @param user a User instance, which should be an implementation class which
098: * is handled by this Repostory implementation.
099: * @param userUpdate a PreparedStatement initialised with SQL taken from the "update" SQL definition.
100: * @throws SQLException
101: * if an exception occurs while setting parameter values.
102: */
103: protected void setUserForUpdateStatement(User user,
104: PreparedStatement userUpdate) throws SQLException {
105: setUserForStatement(user, userUpdate, true);
106: }
107:
108: /**
109: * Sets the data for the prepared statement to match the information
110: * in the user object.
111: *
112: * @param user the user whose data is to be stored in the PreparedStatement.
113: * @param stmt the PreparedStatement to be modified.
114: * @param userNameLast whether the user id is the last or the first column
115: */
116: private void setUserForStatement(User user, PreparedStatement stmt,
117: boolean userNameLast) throws SQLException {
118: // Determine column offsets to use, based on username column pos.
119: int nameIndex = 1;
120: int colOffset = 1;
121: if (userNameLast) {
122: nameIndex = 7;
123: colOffset = 0;
124: }
125:
126: // Can handle instances of DefaultJamesUser and DefaultUser.
127: DefaultJamesUser jamesUser;
128: if (user instanceof DefaultJamesUser) {
129: jamesUser = (DefaultJamesUser) user;
130: } else if (user instanceof DefaultUser) {
131: DefaultUser aUser = (DefaultUser) user;
132: jamesUser = new DefaultJamesUser(aUser.getUserName(), aUser
133: .getHashedPassword(), aUser.getHashAlgorithm());
134: }
135: // Can't handle any other implementations.
136: else {
137: throw new RuntimeException(
138: "An unknown implementation of User was "
139: + "found. This implementation cannot be "
140: + "persisted to a UsersJDBCRepsitory.");
141: }
142:
143: // Get the user details to save.
144: stmt.setString(nameIndex, jamesUser.getUserName());
145: stmt.setString(1 + colOffset, jamesUser.getHashedPassword());
146: stmt.setString(2 + colOffset, jamesUser.getHashAlgorithm());
147: stmt.setInt(3 + colOffset, (jamesUser.getForwarding() ? 1 : 0));
148:
149: MailAddress forwardAddress = jamesUser
150: .getForwardingDestination();
151: String forwardDestination = null;
152: if (forwardAddress != null) {
153: forwardDestination = forwardAddress.toString();
154: }
155: stmt.setString(4 + colOffset, forwardDestination);
156: stmt.setInt(5 + colOffset, (jamesUser.getAliasing() ? 1 : 0));
157: stmt.setString(6 + colOffset, jamesUser.getAlias());
158: }
159:
160: /**
161: * @see org.apache.james.services.UsersRepository#addUser(java.lang.String, java.lang.String)
162: */
163: public boolean addUser(String username, String password) {
164: User newbie = new DefaultJamesUser(username, "SHA");
165: newbie.setPassword(password);
166: return addUser(newbie);
167: }
168:
169: }
|