MultiThreading test finished, use them as default

This commit is contained in:
Xephi
2014-08-08 21:40:32 +02:00
parent e9c2535a18
commit ec7ac60340
8 changed files with 22 additions and 2256 deletions
@@ -10,6 +10,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.api.API;
import fr.xephi.authme.cache.auth.PlayerAuth;
@@ -17,7 +18,7 @@ import fr.xephi.authme.settings.PlayersLogs;
import fr.xephi.authme.settings.Settings;
public class FileDataSource implements DataSource {
public class FlatFileThread extends Thread implements DataSource {
/* file layout:
*
@@ -32,9 +33,20 @@ public class FileDataSource implements DataSource {
*/
private File source;
public FileDataSource() throws IOException {
public void run() {
source = new File(Settings.AUTH_FILE);
source.createNewFile();
try {
source.createNewFile();
} catch (IOException e) {
ConsoleLogger.showError(e.getMessage());
if (Settings.isStopEnabled) {
ConsoleLogger.showError("Can't use FLAT FILE... SHUTDOWN...");
AuthMe.getInstance().getServer().shutdown();
}
if (!Settings.isStopEnabled)
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
return;
}
}
@Override
@@ -120,7 +132,7 @@ public class FileDataSource implements DataSource {
break;
}
default: {
newAuth = new PlayerAuth(args[0], auth.getHash(), args[2], 0, 0.0, 0.0, 0.0, "world", "your@email.com", API.getPlayerRealName(args[0]));
newAuth = new PlayerAuth(args[0], auth.getHash(), args[2], 0, 0, 0, 0, "world", "your@email.com", API.getPlayerRealName(args[0]));
break;
}
}
@@ -177,7 +189,7 @@ public class FileDataSource implements DataSource {
break;
}
default: {
newAuth = new PlayerAuth(args[0], args[1], auth.getIp(), auth.getLastLogin(), 0.0, 0.0, 0.0, "world", "your@email.com", API.getPlayerRealName(args[0]));
newAuth = new PlayerAuth(args[0], args[1], auth.getIp(), auth.getLastLogin(), 0, 0, 0, "world", "your@email.com", API.getPlayerRealName(args[0]));
break;
}
}
@@ -654,5 +666,4 @@ public class FileDataSource implements DataSource {
public void purgeLogged() {
PlayersLogs.getInstance().clear();
}
}
@@ -1,5 +1,14 @@
package fr.xephi.authme.datasource;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import fr.xephi.authme.AuthMe;
@@ -10,17 +19,7 @@ import fr.xephi.authme.datasource.MiniConnectionPoolManager.TimeoutException;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.settings.Settings;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class MySQLDataSource implements DataSource {
public class MySQLThread extends Thread implements DataSource {
private String host;
private String port;
@@ -44,7 +43,7 @@ public class MySQLDataSource implements DataSource {
private List<String> columnOthers;
private MiniConnectionPoolManager conPool;
public MySQLDataSource() throws ClassNotFoundException, SQLException {
public void run() {
this.host = Settings.getMySQLHost;
this.port = Settings.getMySQLPort;
this.username = Settings.getMySQLUsername;
@@ -65,12 +64,40 @@ public class MySQLDataSource implements DataSource {
this.columnOthers = Settings.getMySQLOtherUsernameColumn;
this.columnID = Settings.getMySQLColumnId;
this.columnLogged = Settings.getMySQLColumnLogged;
connect();
setup();
try {
this.connect();
this.setup();
} catch (ClassNotFoundException e) {
ConsoleLogger.showError(e.getMessage());
if (Settings.isStopEnabled) {
ConsoleLogger.showError("Can't use MySQL... Please input correct MySQL informations ! SHUTDOWN...");
AuthMe.getInstance().getServer().shutdown();
}
if (!Settings.isStopEnabled)
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
return;
} catch (SQLException e) {
ConsoleLogger.showError(e.getMessage());
if (Settings.isStopEnabled) {
ConsoleLogger.showError("Can't use MySQL... Please input correct MySQL informations ! SHUTDOWN...");
AuthMe.getInstance().getServer().shutdown();
}
if (!Settings.isStopEnabled)
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
return;
} catch (TimeoutException e) {
ConsoleLogger.showError(e.getMessage());
if (Settings.isStopEnabled) {
ConsoleLogger.showError("Can't use MySQL... Please input correct MySQL informations ! SHUTDOWN...");
AuthMe.getInstance().getServer().shutdown();
}
if (!Settings.isStopEnabled)
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
return;
}
}
private synchronized void connect() throws ClassNotFoundException, SQLException {
private synchronized void connect() throws ClassNotFoundException, SQLException, TimeoutException {
Class.forName("com.mysql.jdbc.Driver");
ConsoleLogger.info("MySQL driver loaded");
MysqlConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();
@@ -79,7 +106,7 @@ public class MySQLDataSource implements DataSource {
dataSource.setPort(Integer.parseInt(port));
dataSource.setUser(username);
dataSource.setPassword(password);
conPool = new MiniConnectionPoolManager(dataSource, 20);
conPool = new MiniConnectionPoolManager(dataSource, 10);
ConsoleLogger.info("Connection pool ready");
}
@@ -112,13 +139,13 @@ public class MySQLDataSource implements DataSource {
rs = con.getMetaData().getColumns(null, null, tableName, columnIp);
if (!rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
+ columnIp + " VARCHAR(40) NOT NULL DEFAULT '127.0.0.1';");
+ columnIp + " VARCHAR(40) NOT NULL;");
}
rs.close();
rs = con.getMetaData().getColumns(null, null, tableName, columnLastLogin);
if (!rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
+ columnLastLogin + " BIGINT DEFAULT '0';");
+ columnLastLogin + " BIGINT;");
}
rs.close();
rs = con.getMetaData().getColumns(null, null, tableName, lastlocX);
@@ -213,7 +240,7 @@ public class MySQLDataSource implements DataSource {
if (rs.next()) {
Blob blob = rs.getBlob("data");
byte[] bytes = blob.getBytes(1, (int) blob.length());
pAuth.setHash(new String(bytes));
pAuth.setHash(new String(bytes));
}
}
} else {
@@ -428,17 +455,17 @@ public class MySQLDataSource implements DataSource {
pst.setString(1, auth.getNickname());
rs = pst.executeQuery();
if (rs.next()) {
id = rs.getInt(columnID);
// Insert password in the correct table
pst = con.prepareStatement("UPDATE xf_user_authenticate SET data=? WHERE " + columnID + "=?;");
id = rs.getInt(columnID);
// Insert password in the correct table
pst = con.prepareStatement("UPDATE xf_user_authenticate SET data=? WHERE " + columnID + "=?;");
byte[] bytes = auth.getHash().getBytes();
Blob blob = con.createBlob();
blob.setBytes(1, bytes);
pst.setBlob(1, blob);
pst.setInt(2, id);
pst.executeUpdate();
pst = con.prepareStatement("UPDATE xf_user_authenticate SET scheme_class=? WHERE " + columnID + "=?;");
pst.setString(1, "XenForo_Authentication_Core12");
pst.setInt(2, id);
pst.executeUpdate();
pst = con.prepareStatement("UPDATE xf_user_authenticate SET scheme_class=? WHERE " + columnID + "=?;");
pst.setString(1, "XenForo_Authentication_Core12");
pst.setInt(2, id);
pst.executeUpdate();
}
@@ -645,9 +672,9 @@ public class MySQLDataSource implements DataSource {
return true;
}
@Override
@Override
public synchronized boolean updateSalt(PlayerAuth auth) {
if (columnSalt.isEmpty() || auth.getSalt() == null || auth.getSalt().isEmpty()) {
if (columnSalt.isEmpty()) {
return false;
}
Connection con = null;
@@ -725,7 +752,7 @@ public class MySQLDataSource implements DataSource {
}
}
@Override
@Override
public synchronized List<String> getAllAuthsByName(PlayerAuth auth) {
Connection con = null;
PreparedStatement pst = null;
@@ -754,7 +781,7 @@ public class MySQLDataSource implements DataSource {
}
}
@Override
@Override
public synchronized List<String> getAllAuthsByIp(String ip) {
Connection con = null;
PreparedStatement pst = null;
@@ -783,7 +810,7 @@ public class MySQLDataSource implements DataSource {
}
}
@Override
@Override
public synchronized List<String> getAllAuthsByEmail(String email) {
Connection con = null;
PreparedStatement pst = null;
@@ -809,10 +836,10 @@ public class MySQLDataSource implements DataSource {
close(rs);
close(pst);
close(con);
}
}
}
@Override
@Override
public synchronized void purgeBanned(List<String> banned) {
Connection con = null;
PreparedStatement pst = null;
@@ -880,7 +907,7 @@ public class MySQLDataSource implements DataSource {
dataSource.setUser(username);
dataSource.setPassword(password);
conPool = new MiniConnectionPoolManager(dataSource, 10);
if(!reload)
if (!reload)
ConsoleLogger.info("ConnectionPool was unavailable... Reconnected!");
}
@@ -1,11 +1,15 @@
package fr.xephi.authme.datasource;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.sqlite.*;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.api.API;
import fr.xephi.authme.cache.auth.PlayerAuth;
@@ -13,12 +17,8 @@ import fr.xephi.authme.datasource.MiniConnectionPoolManager.TimeoutException;
import fr.xephi.authme.settings.PlayersLogs;
import fr.xephi.authme.settings.Settings;
/**
*
* @author stefano
*/
@SuppressWarnings("unused")
public class SqliteDataSource implements DataSource {
public class SQLiteThread extends Thread implements DataSource {
private String database;
private String tableName;
@@ -36,8 +36,8 @@ public class SqliteDataSource implements DataSource {
private String columnID;
private Connection con;
public SqliteDataSource() throws ClassNotFoundException, SQLException {
this.database = Settings.getMySQLDatabase;
public void run() {
this.database = Settings.getMySQLDatabase;
this.tableName = Settings.getMySQLTablename;
this.columnName = Settings.getMySQLColumnName;
this.columnPassword = Settings.getMySQLColumnPassword;
@@ -52,14 +52,35 @@ public class SqliteDataSource implements DataSource {
this.columnEmail = Settings.getMySQLColumnEmail;
this.columnID = Settings.getMySQLColumnId;
connect();
setup();
try {
this.connect();
this.setup();
} catch (ClassNotFoundException e) {
ConsoleLogger.showError(e.getMessage());
if (Settings.isStopEnabled) {
ConsoleLogger.showError("Can't use SQLITE... ! SHUTDOWN...");
AuthMe.getInstance().getServer().shutdown();
}
if (!Settings.isStopEnabled)
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
return;
} catch (SQLException e) {
ConsoleLogger.showError(e.getMessage());
if (Settings.isStopEnabled) {
ConsoleLogger.showError("Can't use SQLITE... ! SHUTDOWN...");
AuthMe.getInstance().getServer().shutdown();
}
if (!Settings.isStopEnabled)
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
return;
}
}
private synchronized void connect() throws ClassNotFoundException, SQLException {
Class.forName("org.sqlite.JDBC");
ConsoleLogger.info("SQLite driver loaded");
this.con = DriverManager.getConnection("jdbc:sqlite:plugins/AuthMe/"+database+".db");
}
private synchronized void setup() throws SQLException {
@@ -400,16 +421,6 @@ public class SqliteDataSource implements DataSource {
}
}
private void close(Connection con) {
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
}
}
}
@Override
public List<String> getAllAuthsByName(PlayerAuth auth) {
PreparedStatement pst = null;