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   * A very lightweight UserRepository, which persists a list
30   * of user names in a database. Password information is not 
31   * persisted.
32   * 
33   */
34  public class ListUsersJdbcRepository extends AbstractJdbcUsersRepository
35  {
36      /***
37       * Reads properties for a User from an open ResultSet.
38       * 
39       * @param rsUsers A ResultSet with a User record in the current row.
40       * @return A User instance
41       * @throws SQLException
42       *                   if an exception occurs reading from the ResultSet
43       */
44      protected User readUserFromResultSet(ResultSet rsUsers) throws SQLException {
45          // Get the username, and build a DefaultUser with it.
46          String username = rsUsers.getString(1);
47          DefaultUser user = new DefaultUser(username, "SHA");
48          return user;
49      }
50  
51      /***
52       * Set parameters of a PreparedStatement object with 
53       * property values from a User instance.
54       * @param user       a User instance, which should be an implementation class which
55       *                   is handled by this Repostory implementation.
56       * @param userInsert a PreparedStatement initialised with SQL taken from the "insert" SQL definition.
57       * @throws SQLException
58       *                   if an exception occurs while setting parameter values.
59       */
60      protected void setUserForInsertStatement(User user, 
61                                               PreparedStatement userInsert) 
62          throws SQLException {
63          userInsert.setString(1, user.getUserName());
64      }
65  
66      /***
67       * Set parameters of a PreparedStatement object with
68       * property values from a User instance.
69       * 
70       * @param user       a User instance, which should be an implementation class which
71       *                   is handled by this Repostory implementation.
72       * @param userUpdate a PreparedStatement initialised with SQL taken from the "update" SQL definition.
73       * @throws SQLException
74       *                   if an exception occurs while setting parameter values.
75       */
76      protected void setUserForUpdateStatement(User user, 
77                                               PreparedStatement userUpdate) 
78          throws SQLException {
79          throw new UnsupportedOperationException("Can't update a List User - " +
80                                                  "only has a single attribute.");
81      }
82  
83      /***
84       * @see org.apache.james.services.UsersRepository#addUser(java.lang.String, java.lang.String)
85       */
86      public boolean addUser(String username, String password)  {
87          User newbie = new DefaultUser(username, "SHA");
88          newbie.setPassword(password);
89          return addUser(newbie);
90      }
91  
92  
93  }