Commit 7ce20777 authored by Derek DeMoro's avatar Derek DeMoro Committed by derek

Refactoring work.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@586 b35dd754-fafc-0310-a699-88a17e54d16e
parent 9b8b9dfd
...@@ -11,22 +11,30 @@ ...@@ -11,22 +11,30 @@
package org.jivesoftware.messenger.user.spi; package org.jivesoftware.messenger.user.spi;
import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.messenger.container.Container;
import org.jivesoftware.util.*;
import org.jivesoftware.messenger.NodePrep;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.user.*;
import org.jivesoftware.database.DbConnectionManager;
import java.util.Iterator;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.messenger.container.Container;
import org.jivesoftware.messenger.user.User;
import org.jivesoftware.messenger.user.UserAlreadyExistsException;
import org.jivesoftware.messenger.user.UserManager;
import org.jivesoftware.messenger.user.UserNotFoundException;
import org.jivesoftware.messenger.user.UserProviderFactory;
import org.jivesoftware.stringprep.Stringprep;
import org.jivesoftware.stringprep.StringprepException;
import org.jivesoftware.util.Cache;
import org.jivesoftware.util.CacheManager;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.util.StringUtils;
/** /**
* Database implementation of the UserManager interface. * Database implementation of the UserManager interface.
...@@ -49,16 +57,16 @@ public class UserManagerImpl extends BasicModule implements UserManager { ...@@ -49,16 +57,16 @@ public class UserManagerImpl extends BasicModule implements UserManager {
private static final String USER_COUNT = "SELECT count(*) FROM jiveUser"; private static final String USER_COUNT = "SELECT count(*) FROM jiveUser";
private static final String ALL_USERS = "SELECT username from jiveUser"; private static final String ALL_USERS = "SELECT username from jiveUser";
private static final String INSERT_USER = private static final String INSERT_USER =
"INSERT INTO jiveUser (username,password,name,email,creationDate,modificationDate) " + "INSERT INTO jiveUser (username,password,name,email,creationDate,modificationDate) " +
"VALUES (?,?,?,?,?,?)"; "VALUES (?,?,?,?,?,?)";
private static final String DELETE_USER_GROUPS = private static final String DELETE_USER_GROUPS =
"DELETE FROM jiveGroupUser WHERE username=?"; "DELETE FROM jiveGroupUser WHERE username=?";
private static final String DELETE_USER_PROPS = private static final String DELETE_USER_PROPS =
"DELETE FROM jiveUserProp WHERE username=?"; "DELETE FROM jiveUserProp WHERE username=?";
private static final String DELETE_VCARD_PROPS = private static final String DELETE_VCARD_PROPS =
"DELETE FROM jiveVCard WHERE username=?"; "DELETE FROM jiveVCard WHERE username=?";
private static final String DELETE_USER = private static final String DELETE_USER =
"DELETE FROM jiveUser WHERE username=?"; "DELETE FROM jiveUser WHERE username=?";
private Cache userCache; private Cache userCache;
...@@ -67,17 +75,21 @@ public class UserManagerImpl extends BasicModule implements UserManager { ...@@ -67,17 +75,21 @@ public class UserManagerImpl extends BasicModule implements UserManager {
} }
private void initializeCaches() { private void initializeCaches() {
CacheManager.initializeCache("userCache", 512*1024); CacheManager.initializeCache("userCache", 512 * 1024);
CacheManager.initializeCache("username2roster", 512*1024); CacheManager.initializeCache("username2roster", 512 * 1024);
userCache = CacheManager.getCache("userCache"); userCache = CacheManager.getCache("userCache");
} }
public User createUser(String username, String password, String email) public User createUser(String username, String password, String email) throws UserAlreadyExistsException, UnauthorizedException {
throws UserAlreadyExistsException, UnauthorizedException {
User newUser = null; User newUser = null;
// Strip extra or invisible characters from the username so that existing // Strip extra or invisible characters from the username so that existing
// usernames can't be forged. // usernames can't be forged.
username = NodePrep.prep(username); try {
username = Stringprep.nameprep(username, true);
}
catch (StringprepException e) {
Log.error(e);
}
username = StringUtils.replace(username, " ", ""); username = StringUtils.replace(username, " ", "");
try { try {
getUser(username); getUser(username);
...@@ -109,10 +121,7 @@ public class UserManagerImpl extends BasicModule implements UserManager { ...@@ -109,10 +121,7 @@ public class UserManagerImpl extends BasicModule implements UserManager {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.close(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
newUser = getUser(username); newUser = getUser(username);
} }
...@@ -128,7 +137,12 @@ public class UserManagerImpl extends BasicModule implements UserManager { ...@@ -128,7 +137,12 @@ public class UserManagerImpl extends BasicModule implements UserManager {
if (username == null) { if (username == null) {
throw new UserNotFoundException("Username with null value is not valid."); throw new UserNotFoundException("Username with null value is not valid.");
} }
username = NodePrep.prep(username); try {
username = Stringprep.nameprep(username, false);
}
catch (StringprepException e) {
e.printStackTrace();
}
User user = (User)userCache.get(username); User user = (User)userCache.get(username);
if (user == null) { if (user == null) {
// Hack to make sure user exists. // Hack to make sure user exists.
...@@ -179,8 +193,14 @@ public class UserManagerImpl extends BasicModule implements UserManager { ...@@ -179,8 +193,14 @@ public class UserManagerImpl extends BasicModule implements UserManager {
abortTransaction = true; abortTransaction = true;
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } try {
catch (Exception e) { Log.error(e); } if (pstmt != null) {
pstmt.close();
}
}
catch (Exception e) {
Log.error(e);
}
DbConnectionManager.closeTransactionConnection(con, abortTransaction); DbConnectionManager.closeTransactionConnection(con, abortTransaction);
} }
// Expire user cache. // Expire user cache.
...@@ -204,10 +224,7 @@ public class UserManagerImpl extends BasicModule implements UserManager { ...@@ -204,10 +224,7 @@ public class UserManagerImpl extends BasicModule implements UserManager {
Log.error(e); Log.error(e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.close(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
return count; return count;
} }
...@@ -232,12 +249,9 @@ public class UserManagerImpl extends BasicModule implements UserManager { ...@@ -232,12 +249,9 @@ public class UserManagerImpl extends BasicModule implements UserManager {
Log.error(e); Log.error(e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.close(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
return new UserIterator((String [])users.toArray(new String[users.size()])); return new UserIterator((String[])users.toArray(new String[users.size()]));
} }
public Iterator users(int startIndex, int numResults) throws UnauthorizedException { public Iterator users(int startIndex, int numResults) throws UnauthorizedException {
...@@ -268,12 +282,9 @@ public class UserManagerImpl extends BasicModule implements UserManager { ...@@ -268,12 +282,9 @@ public class UserManagerImpl extends BasicModule implements UserManager {
Log.error(e); Log.error(e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.close(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
return new UserIterator((String [])users.toArray(new String[users.size()])); return new UserIterator((String[])users.toArray(new String[users.size()]));
} }
// ##################################################################### // #####################################################################
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment