1 43 package org.exolab.jms.persistence; 44 45 46 import java.sql.Connection ; 47 import java.sql.PreparedStatement ; 48 import java.sql.ResultSet ; 49 import java.util.Vector ; 50 51 import org.exolab.jms.authentication.User; 52 53 54 62 class Users { 63 64 67 private static Users _instance; 68 69 72 private static final Object _block = new Object (); 73 74 77 private Users() { 78 } 79 80 88 public static Users instance() { 89 return _instance; 90 } 91 92 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 115 public synchronized void add(Connection connection, 116 User user) 117 throws PersistenceException { 118 119 PreparedStatement 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 error) { 127 throw new PersistenceException("Users.add failed with " 128 + error.toString()); 129 } finally { 130 SQLHelper.close(insert); 131 } 132 } 133 134 141 public synchronized void update(Connection connection, 142 User user) 143 throws PersistenceException { 144 145 PreparedStatement 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 error) { 153 throw new PersistenceException("Users.add failed with " 154 + error.toString()); 155 } finally { 156 SQLHelper.close(update); 157 } 158 } 159 160 168 public synchronized boolean remove(Connection connection, 169 User user) 170 throws PersistenceException { 171 172 boolean success = false; 173 PreparedStatement 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 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 200 public synchronized User get(Connection connection, 201 User user) 202 throws PersistenceException { 203 204 PreparedStatement getUser = null; 205 ResultSet 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 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 236 public synchronized Vector getAllUsers(Connection connection) 237 throws PersistenceException { 238 239 PreparedStatement getUsers = null; 240 ResultSet set = null; 241 User user = null; 242 Vector result = new Vector (); 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 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 266 public synchronized void close() { 267 _instance = null; 268 } 269 270 } 271 | Popular Tags |