1 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 32 public class JamesUsersJdbcRepository extends AbstractJdbcUsersRepository 33 { 34 42 protected User readUserFromResultSet(ResultSet rsUsers) throws SQLException 43 { 44 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 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 90 protected void setUserForInsertStatement(User user, 91 PreparedStatement userInsert) 92 throws SQLException { 93 setUserForStatement(user, userInsert, false); 94 } 95 96 106 protected void setUserForUpdateStatement(User user, 107 PreparedStatement userUpdate) 108 throws SQLException { 109 setUserForStatement(user, userUpdate, true); 110 } 111 112 120 private void setUserForStatement(User user, PreparedStatement stmt, 121 boolean userNameLast) throws SQLException { 122 int nameIndex = 1; 124 int colOffset = 1; 125 if ( userNameLast ) { 126 nameIndex = 7; 127 colOffset = 0; 128 } 129 130 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 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 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 | Popular Tags |