- Timestamp:
- 01/31/11 11:36:42 (13 years ago)
- Location:
- trunk/src/com/bowman/cardserv
- Files:
-
- 1 added
- 3 deleted
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/bowman/cardserv/MySQLUserManager.java
r65 r72 8 8 import java.util.ArrayList; 9 9 import java.util.Collections; 10 import java.util.Iterator; 10 11 import java.util.Set; 11 12 … … 237 238 */ 238 239 public void addUserToDB(User user) { 239 cacheManager.addUserToDB(user); 240 String profiles = ""; 241 Iterator iterator = user.getAllowedProfiles().iterator(); 242 while(iterator.hasNext()){ 243 profiles += (String) iterator.next() + " "; 244 } 245 addUserToDB(user.getUsername(), user.getPassword(), user.getDisplayName(), user.getIpMask(), 246 user.getMaxConnections(), user.isEnabled(), user.isDebug(), user.isAdmin(), 247 user.getEmail(), user.isMapExcluded(), profiles); 240 248 } 241 249 -
trunk/src/com/bowman/cardserv/mysql/CacheManager.java
r65 r72 8 8 import java.util.HashSet; 9 9 import java.util.Iterator; 10 import java.util. Set;10 import java.util.Map; 11 11 import java.util.StringTokenizer; 12 12 … … 27 27 public class CacheManager extends Thread { 28 28 29 private HashMap userCache = new HashMap();30 private HashMap profileCache = new HashMap();31 29 private Map userCache = Collections.synchronizedMap(new HashMap()); 30 private Map profileCache = Collections.synchronizedMap(new HashMap()); 31 32 32 private final long cleaningInterval = 5000; 33 33 private ProxyLogger logger = null; 34 34 private ConnectionPoolManager connectionPoolManager = null; 35 36 35 36 37 37 public CacheManager(String databaseHost, int databasePort, String databaseName, 38 38 String databaseUser, String databasePassword) throws ConfigException { 39 39 this.logger = ProxyLogger.getLabeledLogger(getClass().getName()); 40 40 this.connectionPoolManager = new ConnectionPoolManager( 41 41 databaseHost, databasePort, databaseName, databaseUser, databasePassword); … … 46 46 createUserTable(); 47 47 } 48 48 49 49 if (!existsProfileTable()) { 50 50 logger.info("creating new profile table."); 51 51 createProfileTable(); 52 52 } 53 53 54 54 if (!existsUsersHasProfilesTable()) { 55 55 logger.info("creating new users_has_profiles table."); 56 56 createUsersHasProfilesTable(); 57 57 } 58 58 59 59 if (existsUserTable() && isUserTableEmpty()) { 60 logger. warning("MySQL database is empty. Adding default user 'admin' with the password 'secret'.");60 logger.info("MySQL database is empty. Adding default user 'admin' with the password 'secret'."); 61 61 addDefaultUserToDB(); 62 62 } 63 63 64 64 this.setName("CacheManagerThread"); 65 65 this.setPriority(MIN_PRIORITY); 66 66 this.start(); 67 67 } 68 69 68 70 69 /* ############################################################################################ */ 71 70 /* database prerequisites */ 72 71 /* ############################################################################################ */ 73 72 74 73 /** 75 74 * create the "user" table in the mysql database. 76 75 */ 77 76 private void createUserTable() { 78 PoolConnection poolConnection = null; 79 80 try { 81 poolConnection = connectionPoolManager.getPoolConnection(); 82 poolConnection.createUserTable(); 83 } catch (SQLException e) { 84 logger.warning("(createUserTable) Failed to create the user table : " + e); 85 } finally { 86 connectionPoolManager.returnPoolConnection(poolConnection); 87 } 88 } 89 77 createTable(0); 78 } 79 90 80 /** 91 81 * create the "profile" table in the mysql database. 92 82 */ 93 83 private void createProfileTable() { 94 PoolConnection poolConnection = null; 95 96 try { 97 poolConnection = connectionPoolManager.getPoolConnection(); 98 poolConnection.createProfileTable(); 99 } catch (SQLException e) { 100 logger.warning("(createProfileTable) Failed to create the profile table : " + e); 101 } finally { 102 connectionPoolManager.returnPoolConnection(poolConnection); 103 } 104 } 105 84 createTable(1); 85 } 86 106 87 /** 107 88 * create the "users_has_profiles" table in the mysql datbase. … … 110 91 // the user and profile table must exist before this one can be created. 111 92 if (existsUserTable() && existsProfileTable()) { 112 PoolConnection poolConnection = null; 113 114 try { 115 poolConnection = connectionPoolManager.getPoolConnection(); 93 createTable(2); 94 } 95 } 96 97 /** 98 * create table 99 * @param table - 0 = users; 1 = profiles; 2 = users_has_profiles 100 */ 101 private void createTable(int table) { 102 PoolConnection poolConnection = null; 103 104 try { 105 poolConnection = connectionPoolManager.getPoolConnection(); 106 switch (table) { 107 case 0 : 108 poolConnection.createUserTable(); 109 break; 110 case 1 : 111 poolConnection.createProfileTable(); 112 break; 113 case 2 : 116 114 poolConnection.createUsersHasProfilesTable(); 117 } catch (SQLException e) { 118 logger.warning("(createUsersHasProfilesTable) Failed to create the users_has_profiles table : " + e); 119 } finally { 120 connectionPoolManager.returnPoolConnection(poolConnection); 121 } 122 } else { 123 // TODO add exception 124 } 125 } 126 115 break; 116 } 117 } catch (SQLException e) { 118 logger.warning("(createTable) Failed to create the table : " + e); 119 } finally { 120 connectionPoolManager.returnPoolConnection(poolConnection); 121 } 122 } 123 127 124 /** 128 125 * tests whether the "user" table exists in the mysql database. … … 130 127 */ 131 128 private boolean existsUserTable() { 132 boolean result = false; 133 PoolConnection poolConnection = null; 134 135 try { 136 poolConnection = connectionPoolManager.getPoolConnection(); 137 result = poolConnection.existsUserTable(); 138 } catch (SQLException e) { 139 logger.warning("(existsUserTable) Failed to query the existance of user table : " + e); 140 } finally { 141 connectionPoolManager.returnPoolConnection(poolConnection); 142 } 143 return result; 144 } 145 129 return existsTable(0); 130 } 131 146 132 /** 147 133 * tests whether the "profile" table exists in the mysql database. … … 149 135 */ 150 136 private boolean existsProfileTable() { 151 boolean result = false; 152 PoolConnection poolConnection = null; 153 154 try { 155 poolConnection = connectionPoolManager.getPoolConnection(); 156 result = poolConnection.existsProfileTable(); 157 } catch (SQLException e) { 158 logger.warning("(existsProfileTable) Failed to query the existance of profile table : " + e); 159 } finally { 160 connectionPoolManager.returnPoolConnection(poolConnection); 161 } 162 return result; 163 } 164 137 return existsTable(1); 138 } 139 165 140 /** 166 141 * tests whether the "users_has_profiles" table exists in the mysql database. … … 168 143 */ 169 144 private boolean existsUsersHasProfilesTable() { 145 return existsTable(2); 146 } 147 148 /** 149 * tests whether a table exists. 150 * @param table - 0 = users; 1 = profiles; 2 = users_has_profiles 151 * @return TRUE, when table exists. 152 */ 153 private boolean existsTable(int table) { 170 154 boolean result = false; 171 155 PoolConnection poolConnection = null; 172 173 try { 174 poolConnection = connectionPoolManager.getPoolConnection(); 175 result = poolConnection.existsUsersHasProfilesTable(); 176 } catch (SQLException e) { 177 logger.warning("(existsUsersHasProfilesTable) Failed to query the existance of users_has_profiles table : " + e); 156 157 try { 158 poolConnection = connectionPoolManager.getPoolConnection(); 159 switch (table) { 160 case 0 : 161 result = poolConnection.existsUserTable(); 162 break; 163 case 1 : 164 result = poolConnection.existsProfileTable(); 165 break; 166 case 2 : 167 result = poolConnection.existsUsersHasProfilesTable(); 168 break; 169 } 170 } catch (SQLException e) { 171 logger.warning("(existsTable) Failed to query the existance of table : " + e); 178 172 } finally { 179 173 connectionPoolManager.returnPoolConnection(poolConnection); … … 181 175 return result; 182 176 } 183 177 184 178 /** 185 179 * tests whether the "user" table is empty. … … 189 183 boolean result = false; 190 184 PoolConnection poolConnection = null; 191 185 192 186 try { 193 187 poolConnection = connectionPoolManager.getPoolConnection(); … … 200 194 return result; 201 195 } 202 203 /* ############################################################################################ */ 204 /* some universal database operations */ 205 /* ############################################################################################ */ 206 207 /** 208 * universal function to send query to database. 209 * @param pst - describes which PreparedStatement should be used for they query. 210 * @param obj - result object 211 * @param column - specifies which data is going to be returned 212 * @param info - some additional info needed for the query. e.g. username 213 * @return obj with the wanted database info 214 */ 215 private Object dbQueryHelper(PreparedStatementType pst, Object obj, DBColumnType column, String info) { 216 Object result = new String(); 217 ResultSet resultSet = null; 218 PoolConnection poolConnection = null; 219 220 try { 221 poolConnection = connectionPoolManager.getPoolConnection(); 222 if (pst == PreparedStatementType.GET_USERNAMES) { 223 resultSet = poolConnection.getUserNames(); 224 } else if (pst == PreparedStatementType.GET_PROFILENAMES) { 225 resultSet = poolConnection.getProfileNames(); 226 } 227 if (resultSet.next() && resultSet.getObject(column.toString()) != null) { 228 result = resultSet.getObject(column.toString()).equals(new String()) ? obj : resultSet.getObject(column.toString()); 229 } 230 } catch (SQLException e) { 231 logger.warning("(dbQueryHelper) column: " + column + " info: " + info + " Failed to query the database: " + e); 232 } finally { 233 closeResultSet(resultSet); 234 connectionPoolManager.returnPoolConnection(poolConnection); 235 } 236 return result; 237 } 238 239 /** 240 * send changes to database. 241 * @param pst - describes the type which prepared Statement is used to query the database 242 * @param user - describes which user gets updated 243 * @param value - new value for the described user 244 */ 245 private void updateRowInDB(PreparedStatementType pst, String user, Object value) { 246 PoolConnection poolConnection = null; 247 248 try { 249 poolConnection = connectionPoolManager.getPoolConnection(); 250 if (pst == PreparedStatementType.SET_USER_DEBUG) { 251 poolConnection.setUserDebug(user, (Boolean)value); 252 } 253 } catch (SQLException e) { 254 logger.warning("(updateRowInDB) Failed to update row in database for " + user + " : " + e); 255 } finally { 256 connectionPoolManager.returnPoolConnection(poolConnection); 257 } 258 } 259 196 260 197 /** 261 198 * close the specified ResultSet to free the ressources. … … 272 209 } 273 210 } 274 211 212 /** 213 * converts a String separated with whitespaces to an sorted ArrayList 214 * @param resultSet 215 * @param column 216 * @return 217 * @throws SQLException 218 */ 219 private ArrayList stringToArrayList(ResultSet resultSet, String column) throws SQLException { 220 ArrayList al = new ArrayList(); 221 if (resultSet.next() && resultSet.getString(column) != null) { 222 for(StringTokenizer st = new StringTokenizer(resultSet.getString(column)); st.hasMoreTokens(); ) { 223 al.add(st.nextToken()); 224 } 225 } 226 Collections.sort(al); 227 return al; 228 } 229 275 230 /* ############################################################################################ */ 276 231 /* users */ 277 232 /* ############################################################################################ */ 278 233 279 234 /** 280 235 * if the user information are not already in the cache, it must be fetched from the database. … … 291 246 PoolConnection poolConnection = null; 292 247 User result = null; 293 248 294 249 try { 295 250 poolConnection = connectionPoolManager.getPoolConnection(); … … 297 252 if (resultSet.next()) { 298 253 logger.fine("(getUserFromDB) getting user: " + username + " from database."); 299 // create new user object with informations from database300 254 result = new User( 301 255 resultSet.getInt(DBColumnType.ID.toString()), … … 313 267 closeResultSet(resultSet); 314 268 resultSet = poolConnection.getUserProfiles(result.getId()); 315 Set allowedProfiles = new HashSet(); 316 if (resultSet.next() && resultSet.getObject("GROUP_CONCAT(p.profilename SEPARATOR ' ')") != null) { 317 for(StringTokenizer st = new StringTokenizer(resultSet.getString("GROUP_CONCAT(p.profilename SEPARATOR ' ')")); st.hasMoreTokens(); ){ 318 allowedProfiles.add(st.nextToken()); 319 } 320 } 321 result.setAllowedProfiles(allowedProfiles); 322 323 if (addToCache) 324 putUserIntoCache(result); 269 result.setAllowedProfiles(new HashSet(stringToArrayList(resultSet, "GROUP_CONCAT(p.profilename SEPARATOR ' ')"))); 270 if (addToCache) putUserIntoCache(result); 325 271 } 326 272 } catch (SQLException e) { … … 328 274 } finally { 329 275 closeResultSet(resultSet); 330 276 connectionPoolManager.returnPoolConnection(poolConnection); 331 277 } 332 278 return result; 333 279 } 334 280 335 281 /** 336 282 * add a default user with admin rights to the database. … … 339 285 addUserToDB("admin", "secret", "admin", "*", 1, true, false, true, "", false, ""); 340 286 } 341 287 342 288 /** 343 289 * add an new user to the MySQL database. … … 347 293 String mail, boolean mapexcluded, String allowedProfiles) { 348 294 PoolConnection poolConnection = null; 349 350 try { 351 poolConnection = connectionPoolManager.getPoolConnection(); 352 poolConnection.addUser(username, password, displayname, ipmask, maxconnections,295 296 try { 297 poolConnection = connectionPoolManager.getPoolConnection(); 298 int userID = poolConnection.addUser(username, password, displayname, ipmask, maxconnections, 353 299 enabled, debug, admin, mail, mapexcluded); 354 // get the newly created user 355 int userID = getUserFromDB(username, false).getId(); 300 356 301 for(StringTokenizer st = new StringTokenizer(allowedProfiles); st.hasMoreTokens(); ){ 357 302 String currentProfile = st.nextToken(); 358 // when profile does not exist, add it, mainly used for the xml import 359 if (getProfile(currentProfile) == null) { 360 poolConnection.addProfile(currentProfile); 361 } 362 if (getProfile(currentProfile) != null) { 363 int profileID = getProfile(currentProfile).getId(); 364 // add to users_has_profiles table 365 poolConnection.addUserProfile(userID, profileID); 366 } 367 } 368 303 if (getProfile(currentProfile) == null) poolConnection.addProfile(currentProfile); 304 poolConnection.addUserProfile(userID, getProfile(currentProfile).getId()); 305 } 369 306 } catch (SQLException e) { 370 307 logger.warning("(addUserToDB) Failed to add user '" + username + "' to database. " + e); … … 372 309 connectionPoolManager.returnPoolConnection(poolConnection); 373 310 } 374 } 375 376 /** 377 * add an new user to the MySQL database. 378 * @param user - User object 379 */ 380 public void addUserToDB(User user) { 381 PoolConnection poolConnection = null; 382 383 try { 384 poolConnection = connectionPoolManager.getPoolConnection(); 385 poolConnection.addUser(user.getUsername(), user.getPassword(), user.getDisplayName(), user.getIpMask(), 386 user.getMaxConnections(), user.isEnabled(), user.isDebug(), user.isAdmin(), user.getEmail(), user.isMapExcluded()); 387 // get the newly created user 388 int userID = getUserFromDB(user.getUsername(), false).getId(); 389 Iterator iterator = user.getAllowedProfiles().iterator(); 390 while(iterator.hasNext()){ 391 String currentProfile = (String) iterator.next(); 392 // when profile does not exist, add it, mainly used for the xml import 393 if (getProfile(currentProfile) == null) { 394 poolConnection.addProfile(currentProfile); 395 } 396 if (getProfile(currentProfile) != null) { 397 int profileID = getProfile(currentProfile).getId(); 398 // add to users_has_profiles table 399 poolConnection.addUserProfile(userID, profileID); 400 } 401 } 402 403 } catch (SQLException e) { 404 logger.warning("(addUserToDB) Failed to add user '" + user.getUsername() + "' to database. " + e); 405 } finally { 406 connectionPoolManager.returnPoolConnection(poolConnection); 407 } 408 } 409 311 } 312 410 313 /** 411 314 * edit an existing MySQL database user. … … 415 318 String mail, boolean mapexcluded, String allowedProfiles) { 416 319 PoolConnection poolConnection = null; 417 320 418 321 try { 419 322 poolConnection = connectionPoolManager.getPoolConnection(); … … 421 324 enabled, debug, admin, mail, mapexcluded); 422 325 423 int userID = getUserFromDB(username, false).getId(); 424 poolConnection.deleteAllUserProfiles(userID); 326 poolConnection.deleteAllUserProfiles(getUser(username).getId()); 425 327 for(StringTokenizer st = new StringTokenizer(allowedProfiles); st.hasMoreTokens(); ){ 426 String currentProfile = st.nextToken(); 427 int profileID = getProfile(currentProfile).getId(); 428 // add to users_has_profiles table 429 poolConnection.addUserProfile(userID, profileID); 430 } 431 synchronized(userCache) { 432 if (userCache.containsKey(username)) 433 userCache.remove(username); 434 } 328 poolConnection.addUserProfile(getUser(username).getId(), getProfile(st.nextToken()).getId()); 329 } 330 delUserFromCache(username); 435 331 } catch (SQLException e) { 436 332 logger.warning("(editUserInDB) Failed to edit user '" + username + "'. " + e); … … 438 334 connectionPoolManager.returnPoolConnection(poolConnection); 439 335 } 440 } 441 336 } 337 442 338 /** 443 339 * delete specified user from the database and the local cache. … … 450 346 poolConnection = connectionPoolManager.getPoolConnection(); 451 347 poolConnection.deleteUser(username); 452 synchronized(userCache) { 453 if (userCache.containsKey(username)) 454 userCache.remove(username); 455 } 348 delUserFromCache(username); 456 349 } catch (SQLException e) { 457 350 logger.warning("(deleteUserFromDB) Failed to delete user '" + username + "' from database. " + e); … … 460 353 } 461 354 } 462 355 463 356 /** 464 357 * delete all users from the database. … … 466 359 */ 467 360 public void deleteAllUsersFromDB(String username) { 468 ArrayList userNames = getUserNames(); 469 Iterator iterator = userNames.iterator(); 470 while (iterator.hasNext()) { 361 Iterator iterator = getUserNames().iterator(); 362 while (getUserNames().iterator().hasNext()) { 471 363 String currentUser = (String)iterator.next(); 472 if (!currentUser.equals(username)) { 473 deleteUserFromDB(currentUser); 474 } 475 } 476 } 477 364 if (!currentUser.equals(username)) deleteUserFromDB(currentUser); 365 } 366 } 367 478 368 /** 479 369 * returns all usernames which are stored in the database as an sorted ArrayList. … … 481 371 */ 482 372 public ArrayList getUserNames() { 483 ArrayList userNames = new ArrayList(); 484 // get all database user 485 for(StringTokenizer st = new StringTokenizer((String) dbQueryHelper(PreparedStatementType.GET_USERNAMES, 486 new String(), DBColumnType.USERS, null)); st.hasMoreTokens(); ) { 487 userNames.add(st.nextToken()); 488 } 489 Collections.sort(userNames); 373 ArrayList userNames = new ArrayList(); 374 ResultSet resultSet = null; 375 PoolConnection poolConnection = null; 376 377 try { 378 poolConnection = connectionPoolManager.getPoolConnection(); 379 resultSet = poolConnection.getUserNames(); 380 userNames = stringToArrayList(resultSet, DBColumnType.USERS.toString()); 381 } catch (SQLException e) { 382 logger.warning("(getUserNames) Failed to query the database: " + e); 383 } finally { 384 closeResultSet(resultSet); 385 connectionPoolManager.returnPoolConnection(poolConnection); 386 } 387 490 388 return userNames; 491 389 } … … 497 395 */ 498 396 public void setUserDebug(String username, boolean debug) { 499 updateRowInDB(PreparedStatementType.SET_USER_DEBUG , username, new Boolean(debug)); 500 } 501 397 PoolConnection poolConnection = null; 398 399 try { 400 poolConnection = connectionPoolManager.getPoolConnection(); 401 poolConnection.setUserDebug(username, debug); 402 } catch (SQLException e) { 403 logger.warning("(setUserDebug) Failed to set debug for user " + username + " : " + e); 404 } finally { 405 connectionPoolManager.returnPoolConnection(poolConnection); 406 } 407 } 408 502 409 /* ############################################################################################ */ 503 410 /* database informations */ 504 411 /* ############################################################################################ */ 505 412 506 413 /** 507 414 * the hostname or ip address the MySQL server is reached. … … 511 418 return connectionPoolManager.getDataSource().getServerName(); 512 419 } 513 420 514 421 /** 515 422 * the database name which is used for the tables. … … 519 426 return connectionPoolManager.getDataSource().getDatabaseName(); 520 427 } 521 428 522 429 /** 523 430 * the port the MySQL database listens on. … … 527 434 return connectionPoolManager.getDataSource().getPort(); 528 435 } 529 436 530 437 /** 531 438 * the username used to connect to the database. … … 535 442 return connectionPoolManager.getDataSource().getUser(); 536 443 } 537 444 538 445 /* ############################################################################################ */ 539 446 /* profiles */ 540 447 /* ############################################################################################ */ 541 448 542 449 /** 543 450 * if the profile information are not already in the cache, it must be fetched from the database. … … 554 461 PoolConnection poolConnection = null; 555 462 Profile result = null; 556 463 557 464 try { 558 465 poolConnection = connectionPoolManager.getPoolConnection(); … … 560 467 if (resultSet.next()) { 561 468 logger.fine("(getProfileFromDB) getting profile: " + profileName + " from database."); 562 // create new profile object with informations from database563 469 result = new Profile( 564 470 resultSet.getInt(DBColumnType.ID.toString()), 565 471 resultSet.getString(DBColumnType.PROFILENAME.toString()) 566 472 ); 567 if (addToCache) 568 putProfileIntoCache(result); 473 if (addToCache) putProfileIntoCache(result); 569 474 } 570 475 } catch (SQLException e) { … … 572 477 } finally { 573 478 closeResultSet(resultSet); 574 479 connectionPoolManager.returnPoolConnection(poolConnection); 575 480 } 576 481 return result; 577 482 } 578 483 579 484 /** 580 485 * add a new profile to the MySQL database. … … 593 498 } 594 499 } 595 500 596 501 /** 597 502 * delete all profiles from the MySQL database. 598 503 */ 599 504 public void deleteAllProfilesFromDB() { 600 ArrayList profileNames = getProfileNames(); 601 Iterator iterator = profileNames.iterator(); 505 Iterator iterator = getProfileNames().iterator(); 602 506 while (iterator.hasNext()) { 603 507 deleteProfileFromDB((String)iterator.next()); 604 508 } 605 509 } 606 510 607 511 /** 608 512 * delete specified profile from the MySQL database and the local cache. … … 624 528 } 625 529 } 626 530 627 531 /** 628 532 * returns all profile names as a sorted ArrayList. … … 630 534 */ 631 535 public ArrayList getProfileNames() { 632 ArrayList profileNames = new ArrayList(); 633 // get all database profiles 634 for(StringTokenizer st = new StringTokenizer((String) dbQueryHelper(PreparedStatementType.GET_PROFILENAMES, 635 new String(), DBColumnType.PROFILENAMES, null)); st.hasMoreTokens(); ) { 636 profileNames.add(st.nextToken()); 637 } 638 Collections.sort(profileNames); 536 ArrayList profileNames = new ArrayList(); 537 ResultSet resultSet = null; 538 PoolConnection poolConnection = null; 539 540 try { 541 poolConnection = connectionPoolManager.getPoolConnection(); 542 resultSet = poolConnection.getProfileNames(); 543 profileNames = stringToArrayList(resultSet, DBColumnType.PROFILENAMES.toString()); 544 } catch (SQLException e) { 545 logger.warning("(getProfileNames) Failed to query the database: " + e); 546 } finally { 547 closeResultSet(resultSet); 548 connectionPoolManager.returnPoolConnection(poolConnection); 549 } 550 639 551 return profileNames; 640 552 } 641 553 642 554 /* ############################################################################################ */ 643 /* cache operations 644 /* ############################################################################################ */ 645 555 /* cache operations */ 556 /* ############################################################################################ */ 557 646 558 /** 647 559 * returns the User object specified through the identifier username out of the local cache. … … 653 565 synchronized(userCache) { 654 566 if (userCache.containsKey(username)) 655 return ( (CacheUser) userCache.get(username)).getUser();567 return (User)((CacheEntry) userCache.get(username)).getData(); 656 568 } 657 569 return getUserFromDB(username, true); 658 570 } 659 571 660 572 /** 661 573 * returns the Profile object specified through the identifier profileName out of the local cache. … … 667 579 synchronized(profileCache) { 668 580 if (profileCache.containsKey(profileName)) 669 return ( (CacheProfile) profileCache.get(profileName)).getProfile();581 return (Profile)((CacheEntry) profileCache.get(profileName)).getData(); 670 582 } 671 583 return getProfileFromDB(profileName, true); 672 584 } 673 585 674 586 /** 675 587 * add the specified profile to the cache by makeing a cache object out of it. … … 677 589 */ 678 590 private void putProfileIntoCache(Profile profile) { 679 Cache Profile cacheProfile = new CacheProfile(profile);591 CacheEntry cacheProfile = new CacheEntry(profile); 680 592 synchronized(profileCache) { 681 profileCache.put( cacheProfile.getIdentifier(), cacheProfile);593 profileCache.put(((Profile)cacheProfile.getData()).getProfileName(), cacheProfile); 682 594 } 683 595 } … … 688 600 */ 689 601 private void putUserIntoCache(User user) { 690 Cache User cacheUser = new CacheUser(user);602 CacheEntry cacheUser = new CacheEntry(user); 691 603 synchronized(userCache) { 692 userCache.put(cacheUser.getIdentifier(), cacheUser); 604 userCache.put(((User)cacheUser.getData()).getUsername(), cacheUser); 605 } 606 } 607 608 /** 609 * delete user identified through the user-name from the cache. 610 * @param userName 611 */ 612 private void delUserFromCache(String userName) { 613 synchronized(userCache) { 614 if (userCache.containsKey(userName)) 615 userCache.remove(userName); 693 616 } 694 617 } … … 703 626 Iterator iterator = userCache.keySet().iterator(); 704 627 while (iterator.hasNext()) { 705 Cache User cacheUser = (CacheUser) userCache.get(iterator.next());628 CacheEntry cacheUser = (CacheEntry) userCache.get(iterator.next()); 706 629 if (cacheUser.needsRefresh()) { 707 cacheUser.set User(getUserFromDB(cacheUser.getUser().getUsername(), false));708 logger.fine("(updateCacheEntries) refreshed user: " + cacheUser.getUser().getUsername() + " from cache!");630 cacheUser.setData(getUserFromDB(((User)cacheUser.getData()).getUsername(), false)); 631 logger.fine("(updateCacheEntries) refreshed user: " + ((User)cacheUser.getData()).getUsername() + " from cache!"); 709 632 } else if (cacheUser.isExpired()) { 710 633 iterator.remove(); 711 logger.fine("(updateCacheEntries) removed user: " + cacheUser.getUser().getUsername() + " from cache!");634 logger.fine("(updateCacheEntries) removed user: " + ((User)cacheUser.getData()).getUsername() + " from cache!"); 712 635 } 713 636 } … … 717 640 Iterator iterator = profileCache.keySet().iterator(); 718 641 while (iterator.hasNext()) { 719 Cache Profile cacheProfile = (CacheProfile) profileCache.get(iterator.next());642 CacheEntry cacheProfile = (CacheEntry) profileCache.get(iterator.next()); 720 643 if (cacheProfile.needsRefresh()) { 721 cacheProfile.set Profile(getProfileFromDB(cacheProfile.getProfile().getName(), false));722 logger.fine("(updateCacheEntries) refreshed profile: " + cacheProfile.getProfile().getName() + " from cache!");644 cacheProfile.setData(getProfileFromDB(((Profile)cacheProfile.getData()).getProfileName(), false)); 645 logger.fine("(updateCacheEntries) refreshed profile: " + ((Profile)cacheProfile.getData()).getProfileName() + " from cache!"); 723 646 } else if (cacheProfile.isExpired()) { 724 647 iterator.remove(); 725 logger.fine("(updateCacheEntries) removed profile: " + cacheProfile.getProfile().getName() + " from cache!");648 logger.fine("(updateCacheEntries) removed profile: " + ((Profile)cacheProfile.getData()).getProfileName() + " from cache!"); 726 649 } 727 650 } 728 651 } 729 652 } 730 653 731 654 /** 732 655 * removes all CacheUser and CacheProfile objects from the local caches. … … 742 665 } 743 666 } 744 667 745 668 /** 746 669 * deletes all cache entries and disposes the ConnectionPoolManager … … 751 674 connectionPoolManager.interrupt(); 752 675 } 753 754 755 756 676 677 public void run() { 678 try { 679 while(!interrupted()) { 757 680 sleep(cleaningInterval); 758 681 updateCacheEntries(); … … 761 684 disposeCacheManager(); 762 685 logger.info("CacheManager interrupted!"); 763 } 764 } 765 686 } 687 } 766 688 } -
trunk/src/com/bowman/cardserv/mysql/ConnectionPoolManager.java
r65 r72 2 2 3 3 import java.util.ArrayList; 4 import java.util.Collections; 4 5 import java.util.Iterator; 6 import java.util.List; 5 7 import java.sql.SQLException; 6 8 … … 27 29 private long cleaningInterval = 15000; 28 30 private MysqlConnectionPoolDataSource dataSource = null; 29 private ArrayList connectionPool = new ArrayList();31 private List connectionPool = Collections.synchronizedList(new ArrayList()); 30 32 31 33 /** … … 154 156 } 155 157 156 157 158 158 public void run() { 159 try { 160 while(!interrupted()) { 159 161 sleep(cleaningInterval); 160 162 cleanInactivePoolConnections(); … … 164 166 closePoolConnections(); 165 167 logger.info("ConnectionPoolManager interrupted!"); 166 167 168 } 169 } 168 170 169 171 } -
trunk/src/com/bowman/cardserv/mysql/PoolConnection.java
r65 r72 144 144 DBColumnType.ADMIN + ", " + 145 145 DBColumnType.MAIL + ", " + 146 DBColumnType.MAPEXCLUDE + ") VALUES(?,?,?,?,?,?,?,?,?,?)" 147 );146 DBColumnType.MAPEXCLUDE + ") VALUES(?,?,?,?,?,?,?,?,?,?)", 147 Statement.RETURN_GENERATED_KEYS); 148 148 ps_editUser = connection.prepareStatement( 149 149 "UPDATE " + DB_USER_TABLE + " SET " + … … 163 163 ps_addProfile = connection.prepareStatement( 164 164 "INSERT INTO " + DB_PROFILE_TABLE + " (" + 165 DBColumnType.PROFILENAME + ") VALUES(?)" 166 );165 DBColumnType.PROFILENAME + ") VALUES(?)", 166 Statement.RETURN_GENERATED_KEYS); 167 167 ps_deleteProfile = connection.prepareStatement( 168 168 "DELETE FROM " + DB_PROFILE_TABLE + " WHERE " + DBColumnType.PROFILENAME + " = ?"); … … 307 307 } 308 308 309 /** 310 * Returns the ID of the latest prepared statement execution. 311 * For this to work the prepared statement must have been executed 312 * with the parameter "Statement.RETURN_GENERATED_KEYS". 313 * @param ps 314 * @return ID of last query 315 * @throws SQLException 316 */ 317 private int getIDOfLastQuery(PreparedStatement ps) throws SQLException { 318 ResultSet keys = ps.getGeneratedKeys(); 319 keys.next(); 320 int id = keys.getInt(1); 321 keys.close(); 322 return id; 323 } 324 309 325 /* ############################################################################################ */ 310 326 /* database */ … … 436 452 * add user to the MySQL database. 437 453 */ 438 public voidaddUser(String username, String password, String displayname, String ipmask,454 public int addUser(String username, String password, String displayname, String ipmask, 439 455 int maxconnections, boolean enabled, boolean debug, boolean admin, 440 456 String mail, boolean mapexcluded) throws SQLException { … … 450 466 ps_addUser.setBoolean(10, mapexcluded); 451 467 executeUpdate(ps_addUser); 468 return getIDOfLastQuery(ps_addUser); 452 469 } 453 470 … … 499 516 * @throws SQLException 500 517 */ 501 public void setUserDebug(String username, Boolean debug) throws SQLException {502 ps_setUserDebug.setBoolean(1, debug .booleanValue());518 public void setUserDebug(String username, boolean debug) throws SQLException { 519 ps_setUserDebug.setBoolean(1, debug); 503 520 ps_setUserDebug.setString(2, username); 504 521 executeUpdate(ps_setUserDebug); … … 514 531 * @throws SQLException 515 532 */ 516 public voidaddProfile(String profileName) throws SQLException {533 public int addProfile(String profileName) throws SQLException { 517 534 ps_addProfile.setString(1, profileName); 518 535 executeUpdate(ps_addProfile); 536 return getIDOfLastQuery(ps_addProfile); 519 537 } 520 538 -
trunk/src/com/bowman/cardserv/mysql/Profile.java
r65 r72 4 4 * A simple profile object 5 5 * 6 * @author Pascal Nijhof6 * @author DonCarlo 7 7 * @since 02.01.2011 8 8 */ … … 10 10 11 11 private int id; 12 private String name;12 private String profileName; 13 13 14 14 public Profile(String name) { … … 16 16 } 17 17 18 public Profile(int id, String name) {18 public Profile(int id, String profileName) { 19 19 this.id = id; 20 this. name = name;20 this.profileName = profileName; 21 21 } 22 22 … … 31 31 * @return the name 32 32 */ 33 public String get Name() {34 return name;33 public String getProfileName() { 34 return profileName; 35 35 } 36 37 36 } -
trunk/src/com/bowman/cardserv/mysql/User.java
r65 r72 155 155 "mapExcluded: " + mapExcluded + " " 156 156 ; 157 } 158 157 } 159 158 }
Note:
See TracChangeset
for help on using the changeset viewer.