From 55748b0d5e26cbd98a30bdbe629ae6f27b29caed Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Tue, 26 Jul 2016 02:06:21 +0200 Subject: [PATCH] Bugs + nettoyage code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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. --- .../java/util/ServerPropertyFile.java | 4 +- .../java/util/config/AbstractConfig.java | 133 ++++++++++++++++++ .../util/config/AbstractConfigManager.java | 39 +++++ src/fr/pandacube/java/util/db/SQLContact.java | 2 +- .../java/util/db/SQLForumThread.java | 2 +- .../java/util/db/SQLLoginHistory.java | 3 +- .../java/util/db/SQLLoginKickHistory.java | 42 ++++++ .../java/util/db/SQLMPGroupUser.java | 2 +- .../pandacube/java/util/db/SQLMPMessage.java | 6 +- .../java/util/db/SQLModoHistory.java | 6 +- .../java/util/db/SQLOnlineshopHistory.java | 4 +- .../java/util/db/SQLPingHistory.java | 30 ++++ .../java/util/db/SQLPlayerIgnore.java | 4 +- .../java/util/db/SQLStaffTicket.java | 4 +- .../pandacube/java/util/db/SQLUUIDPlayer.java | 2 +- .../java/util/db/sql_tools/DBConnection.java | 8 +- .../pandacube/java/util/db/sql_tools/ORM.java | 12 +- .../db/sql_tools/ORMInitTableException.java | 4 +- .../java/util/db/sql_tools/SQLElement.java | 12 +- .../util/db/sql_tools/SQLElementList.java | 2 +- .../java/util/db/sql_tools/SQLFKField.java | 18 ++- .../server/AbstractRequestExecutor.java | 4 +- .../network_api/server/PacketExecutor.java | 6 +- 23 files changed, 305 insertions(+), 44 deletions(-) create mode 100644 src/fr/pandacube/java/util/config/AbstractConfig.java create mode 100644 src/fr/pandacube/java/util/config/AbstractConfigManager.java create mode 100644 src/fr/pandacube/java/util/db/SQLLoginKickHistory.java create mode 100644 src/fr/pandacube/java/util/db/SQLPingHistory.java diff --git a/src/fr/pandacube/java/util/ServerPropertyFile.java b/src/fr/pandacube/java/util/ServerPropertyFile.java index 613875e..128f464 100644 --- a/src/fr/pandacube/java/util/ServerPropertyFile.java +++ b/src/fr/pandacube/java/util/ServerPropertyFile.java @@ -60,7 +60,7 @@ public class ServerPropertyFile { return true; } catch (IOException e) { - e.printStackTrace(); + Log.severe(e); } finally { try { in.close(); @@ -82,7 +82,7 @@ public class ServerPropertyFile { return true; } catch (IOException e) { - e.printStackTrace(); + Log.severe(e); } finally { try { out.close(); diff --git a/src/fr/pandacube/java/util/config/AbstractConfig.java b/src/fr/pandacube/java/util/config/AbstractConfig.java new file mode 100644 index 0000000..4684cd9 --- /dev/null +++ b/src/fr/pandacube/java/util/config/AbstractConfig.java @@ -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 true si il s'agit d'un dossier, false 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 true si on doit ignorer les lignes vides + * @param ignoreHashtagComment true si on doit ignorer les lignes commentés (commençant par un #) + * @param trimOutput true 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 getFileLines(boolean ignoreEmpty, boolean ignoreHashtagComment, boolean trimOutput, File f) throws IOException { + if (!f.isFile()) + return null; + + BufferedReader reader = new BufferedReader(new FileReader(f)); + + List lines = new ArrayList(); + + 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 true si on doit ignorer les lignes vides + * @param ignoreHashtagComment true si on doit ignorer les lignes commentés (commençant par un #) + * @param trimOutput true si on doit appeller la méthode String.trim() sur chaque ligne retournée + * @return la liste des lignes utiles + * @throws IOException + */ + protected List getFileLines(boolean ignoreEmpty, boolean ignoreHashtagComment, boolean trimOutput) throws IOException { + return getFileLines(ignoreEmpty, ignoreHashtagComment, trimOutput, configFile); + } + + + + protected List 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 null si le paramètre est nulle ou si perms.equals("*"), ou alors la chaine splittée. + */ + public static List 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 + } + +} diff --git a/src/fr/pandacube/java/util/config/AbstractConfigManager.java b/src/fr/pandacube/java/util/config/AbstractConfigManager.java new file mode 100644 index 0000000..7713301 --- /dev/null +++ b/src/fr/pandacube/java/util/config/AbstractConfigManager.java @@ -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(); + } + + +} diff --git a/src/fr/pandacube/java/util/db/SQLContact.java b/src/fr/pandacube/java/util/db/SQLContact.java index d543527..c95b558 100644 --- a/src/fr/pandacube/java/util/db/SQLContact.java +++ b/src/fr/pandacube/java/util/db/SQLContact.java @@ -24,7 +24,7 @@ public class SQLContact extends SQLElement { public static final SQLField time = new SQLField<>("time", SQLType.INT, false); public static final SQLFKField playerId = new SQLFKField<>("playerId", SQLType.CHAR(36), true, - SQLPlayer.playerId); + SQLPlayer.class, SQLPlayer.playerId); public static final SQLField userName = new SQLField<>("userName", SQLType.VARCHAR(50), true); public static final SQLField userMail = new SQLField<>("userMail", SQLType.VARCHAR(50), true); public static final SQLField titre = new SQLField<>("titre", SQLType.VARCHAR(100), false); diff --git a/src/fr/pandacube/java/util/db/SQLForumThread.java b/src/fr/pandacube/java/util/db/SQLForumThread.java index 31c7fbb..f39dc17 100644 --- a/src/fr/pandacube/java/util/db/SQLForumThread.java +++ b/src/fr/pandacube/java/util/db/SQLForumThread.java @@ -26,7 +26,7 @@ public class SQLForumThread extends SQLElement { SQLForumForum.class); public static final SQLField titre = new SQLField<>("titre", SQLType.VARCHAR(60), false); public static final SQLFKField createur = new SQLFKField<>("createur", SQLType.CHAR(36), false, - SQLPlayer.playerId); + SQLPlayer.class, SQLPlayer.playerId); public static final SQLField vu = new SQLField<>("vu", SQLType.INT, false); public static final SQLField time = new SQLField<>("time", SQLType.BIGINT, false); public static final SQLField anchored = new SQLField<>("anchored", SQLType.BOOLEAN, false); diff --git a/src/fr/pandacube/java/util/db/SQLLoginHistory.java b/src/fr/pandacube/java/util/db/SQLLoginHistory.java index 0da2ea9..7abd3d8 100644 --- a/src/fr/pandacube/java/util/db/SQLLoginHistory.java +++ b/src/fr/pandacube/java/util/db/SQLLoginHistory.java @@ -24,13 +24,14 @@ public class SQLLoginHistory extends SQLElement { public static final SQLField time = new SQLField<>("time", SQLType.BIGINT, false); public static final SQLFKField playerId = new SQLFKField<>("playerId", SQLType.CHAR(36), false, - SQLPlayer.playerId); + SQLPlayer.class, SQLPlayer.playerId); public static final SQLField ip = new SQLField<>("ip", SQLType.VARCHAR(128), true); public static final SQLField actionType = new SQLField<>("actionType", SQLType.ENUM(ActionType.class), false); public static final SQLField nbOnline = new SQLField<>("nbOnline", SQLType.INT, false); public static final SQLField playerName = new SQLField<>("playerName", SQLType.VARCHAR(16), true); public static final SQLField minecraftVersion = new SQLField<>("minecraftVersion", SQLType.INT, false, 0); + public static final SQLField hostName = new SQLField<>("hostName", SQLType.VARCHAR(128), true); public UUID getPlayerId() { String id = get(playerId); diff --git a/src/fr/pandacube/java/util/db/SQLLoginKickHistory.java b/src/fr/pandacube/java/util/db/SQLLoginKickHistory.java new file mode 100644 index 0000000..068caf0 --- /dev/null +++ b/src/fr/pandacube/java/util/db/SQLLoginKickHistory.java @@ -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 { + + public SQLLoginKickHistory() { + super(); + } + + public SQLLoginKickHistory(int id) { + super(id); + } + + @Override + protected String tableName() { + return "pandacube_loginkick_history"; + } + + public static final SQLField time = new SQLField<>("time", SQLType.BIGINT, false); + public static final SQLFKField playerId = new SQLFKField<>("playerId", SQLType.CHAR(36), false, + SQLPlayer.class, SQLPlayer.playerId); + public static final SQLField ip = new SQLField<>("ip", SQLType.VARCHAR(128), true); + public static final SQLField playerName = new SQLField<>("playerName", SQLType.VARCHAR(16), true); + public static final SQLField minecraftVersion = new SQLField<>("minecraftVersion", SQLType.INT, false, 0); + public static final SQLField hostName = new SQLField<>("hostName", SQLType.VARCHAR(128), true); + public static final SQLField 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()); + } +} diff --git a/src/fr/pandacube/java/util/db/SQLMPGroupUser.java b/src/fr/pandacube/java/util/db/SQLMPGroupUser.java index 46e7a67..bfaefd1 100644 --- a/src/fr/pandacube/java/util/db/SQLMPGroupUser.java +++ b/src/fr/pandacube/java/util/db/SQLMPGroupUser.java @@ -30,7 +30,7 @@ public class SQLMPGroupUser extends SQLElement { public static final SQLFKField groupId = SQLFKField.idFK("groupId", SQLType.INT, false, SQLMPGroup.class); public static final SQLFKField 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 diff --git a/src/fr/pandacube/java/util/db/SQLMPMessage.java b/src/fr/pandacube/java/util/db/SQLMPMessage.java index 2916810..2fce2e9 100644 --- a/src/fr/pandacube/java/util/db/SQLMPMessage.java +++ b/src/fr/pandacube/java/util/db/SQLMPMessage.java @@ -37,11 +37,11 @@ public class SQLMPMessage extends SQLElement { public static final SQLField time = new SQLField<>("time", SQLType.BIGINT, false); public static final SQLField securityKey = new SQLField<>("securityKey", SQLType.INT, false); public static final SQLFKField viewerId = new SQLFKField<>("viewerId", SQLType.CHAR(36), false, - SQLPlayer.playerId); + SQLPlayer.class, SQLPlayer.playerId); public static final SQLFKField sourceId = new SQLFKField<>("sourceId", SQLType.CHAR(36), true, - SQLPlayer.playerId); + SQLPlayer.class, SQLPlayer.playerId); public static final SQLFKField destId = new SQLFKField<>("destId", SQLType.CHAR(36), true, - SQLPlayer.playerId); + SQLPlayer.class, SQLPlayer.playerId); public static final SQLFKField destGroup = SQLFKField.idFK("destGroup", SQLType.INT, true, SQLMPGroup.class); public static final SQLField message = new SQLField<>("message", SQLType.VARCHAR(512), false); diff --git a/src/fr/pandacube/java/util/db/SQLModoHistory.java b/src/fr/pandacube/java/util/db/SQLModoHistory.java index 328f834..de47b68 100644 --- a/src/fr/pandacube/java/util/db/SQLModoHistory.java +++ b/src/fr/pandacube/java/util/db/SQLModoHistory.java @@ -23,14 +23,14 @@ public class SQLModoHistory extends SQLElement { } public static final SQLFKField modoId = new SQLFKField<>("modoId", SQLType.CHAR(36), true, - SQLPlayer.playerId); + SQLPlayer.class, SQLPlayer.playerId); public static final SQLField actionType = new SQLField<>("actionType", SQLType.ENUM(ActionType.class), false); public static final SQLField time = new SQLField<>("time", SQLType.BIGINT, false); public static final SQLFKField playerId = new SQLFKField<>("playerId", SQLType.CHAR(36), false, - SQLPlayer.playerId); + SQLPlayer.class, SQLPlayer.playerId); public static final SQLField value = new SQLField<>("value", SQLType.BIGINT, true); - public static final SQLField message = new SQLField<>("message", SQLType.VARCHAR(512), false); + public static final SQLField message = new SQLField<>("message", SQLType.VARCHAR(2048), false); public UUID getModoId() { String id = get(modoId); diff --git a/src/fr/pandacube/java/util/db/SQLOnlineshopHistory.java b/src/fr/pandacube/java/util/db/SQLOnlineshopHistory.java index 16b5b29..595ae8d 100644 --- a/src/fr/pandacube/java/util/db/SQLOnlineshopHistory.java +++ b/src/fr/pandacube/java/util/db/SQLOnlineshopHistory.java @@ -27,12 +27,12 @@ public class SQLOnlineshopHistory extends SQLElement { public static final SQLField sourceType = new SQLField<>("sourceType", SQLType.ENUM(SourceType.class), false); public static final SQLFKField sourcePlayerId = new SQLFKField<>("sourcePlayerId", - SQLType.CHAR(36), true, SQLPlayer.playerId); + SQLType.CHAR(36), true, SQLPlayer.class, SQLPlayer.playerId); public static final SQLField sourceQuantity = new SQLField<>("sourceQuantity", SQLType.DOUBLE, false); public static final SQLField sourceName = new SQLField<>("sourceName", SQLType.VARCHAR(64), false); public static final SQLField destType = new SQLField<>("destType", SQLType.ENUM(DestType.class), false); public static final SQLFKField destPlayerId = new SQLFKField<>("destPlayerId", SQLType.CHAR(36), - false, SQLPlayer.playerId); + false, SQLPlayer.class, SQLPlayer.playerId); public static final SQLField destQuantity = new SQLField<>("destQuantity", SQLType.DOUBLE, false); public static final SQLField destName = new SQLField<>("destName", SQLType.VARCHAR(64), false); diff --git a/src/fr/pandacube/java/util/db/SQLPingHistory.java b/src/fr/pandacube/java/util/db/SQLPingHistory.java new file mode 100644 index 0000000..8f918f8 --- /dev/null +++ b/src/fr/pandacube/java/util/db/SQLPingHistory.java @@ -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 { + + public SQLPingHistory() { + super(); + } + + public SQLPingHistory(int id) { + super(id); + } + + @Override + protected String tableName() { + return "pandacube_ping_history"; + } + + + + public static final SQLField time = new SQLField<>("time", SQLType.BIGINT, false); + public static final SQLField ip = new SQLField<>("ip", SQLType.VARCHAR(128), true); + public static final SQLField minecraftVersion = new SQLField<>("minecraftVersion", SQLType.INT, false, 0); + public static final SQLField hostName = new SQLField<>("hostName", SQLType.VARCHAR(128), true); + + +} diff --git a/src/fr/pandacube/java/util/db/SQLPlayerIgnore.java b/src/fr/pandacube/java/util/db/SQLPlayerIgnore.java index ed6f135..2c76b67 100644 --- a/src/fr/pandacube/java/util/db/SQLPlayerIgnore.java +++ b/src/fr/pandacube/java/util/db/SQLPlayerIgnore.java @@ -30,9 +30,9 @@ public class SQLPlayerIgnore extends SQLElement { } public static final SQLFKField ignorer = new SQLFKField<>("ignorer", SQLType.CHAR(36), false, - SQLPlayer.playerId); + SQLPlayer.class, SQLPlayer.playerId); public static final SQLFKField ignored = new SQLFKField<>("ignored", SQLType.CHAR(36), false, - SQLPlayer.playerId); + SQLPlayer.class, SQLPlayer.playerId); public UUID getIgnorerId() { String id = get(ignorer); diff --git a/src/fr/pandacube/java/util/db/SQLStaffTicket.java b/src/fr/pandacube/java/util/db/SQLStaffTicket.java index f55ae0d..02b7666 100644 --- a/src/fr/pandacube/java/util/db/SQLStaffTicket.java +++ b/src/fr/pandacube/java/util/db/SQLStaffTicket.java @@ -23,11 +23,11 @@ public class SQLStaffTicket extends SQLElement { } public static final SQLFKField playerId = new SQLFKField<>("playerId", SQLType.CHAR(36), false, - SQLPlayer.playerId); + SQLPlayer.class, SQLPlayer.playerId); public static final SQLField message = new SQLField<>("message", SQLType.VARCHAR(1024), false); public static final SQLField creationTime = new SQLField<>("creationTime", SQLType.BIGINT, false); public static final SQLFKField staffPlayerId = new SQLFKField<>("staffPlayerId", - SQLType.CHAR(36), true, SQLPlayer.playerId); + SQLType.CHAR(36), true, SQLPlayer.class, SQLPlayer.playerId); public UUID getPlayerId() { String id = get(playerId); diff --git a/src/fr/pandacube/java/util/db/SQLUUIDPlayer.java b/src/fr/pandacube/java/util/db/SQLUUIDPlayer.java index d43ee7a..9573996 100644 --- a/src/fr/pandacube/java/util/db/SQLUUIDPlayer.java +++ b/src/fr/pandacube/java/util/db/SQLUUIDPlayer.java @@ -23,7 +23,7 @@ public class SQLUUIDPlayer extends SQLElement { } public static final SQLFKField uuid = new SQLFKField<>("uuid", SQLType.CHAR(36), false, - SQLPlayer.playerId); + SQLPlayer.class, SQLPlayer.playerId); public static final SQLField player = new SQLField<>("player", SQLType.VARCHAR(16), false); public UUID getUUID() { diff --git a/src/fr/pandacube/java/util/db/sql_tools/DBConnection.java b/src/fr/pandacube/java/util/db/sql_tools/DBConnection.java index 4368bd3..ecd66b1 100644 --- a/src/fr/pandacube/java/util/db/sql_tools/DBConnection.java +++ b/src/fr/pandacube/java/util/db/sql_tools/DBConnection.java @@ -14,7 +14,13 @@ public class DBConnection { public DBConnection(String host, int port, String dbname, String l, String p) throws ClassNotFoundException, SQLException { 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; pass = p; connect(); diff --git a/src/fr/pandacube/java/util/db/sql_tools/ORM.java b/src/fr/pandacube/java/util/db/sql_tools/ORM.java index babc396..85e0b04 100644 --- a/src/fr/pandacube/java/util/db/sql_tools/ORM.java +++ b/src/fr/pandacube/java/util/db/sql_tools/ORM.java @@ -16,11 +16,13 @@ import fr.pandacube.java.util.db.SQLForumForum; import fr.pandacube.java.util.db.SQLForumPost; import fr.pandacube.java.util.db.SQLForumThread; 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.SQLMPGroupUser; import fr.pandacube.java.util.db.SQLMPMessage; import fr.pandacube.java.util.db.SQLModoHistory; 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.SQLPlayerIgnore; import fr.pandacube.java.util.db.SQLShopStock; @@ -64,11 +66,13 @@ public final class ORM { initTable(SQLForumPost.class); initTable(SQLForumThread.class); initTable(SQLLoginHistory.class); + initTable(SQLLoginKickHistory.class); initTable(SQLModoHistory.class); initTable(SQLMPGroup.class); initTable(SQLMPGroupUser.class); initTable(SQLMPMessage.class); initTable(SQLOnlineshopHistory.class); + initTable(SQLPingHistory.class); initTable(SQLPlayer.class); initTable(SQLPlayerIgnore.class); initTable(SQLShopStock.class); @@ -81,14 +85,16 @@ public final class ORM { } - /* package */ static > void initTable(Class elemClass) throws ORMInitTableException { + /* package */ static synchronized > void initTable(Class elemClass) throws ORMInitTableException { if (tables.contains(elemClass)) return; try { + tables.add(elemClass); + Log.info("Start initializing SQL table "+elemClass.getName()); E instance = elemClass.newInstance(); String tableName = instance.tableName(); if (!tableExist(tableName)) createTable(instance); - tables.add(elemClass); - } catch (Exception e) { + Log.info("End of initializing SQL table "+elemClass.getName()); + } catch (Exception|ExceptionInInitializerError e) { throw new ORMInitTableException(elemClass, e); } } diff --git a/src/fr/pandacube/java/util/db/sql_tools/ORMInitTableException.java b/src/fr/pandacube/java/util/db/sql_tools/ORMInitTableException.java index 30a9165..82dd681 100644 --- a/src/fr/pandacube/java/util/db/sql_tools/ORMInitTableException.java +++ b/src/fr/pandacube/java/util/db/sql_tools/ORMInitTableException.java @@ -4,11 +4,11 @@ public class ORMInitTableException extends ORMException { private static final long serialVersionUID = 1L; /* package */ > ORMInitTableException(Class tableElem) { - super("Error while initializing table " + tableElem.getName()); + super("Error while initializing table " + ((tableElem != null) ? tableElem.getName() : "null")); } /* package */ > ORMInitTableException(Class tableElem, Throwable t) { - super("Error while initializing table " + tableElem.getName(), t); + super("Error while initializing table " + ((tableElem != null) ? tableElem.getName() : "null"), t); } } diff --git a/src/fr/pandacube/java/util/db/sql_tools/SQLElement.java b/src/fr/pandacube/java/util/db/sql_tools/SQLElement.java index f5b20c4..dfbe6d6 100644 --- a/src/fr/pandacube/java/util/db/sql_tools/SQLElement.java +++ b/src/fr/pandacube/java/util/db/sql_tools/SQLElement.java @@ -14,7 +14,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; -import java.util.logging.Level; import org.apache.commons.lang.builder.ToStringBuilder; @@ -39,6 +38,12 @@ public abstract class SQLElement> { @SuppressWarnings("unchecked") public SQLElement() { tableName = tableName(); + + try { + ORM.initTable((Class)getClass()); + } catch (ORMInitTableException e) { + throw new RuntimeException(e); + } if (fieldsCache.get(getClass()) == null) { fields = new SQLFieldMap<>((Class)getClass()); @@ -95,7 +100,7 @@ public abstract class SQLElement> { listToFill.addField((SQLField) val); } 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> { /* package */ void set(SQLField sqlField, T value, boolean setModified) { if (sqlField == null) throw new IllegalArgumentException("sqlField can't be null"); 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; if (value == null) { @@ -352,7 +357,6 @@ public abstract class SQLElement> { private void addField(SQLField f) { if (f == null) return; - if (!sqlElemClass.equals(f.getSQLElementType())) return; if (containsKey(f.name)) throw new IllegalArgumentException( "SQLField " + f.name + " already exist in " + sqlElemClass.getName()); @SuppressWarnings("unchecked") diff --git a/src/fr/pandacube/java/util/db/sql_tools/SQLElementList.java b/src/fr/pandacube/java/util/db/sql_tools/SQLElementList.java index 64e423f..fd189cc 100644 --- a/src/fr/pandacube/java/util/db/sql_tools/SQLElementList.java +++ b/src/fr/pandacube/java/util/db/sql_tools/SQLElementList.java @@ -158,7 +158,7 @@ public class SQLElementList> extends ArrayList { } } catch (SQLException e) { - e.printStackTrace(); + Log.severe(e); } } diff --git a/src/fr/pandacube/java/util/db/sql_tools/SQLFKField.java b/src/fr/pandacube/java/util/db/sql_tools/SQLFKField.java index 4cc1f89..2df96b1 100644 --- a/src/fr/pandacube/java/util/db/sql_tools/SQLFKField.java +++ b/src/fr/pandacube/java/util/db/sql_tools/SQLFKField.java @@ -7,21 +7,21 @@ public class SQLFKField, T, F extends SQLElement> ext private SQLField sqlForeignKeyField; private Class sqlForeignKeyElemClass; - public SQLFKField(String n, SQLType t, boolean nul, SQLField fkF) { + public SQLFKField(String n, SQLType t, boolean nul, Class fkEl, SQLField fkF) { super(n, t, nul); - construct(fkF); + construct(fkEl, fkF); } - public SQLFKField(String n, SQLType t, boolean nul, T deflt, SQLField fkF) { + public SQLFKField(String n, SQLType t, boolean nul, T deflt, Class fkEl, SQLField fkF) { super(n, t, nul, deflt); - construct(fkF); + construct(fkEl, fkF); } public static , F extends SQLElement> SQLFKField idFK(String n, SQLType t, boolean nul, Class fkEl) { if (fkEl == null) throw new IllegalArgumentException("foreignKeyElement can't be null"); try { - return new SQLFKField<>(n, t, nul, ORM.getSQLIdField(fkEl)); + return new SQLFKField<>(n, t, nul, fkEl, ORM.getSQLIdField(fkEl)); } catch (ORMInitTableException e) { Log.severe("Can't create Foreign key Field called '" + n + "'", e); return null; @@ -32,21 +32,19 @@ public class SQLFKField, T, F extends SQLElement> ext Integer deflt, Class fkEl) { if (fkEl == null) throw new IllegalArgumentException("foreignKeyElement can't be null"); 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) { Log.severe("Can't create Foreign key Field called '" + n + "'", e); return null; } } - private void construct(SQLField fkF) { + private void construct(Class fkEl, SQLField fkF) { if (fkF == null) throw new IllegalArgumentException("foreignKeyField can't be null"); - Class fkEl = fkF.getSQLElementType(); try { ORM.initTable(fkEl); } catch (ORMInitTableException e) { - Log.severe(e); - return; + throw new RuntimeException(e); } if (!fkEl.equals(fkF.getSQLElementType())) throw new IllegalArgumentException("foreignKeyField must be from supplied foreignKeyElement"); diff --git a/src/fr/pandacube/java/util/network_api/server/AbstractRequestExecutor.java b/src/fr/pandacube/java/util/network_api/server/AbstractRequestExecutor.java index 4226d95..705555d 100644 --- a/src/fr/pandacube/java/util/network_api/server/AbstractRequestExecutor.java +++ b/src/fr/pandacube/java/util/network_api/server/AbstractRequestExecutor.java @@ -5,6 +5,8 @@ import java.io.PrintStream; import java.net.InetAddress; import java.net.Socket; +import fr.pandacube.java.util.Log; + public abstract class AbstractRequestExecutor { public final String command; @@ -25,7 +27,7 @@ public abstract class AbstractRequestExecutor { } catch (Exception e) { new Response(false, e.toString()).sendPacket(new PrintStream(socket.getOutputStream())); - e.printStackTrace(); + Log.severe(e); } } diff --git a/src/fr/pandacube/java/util/network_api/server/PacketExecutor.java b/src/fr/pandacube/java/util/network_api/server/PacketExecutor.java index bff8ab0..0d9bf01 100644 --- a/src/fr/pandacube/java/util/network_api/server/PacketExecutor.java +++ b/src/fr/pandacube/java/util/network_api/server/PacketExecutor.java @@ -42,10 +42,10 @@ public class PacketExecutor implements Runnable { try { rep.sendPacket(new PrintStream(socket.getOutputStream())); } catch (IOException e1) {} - if (e instanceof IOException) Log.getLogger() - .warning("Impossible de lire le packet reçu sur le socket " + socket + " : " + e.toString()); + if (e instanceof IOException) + Log.warning("Impossible de lire le packet reçu sur le socket " + socket + " : " + e.toString()); else - e.printStackTrace(); + Log.severe(e); } try {