KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > persistence > Users


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
46 import java.sql.Connection JavaDoc;
47 import java.sql.PreparedStatement JavaDoc;
48 import java.sql.ResultSet JavaDoc;
49 import java.util.Vector JavaDoc;
50
51 import org.exolab.jms.authentication.User;
52
53
54 /**
55  * This class provides persistency for Users objects
56  * in an RDBMS database
57  *
58  * @version $Revision: 1.2 $ $Date: 2005/03/18 04:04:10 $
59  * @author <a HREF="mailto:knut@lerpold.no">Knut Lerpold</a>
60  * @see org.exolab.jms.persistence.RDBMSAdapter
61  */

62 class Users {
63
64     /**
65      * Singleton instance of this class
66      */

67     private static Users _instance;
68
69     /**
70      * This is used to synchronize the creation of the singleton
71      */

72     private static final Object JavaDoc _block = new Object JavaDoc();
73
74     /**
75      * Constructor
76      */

77     private Users() {
78     }
79
80     /**
81      * Returns the singleton instance.
82      *
83      * Note that initialise() must have been invoked first for this
84      * to return a valid instance.
85      *
86      * @return Users the singleton instance
87      */

88     public static Users instance() {
89         return _instance;
90     }
91
92     /**
93      * Initialise the singleton instance
94      *
95      * @return the singleton instance
96      */

97     public static Users initialise() {
98         if (_instance == null) {
99             synchronized (_block) {
100                 if (_instance == null) {
101                     _instance = new Users();
102                 }
103             }
104         }
105         return _instance;
106     }
107
108     /**
109      * Add a new user to the database.
110      *
111      * @param connection - the connection to use.
112      * @param user - the user to add
113      * @throws PersistenceException - if the user cannot be added
114      */

115     public synchronized void add(Connection JavaDoc connection,
116                                  User user)
117         throws PersistenceException {
118
119         PreparedStatement JavaDoc insert = null;
120         try {
121             insert = connection.prepareStatement(
122                 "insert into users values (?, ?)");
123             insert.setString(1, user.getUsername());
124             insert.setString(2, user.getPassword());
125             insert.executeUpdate();
126         } catch (Exception JavaDoc error) {
127             throw new PersistenceException("Users.add failed with "
128                 + error.toString());
129         } finally {
130             SQLHelper.close(insert);
131         }
132     }
133
134     /**
135      * Update a a user in the database.
136      *
137      * @param connection - the connection to use
138      * @param user - the user
139      * @throws PersistenceException - if the request fails
140      */

141     public synchronized void update(Connection JavaDoc connection,
142                                     User user)
143         throws PersistenceException {
144
145         PreparedStatement JavaDoc update = null;
146         try {
147             update = connection.prepareStatement(
148                 "update users set password=? where username=?");
149             update.setString(1, user.getPassword());
150             update.setString(2, user.getUsername());
151             update.executeUpdate();
152         } catch (Exception JavaDoc error) {
153             throw new PersistenceException("Users.add failed with "
154                 + error.toString());
155         } finally {
156             SQLHelper.close(update);
157         }
158     }
159
160     /**
161      * Remove a user from the database.
162      *
163      * @param connection - the connection to use
164      * @param user - the user
165      * @return boolean - <tt>true</tt> if it was removed
166      * @throws PersistenceException - if the request fails
167      */

168     public synchronized boolean remove(Connection JavaDoc connection,
169                                        User user)
170         throws PersistenceException {
171
172         boolean success = false;
173         PreparedStatement JavaDoc deleteUsers = null;
174
175         if (user != null) {
176             try {
177                 deleteUsers = connection.prepareStatement(
178                     "delete from users where username=?");
179                 deleteUsers.setString(1, user.getUsername());
180                 deleteUsers.executeUpdate();
181             } catch (Exception JavaDoc error) {
182                 throw new PersistenceException("Users.remove failed "
183                     + error.toString());
184             } finally {
185                 SQLHelper.close(deleteUsers);
186             }
187         }
188
189         return success;
190     }
191
192     /**
193      * Get a user from DB.
194      *
195      * @param connection - the connection to use
196      * @param user - the user
197      * @return boolean - <tt>true</tt> if it was removed
198      * @throws PersistenceException - if the request fails
199      */

200     public synchronized User get(Connection JavaDoc connection,
201                                  User user)
202         throws PersistenceException {
203
204         PreparedStatement JavaDoc getUser = null;
205         ResultSet JavaDoc set = null;
206         User result = null;
207
208         if (user != null) {
209             try {
210                 getUser = connection.prepareStatement(
211                     "select * from users where username=?");
212                 getUser.setString(1, user.getUsername());
213                 set = getUser.executeQuery();
214                 if (set.next()) {
215                     result = new User(set.getString(1), set.getString(2));
216                 }
217             } catch (Exception JavaDoc error) {
218                 throw new PersistenceException("Users.get failed "
219                     + error.toString());
220             } finally {
221                 SQLHelper.close(set);
222                 SQLHelper.close(getUser);
223             }
224         }
225
226         return result;
227     }
228
229     /**
230      * List of all users from DB.
231      *
232      * @param connection - the connection to use
233      * @return Vector - all users
234      * @throws PersistenceException - if the request fails
235      */

236     public synchronized Vector JavaDoc getAllUsers(Connection JavaDoc connection)
237         throws PersistenceException {
238
239         PreparedStatement JavaDoc getUsers = null;
240         ResultSet JavaDoc set = null;
241         User user = null;
242         Vector JavaDoc result = new Vector JavaDoc();
243
244         try {
245             getUsers = connection.prepareStatement(
246                 "select * from users");
247             set = getUsers.executeQuery();
248             while (set.next()) {
249                 user = new User(set.getString(1), set.getString(2));
250                 result.add(user);
251             }
252         } catch (Exception JavaDoc error) {
253             throw new PersistenceException("Users.remove failed "
254                 + error.toString());
255         } finally {
256             SQLHelper.close(set);
257             SQLHelper.close(getUsers);
258         }
259
260         return result;
261     }
262
263     /**
264      * Deallocates resources owned or referenced by the instance
265      */

266     public synchronized void close() {
267         _instance = null;
268     }
269
270 }
271
Popular Tags