]> git.mxchange.org Git - jaddressbook-lib.git/commitdiff
Basic support for properties file
authorRoland Haeder <roland@mxchange.org>
Wed, 29 Jul 2015 12:26:55 +0000 (14:26 +0200)
committerRoland Haeder <roland@mxchange.org>
Wed, 29 Jul 2015 12:26:55 +0000 (14:26 +0200)
Signed-off-by:Roland Häder <roland@mxchange.org>

Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java
Addressbook/src/org/mxchange/addressbook/FrameworkInterface.java
Addressbook/src/org/mxchange/addressbook/database/frontend/BaseDatabaseFrontend.java

index b43b42b54a49c3f2de6f9b5953f639957c76e461..6315877d1f81f9d9387e012b0451777e77637b3f 100644 (file)
  */
 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;
@@ -55,6 +62,11 @@ 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
         */
@@ -72,6 +84,11 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         * No instances can be created of this class
         */
        protected BaseFrameworkSystem () {
+               // Init properties instance
+               this.properties = new Properties();
+
+               // Init properties file
+               this.initProperties();
        }
 
        /**
@@ -104,6 +121,54 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                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
         *
@@ -131,6 +196,32 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                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
         *
@@ -149,6 +240,16 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                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
         *
@@ -166,30 +267,4 @@ public class BaseFrameworkSystem implements FrameworkInterface {
        protected final void setTableName (final String tableName) {
                this.tableName = tableName;
        }
-
-       /**
-        * 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);
-
-       }
 }
index 822be62dbdfd42055e68583673eccbfc6ca9d35b..02144db0366c4722625e31bc72adbb1baf1d166f 100644 (file)
@@ -26,6 +26,10 @@ import org.mxchange.addressbook.manager.contact.ManageableContact;
  * @author Roland Haeder
  */
 public interface FrameworkInterface {
+       /*
+        * Properties file name
+        */
+       public static final String PROPERTIES_CONFIG_FILE = "config.properties";
 
        /**
         * Getter for contact manager
index 56b9d7074390417f27f96063d025097858e9dd53..63fd4287a938a12a2335e15088292fc5f6b31b8b 100644 (file)
@@ -70,7 +70,7 @@ public class BaseDatabaseFrontend extends BaseFrameworkSystem {
 
                // Read property
                // @TODO rewrite this to own properties file
-               String backendType = System.getProperty("org.mxchange.addressbook.database.backendType", "base64csv"); //NOI18N
+               String backendType = this.getProperty("org.mxchange.addressbook.database.backendType"); //NOI18N
 
                // Try it
                try {