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.text.MessageFormat;
26 import java.util.Properties;
27 import java.util.ResourceBundle;
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
30 import org.mxchange.addressbook.application.Application;
31 import org.mxchange.addressbook.client.Client;
32 import org.mxchange.addressbook.manager.contact.ManageableContact;
37 * @author Roland Haeder
39 public class BaseFrameworkSystem implements FrameworkInterface {
41 * Instance for own properties
43 private static final Properties properties = new Properties(System.getProperties());
48 private final Logger LOG;
51 * Application instance
53 private Application application;
58 private final ResourceBundle bundle;
63 private Client client;
66 * Contact manager instance
68 private ManageableContact contactManager;
72 * Name of used database table, handled over to backend
74 private String tableName;
80 LOG = LogManager.getLogger(this);
81 bundle = ResourceBundle.getBundle("org/mxchange/addressbook/localization/bundle"); // NOI18N
85 * No instances can be created of this class
87 protected BaseFrameworkSystem () {
88 // Init properties file
89 this.initProperties();
93 * Application instance
95 * @return the application
98 public final Application getApplication () {
99 return this.application;
103 * Getter for human-readable string from given key
105 * @param key Key to return
106 * @return Human-readable message
109 public final String getMessageStringFromKey (final String key) {
111 return this.getBundle().getString(key);
115 * Aborts program with given exception
117 * @param throwable Any type of Throwable
119 protected final void abortProgramWithException (final Throwable throwable) {
121 this.getLogger().catching(throwable);
129 * Application instance
131 * @param application the application to set
133 protected final void setApplication (final Application application) {
134 this.application = application;
143 public final Client getClient () {
148 * Getter for bundle instance
150 * @return Resource bundle
152 protected final ResourceBundle getBundle () {
159 * @param client the client to set
161 protected final void setClient (final Client client) {
162 this.client = client;
166 * Contact manager instance
168 * @return the contactManager
171 public final ManageableContact getContactManager () {
172 return this.contactManager;
176 * Contact manager instance
178 * @param contactManager the contactManager to set
180 protected final void setContactManager (final ManageableContact contactManager) {
181 this.contactManager = contactManager;
187 * @param exception Exception to log
190 public void logException (final Throwable exception) {
191 // Log this exception
192 this.getLogger().catching(exception);
196 * Prepares all properties, the file is written if it is not found
198 private void initProperties () {
200 this.getLogger().trace("CALLED!"); //NOI18N
203 this.getLogger().debug(MessageFormat.format("{0} properties are loaded already.", BaseFrameworkSystem.properties.size())); //NOI18N
205 // Are some properties loaded?
206 if (!BaseFrameworkSystem.properties.isEmpty()) {
207 // Some are already loaded, abort here
213 BaseFrameworkSystem.properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(FrameworkInterface.PROPERTIES_CONFIG_FILE))));
216 this.getLogger().debug(MessageFormat.format("{0} properties has been loaded.", BaseFrameworkSystem.properties.size())); //NOI18N
217 } catch (final FileNotFoundException ex) {
219 this.getLogger().debug(MessageFormat.format("Properties file {0} not found: {1}", FrameworkInterface.PROPERTIES_CONFIG_FILE, ex)); //NOI18N
222 * The file is not found which is normal for first run, so
223 * initialize default values.
225 this.initPropertiesWithDefault();
228 this.writePropertiesFile();
229 } catch (final IOException ex) {
230 // Something else didn't work
231 this.abortProgramWithException(ex);
235 this.getLogger().trace("EXIT!"); //NOI18N
239 * Initializes properties with default values
241 private void initPropertiesWithDefault () {
243 this.getLogger().trace("CALLED!"); //NOI18N
245 // Init default values:
246 // Default database backend
247 BaseFrameworkSystem.properties.put("org.mxchange.addressbook.database.backendType", "base64csv"); //NOI18N
250 BaseFrameworkSystem.properties.put("org.mxchange.addressbook.database.mysql.host", "localhost"); //NOI18N
251 BaseFrameworkSystem.properties.put("org.mxchange.addressbook.database.mysql.dbname", "test"); //NOI18N
252 BaseFrameworkSystem.properties.put("org.mxchange.addressbook.database.mysql.login", ""); //NOI18N
253 BaseFrameworkSystem.properties.put("org.mxchange.addressbook.database.mysql.password", ""); //NOI18N
256 this.getLogger().trace("EXIT!"); //NOI18N
260 * Writes the properties file to disk
262 private void writePropertiesFile () {
264 this.getLogger().trace("CALLED!"); //NOI18N
268 BaseFrameworkSystem.properties.store(new PrintWriter(FrameworkInterface.PROPERTIES_CONFIG_FILE), "This file is automatically generated. You may wish to alter it."); //NOI18N
269 } catch (final IOException ex) {
270 this.abortProgramWithException(ex);
274 this.getLogger().trace("EXIT!"); //NOI18N
282 protected final Logger getLogger () {
287 * Getter for property which must exist
289 * @param key Key to get
290 * @return Propety value
292 protected String getProperty (final String key) {
293 return BaseFrameworkSystem.properties.getProperty(String.format("org.mxchange.addressbook.%s", key)); //NOI18N
297 * Name of used database table, handled over to backend
299 * @return the tableName
301 protected final String getTableName () {
302 return this.tableName;
306 * Name of used database table, handled over to backend
308 * @param tableName the tableName to set
310 protected final void setTableName (final String tableName) {
311 this.tableName = tableName;