-/*\r
- * Copyright (C) 2015 Roland Haeder\r
- *\r
- * This program is free software: you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation, either version 3 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
- */\r
-package org.mxchange.addressbook;\r
-\r
-import java.util.ResourceBundle;\r
-import org.apache.logging.log4j.LogManager;\r
-import org.apache.logging.log4j.Logger;\r
-import org.mxchange.addressbook.application.Application;\r
-import org.mxchange.addressbook.client.Client;\r
-import org.mxchange.addressbook.manager.contact.ManageableContact;\r
-\r
-/**\r
- * General class\r
- *\r
- * @author Roland Haeder\r
- */\r
-public class BaseFrameworkSystem implements FrameworkInterface {\r
- /**\r
- * Class' logger\r
- */\r
- private final Logger LOG;\r
-\r
- /**\r
- * Application instance\r
- */\r
- private Application application;\r
-\r
- /**\r
- * Bundle instance\r
- */\r
- private final ResourceBundle bundle;\r
-\r
- /**\r
- * Client instance\r
- */\r
- private Client client;\r
-\r
- /**\r
- * Contact manager instance\r
- */\r
- private ManageableContact contactManager;\r
-\r
- /**\r
- * Name of used database table, handled over to backend\r
- */\r
- private String tableName;\r
-\r
-\r
- /**\r
- * Initialize object\r
- */\r
- {\r
- LOG = LogManager.getLogger(this);\r
- bundle = ResourceBundle.getBundle("org/mxchange/addressbook/localization/bundle"); // NOI18N\r
- }\r
-\r
- /**\r
- * No instances can be created of this class\r
- */\r
- protected BaseFrameworkSystem () {\r
- }\r
-\r
- /**\r
- * Application instance\r
- * \r
- * @return the application\r
- */\r
- @Override\r
- public final Application getApplication () {\r
- return this.application;\r
- }\r
-\r
- /**\r
- * Client instance\r
- * \r
- * @return the client\r
- */\r
- @Override\r
- public final Client getClient () {\r
- return this.client;\r
- }\r
-\r
- /**\r
- * Contact manager instance\r
- * @return the contactManager\r
- */\r
- @Override\r
- public final ManageableContact getContactManager () {\r
- return this.contactManager;\r
- }\r
-\r
- /**\r
- * Contact manager instance\r
- * @param contactManager the contactManager to set\r
- */\r
- protected final void setContactManager (final ManageableContact contactManager) {\r
- this.contactManager = contactManager;\r
- }\r
-\r
- /**\r
- * Client instance\r
- * @param client the client to set\r
- */\r
- protected final void setClient (final Client client) {\r
- this.client = client;\r
- }\r
-\r
- /**\r
- * Application instance\r
- *\r
- * @param application the application to set\r
- */\r
- protected final void setApplication(final Application application) {\r
- this.application = application;\r
- }\r
-\r
- /**\r
- * Getter for logger\r
- *\r
- * @return Logger\r
- */\r
- protected final Logger getLogger () {\r
- return this.LOG;\r
- }\r
-\r
- /**\r
- * Name of used database table, handled over to backend\r
- * \r
- * @return the tableName\r
- */\r
- protected final String getTableName () {\r
- return this.tableName;\r
- }\r
-\r
- /**\r
- * Name of used database table, handled over to backend\r
- * \r
- * @param tableName the tableName to set\r
- */\r
- protected final void setTableName (final String tableName) {\r
- this.tableName = tableName;\r
- }\r
-\r
- /**\r
- * Getter for bundle instance\r
- * \r
- * @return Resource bundle\r
- */\r
- protected ResourceBundle getBundle () {\r
- return this.bundle;\r
- }\r
-}\r
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.addressbook;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.util.Properties;
+import java.util.ResourceBundle;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.mxchange.addressbook.application.Application;
+import org.mxchange.addressbook.client.Client;
+import org.mxchange.addressbook.manager.contact.ManageableContact;
+
+/**
+ * General class
+ *
+ * @author Roland Haeder
+ */
+public class BaseFrameworkSystem implements FrameworkInterface {
+
+ /**
+ * Class' logger
+ */
+ private final Logger LOG;
+
+ /**
+ * Application instance
+ */
+ private Application application;
+
+ /**
+ * Bundle instance
+ */
+ private final ResourceBundle bundle;
+
+ /**
+ * Client instance
+ */
+ private Client client;
+
+ /**
+ * Contact manager instance
+ */
+ private ManageableContact contactManager;
+
+ /**
+ * Instance for own properties
+ */
+ private final Properties properties;
+
+ /**
+ * Name of used database table, handled over to backend
+ */
+ private String tableName;
+
+ /**
+ * Initialize object
+ */
+ {
+ LOG = LogManager.getLogger(this);
+ bundle = ResourceBundle.getBundle("org/mxchange/addressbook/localization/bundle"); // NOI18N
+ }
+
+ /**
+ * No instances can be created of this class
+ */
+ protected BaseFrameworkSystem () {
+ // Init properties instance
+ this.properties = new Properties();
+
+ // Init properties file
+ this.initProperties();
+ }
+
+ /**
+ * Application instance
+ *
+ * @return the application
+ */
+ @Override
+ public final Application getApplication () {
+ return this.application;
+ }
+
+ /**
+ * Client instance
+ *
+ * @return the client
+ */
+ @Override
+ public final Client getClient () {
+ return this.client;
+ }
+
+ /**
+ * Contact manager instance
+ *
+ * @return the contactManager
+ */
+ @Override
+ public final ManageableContact getContactManager () {
+ return this.contactManager;
+ }
+
+ /**
+ * Prepares all properties, the file is written if it is not found
+ */
+ private void initProperties () {
+ try {
+ // Try to read it
+ this.properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(FrameworkInterface.PROPERTIES_CONFIG_FILE))));
+ } catch (final FileNotFoundException ex) {
+ /*
+ * The file is not found which is normal for first run, so
+ * initialize default values.
+ */
+ this.initPropertiesWithDefault();
+
+ // Write file
+ this.writePropertiesFile();
+ } catch (final IOException ex) {
+ // Something else didn't work
+ this.abortProgramWithException(ex);
+ }
+ }
+
+ /**
+ * Initializes properties with default values
+ */
+ private void initPropertiesWithDefault () {
+ // Init default values:
+ // Default database backend
+ this.properties.put("org.mxchange.addressbook.database.backendType", "base64csv");
+
+ // For MySQL backend
+ this.properties.put("org.mxchange.addressbook.database.mysql.host", "localhost");
+ this.properties.put("org.mxchange.addressbook.database.mysql.login", "");
+ this.properties.put("org.mxchange.addressbook.database.mysql.password", "");
+ }
+
+ /**
+ * Writes the properties file to disk
+ */
+ private void writePropertiesFile () {
+ try {
+ // Write it
+ this.properties.store(new PrintWriter(FrameworkInterface.PROPERTIES_CONFIG_FILE), "This file is automatically generated. You may wish to alter it.");
+ } catch (final IOException ex) {
+ this.abortProgramWithException(ex);
+ }
+ }
+
+ /**
+ * 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
+ *
+ * @return Logger
+ */
+ protected final Logger getLogger () {
+ return this.LOG;
+ }
+
+ /**
+ * Getter for property which must exist
+ *
+ * @param key Key to get
+ * @return Propety value
+ */
+ protected String getProperty (final String key) {
+ return this.properties.getProperty(key);
+ }
+
+ /**
+ * Name of used database table, handled over to backend
+ *
+ * @return the tableName
+ */
+ protected final String getTableName () {
+ return this.tableName;
+ }
+
+ /**
+ * Name of used database table, handled over to backend
+ *
+ * @param tableName the tableName to set
+ */
+ protected final void setTableName (final String tableName) {
+ this.tableName = tableName;
+ }
+}