1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2003-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42 */
43 package org.exolab.jms.persistence;
44
45 import java.sql.Connection;
46 import java.sql.PreparedStatement;
47 import java.sql.ResultSet;
48 import java.util.Vector;
49
50 import org.exolab.jms.authentication.User;
51
52
53 /***
54 * This class provides persistency for Users objects
55 * in an RDBMS database
56 *
57 * @version $Revision: 1.4 $ $Date: 2005/08/31 05:45:50 $
58 * @author <a href="mailto:knut@lerpold.no">Knut Lerpold</a>
59 */
60 class Users {
61
62 /***
63 * Construct a new <code>Users</code>.
64 */
65 public Users() {
66 }
67
68 /***
69 * Add a new user to the database.
70 *
71 * @param connection - the connection to use.
72 * @param user - the user to add
73 * @throws PersistenceException - if the user cannot be added
74 */
75 public synchronized void add(Connection connection,
76 User user)
77 throws PersistenceException {
78
79 PreparedStatement insert = null;
80 try {
81 insert = connection.prepareStatement(
82 "insert into users values (?, ?)");
83 insert.setString(1, user.getUsername());
84 insert.setString(2, user.getPassword());
85 insert.executeUpdate();
86 } catch (Exception error) {
87 throw new PersistenceException("Users.add failed with "
88 + error.toString());
89 } finally {
90 SQLHelper.close(insert);
91 }
92 }
93
94 /***
95 * Update a a user in the database.
96 *
97 * @param connection - the connection to use
98 * @param user - the user
99 * @throws PersistenceException - if the request fails
100 */
101 public synchronized void update(Connection connection,
102 User user)
103 throws PersistenceException {
104
105 PreparedStatement update = null;
106 try {
107 update = connection.prepareStatement(
108 "update users set password=? where username=?");
109 update.setString(1, user.getPassword());
110 update.setString(2, user.getUsername());
111 update.executeUpdate();
112 } catch (Exception error) {
113 throw new PersistenceException("Users.add failed with "
114 + error.toString());
115 } finally {
116 SQLHelper.close(update);
117 }
118 }
119
120 /***
121 * Remove a user from the database.
122 *
123 * @param connection - the connection to use
124 * @param user - the user
125 * @return boolean - <tt>true</tt> if it was removed
126 * @throws PersistenceException - if the request fails
127 */
128 public synchronized boolean remove(Connection connection,
129 User user)
130 throws PersistenceException {
131
132 boolean success = false;
133 PreparedStatement deleteUsers = null;
134
135 if (user != null) {
136 try {
137 deleteUsers = connection.prepareStatement(
138 "delete from users where username=?");
139 deleteUsers.setString(1, user.getUsername());
140 deleteUsers.executeUpdate();
141 } catch (Exception error) {
142 throw new PersistenceException("Users.remove failed "
143 + error.toString());
144 } finally {
145 SQLHelper.close(deleteUsers);
146 }
147 }
148
149 return success;
150 }
151
152 /***
153 * Get a user from DB.
154 *
155 * @param connection - the connection to use
156 * @param user - the user
157 * @return boolean - <tt>true</tt> if it was removed
158 * @throws PersistenceException - if the request fails
159 */
160 public synchronized User get(Connection connection,
161 User user)
162 throws PersistenceException {
163
164 PreparedStatement getUser = null;
165 ResultSet set = null;
166 User result = null;
167
168 if (user != null) {
169 try {
170 getUser = connection.prepareStatement(
171 "select * from users where username=?");
172 getUser.setString(1, user.getUsername());
173 set = getUser.executeQuery();
174 if (set.next()) {
175 result = new User(set.getString(1), set.getString(2));
176 }
177 } catch (Exception error) {
178 throw new PersistenceException("Users.get failed "
179 + error.toString());
180 } finally {
181 SQLHelper.close(set);
182 SQLHelper.close(getUser);
183 }
184 }
185
186 return result;
187 }
188
189 /***
190 * List of all users from DB.
191 *
192 * @param connection - the connection to use
193 * @return Vector - all users
194 * @throws PersistenceException - if the request fails
195 */
196 public synchronized Vector getAllUsers(Connection connection)
197 throws PersistenceException {
198
199 PreparedStatement getUsers = null;
200 ResultSet set = null;
201 User user = null;
202 Vector result = new Vector();
203
204 try {
205 getUsers = connection.prepareStatement(
206 "select * from users");
207 set = getUsers.executeQuery();
208 while (set.next()) {
209 user = new User(set.getString(1), set.getString(2));
210 result.add(user);
211 }
212 } catch (Exception error) {
213 throw new PersistenceException("Users.getAllUsers failed ",
214 error);
215 } finally {
216 SQLHelper.close(set);
217 SQLHelper.close(getUsers);
218 }
219
220 return result;
221 }
222
223 }