1 45 package org.exolab.jms.authentication; 46 47 import java.sql.Connection ; 48 import java.util.Enumeration ; 49 import java.util.HashMap ; 50 import java.util.Iterator ; 51 52 import javax.transaction.TransactionManager ; 53 54 import org.apache.commons.logging.Log; 55 import org.apache.commons.logging.LogFactory; 56 57 import org.exolab.jms.service.ServiceException; 58 import org.exolab.jms.config.Configuration; 59 import org.exolab.jms.config.ConfigurationManager; 60 import org.exolab.jms.config.SecurityConfiguration; 61 import org.exolab.jms.persistence.DatabaseService; 62 import org.exolab.jms.persistence.PersistenceAdapter; 63 import org.exolab.jms.persistence.SQLHelper; 64 65 66 72 public class UserManager { 73 74 78 private HashMap _userCache = new HashMap (); 79 80 83 private static final Log _log = LogFactory.getLog(UserManager.class); 84 85 86 91 protected UserManager() throws ServiceException { 92 init(); 93 } 94 95 102 public synchronized boolean createUser(User user) { 103 boolean success = false; 104 PersistenceAdapter adapter = DatabaseService.getAdapter(); 105 106 if (_userCache.get(user.getUsername()) == null) { 107 Connection connection = null; 108 try { 109 connection = DatabaseService.getConnection(); 110 adapter.addUser(connection, user); 111 addToUserCache(user); 112 connection.commit(); 113 success = true; 114 } catch (Exception exception) { 115 _log.error("Failed to create user", exception); 116 SQLHelper.rollback(connection); 117 } finally { 118 SQLHelper.close(connection); 119 } 120 } 121 122 return success; 123 } 124 125 133 public synchronized boolean updateUser(User user) { 134 boolean success = false; 135 PersistenceAdapter adapter = DatabaseService.getAdapter(); 136 137 if (_userCache.get(user.getUsername()) != null) { 138 Connection connection = null; 139 try { 140 connection = DatabaseService.getConnection(); 141 adapter.updateUser(connection, user); 142 connection.commit(); 143 addToUserCache(user); 144 success = true; 145 } catch (Exception exception) { 146 _log.error("Failed to update user", exception); 147 SQLHelper.rollback(connection); 148 } finally { 149 SQLHelper.close(connection); 150 } 151 } 152 153 return success; 154 } 155 156 163 public synchronized boolean deleteUser(User user) { 164 boolean success = false; 165 PersistenceAdapter adapter = DatabaseService.getAdapter(); 166 167 if (_userCache.get(user.getUsername()) != null) { 168 Connection connection = null; 169 try { 170 connection = DatabaseService.getConnection(); 171 adapter.removeUser(connection, user); 172 removeFromUserCache(user); 173 success = true; 174 connection.commit(); 175 } catch (Exception exception) { 176 _log.error("Failed to remove user", exception); 177 SQLHelper.rollback(connection); 178 } finally { 179 SQLHelper.close(connection); 180 } 181 } 182 return success; 183 } 184 185 191 public synchronized User getUser(User user) { 192 return (User) _userCache.get(user.getUsername()); 193 } 194 195 201 public Iterator userNames() { 202 return _userCache.keySet().iterator(); 203 } 204 205 208 public synchronized void destroy() { 209 _userCache.clear(); 210 _userCache = null; 211 } 212 213 221 public synchronized boolean validateUser(String username, 222 String password) { 223 boolean result = false; 224 225 SecurityConfiguration config = 226 ConfigurationManager.getConfig().getSecurityConfiguration(); 227 if (!config.getSecurityEnabled()) { 228 result = true; 230 } 231 232 User user = (User) _userCache.get(username); 233 if (user != null && user.getPassword().equals(password)) { 234 result = true; 235 } 236 237 return result; 238 } 239 240 245 protected void init() throws ServiceException { 246 Connection connection = null; 247 TransactionManager tm = null; 248 try { 249 connection = DatabaseService.getConnection(); 250 251 Enumeration iter = 252 DatabaseService.getAdapter().getAllUsers(connection); 253 connection.commit(); 254 255 while (iter.hasMoreElements()) { 256 User user = (User) iter.nextElement(); 258 addToUserCache(user); 259 } 260 } catch (Exception exception) { 261 SQLHelper.rollback(connection); 262 _log.error("Failed to initialise UserManager", exception); 263 throw new ServiceException(exception); 264 } finally { 265 SQLHelper.close(connection); 266 } 267 268 registerConfiguredUsers(); 269 } 270 271 277 protected void addToUserCache(User user) { 278 if (!_userCache.containsKey(user.getUsername())) { 279 _userCache.put(user.getUsername(), user); 280 } 281 } 282 283 288 protected void removeFromUserCache(User user) { 289 _userCache.remove(user.getUsername()); 290 } 291 292 295 protected void registerConfiguredUsers() { 296 Configuration config = ConfigurationManager.getConfig(); 297 if (config.getUsers() != null) { 298 org.exolab.jms.config.User[] users = config.getUsers().getUser(); 299 for (int i = 0; i < users.length; ++i) { 300 User user = new User(users[i].getName(), 301 users[i].getPassword()); 302 createUser(user); 303 } 304 } 305 } 306 307 } 308 | Popular Tags |