1
0
mirror of https://github.com/moparisthebest/xAuth synced 2024-12-21 22:58:51 -05:00

Divide MySQL and H2 stuff up

This commit is contained in:
Zach Iverson 2011-07-11 15:29:40 -04:00
parent 7d477cec96
commit 13279eb61c
4 changed files with 108 additions and 10 deletions

View File

@ -1,7 +1,7 @@
package com.cypherx.xauth.datamanager;
import java.sql.Connection;
import java.sql.DriverManager;
//import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -17,14 +17,14 @@ import com.cypherx.xauth.Session;
import com.cypherx.xauth.StrikeBan;
import com.cypherx.xauth.TeleLocation;
import com.cypherx.xauth.Util;
import com.cypherx.xauth.xAuth;
//import com.cypherx.xauth.xAuth;
import com.cypherx.xauth.xAuthLog;
import com.cypherx.xauth.xAuthPlayer;
import com.cypherx.xauth.xAuthSettings;
public class DataManager {
private Connection connection = null;
private Statement stmt = null;
public abstract class DataManager {
protected Connection connection = null;
protected Statement stmt = null;
private PreparedStatement prepStmt = null;
private ResultSet rs = null;
@ -33,9 +33,18 @@ public class DataManager {
public DataManager() {
connect();
if (isConnected()) {
try {
stmt = connection.createStatement();
} catch (SQLException e) {}
}
}
private void connect() {
public abstract void connect();
public abstract void deleteExpiredSessions();
/*private void connect() {
if (xAuthSettings.datasource.equals("mysql"))
connectMySQL();
else
@ -71,13 +80,14 @@ public class DataManager {
} catch (SQLException e) {
xAuthLog.severe("Could not connect to H2 database!", e);
}
}
}*/
public void runStartupTasks() {
createTables();
loadTeleLocations();
deleteExpiredSessions();
String sql;
/*String sql;
if (xAuthSettings.datasource.equals("mysql")) {
sql = "DELETE FROM `" + xAuthSettings.tblSession + "`" +
@ -95,7 +105,7 @@ public class DataManager {
stmt.executeUpdate(sql);
} catch (SQLException e) {
xAuthLog.severe("Could not delete expired settings!", e);
}
}*/
}
public void createTables() {

View File

@ -0,0 +1,44 @@
package com.cypherx.xauth.datamanager;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.cypherx.xauth.xAuth;
import com.cypherx.xauth.xAuthLog;
import com.cypherx.xauth.xAuthSettings;
public class H2 extends DataManager {
public H2() {
super();
}
public void connect() {
if (!xAuthSettings.datasource.equals("default"))
System.out.println("[" + xAuth.desc.getName() + "] Unknown datasource '" + xAuthSettings.datasource + "' - Using default (H2)");
try {
Class.forName("org.h2.Driver");
connection = DriverManager.getConnection("jdbc:h2:" + xAuth.dataFolder.toString() +
System.getProperty("file.separator") + "xAuth;IGNORECASE=TRUE", "sa", "");
xAuthLog.info("Connection to H2 database established!");
} catch (ClassNotFoundException e) {
xAuthLog.severe("Missing H2 library!", e);
} catch (SQLException e) {
xAuthLog.severe("Could not connect to H2 database!", e);
}
}
public void deleteExpiredSessions() {
if (!isConnected())
connect();
try {
stmt.executeUpdate(
"DELETE FROM `" + xAuthSettings.tblSession + "`" +
"WHERE NOW() > DATEADD('SECOND', " + xAuthSettings.sessionLength + ", `logintime`)"
);
} catch (SQLException e) {
xAuthLog.severe("Could not delete expired settings!", e);
}
}
}

View File

@ -0,0 +1,40 @@
package com.cypherx.xauth.datamanager;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.cypherx.xauth.xAuthLog;
import com.cypherx.xauth.xAuthSettings;
public class MySQL extends DataManager {
public MySQL() {
super();
}
public void connect() {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://" + xAuthSettings.mysqlHost + ":" + xAuthSettings.mysqlPort + "/" +
xAuthSettings.mysqlDb + "?zeroDateTimeBehavior=convertToNull", xAuthSettings.mysqlUser, xAuthSettings.mysqlPass);
xAuthLog.info("Connection to MySQL server established!");
} catch (ClassNotFoundException e) {
xAuthLog.severe("Missing MySQL library!", e);
} catch (SQLException e) {
xAuthLog.severe("Could not connect to MySQL server!", e);
}
}
public void deleteExpiredSessions() {
if (!isConnected())
connect();
try {
stmt.executeUpdate(
"DELETE FROM `" + xAuthSettings.tblSession + "`" +
"WHERE NOW() > ADDDATE(`logintime`, INTERVAL " + xAuthSettings.sessionLength + " SECOND)"
);
} catch (SQLException e) {
xAuthLog.severe("Could not delete expired settings!", e);
}
}
}

View File

@ -61,7 +61,11 @@ public class xAuth extends JavaPlugin {
xAuthPermissions.setup(this);
xAuthHelp.setup(this);
dataManager = new DataManager();
if (xAuthSettings.datasource.equals("mysql"))
dataManager = new MySQL();
else
dataManager = new H2();
if (!dataManager.isConnected()) {
xAuthLog.severe("Disabling - No connection to database");
getServer().getPluginManager().disablePlugin(this);