Bugs + nettoyage code

- tous les appels à printStackTrace() sont supprimés et remplacés par Log.severe()
- Ajout de de l'historiqe de ping et de login kick
- AbstractConfig et AbstractConfigManager maintenant dans PandacubeUtil pour être utilisé par tous les plugins (évite code en double)
- Connexion MySQL supporte UTF-8 (mb4)
- Correction de Quelques erreurs lorsqu'une table SQL n'est pas initialisée avant son utilisation
- Correction de bugs lors de le gestion des clé étrangères dans l'ORM.
This commit is contained in:
Marc Baloup 2016-07-26 02:06:21 +02:00
parent 805ff052d3
commit 55748b0d5e
23 changed files with 305 additions and 44 deletions

View File

@ -60,7 +60,7 @@ public class ServerPropertyFile {
return true; return true;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.severe(e);
} finally { } finally {
try { try {
in.close(); in.close();
@ -82,7 +82,7 @@ public class ServerPropertyFile {
return true; return true;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Log.severe(e);
} finally { } finally {
try { try {
out.close(); out.close();

View File

@ -0,0 +1,133 @@
package fr.pandacube.java.util.config;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import fr.pandacube.java.util.Log;
/**
* Classe chargeant en mémoire un fichier de configuration ou un dossier donné
* @author Marc Baloup
*
*/
public abstract class AbstractConfig {
/**
* Correspond au dossier ou au fichier de configuration traité par la sous-classe
* courante de {@link AbstractConfig}
*/
protected File configFile;
/**
* @param fileOrDirName le nom du fichier ou du dossier correspondant à la sous-classe de {@link AbstractConfig}
* @param isDir <code>true</code> si il s'agit d'un dossier, <code>false</code> sinon
* @throws IOException si le fichier est impossible à créer
*/
public AbstractConfig(File configDir, String fileOrDirName, FileType type) throws IOException {
configFile = new File(configDir, fileOrDirName);
if (type == FileType.DIR)
configFile.mkdir();
else
configFile.createNewFile();
}
/**
* Retourne toutes les lignes d'un fichier donné
* @param ignoreEmpty <code>true</code> si on doit ignorer les lignes vides
* @param ignoreHashtagComment <code>true</code> si on doit ignorer les lignes commentés (commençant par un #)
* @param trimOutput <code>true</code> si on doit appeller la méthode String.trim() sur chaque ligne retournée
* @param f le fichier à lire
* @return la liste des lignes utiles
* @throws IOException
*/
protected List<String> getFileLines(boolean ignoreEmpty, boolean ignoreHashtagComment, boolean trimOutput, File f) throws IOException {
if (!f.isFile())
return null;
BufferedReader reader = new BufferedReader(new FileReader(f));
List<String> lines = new ArrayList<String>();
String line;
while ((line = reader.readLine()) != null) {
String trimmedLine = line.trim();
if (ignoreEmpty && trimmedLine.equals(""))
continue;
if (ignoreHashtagComment && trimmedLine.startsWith("#"))
continue;
if (trimOutput)
lines.add(trimmedLine);
else
lines.add(line);
}
reader.close();
return lines;
}
/**
* Retourne toutes les lignes du fichier de configuration
* @param ignoreEmpty <code>true</code> si on doit ignorer les lignes vides
* @param ignoreHashtagComment <code>true</code> si on doit ignorer les lignes commentés (commençant par un #)
* @param trimOutput <code>true</code> si on doit appeller la méthode String.trim() sur chaque ligne retournée
* @return la liste des lignes utiles
* @throws IOException
*/
protected List<String> getFileLines(boolean ignoreEmpty, boolean ignoreHashtagComment, boolean trimOutput) throws IOException {
return getFileLines(ignoreEmpty, ignoreHashtagComment, trimOutput, configFile);
}
protected List<File> getFileList() {
if (!configFile.isDirectory())
return null;
return Arrays.asList(configFile.listFiles());
}
/**
* Découpe une chaine de caractère contenant une série de noeuds
* de permissions séparés par des point-virgules et la retourne sous forme d'une liste.
* @param perms la chaine de permissions à traiter
* @return <code>null</code> si le paramètre est nulle ou si <code>perms.equals("*")</code>, ou alors la chaine splittée.
*/
public static List<String> splitPermissionsString(String perms) {
if (perms == null || perms.equals("*"))
return null;
else
return Arrays.asList(perms.split(";"));
}
protected void warning(String message) {
Log.warning("Erreur dans la configuration de '"+configFile.getName()+"' : "+message);
}
protected enum FileType {
FILE, DIR
}
}

View File

@ -0,0 +1,39 @@
package fr.pandacube.java.util.config;
import java.io.File;
import java.io.IOException;
public abstract class AbstractConfigManager {
protected File configDir;
public AbstractConfigManager(File configD) throws IOException {
configDir = configD;
configDir.mkdirs();
init();
}
/**
* Implementation must close all closeable configuration (saving for example)
* @throws IOException
*/
public abstract void close() throws IOException;
/**
* Implementation must init all config data
* @throws IOException
*/
public abstract void init() throws IOException;
public synchronized void reloadConfig() throws IOException {
close();
init();
}
}

View File

@ -24,7 +24,7 @@ public class SQLContact extends SQLElement<SQLContact> {
public static final SQLField<SQLContact, Integer> time = new SQLField<>("time", SQLType.INT, false); public static final SQLField<SQLContact, Integer> time = new SQLField<>("time", SQLType.INT, false);
public static final SQLFKField<SQLContact, String, SQLPlayer> playerId = new SQLFKField<>("playerId", SQLType.CHAR(36), true, public static final SQLFKField<SQLContact, String, SQLPlayer> playerId = new SQLFKField<>("playerId", SQLType.CHAR(36), true,
SQLPlayer.playerId); SQLPlayer.class, SQLPlayer.playerId);
public static final SQLField<SQLContact, String> userName = new SQLField<>("userName", SQLType.VARCHAR(50), true); public static final SQLField<SQLContact, String> userName = new SQLField<>("userName", SQLType.VARCHAR(50), true);
public static final SQLField<SQLContact, String> userMail = new SQLField<>("userMail", SQLType.VARCHAR(50), true); public static final SQLField<SQLContact, String> userMail = new SQLField<>("userMail", SQLType.VARCHAR(50), true);
public static final SQLField<SQLContact, String> titre = new SQLField<>("titre", SQLType.VARCHAR(100), false); public static final SQLField<SQLContact, String> titre = new SQLField<>("titre", SQLType.VARCHAR(100), false);

View File

@ -26,7 +26,7 @@ public class SQLForumThread extends SQLElement<SQLForumThread> {
SQLForumForum.class); SQLForumForum.class);
public static final SQLField<SQLForumThread, String> titre = new SQLField<>("titre", SQLType.VARCHAR(60), false); public static final SQLField<SQLForumThread, String> titre = new SQLField<>("titre", SQLType.VARCHAR(60), false);
public static final SQLFKField<SQLForumThread, String, SQLPlayer> createur = new SQLFKField<>("createur", SQLType.CHAR(36), false, public static final SQLFKField<SQLForumThread, String, SQLPlayer> createur = new SQLFKField<>("createur", SQLType.CHAR(36), false,
SQLPlayer.playerId); SQLPlayer.class, SQLPlayer.playerId);
public static final SQLField<SQLForumThread, Integer> vu = new SQLField<>("vu", SQLType.INT, false); public static final SQLField<SQLForumThread, Integer> vu = new SQLField<>("vu", SQLType.INT, false);
public static final SQLField<SQLForumThread, Long> time = new SQLField<>("time", SQLType.BIGINT, false); public static final SQLField<SQLForumThread, Long> time = new SQLField<>("time", SQLType.BIGINT, false);
public static final SQLField<SQLForumThread, Boolean> anchored = new SQLField<>("anchored", SQLType.BOOLEAN, false); public static final SQLField<SQLForumThread, Boolean> anchored = new SQLField<>("anchored", SQLType.BOOLEAN, false);

View File

@ -24,13 +24,14 @@ public class SQLLoginHistory extends SQLElement<SQLLoginHistory> {
public static final SQLField<SQLLoginHistory, Long> time = new SQLField<>("time", SQLType.BIGINT, false); public static final SQLField<SQLLoginHistory, Long> time = new SQLField<>("time", SQLType.BIGINT, false);
public static final SQLFKField<SQLLoginHistory, String, SQLPlayer> playerId = new SQLFKField<>("playerId", SQLType.CHAR(36), false, public static final SQLFKField<SQLLoginHistory, String, SQLPlayer> playerId = new SQLFKField<>("playerId", SQLType.CHAR(36), false,
SQLPlayer.playerId); SQLPlayer.class, SQLPlayer.playerId);
public static final SQLField<SQLLoginHistory, String> ip = new SQLField<>("ip", SQLType.VARCHAR(128), true); public static final SQLField<SQLLoginHistory, String> ip = new SQLField<>("ip", SQLType.VARCHAR(128), true);
public static final SQLField<SQLLoginHistory, ActionType> actionType = new SQLField<>("actionType", SQLType.ENUM(ActionType.class), public static final SQLField<SQLLoginHistory, ActionType> actionType = new SQLField<>("actionType", SQLType.ENUM(ActionType.class),
false); false);
public static final SQLField<SQLLoginHistory, Integer> nbOnline = new SQLField<>("nbOnline", SQLType.INT, false); public static final SQLField<SQLLoginHistory, Integer> nbOnline = new SQLField<>("nbOnline", SQLType.INT, false);
public static final SQLField<SQLLoginHistory, String> playerName = new SQLField<>("playerName", SQLType.VARCHAR(16), true); public static final SQLField<SQLLoginHistory, String> playerName = new SQLField<>("playerName", SQLType.VARCHAR(16), true);
public static final SQLField<SQLLoginHistory, Integer> minecraftVersion = new SQLField<>("minecraftVersion", SQLType.INT, false, 0); public static final SQLField<SQLLoginHistory, Integer> minecraftVersion = new SQLField<>("minecraftVersion", SQLType.INT, false, 0);
public static final SQLField<SQLLoginHistory, String> hostName = new SQLField<>("hostName", SQLType.VARCHAR(128), true);
public UUID getPlayerId() { public UUID getPlayerId() {
String id = get(playerId); String id = get(playerId);

View File

@ -0,0 +1,42 @@
package fr.pandacube.java.util.db;
import java.util.UUID;
import fr.pandacube.java.util.db.sql_tools.SQLElement;
import fr.pandacube.java.util.db.sql_tools.SQLFKField;
import fr.pandacube.java.util.db.sql_tools.SQLField;
import fr.pandacube.java.util.db.sql_tools.SQLType;
public class SQLLoginKickHistory extends SQLElement<SQLLoginKickHistory> {
public SQLLoginKickHistory() {
super();
}
public SQLLoginKickHistory(int id) {
super(id);
}
@Override
protected String tableName() {
return "pandacube_loginkick_history";
}
public static final SQLField<SQLLoginKickHistory, Long> time = new SQLField<>("time", SQLType.BIGINT, false);
public static final SQLFKField<SQLLoginKickHistory, String, SQLPlayer> playerId = new SQLFKField<>("playerId", SQLType.CHAR(36), false,
SQLPlayer.class, SQLPlayer.playerId);
public static final SQLField<SQLLoginKickHistory, String> ip = new SQLField<>("ip", SQLType.VARCHAR(128), true);
public static final SQLField<SQLLoginKickHistory, String> playerName = new SQLField<>("playerName", SQLType.VARCHAR(16), true);
public static final SQLField<SQLLoginKickHistory, Integer> minecraftVersion = new SQLField<>("minecraftVersion", SQLType.INT, false, 0);
public static final SQLField<SQLLoginKickHistory, String> hostName = new SQLField<>("hostName", SQLType.VARCHAR(128), true);
public static final SQLField<SQLLoginKickHistory, String> kickReason = new SQLField<>("kickReason", SQLType.VARCHAR(512), true);
public UUID getPlayerId() {
String id = get(playerId);
return (id == null) ? null : UUID.fromString(id);
}
public void setPlayerId(UUID pName) {
set(playerId, (pName == null) ? (String) null : pName.toString());
}
}

View File

@ -30,7 +30,7 @@ public class SQLMPGroupUser extends SQLElement<SQLMPGroupUser> {
public static final SQLFKField<SQLMPGroupUser, Integer, SQLMPGroup> groupId = SQLFKField.idFK("groupId", SQLType.INT, false, public static final SQLFKField<SQLMPGroupUser, Integer, SQLMPGroup> groupId = SQLFKField.idFK("groupId", SQLType.INT, false,
SQLMPGroup.class); SQLMPGroup.class);
public static final SQLFKField<SQLMPGroupUser, String, SQLPlayer> playerId = new SQLFKField<>("playerId", SQLType.CHAR(36), false, public static final SQLFKField<SQLMPGroupUser, String, SQLPlayer> playerId = new SQLFKField<>("playerId", SQLType.CHAR(36), false,
SQLPlayer.playerId); SQLPlayer.class, SQLPlayer.playerId);
// TODO ajouter un champ qui dit si le joueur est admin du groupe // TODO ajouter un champ qui dit si le joueur est admin du groupe

View File

@ -37,11 +37,11 @@ public class SQLMPMessage extends SQLElement<SQLMPMessage> {
public static final SQLField<SQLMPMessage, Long> time = new SQLField<>("time", SQLType.BIGINT, false); public static final SQLField<SQLMPMessage, Long> time = new SQLField<>("time", SQLType.BIGINT, false);
public static final SQLField<SQLMPMessage, Integer> securityKey = new SQLField<>("securityKey", SQLType.INT, false); public static final SQLField<SQLMPMessage, Integer> securityKey = new SQLField<>("securityKey", SQLType.INT, false);
public static final SQLFKField<SQLMPMessage, String, SQLPlayer> viewerId = new SQLFKField<>("viewerId", SQLType.CHAR(36), false, public static final SQLFKField<SQLMPMessage, String, SQLPlayer> viewerId = new SQLFKField<>("viewerId", SQLType.CHAR(36), false,
SQLPlayer.playerId); SQLPlayer.class, SQLPlayer.playerId);
public static final SQLFKField<SQLMPMessage, String, SQLPlayer> sourceId = new SQLFKField<>("sourceId", SQLType.CHAR(36), true, public static final SQLFKField<SQLMPMessage, String, SQLPlayer> sourceId = new SQLFKField<>("sourceId", SQLType.CHAR(36), true,
SQLPlayer.playerId); SQLPlayer.class, SQLPlayer.playerId);
public static final SQLFKField<SQLMPMessage, String, SQLPlayer> destId = new SQLFKField<>("destId", SQLType.CHAR(36), true, public static final SQLFKField<SQLMPMessage, String, SQLPlayer> destId = new SQLFKField<>("destId", SQLType.CHAR(36), true,
SQLPlayer.playerId); SQLPlayer.class, SQLPlayer.playerId);
public static final SQLFKField<SQLMPMessage, Integer, SQLMPGroup> destGroup = SQLFKField.idFK("destGroup", SQLType.INT, true, public static final SQLFKField<SQLMPMessage, Integer, SQLMPGroup> destGroup = SQLFKField.idFK("destGroup", SQLType.INT, true,
SQLMPGroup.class); SQLMPGroup.class);
public static final SQLField<SQLMPMessage, String> message = new SQLField<>("message", SQLType.VARCHAR(512), false); public static final SQLField<SQLMPMessage, String> message = new SQLField<>("message", SQLType.VARCHAR(512), false);

View File

@ -23,14 +23,14 @@ public class SQLModoHistory extends SQLElement<SQLModoHistory> {
} }
public static final SQLFKField<SQLModoHistory, String, SQLPlayer> modoId = new SQLFKField<>("modoId", SQLType.CHAR(36), true, public static final SQLFKField<SQLModoHistory, String, SQLPlayer> modoId = new SQLFKField<>("modoId", SQLType.CHAR(36), true,
SQLPlayer.playerId); SQLPlayer.class, SQLPlayer.playerId);
public static final SQLField<SQLModoHistory, ActionType> actionType = new SQLField<>("actionType", SQLType.ENUM(ActionType.class), public static final SQLField<SQLModoHistory, ActionType> actionType = new SQLField<>("actionType", SQLType.ENUM(ActionType.class),
false); false);
public static final SQLField<SQLModoHistory, Long> time = new SQLField<>("time", SQLType.BIGINT, false); public static final SQLField<SQLModoHistory, Long> time = new SQLField<>("time", SQLType.BIGINT, false);
public static final SQLFKField<SQLModoHistory, String, SQLPlayer> playerId = new SQLFKField<>("playerId", SQLType.CHAR(36), false, public static final SQLFKField<SQLModoHistory, String, SQLPlayer> playerId = new SQLFKField<>("playerId", SQLType.CHAR(36), false,
SQLPlayer.playerId); SQLPlayer.class, SQLPlayer.playerId);
public static final SQLField<SQLModoHistory, Long> value = new SQLField<>("value", SQLType.BIGINT, true); public static final SQLField<SQLModoHistory, Long> value = new SQLField<>("value", SQLType.BIGINT, true);
public static final SQLField<SQLModoHistory, String> message = new SQLField<>("message", SQLType.VARCHAR(512), false); public static final SQLField<SQLModoHistory, String> message = new SQLField<>("message", SQLType.VARCHAR(2048), false);
public UUID getModoId() { public UUID getModoId() {
String id = get(modoId); String id = get(modoId);

View File

@ -27,12 +27,12 @@ public class SQLOnlineshopHistory extends SQLElement<SQLOnlineshopHistory> {
public static final SQLField<SQLOnlineshopHistory, SourceType> sourceType = new SQLField<>("sourceType", SQLType.ENUM(SourceType.class), public static final SQLField<SQLOnlineshopHistory, SourceType> sourceType = new SQLField<>("sourceType", SQLType.ENUM(SourceType.class),
false); false);
public static final SQLFKField<SQLOnlineshopHistory, String, SQLPlayer> sourcePlayerId = new SQLFKField<>("sourcePlayerId", public static final SQLFKField<SQLOnlineshopHistory, String, SQLPlayer> sourcePlayerId = new SQLFKField<>("sourcePlayerId",
SQLType.CHAR(36), true, SQLPlayer.playerId); SQLType.CHAR(36), true, SQLPlayer.class, SQLPlayer.playerId);
public static final SQLField<SQLOnlineshopHistory, Double> sourceQuantity = new SQLField<>("sourceQuantity", SQLType.DOUBLE, false); public static final SQLField<SQLOnlineshopHistory, Double> sourceQuantity = new SQLField<>("sourceQuantity", SQLType.DOUBLE, false);
public static final SQLField<SQLOnlineshopHistory, String> sourceName = new SQLField<>("sourceName", SQLType.VARCHAR(64), false); public static final SQLField<SQLOnlineshopHistory, String> sourceName = new SQLField<>("sourceName", SQLType.VARCHAR(64), false);
public static final SQLField<SQLOnlineshopHistory, DestType> destType = new SQLField<>("destType", SQLType.ENUM(DestType.class), false); public static final SQLField<SQLOnlineshopHistory, DestType> destType = new SQLField<>("destType", SQLType.ENUM(DestType.class), false);
public static final SQLFKField<SQLOnlineshopHistory, String, SQLPlayer> destPlayerId = new SQLFKField<>("destPlayerId", SQLType.CHAR(36), public static final SQLFKField<SQLOnlineshopHistory, String, SQLPlayer> destPlayerId = new SQLFKField<>("destPlayerId", SQLType.CHAR(36),
false, SQLPlayer.playerId); false, SQLPlayer.class, SQLPlayer.playerId);
public static final SQLField<SQLOnlineshopHistory, Double> destQuantity = new SQLField<>("destQuantity", SQLType.DOUBLE, false); public static final SQLField<SQLOnlineshopHistory, Double> destQuantity = new SQLField<>("destQuantity", SQLType.DOUBLE, false);
public static final SQLField<SQLOnlineshopHistory, String> destName = new SQLField<>("destName", SQLType.VARCHAR(64), false); public static final SQLField<SQLOnlineshopHistory, String> destName = new SQLField<>("destName", SQLType.VARCHAR(64), false);

View File

@ -0,0 +1,30 @@
package fr.pandacube.java.util.db;
import fr.pandacube.java.util.db.sql_tools.SQLElement;
import fr.pandacube.java.util.db.sql_tools.SQLField;
import fr.pandacube.java.util.db.sql_tools.SQLType;
public class SQLPingHistory extends SQLElement<SQLPingHistory> {
public SQLPingHistory() {
super();
}
public SQLPingHistory(int id) {
super(id);
}
@Override
protected String tableName() {
return "pandacube_ping_history";
}
public static final SQLField<SQLPingHistory, Long> time = new SQLField<>("time", SQLType.BIGINT, false);
public static final SQLField<SQLPingHistory, String> ip = new SQLField<>("ip", SQLType.VARCHAR(128), true);
public static final SQLField<SQLPingHistory, Integer> minecraftVersion = new SQLField<>("minecraftVersion", SQLType.INT, false, 0);
public static final SQLField<SQLPingHistory, String> hostName = new SQLField<>("hostName", SQLType.VARCHAR(128), true);
}

View File

@ -30,9 +30,9 @@ public class SQLPlayerIgnore extends SQLElement<SQLPlayerIgnore> {
} }
public static final SQLFKField<SQLPlayerIgnore, String, SQLPlayer> ignorer = new SQLFKField<>("ignorer", SQLType.CHAR(36), false, public static final SQLFKField<SQLPlayerIgnore, String, SQLPlayer> ignorer = new SQLFKField<>("ignorer", SQLType.CHAR(36), false,
SQLPlayer.playerId); SQLPlayer.class, SQLPlayer.playerId);
public static final SQLFKField<SQLPlayerIgnore, String, SQLPlayer> ignored = new SQLFKField<>("ignored", SQLType.CHAR(36), false, public static final SQLFKField<SQLPlayerIgnore, String, SQLPlayer> ignored = new SQLFKField<>("ignored", SQLType.CHAR(36), false,
SQLPlayer.playerId); SQLPlayer.class, SQLPlayer.playerId);
public UUID getIgnorerId() { public UUID getIgnorerId() {
String id = get(ignorer); String id = get(ignorer);

View File

@ -23,11 +23,11 @@ public class SQLStaffTicket extends SQLElement<SQLStaffTicket> {
} }
public static final SQLFKField<SQLStaffTicket, String, SQLPlayer> playerId = new SQLFKField<>("playerId", SQLType.CHAR(36), false, public static final SQLFKField<SQLStaffTicket, String, SQLPlayer> playerId = new SQLFKField<>("playerId", SQLType.CHAR(36), false,
SQLPlayer.playerId); SQLPlayer.class, SQLPlayer.playerId);
public static final SQLField<SQLStaffTicket, String> message = new SQLField<>("message", SQLType.VARCHAR(1024), false); public static final SQLField<SQLStaffTicket, String> message = new SQLField<>("message", SQLType.VARCHAR(1024), false);
public static final SQLField<SQLStaffTicket, Long> creationTime = new SQLField<>("creationTime", SQLType.BIGINT, false); public static final SQLField<SQLStaffTicket, Long> creationTime = new SQLField<>("creationTime", SQLType.BIGINT, false);
public static final SQLFKField<SQLStaffTicket, String, SQLPlayer> staffPlayerId = new SQLFKField<>("staffPlayerId", public static final SQLFKField<SQLStaffTicket, String, SQLPlayer> staffPlayerId = new SQLFKField<>("staffPlayerId",
SQLType.CHAR(36), true, SQLPlayer.playerId); SQLType.CHAR(36), true, SQLPlayer.class, SQLPlayer.playerId);
public UUID getPlayerId() { public UUID getPlayerId() {
String id = get(playerId); String id = get(playerId);

View File

@ -23,7 +23,7 @@ public class SQLUUIDPlayer extends SQLElement<SQLUUIDPlayer> {
} }
public static final SQLFKField<SQLUUIDPlayer, String, SQLPlayer> uuid = new SQLFKField<>("uuid", SQLType.CHAR(36), false, public static final SQLFKField<SQLUUIDPlayer, String, SQLPlayer> uuid = new SQLFKField<>("uuid", SQLType.CHAR(36), false,
SQLPlayer.playerId); SQLPlayer.class, SQLPlayer.playerId);
public static final SQLField<SQLUUIDPlayer, String> player = new SQLField<>("player", SQLType.VARCHAR(16), false); public static final SQLField<SQLUUIDPlayer, String> player = new SQLField<>("player", SQLType.VARCHAR(16), false);
public UUID getUUID() { public UUID getUUID() {

View File

@ -14,7 +14,13 @@ public class DBConnection {
public DBConnection(String host, int port, String dbname, String l, String p) public DBConnection(String host, int port, String dbname, String l, String p)
throws ClassNotFoundException, SQLException { throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver"); Class.forName("com.mysql.jdbc.Driver");
url = "jdbc:mysql://" + host + ":" + port + "/" + dbname; url = "jdbc:mysql://" + host + ":" + port + "/" + dbname
+ "?autoReconnect=true"
+ "&useUnicode=true"
+ "&characterEncoding=utf8"
+ "&characterSetResults=utf8"
+ "&character_set_server=utf8mb4"
+ "&character_set_connection=utf8mb4";
login = l; login = l;
pass = p; pass = p;
connect(); connect();

View File

@ -16,11 +16,13 @@ import fr.pandacube.java.util.db.SQLForumForum;
import fr.pandacube.java.util.db.SQLForumPost; import fr.pandacube.java.util.db.SQLForumPost;
import fr.pandacube.java.util.db.SQLForumThread; import fr.pandacube.java.util.db.SQLForumThread;
import fr.pandacube.java.util.db.SQLLoginHistory; import fr.pandacube.java.util.db.SQLLoginHistory;
import fr.pandacube.java.util.db.SQLLoginKickHistory;
import fr.pandacube.java.util.db.SQLMPGroup; import fr.pandacube.java.util.db.SQLMPGroup;
import fr.pandacube.java.util.db.SQLMPGroupUser; import fr.pandacube.java.util.db.SQLMPGroupUser;
import fr.pandacube.java.util.db.SQLMPMessage; import fr.pandacube.java.util.db.SQLMPMessage;
import fr.pandacube.java.util.db.SQLModoHistory; import fr.pandacube.java.util.db.SQLModoHistory;
import fr.pandacube.java.util.db.SQLOnlineshopHistory; import fr.pandacube.java.util.db.SQLOnlineshopHistory;
import fr.pandacube.java.util.db.SQLPingHistory;
import fr.pandacube.java.util.db.SQLPlayer; import fr.pandacube.java.util.db.SQLPlayer;
import fr.pandacube.java.util.db.SQLPlayerIgnore; import fr.pandacube.java.util.db.SQLPlayerIgnore;
import fr.pandacube.java.util.db.SQLShopStock; import fr.pandacube.java.util.db.SQLShopStock;
@ -64,11 +66,13 @@ public final class ORM {
initTable(SQLForumPost.class); initTable(SQLForumPost.class);
initTable(SQLForumThread.class); initTable(SQLForumThread.class);
initTable(SQLLoginHistory.class); initTable(SQLLoginHistory.class);
initTable(SQLLoginKickHistory.class);
initTable(SQLModoHistory.class); initTable(SQLModoHistory.class);
initTable(SQLMPGroup.class); initTable(SQLMPGroup.class);
initTable(SQLMPGroupUser.class); initTable(SQLMPGroupUser.class);
initTable(SQLMPMessage.class); initTable(SQLMPMessage.class);
initTable(SQLOnlineshopHistory.class); initTable(SQLOnlineshopHistory.class);
initTable(SQLPingHistory.class);
initTable(SQLPlayer.class); initTable(SQLPlayer.class);
initTable(SQLPlayerIgnore.class); initTable(SQLPlayerIgnore.class);
initTable(SQLShopStock.class); initTable(SQLShopStock.class);
@ -81,14 +85,16 @@ public final class ORM {
} }
/* package */ static <E extends SQLElement<E>> void initTable(Class<E> elemClass) throws ORMInitTableException { /* package */ static synchronized <E extends SQLElement<E>> void initTable(Class<E> elemClass) throws ORMInitTableException {
if (tables.contains(elemClass)) return; if (tables.contains(elemClass)) return;
try { try {
tables.add(elemClass);
Log.info("Start initializing SQL table "+elemClass.getName());
E instance = elemClass.newInstance(); E instance = elemClass.newInstance();
String tableName = instance.tableName(); String tableName = instance.tableName();
if (!tableExist(tableName)) createTable(instance); if (!tableExist(tableName)) createTable(instance);
tables.add(elemClass); Log.info("End of initializing SQL table "+elemClass.getName());
} catch (Exception e) { } catch (Exception|ExceptionInInitializerError e) {
throw new ORMInitTableException(elemClass, e); throw new ORMInitTableException(elemClass, e);
} }
} }

View File

@ -4,11 +4,11 @@ public class ORMInitTableException extends ORMException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/* package */ <E extends SQLElement<E>> ORMInitTableException(Class<E> tableElem) { /* package */ <E extends SQLElement<E>> ORMInitTableException(Class<E> tableElem) {
super("Error while initializing table " + tableElem.getName()); super("Error while initializing table " + ((tableElem != null) ? tableElem.getName() : "null"));
} }
/* package */ <E extends SQLElement<E>> ORMInitTableException(Class<E> tableElem, Throwable t) { /* package */ <E extends SQLElement<E>> ORMInitTableException(Class<E> tableElem, Throwable t) {
super("Error while initializing table " + tableElem.getName(), t); super("Error while initializing table " + ((tableElem != null) ? tableElem.getName() : "null"), t);
} }
} }

View File

@ -14,7 +14,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.logging.Level;
import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringBuilder;
@ -40,6 +39,12 @@ public abstract class SQLElement<E extends SQLElement<E>> {
public SQLElement() { public SQLElement() {
tableName = tableName(); tableName = tableName();
try {
ORM.initTable((Class<E>)getClass());
} catch (ORMInitTableException e) {
throw new RuntimeException(e);
}
if (fieldsCache.get(getClass()) == null) { if (fieldsCache.get(getClass()) == null) {
fields = new SQLFieldMap<>((Class<E>)getClass()); fields = new SQLFieldMap<>((Class<E>)getClass());
@ -95,7 +100,7 @@ public abstract class SQLElement<E extends SQLElement<E>> {
listToFill.addField((SQLField<?, ?>) val); listToFill.addField((SQLField<?, ?>) val);
} catch (IllegalArgumentException | IllegalAccessException e) { } catch (IllegalArgumentException | IllegalAccessException e) {
Log.getLogger().log(Level.SEVERE, "Can't get value of static field " + field.toString(), e); Log.severe("Can't get value of static field " + field.toString(), e);
} }
} }
@ -116,7 +121,7 @@ public abstract class SQLElement<E extends SQLElement<E>> {
/* package */ <T> void set(SQLField<E, T> sqlField, T value, boolean setModified) { /* package */ <T> void set(SQLField<E, T> sqlField, T value, boolean setModified) {
if (sqlField == null) throw new IllegalArgumentException("sqlField can't be null"); if (sqlField == null) throw new IllegalArgumentException("sqlField can't be null");
if (!fields.containsValue(sqlField)) if (!fields.containsValue(sqlField))
throw new IllegalArgumentException(sqlField.name + " is not a SQLField of " + getClass().getName()); throw new IllegalArgumentException(sqlField.getSQLElementType().getName()+sqlField.name + " is not a SQLField of " + getClass().getName());
boolean modify = false; boolean modify = false;
if (value == null) { if (value == null) {
@ -352,7 +357,6 @@ public abstract class SQLElement<E extends SQLElement<E>> {
private void addField(SQLField<?, ?> f) { private void addField(SQLField<?, ?> f) {
if (f == null) return; if (f == null) return;
if (!sqlElemClass.equals(f.getSQLElementType())) return;
if (containsKey(f.name)) throw new IllegalArgumentException( if (containsKey(f.name)) throw new IllegalArgumentException(
"SQLField " + f.name + " already exist in " + sqlElemClass.getName()); "SQLField " + f.name + " already exist in " + sqlElemClass.getName());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -158,7 +158,7 @@ public class SQLElementList<E extends SQLElement<E>> extends ArrayList<E> {
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); Log.severe(e);
} }
} }

View File

@ -7,21 +7,21 @@ public class SQLFKField<E extends SQLElement<E>, T, F extends SQLElement<F>> ext
private SQLField<F, T> sqlForeignKeyField; private SQLField<F, T> sqlForeignKeyField;
private Class<F> sqlForeignKeyElemClass; private Class<F> sqlForeignKeyElemClass;
public SQLFKField(String n, SQLType<T> t, boolean nul, SQLField<F, T> fkF) { public SQLFKField(String n, SQLType<T> t, boolean nul, Class<F> fkEl, SQLField<F, T> fkF) {
super(n, t, nul); super(n, t, nul);
construct(fkF); construct(fkEl, fkF);
} }
public SQLFKField(String n, SQLType<T> t, boolean nul, T deflt, SQLField<F, T> fkF) { public SQLFKField(String n, SQLType<T> t, boolean nul, T deflt, Class<F> fkEl, SQLField<F, T> fkF) {
super(n, t, nul, deflt); super(n, t, nul, deflt);
construct(fkF); construct(fkEl, fkF);
} }
public static <E extends SQLElement<E>, F extends SQLElement<F>> SQLFKField<E, Integer, F> idFK(String n, SQLType<Integer> t, boolean nul, public static <E extends SQLElement<E>, F extends SQLElement<F>> SQLFKField<E, Integer, F> idFK(String n, SQLType<Integer> t, boolean nul,
Class<F> fkEl) { Class<F> fkEl) {
if (fkEl == null) throw new IllegalArgumentException("foreignKeyElement can't be null"); if (fkEl == null) throw new IllegalArgumentException("foreignKeyElement can't be null");
try { try {
return new SQLFKField<>(n, t, nul, ORM.getSQLIdField(fkEl)); return new SQLFKField<>(n, t, nul, fkEl, ORM.getSQLIdField(fkEl));
} catch (ORMInitTableException e) { } catch (ORMInitTableException e) {
Log.severe("Can't create Foreign key Field called '" + n + "'", e); Log.severe("Can't create Foreign key Field called '" + n + "'", e);
return null; return null;
@ -32,21 +32,19 @@ public class SQLFKField<E extends SQLElement<E>, T, F extends SQLElement<F>> ext
Integer deflt, Class<F> fkEl) { Integer deflt, Class<F> fkEl) {
if (fkEl == null) throw new IllegalArgumentException("foreignKeyElement can't be null"); if (fkEl == null) throw new IllegalArgumentException("foreignKeyElement can't be null");
try { try {
return new SQLFKField<>(n, t, nul, deflt, ORM.getSQLIdField(fkEl)); return new SQLFKField<>(n, t, nul, deflt, fkEl, ORM.getSQLIdField(fkEl));
} catch (ORMInitTableException e) { } catch (ORMInitTableException e) {
Log.severe("Can't create Foreign key Field called '" + n + "'", e); Log.severe("Can't create Foreign key Field called '" + n + "'", e);
return null; return null;
} }
} }
private void construct(SQLField<F, T> fkF) { private void construct(Class<F> fkEl, SQLField<F, T> fkF) {
if (fkF == null) throw new IllegalArgumentException("foreignKeyField can't be null"); if (fkF == null) throw new IllegalArgumentException("foreignKeyField can't be null");
Class<F> fkEl = fkF.getSQLElementType();
try { try {
ORM.initTable(fkEl); ORM.initTable(fkEl);
} catch (ORMInitTableException e) { } catch (ORMInitTableException e) {
Log.severe(e); throw new RuntimeException(e);
return;
} }
if (!fkEl.equals(fkF.getSQLElementType())) if (!fkEl.equals(fkF.getSQLElementType()))
throw new IllegalArgumentException("foreignKeyField must be from supplied foreignKeyElement"); throw new IllegalArgumentException("foreignKeyField must be from supplied foreignKeyElement");

View File

@ -5,6 +5,8 @@ import java.io.PrintStream;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
import fr.pandacube.java.util.Log;
public abstract class AbstractRequestExecutor { public abstract class AbstractRequestExecutor {
public final String command; public final String command;
@ -25,7 +27,7 @@ public abstract class AbstractRequestExecutor {
} catch (Exception e) { } catch (Exception e) {
new Response(false, e.toString()).sendPacket(new PrintStream(socket.getOutputStream())); new Response(false, e.toString()).sendPacket(new PrintStream(socket.getOutputStream()));
e.printStackTrace(); Log.severe(e);
} }
} }

View File

@ -42,10 +42,10 @@ public class PacketExecutor implements Runnable {
try { try {
rep.sendPacket(new PrintStream(socket.getOutputStream())); rep.sendPacket(new PrintStream(socket.getOutputStream()));
} catch (IOException e1) {} } catch (IOException e1) {}
if (e instanceof IOException) Log.getLogger() if (e instanceof IOException)
.warning("Impossible de lire le packet reçu sur le socket " + socket + " : " + e.toString()); Log.warning("Impossible de lire le packet reçu sur le socket " + socket + " : " + e.toString());
else else
e.printStackTrace(); Log.severe(e);
} }
try { try {