View Javadoc

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-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42   */
43  
44  package org.exolab.jms.persistence;
45  
46  
47  import java.sql.Connection;
48  import java.sql.PreparedStatement;
49  import java.sql.ResultSet;
50  import java.util.Vector;
51  
52  import org.exolab.jms.authentication.User;
53  
54  
55  /***
56   * This class provides persistency for Users objects
57   * in an RDBMS database
58   *
59   * @version     $Revision: 1.4.2.1 $ $Date: 2004/05/06 11:40:42 $
60   * @author      <a href="mailto:knut@lerpold.no">Knut Lerpold</a>
61   * @see         org.exolab.jms.persistence.RDBMSAdapter
62   */
63  class Users {
64  
65      /***
66       * Singleton instance of this class
67       */
68      private static Users _instance;
69  
70      /***
71       * This is used to synchronize the creation of the singleton
72       */
73      private static final Object _block = new Object();
74  
75      /***
76       * Constructor
77       *
78       * @throws PersistenceException - if constructor fails
79       */
80      private Users() throws PersistenceException {
81      }
82  
83      /***
84       * Returns the singleton instance.
85       *
86       * Note that initialise() must have been invoked first for this
87       * to return a valid instance.
88       *
89       * @return      Users       the singleton instance
90       */
91      public static Users instance() {
92          return _instance;
93      }
94  
95      /***
96       * Initialise the singleton instance
97       *
98       * @param connection - the connection to use
99       * @return Users - the singleton instance
100      * @throws PersistenceException - if initialisation fails
101      */
102     public static Users initialise(Connection connection)
103         throws PersistenceException {
104 
105         if (_instance == null) {
106             synchronized (_block) {
107                 if (_instance == null) {
108                     _instance = new Users();
109                     _instance.load(connection);
110                 }
111             }
112         }
113         return _instance;
114     }
115 
116     /***
117      * Add a new user to the database.
118      *
119      * @param connection - the connection to use.
120      * @param user - the user to add
121      * @throws PersistenceException - if the user cannot be added
122      */
123     public synchronized void add(Connection connection,
124                                  User user)
125         throws PersistenceException {
126 
127         PreparedStatement insert = null;
128         try {
129             insert = connection.prepareStatement(
130                 "insert into users values (?, ?)");
131             insert.setString(1, user.getUsername());
132             insert.setString(2, user.getPassword());
133             insert.executeUpdate();
134         } catch (Exception error) {
135             throw new PersistenceException("Users.add failed with "
136                 + error.toString());
137         } finally {
138             SQLHelper.close(insert);
139         }
140     }
141 
142     /***
143      * Update a a user in the database.
144      *
145      * @param connection - the connection to use
146      * @param user - the user
147      * @throws PersistenceException - if the request fails
148      */
149     public synchronized void update(Connection connection,
150                                     User user)
151         throws PersistenceException {
152 
153         PreparedStatement update = null;
154         try {
155             update = connection.prepareStatement(
156                 "update users set password=? where username=?");
157             update.setString(1, user.getPassword());
158             update.setString(2, user.getUsername());
159             update.executeUpdate();
160         } catch (Exception error) {
161             throw new PersistenceException("Users.add failed with "
162                 + error.toString());
163         } finally {
164             SQLHelper.close(update);
165         }
166     }
167 
168     /***
169      * Remove a user from the database.
170      *
171      * @param connection - the connection to use
172      * @param user - the user
173      * @return boolean - <tt>true</tt> if it was removed
174      * @throws PersistenceException - if the request fails
175      */
176     public synchronized boolean remove(Connection connection,
177                                        User user)
178         throws PersistenceException {
179 
180         boolean success = false;
181         PreparedStatement deleteUsers = null;
182 
183         if (user != null) {
184             try {
185                 deleteUsers = connection.prepareStatement(
186                     "delete from users where username=?");
187                 deleteUsers.setString(1, user.getUsername());
188                 deleteUsers.executeUpdate();
189             } catch (Exception error) {
190                 throw new PersistenceException("Users.remove failed "
191                     + error.toString());
192             } finally {
193                 SQLHelper.close(deleteUsers);
194             }
195         }
196 
197         return success;
198     }
199 
200     /***
201      * Get a user from DB.
202      *
203      * @param connection - the connection to use
204      * @param user - the user
205      * @return boolean - <tt>true</tt> if it was removed
206      * @throws PersistenceException - if the request fails
207      */
208     public synchronized User get(Connection connection,
209                                  User user)
210         throws PersistenceException {
211 
212         boolean success = false;
213         PreparedStatement getUser = null;
214         ResultSet set = null;
215         User result = null;
216 
217         if (user != null) {
218             try {
219                 getUser = connection.prepareStatement(
220                     "select * from users where username=?");
221                 getUser.setString(1, user.getUsername());
222                 set = getUser.executeQuery();
223                 if (set.next()) {
224                     result = new User(set.getString(1), set.getString(2));
225                 }
226             } catch (Exception error) {
227                 throw new PersistenceException("Users.remove failed "
228                     + error.toString());
229             } finally {
230                 SQLHelper.close(set);
231                 SQLHelper.close(getUser);
232             }
233         }
234 
235         return result;
236     }
237 
238     /***
239      * List of all users from DB.
240      *
241      * @param connection - the connection to use
242      * @return Vector - all users
243      * @throws PersistenceException - if the request fails
244      */
245     public synchronized Vector getAllUsers(Connection connection)
246         throws PersistenceException {
247 
248         boolean success = false;
249         PreparedStatement getUsers = null;
250         ResultSet set = null;
251         User user = null;
252         Vector result = new Vector();
253 
254         try {
255             getUsers = connection.prepareStatement(
256                 "select * from users");
257             set = getUsers.executeQuery();
258             while (set.next()) {
259                 user = new User(set.getString(1), set.getString(2));
260                 result.add(user);
261             }
262         } catch (Exception error) {
263             throw new PersistenceException("Users.remove failed "
264                 + error.toString());
265         } finally {
266             SQLHelper.close(set);
267             SQLHelper.close(getUsers);
268         }
269 
270         return result;
271     }
272 
273     /***
274      * Deallocates resources owned or referenced by the instance
275      */
276     public synchronized void close() {
277         _instance = null;
278     }
279 
280     /***
281      * Load all the users in memory. It uses the transaction service
282      * and the database service to access the appropriate resources.
283      *
284      * @param connection - the connection to use
285      * @throws PersistenceException - problems loading the users
286      */
287     private void load(Connection connection)
288         throws PersistenceException {
289 
290         PreparedStatement select = null;
291         ResultSet set = null;
292         try {
293             select = connection.prepareStatement("select * from users");
294 
295             set = select.executeQuery();
296             while (set.next()) {
297                 String name = set.getString(1);
298                 String password = set.getString(2);
299             }
300         } catch (Exception error) {
301             throw new PersistenceException("Error in Users.load "
302                 + error.toString());
303         } finally {
304             SQLHelper.close(set);
305             SQLHelper.close(select);
306         }
307     }
308 
309 }