View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  
21  
22  package org.apache.james.userrepository;
23  
24  import org.apache.james.api.user.User;
25  import org.apache.james.impl.user.DefaultUser;
26  
27  import java.sql.PreparedStatement;
28  import java.sql.ResultSet;
29  import java.sql.SQLException;
30  
31  
32  /**
33   * A Jdbc-backed UserRepository which handles User instances
34   * of the <CODE>DefaultUser</CODE> class.
35   * Although this repository can handle subclasses of DefaultUser,
36   * like <CODE>DefaultJamesUser</CODE>, only properties from
37   * the DefaultUser class are persisted.
38   * 
39   * TODO Please note that default configuration uses JamesUsersJdbcRepository
40   * instead of this class. So we could also delete this implementation.
41   * 
42   */
43  public class DefaultUsersJdbcRepository extends AbstractJdbcUsersRepository
44  {
45      /**
46       * @see org.apache.james.userrepository.AbstractJdbcUsersRepository#readUserFromResultSet(java.sql.ResultSet)
47       */
48      protected User readUserFromResultSet(ResultSet rsUsers) throws SQLException 
49      {
50          // Get the username, and build a DefaultUser with it.
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       * @see org.apache.james.userrepository.AbstractJdbcUsersRepository#setUserForInsertStatement(org.apache.james.api.user.User, java.sql.PreparedStatement)
60       */
61      protected void setUserForInsertStatement(User user, 
62                                               PreparedStatement userInsert) 
63          throws SQLException 
64      {
65          DefaultUser defUser = (DefaultUser)user;
66          userInsert.setString(1, defUser.getUserName());
67          userInsert.setString(2, defUser.getHashedPassword());
68          userInsert.setString(3, defUser.getHashAlgorithm());
69      }
70  
71      /**
72       * @see org.apache.james.userrepository.AbstractJdbcUsersRepository#setUserForUpdateStatement(org.apache.james.api.user.User, java.sql.PreparedStatement)
73       */
74      protected void setUserForUpdateStatement(User user, 
75                                               PreparedStatement userUpdate) 
76          throws SQLException 
77      {
78          DefaultUser defUser = (DefaultUser)user;
79          userUpdate.setString(1, defUser.getHashedPassword());
80          userUpdate.setString(2, defUser.getHashAlgorithm());
81          userUpdate.setString(3, defUser.getUserName());
82      }
83      
84      /**
85       * @see org.apache.james.api.user.UsersRepository#addUser(java.lang.String, java.lang.String)
86       */
87      public boolean addUser(String username, String password)  {
88          User newbie = new DefaultUser(username, "SHA");
89          newbie.setPassword(password);
90          return addUser(newbie);
91      }
92  
93  
94  }
95