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