2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook;
19 import java.io.BufferedReader;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.io.PrintWriter;
25 import java.util.Properties;
26 import java.util.ResourceBundle;
27 import org.apache.logging.log4j.LogManager;
28 import org.apache.logging.log4j.Logger;
29 import org.mxchange.addressbook.application.Application;
30 import org.mxchange.addressbook.client.Client;
31 import org.mxchange.addressbook.manager.contact.ManageableContact;
36 * @author Roland Haeder
38 public class BaseFrameworkSystem implements FrameworkInterface {
43 private final Logger LOG;
46 * Application instance
48 private Application application;
53 private final ResourceBundle bundle;
58 private Client client;
61 * Contact manager instance
63 private ManageableContact contactManager;
66 * Instance for own properties
68 private final Properties properties;
71 * Name of used database table, handled over to backend
73 private String tableName;
79 LOG = LogManager.getLogger(this);
80 bundle = ResourceBundle.getBundle("org/mxchange/addressbook/localization/bundle"); // NOI18N
84 * No instances can be created of this class
86 protected BaseFrameworkSystem () {
87 // Init properties instance
88 this.properties = new Properties();
90 // Init properties file
91 this.initProperties();
95 * Application instance
97 * @return the application
100 public final Application getApplication () {
101 return this.application;
110 public final Client getClient () {
115 * Contact manager instance
117 * @return the contactManager
120 public final ManageableContact getContactManager () {
121 return this.contactManager;
125 * Prepares all properties, the file is written if it is not found
127 private void initProperties () {
130 this.properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(FrameworkInterface.PROPERTIES_CONFIG_FILE))));
131 } catch (final FileNotFoundException ex) {
133 * The file is not found which is normal for first run, so
134 * initialize default values.
136 this.initPropertiesWithDefault();
139 this.writePropertiesFile();
140 } catch (final IOException ex) {
141 // Something else didn't work
142 this.abortProgramWithException(ex);
147 * Initializes properties with default values
149 private void initPropertiesWithDefault () {
150 // Init default values:
151 // Default database backend
152 this.properties.put("org.mxchange.addressbook.database.backendType", "base64csv");
155 this.properties.put("org.mxchange.addressbook.database.mysql.host", "localhost");
156 this.properties.put("org.mxchange.addressbook.database.mysql.login", "");
157 this.properties.put("org.mxchange.addressbook.database.mysql.password", "");
161 * Writes the properties file to disk
163 private void writePropertiesFile () {
166 this.properties.store(new PrintWriter(FrameworkInterface.PROPERTIES_CONFIG_FILE), "This file is automatically generated. You may wish to alter it.");
167 } catch (final IOException ex) {
168 this.abortProgramWithException(ex);
173 * Contact manager instance
175 * @param contactManager the contactManager to set
177 protected final void setContactManager (final ManageableContact contactManager) {
178 this.contactManager = contactManager;
184 * @param client the client to set
186 protected final void setClient (final Client client) {
187 this.client = client;
191 * Application instance
193 * @param application the application to set
195 protected final void setApplication (final Application application) {
196 this.application = application;
200 * Getter for human-readable string from given key
202 * @param key Key to return
203 * @return Human-readable message
206 public final String getMessageStringFromKey (final String key) {
208 return this.getBundle().getString(key);
212 * Aborts program with given exception
214 * @param throwable Any type of Throwable
216 protected final void abortProgramWithException (final Throwable throwable) {
218 this.getLogger().catching(throwable);
226 * Getter for bundle instance
228 * @return Resource bundle
230 protected final ResourceBundle getBundle () {
239 protected final Logger getLogger () {
244 * Getter for property which must exist
246 * @param key Key to get
247 * @return Propety value
249 protected String getProperty (final String key) {
250 return this.properties.getProperty(key);
254 * Name of used database table, handled over to backend
256 * @return the tableName
258 protected final String getTableName () {
259 return this.tableName;
263 * Name of used database table, handled over to backend
265 * @param tableName the tableName to set
267 protected final void setTableName (final String tableName) {
268 this.tableName = tableName;