Merge branch 'db-improve'

Conflicts:
	src/main/java/fr/xephi/authme/settings/Settings.java
This commit is contained in:
DNx5
2015-12-11 21:47:19 +07:00
6 changed files with 130 additions and 605 deletions
@@ -1,513 +0,0 @@
package fr.xephi.authme.datasource;
import fr.xephi.authme.cache.auth.PlayerAuth;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*/
public class DatabaseCalls implements DataSource {
private final ExecutorService exec;
private final DataSource database;
/**
* Constructor for DatabaseCalls.
*
* @param database DataSource
*/
public DatabaseCalls(DataSource database) {
this.database = database;
this.exec = Executors.newCachedThreadPool();
}
/**
* Method isAuthAvailable.
*
* @param user String
*
* @return boolean * @see fr.xephi.authme.datasource.DataSource#isAuthAvailable(String)
*/
@Override
public synchronized boolean isAuthAvailable(final String user) {
try {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.isAuthAvailable(user);
}
}).get();
} catch (Exception e) {
return false;
}
}
/**
* Method getAuth.
*
* @param user String
*
* @return PlayerAuth * @see fr.xephi.authme.datasource.DataSource#getAuth(String)
*/
@Override
public synchronized PlayerAuth getAuth(final String user) {
try {
return exec.submit(new Callable<PlayerAuth>() {
public PlayerAuth call() throws Exception {
return database.getAuth(user);
}
}).get();
} catch (Exception e) {
return null;
}
}
/**
* Method saveAuth.
*
* @param auth PlayerAuth
*
* @return boolean * @see fr.xephi.authme.datasource.DataSource#saveAuth(PlayerAuth)
*/
@Override
public synchronized boolean saveAuth(final PlayerAuth auth) {
try {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.saveAuth(auth);
}
}).get();
} catch (Exception e) {
return false;
}
}
/**
* Method updateSession.
*
* @param auth PlayerAuth
*
* @return boolean * @see fr.xephi.authme.datasource.DataSource#updateSession(PlayerAuth)
*/
@Override
public synchronized boolean updateSession(final PlayerAuth auth) {
try {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.updateSession(auth);
}
}).get();
} catch (Exception e) {
return false;
}
}
/**
* Method updatePassword.
*
* @param auth PlayerAuth
*
* @return boolean * @see fr.xephi.authme.datasource.DataSource#updatePassword(PlayerAuth)
*/
@Override
public synchronized boolean updatePassword(final PlayerAuth auth) {
try {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.updatePassword(auth);
}
}).get();
} catch (Exception e) {
return false;
}
}
/**
* Method purgeDatabase.
*
* @param until long
*
* @return int * @see fr.xephi.authme.datasource.DataSource#purgeDatabase(long)
*/
@Override
public synchronized int purgeDatabase(final long until) {
try {
return exec.submit(new Callable<Integer>() {
public Integer call() throws Exception {
return database.purgeDatabase(until);
}
}).get();
} catch (Exception e) {
return -1;
}
}
/**
* Method autoPurgeDatabase.
*
* @param until long
*
* @return List<String> * @see fr.xephi.authme.datasource.DataSource#autoPurgeDatabase(long)
*/
@Override
public synchronized List<String> autoPurgeDatabase(final long until) {
try {
return exec.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return database.autoPurgeDatabase(until);
}
}).get();
} catch (Exception e) {
return new ArrayList<>();
}
}
/**
* Method removeAuth.
*
* @param user String
*
* @return boolean * @see fr.xephi.authme.datasource.DataSource#removeAuth(String)
*/
@Override
public synchronized boolean removeAuth(final String user) {
try {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.removeAuth(user);
}
}).get();
} catch (Exception e) {
return false;
}
}
/**
* Method updateQuitLoc.
*
* @param auth PlayerAuth
*
* @return boolean * @see fr.xephi.authme.datasource.DataSource#updateQuitLoc(PlayerAuth)
*/
@Override
public synchronized boolean updateQuitLoc(final PlayerAuth auth) {
try {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.updateQuitLoc(auth);
}
}).get();
} catch (Exception e) {
return false;
}
}
/**
* Method getIps.
*
* @param ip String
*
* @return int * @see fr.xephi.authme.datasource.DataSource#getIps(String)
*/
@Override
public synchronized int getIps(final String ip) {
try {
return exec.submit(new Callable<Integer>() {
public Integer call() throws Exception {
return database.getIps(ip);
}
}).get();
} catch (Exception e) {
return -1;
}
}
/**
* Method getAllAuthsByName.
*
* @param auth PlayerAuth
*
* @return List<String> * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByName(PlayerAuth)
*/
@Override
public synchronized List<String> getAllAuthsByName(final PlayerAuth auth) {
try {
return exec.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return database.getAllAuthsByName(auth);
}
}).get();
} catch (Exception e) {
return new ArrayList<>();
}
}
/**
* Method getAllAuthsByIp.
*
* @param ip String
*
* @return List<String> * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByIp(String)
*/
@Override
public synchronized List<String> getAllAuthsByIp(final String ip) {
try {
return exec.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return database.getAllAuthsByIp(ip);
}
}).get();
} catch (Exception e) {
return new ArrayList<>();
}
}
/**
* Method getAllAuthsByEmail.
*
* @param email String
*
* @return List<String> * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String)
*/
@Override
public synchronized List<String> getAllAuthsByEmail(final String email) {
try {
return exec.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return database.getAllAuthsByEmail(email);
}
}).get();
} catch (Exception e) {
return new ArrayList<>();
}
}
/**
* Method updateEmail.
*
* @param auth PlayerAuth
*
* @return boolean * @see fr.xephi.authme.datasource.DataSource#updateEmail(PlayerAuth)
*/
@Override
public synchronized boolean updateEmail(final PlayerAuth auth) {
try {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.updateEmail(auth);
}
}).get();
} catch (Exception e) {
return false;
}
}
/**
* Method updateSalt.
*
* @param auth PlayerAuth
*
* @return boolean * @see fr.xephi.authme.datasource.DataSource#updateSalt(PlayerAuth)
*/
@Override
public synchronized boolean updateSalt(final PlayerAuth auth) {
try {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.updateSalt(auth);
}
}).get();
} catch (Exception e) {
return false;
}
}
/**
* Method close.
*
* @see fr.xephi.authme.datasource.DataSource#close()
*/
@Override
public synchronized void close() {
exec.shutdown();
database.close();
}
/**
* Method reload.
*
* @see fr.xephi.authme.datasource.DataSource#reload()
*/
@Override
public synchronized void reload() {
database.reload();
}
/**
* Method purgeBanned.
*
* @param banned List<String>
*
* @see fr.xephi.authme.datasource.DataSource#purgeBanned(List<String>)
*/
@Override
public synchronized void purgeBanned(final List<String> banned) {
new Thread(new Runnable() {
public synchronized void run() {
database.purgeBanned(banned);
}
}).start();
}
/**
* Method getType.
*
* @return DataSourceType * @see fr.xephi.authme.datasource.DataSource#getType()
*/
@Override
public synchronized DataSourceType getType() {
return database.getType();
}
/**
* Method isLogged.
*
* @param user String
*
* @return boolean * @see fr.xephi.authme.datasource.DataSource#isLogged(String)
*/
@Override
public synchronized boolean isLogged(final String user) {
try {
return exec.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
return database.isLogged(user);
}
}).get();
} catch (Exception e) {
return false;
}
}
/**
* Method setLogged.
*
* @param user String
*
* @see fr.xephi.authme.datasource.DataSource#setLogged(String)
*/
@Override
public synchronized void setLogged(final String user) {
exec.execute(new Runnable() {
public synchronized void run() {
database.setLogged(user);
}
});
}
/**
* Method setUnlogged.
*
* @param user String
*
* @see fr.xephi.authme.datasource.DataSource#setUnlogged(String)
*/
@Override
public synchronized void setUnlogged(final String user) {
exec.execute(new Runnable() {
public synchronized void run() {
database.setUnlogged(user);
}
});
}
/**
* Method purgeLogged.
*
* @see fr.xephi.authme.datasource.DataSource#purgeLogged()
*/
@Override
public synchronized void purgeLogged() {
exec.execute(new Runnable() {
public synchronized void run() {
database.purgeLogged();
}
});
}
/**
* Method getAccountsRegistered.
*
* @return int * @see fr.xephi.authme.datasource.DataSource#getAccountsRegistered()
*/
@Override
public synchronized int getAccountsRegistered() {
try {
return exec.submit(new Callable<Integer>() {
public Integer call() throws Exception {
return database.getAccountsRegistered();
}
}).get();
} catch (Exception e) {
return -1;
}
}
/**
* Method updateName.
*
* @param oldOne String
* @param newOne String
*
* @see fr.xephi.authme.datasource.DataSource#updateName(String, String)
*/
@Override
public synchronized void updateName(final String oldOne, final String newOne) {
exec.execute(new Runnable() {
public synchronized void run() {
database.updateName(oldOne, newOne);
}
});
}
/**
* Method getAllAuths.
*
* @return List<PlayerAuth> * @see fr.xephi.authme.datasource.DataSource#getAllAuths()
*/
@Override
public synchronized List<PlayerAuth> getAllAuths() {
try {
return exec.submit(new Callable<List<PlayerAuth>>() {
public List<PlayerAuth> call() throws Exception {
return database.getAllAuths();
}
}).get();
} catch (Exception e) {
return new ArrayList<>();
}
}
/**
* Method getLoggedPlayers.
*
* @return List<PlayerAuth> * @see fr.xephi.authme.datasource.DataSource#getLoggedPlayers()
*/
@Override
public List<PlayerAuth> getLoggedPlayers() {
try {
return exec.submit(new Callable<List<PlayerAuth>>() {
public List<PlayerAuth> call() throws Exception {
return database.getLoggedPlayers();
}
}).get();
} catch (Exception e) {
return new ArrayList<>();
}
}
}
@@ -259,8 +259,9 @@ public class MySQL implements DataSource {
pst.setString(1, user.toLowerCase());
ResultSet rs = pst.executeQuery();
return rs.next();
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
return false;
}
@@ -312,8 +313,9 @@ public class MySQL implements DataSource {
pAuth.setHash(new String(bytes));
}
}
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
return null;
}
return pAuth;
@@ -521,8 +523,9 @@ public class MySQL implements DataSource {
pst.close();
}
return true;
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
return false;
}
@@ -574,8 +577,9 @@ public class MySQL implements DataSource {
pst.close();
}
return true;
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
return false;
}
@@ -602,7 +606,7 @@ public class MySQL implements DataSource {
pst.executeUpdate();
pst.close();
return true;
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
}
return false;
@@ -625,8 +629,9 @@ public class MySQL implements DataSource {
PreparedStatement pst = con.prepareStatement(sql);
pst.setLong(1, until);
result = pst.executeUpdate();
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
return result;
}
@@ -653,8 +658,9 @@ public class MySQL implements DataSource {
sql = "DELETE FROM " + tableName + " WHERE " + columnLastLogin + "<" + until;
st.executeUpdate(sql);
st.close();
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
return list;
}
@@ -693,8 +699,9 @@ public class MySQL implements DataSource {
pst.setString(1, user);
pst.executeUpdate();
return true;
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
return false;
}
@@ -723,8 +730,9 @@ public class MySQL implements DataSource {
pst.executeUpdate();
pst.close();
return true;
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
return false;
}
@@ -751,8 +759,9 @@ public class MySQL implements DataSource {
}
rs.close();
pst.close();
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
return countIp;
}
@@ -776,7 +785,7 @@ public class MySQL implements DataSource {
pst.executeUpdate();
pst.close();
return true;
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
@@ -805,8 +814,9 @@ public class MySQL implements DataSource {
pst.executeUpdate();
pst.close();
return true;
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
return false;
}
@@ -820,9 +830,10 @@ public class MySQL implements DataSource {
public void reload() {
try {
reloadArguments();
} catch (Exception e) {
ConsoleLogger.showError(e.getMessage());
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.showError("Can't reconnect to MySQL database... Please check your MySQL configuration!");
ConsoleLogger.writeStackTrace(ex);
AuthMe.getInstance().stopOrUnload();
}
}
@@ -861,8 +872,9 @@ public class MySQL implements DataSource {
}
rs.close();
pst.close();
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
return result;
}
@@ -889,8 +901,9 @@ public class MySQL implements DataSource {
}
rs.close();
pst.close();
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
return result;
}
@@ -938,8 +951,9 @@ public class MySQL implements DataSource {
pst.executeUpdate();
}
pst.close();
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
}
@@ -969,8 +983,9 @@ public class MySQL implements DataSource {
pst.setString(1, user);
ResultSet rs = pst.executeQuery();
isLogged = rs.next() && (rs.getInt(columnLogged) == 1);
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
return isLogged;
}
@@ -991,8 +1006,9 @@ public class MySQL implements DataSource {
pst.setString(2, user.toLowerCase());
pst.executeUpdate();
pst.close();
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
}
@@ -1012,8 +1028,9 @@ public class MySQL implements DataSource {
pst.setString(2, user.toLowerCase());
pst.executeUpdate();
pst.close();
} catch (Exception ex) {
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
}
@@ -1033,6 +1050,7 @@ public class MySQL implements DataSource {
pst.close();
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
}
@@ -1047,15 +1065,16 @@ public class MySQL implements DataSource {
public int getAccountsRegistered() {
int result = 0;
try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement("SELECT COUNT(*) FROM " + tableName + ";");
ResultSet rs = pst.executeQuery();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM " + tableName);
if (rs.next()) {
result = rs.getInt(1);
}
rs.close();
pst.close();
st.close();
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
return result;
}
@@ -1078,6 +1097,7 @@ public class MySQL implements DataSource {
pst.executeUpdate();
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
}
@@ -1131,6 +1151,7 @@ public class MySQL implements DataSource {
st.close();
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
return auths;
}
@@ -1182,6 +1203,7 @@ public class MySQL implements DataSource {
}
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
return auths;
}