From: Roland Haeder Date: Thu, 30 Jul 2015 08:01:56 +0000 (+0200) Subject: Added method logException() + properties instance should be static as only one instan... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=7a2964a38eab5ff0650f1dae80599583316ec36f;p=jbonuscard-lib.git Added method logException() + properties instance should be static as only one instance in whole application is okay Signed-off-by:Roland Häder --- diff --git a/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java b/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java index 87f2b61..333f68b 100644 --- a/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java +++ b/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java @@ -37,6 +37,10 @@ import org.mxchange.addressbook.manager.contact.ManageableContact; * @author Roland Haeder */ public class BaseFrameworkSystem implements FrameworkInterface { + /** + * Instance for own properties + */ + private static final Properties properties = new Properties(System.getProperties()); /** * Class' logger @@ -63,10 +67,6 @@ public class BaseFrameworkSystem implements FrameworkInterface { */ private ManageableContact contactManager; - /** - * Instance for own properties - */ - private final Properties properties; /** * Name of used database table, handled over to backend @@ -85,9 +85,6 @@ public class BaseFrameworkSystem implements FrameworkInterface { * No instances can be created of this class */ protected BaseFrameworkSystem () { - // Init properties instance with from system - this.properties = new Properties(System.getProperties()); - // Init properties file this.initProperties(); } @@ -102,6 +99,41 @@ public class BaseFrameworkSystem implements FrameworkInterface { return this.application; } + /** + * Getter for human-readable string from given key + * + * @param key Key to return + * @return Human-readable message + */ + @Override + public final String getMessageStringFromKey (final String key) { + // Return message + return this.getBundle().getString(key); + } + + /** + * Aborts program with given exception + * + * @param throwable Any type of Throwable + */ + protected final void abortProgramWithException (final Throwable throwable) { + // Log exception ... + this.getLogger().catching(throwable); + + // .. and exit + System.exit(1); + + } + + /** + * Application instance + * + * @param application the application to set + */ + protected final void setApplication (final Application application) { + this.application = application; + } + /** * Client instance * @@ -112,6 +144,24 @@ public class BaseFrameworkSystem implements FrameworkInterface { return this.client; } + /** + * Getter for bundle instance + * + * @return Resource bundle + */ + protected final ResourceBundle getBundle () { + return this.bundle; + } + + /** + * Client instance + * + * @param client the client to set + */ + protected final void setClient (final Client client) { + this.client = client; + } + /** * Contact manager instance * @@ -122,6 +172,26 @@ public class BaseFrameworkSystem implements FrameworkInterface { return this.contactManager; } + /** + * Contact manager instance + * + * @param contactManager the contactManager to set + */ + protected final void setContactManager (final ManageableContact contactManager) { + this.contactManager = contactManager; + } + + /** + * Log exception + * + * @param exception Exception to log + */ + @Override + public void logException (final Throwable exception) { + // Log this exception + this.getLogger().catching(exception); + } + /** * Prepares all properties, the file is written if it is not found */ @@ -129,12 +199,21 @@ public class BaseFrameworkSystem implements FrameworkInterface { // Trace message this.getLogger().trace("CALLED!"); //NOI18N + // Debug message + this.getLogger().debug(MessageFormat.format("{0} properties are loaded already.", BaseFrameworkSystem.properties.size())); //NOI18N + + // Are some properties loaded? + if (!BaseFrameworkSystem.properties.isEmpty()) { + // Some are already loaded, abort here + return; + } + try { // Try to read it - this.properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(FrameworkInterface.PROPERTIES_CONFIG_FILE)))); + BaseFrameworkSystem.properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(FrameworkInterface.PROPERTIES_CONFIG_FILE)))); // Debug message - this.getLogger().debug(MessageFormat.format("{0} properties has been loaded.", this.properties.size())); //NOI18N + this.getLogger().debug(MessageFormat.format("{0} properties has been loaded.", BaseFrameworkSystem.properties.size())); //NOI18N } catch (final FileNotFoundException ex) { // Debug message this.getLogger().debug(MessageFormat.format("Properties file {0} not found: {1}", FrameworkInterface.PROPERTIES_CONFIG_FILE, ex)); //NOI18N @@ -165,13 +244,13 @@ public class BaseFrameworkSystem implements FrameworkInterface { // Init default values: // Default database backend - this.properties.put("database.backendType", "base64csv"); //NOI18N + BaseFrameworkSystem.properties.put("org.mxchange.addressbook.database.backendType", "base64csv"); //NOI18N // For MySQL backend - this.properties.put("database.mysql.host", "localhost"); //NOI18N - this.properties.put("database.mysql.dbname", "test"); //NOI18N - this.properties.put("database.mysql.login", ""); //NOI18N - this.properties.put("database.mysql.password", ""); //NOI18N + BaseFrameworkSystem.properties.put("org.mxchange.addressbook.database.mysql.host", "localhost"); //NOI18N + BaseFrameworkSystem.properties.put("org.mxchange.addressbook.database.mysql.dbname", "test"); //NOI18N + BaseFrameworkSystem.properties.put("org.mxchange.addressbook.database.mysql.login", ""); //NOI18N + BaseFrameworkSystem.properties.put("org.mxchange.addressbook.database.mysql.password", ""); //NOI18N // Trace message this.getLogger().trace("EXIT!"); //NOI18N @@ -186,7 +265,7 @@ public class BaseFrameworkSystem implements FrameworkInterface { try { // Write it - this.properties.store(new PrintWriter(FrameworkInterface.PROPERTIES_CONFIG_FILE), "This file is automatically generated. You may wish to alter it."); //NOI18N + BaseFrameworkSystem.properties.store(new PrintWriter(FrameworkInterface.PROPERTIES_CONFIG_FILE), "This file is automatically generated. You may wish to alter it."); //NOI18N } catch (final IOException ex) { this.abortProgramWithException(ex); } @@ -195,68 +274,6 @@ public class BaseFrameworkSystem implements FrameworkInterface { this.getLogger().trace("EXIT!"); //NOI18N } - /** - * Contact manager instance - * - * @param contactManager the contactManager to set - */ - protected final void setContactManager (final ManageableContact contactManager) { - this.contactManager = contactManager; - } - - /** - * Client instance - * - * @param client the client to set - */ - protected final void setClient (final Client client) { - this.client = client; - } - - /** - * Application instance - * - * @param application the application to set - */ - protected final void setApplication (final Application application) { - this.application = application; - } - - /** - * Getter for human-readable string from given key - * - * @param key Key to return - * @return Human-readable message - */ - @Override - public final String getMessageStringFromKey (final String key) { - // Return message - return this.getBundle().getString(key); - } - - /** - * Aborts program with given exception - * - * @param throwable Any type of Throwable - */ - protected final void abortProgramWithException (final Throwable throwable) { - // Log exception ... - this.getLogger().catching(throwable); - - // .. and exit - System.exit(1); - - } - - /** - * Getter for bundle instance - * - * @return Resource bundle - */ - protected final ResourceBundle getBundle () { - return this.bundle; - } - /** * Getter for logger * @@ -273,7 +290,7 @@ public class BaseFrameworkSystem implements FrameworkInterface { * @return Propety value */ protected String getProperty (final String key) { - return this.properties.getProperty(String.format("org.mxchange.addressbook.%s", key)); //NOI18N + return BaseFrameworkSystem.properties.getProperty(String.format("org.mxchange.addressbook.%s", key)); //NOI18N } /** diff --git a/Addressbook/src/org/mxchange/addressbook/FrameworkInterface.java b/Addressbook/src/org/mxchange/addressbook/FrameworkInterface.java index 02144db..0ef8206 100644 --- a/Addressbook/src/org/mxchange/addressbook/FrameworkInterface.java +++ b/Addressbook/src/org/mxchange/addressbook/FrameworkInterface.java @@ -59,4 +59,11 @@ public interface FrameworkInterface { * @return Human-readable message */ public String getMessageStringFromKey (final String key); + + /** + * Logs given exception + * + * @param exception Exception to log + */ + public void logException (final Throwable exception); }