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 {
44 private final Logger LOG;
47 * Application instance
49 private Application application;
54 private final ResourceBundle bundle;
59 private Client client;
62 * Contact manager instance
64 private ManageableContact contactManager;
67 * Instance for own properties
69 private final Properties properties;
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 instance with from system
89 this.properties = new Properties(System.getProperties());
91 // Init properties file
92 this.initProperties();
96 * Application instance
98 * @return the application
101 public final Application getApplication () {
102 return this.application;
111 public final Client getClient () {
116 * Contact manager instance
118 * @return the contactManager
121 public final ManageableContact getContactManager () {
122 return this.contactManager;
126 * Prepares all properties, the file is written if it is not found
128 private void initProperties () {
130 this.getLogger().trace("CALLED!"); //NOI18N
134 this.properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(FrameworkInterface.PROPERTIES_CONFIG_FILE))));
137 this.getLogger().debug(MessageFormat.format("{0} properties has been loaded.", this.properties.size())); //NOI18N
138 } catch (final FileNotFoundException ex) {
140 this.getLogger().debug(MessageFormat.format("Properties file {0} not found: {1}", FrameworkInterface.PROPERTIES_CONFIG_FILE, ex)); //NOI18N
143 * The file is not found which is normal for first run, so
144 * initialize default values.
146 this.initPropertiesWithDefault();
149 this.writePropertiesFile();
150 } catch (final IOException ex) {
151 // Something else didn't work
152 this.abortProgramWithException(ex);
156 this.getLogger().trace("EXIT!"); //NOI18N
160 * Initializes properties with default values
162 private void initPropertiesWithDefault () {
164 this.getLogger().trace("CALLED!"); //NOI18N
166 // Init default values:
167 // Default database backend
168 this.properties.put("org.mxchange.addressbook.database.backendType", "base64csv"); //NOI18N
171 this.properties.put("org.mxchange.addressbook.database.mysql.host", "localhost"); //NOI18N
172 this.properties.put("org.mxchange.addressbook.database.mysql.dbname", "test"); //NOI18N
173 this.properties.put("org.mxchange.addressbook.database.mysql.login", ""); //NOI18N
174 this.properties.put("org.mxchange.addressbook.database.mysql.password", ""); //NOI18N
177 this.getLogger().trace("EXIT!"); //NOI18N
181 * Writes the properties file to disk
183 private void writePropertiesFile () {
185 this.getLogger().trace("CALLED!"); //NOI18N
189 this.properties.store(new PrintWriter(FrameworkInterface.PROPERTIES_CONFIG_FILE), "This file is automatically generated. You may wish to alter it."); //NOI18N
190 } catch (final IOException ex) {
191 this.abortProgramWithException(ex);
195 this.getLogger().trace("EXIT!"); //NOI18N
199 * Contact manager instance
201 * @param contactManager the contactManager to set
203 protected final void setContactManager (final ManageableContact contactManager) {
204 this.contactManager = contactManager;
210 * @param client the client to set
212 protected final void setClient (final Client client) {
213 this.client = client;
217 * Application instance
219 * @param application the application to set
221 protected final void setApplication (final Application application) {
222 this.application = application;
226 * Getter for human-readable string from given key
228 * @param key Key to return
229 * @return Human-readable message
232 public final String getMessageStringFromKey (final String key) {
234 return this.getBundle().getString(key);
238 * Aborts program with given exception
240 * @param throwable Any type of Throwable
242 protected final void abortProgramWithException (final Throwable throwable) {
244 this.getLogger().catching(throwable);
252 * Getter for bundle instance
254 * @return Resource bundle
256 protected final ResourceBundle getBundle () {
265 protected final Logger getLogger () {
270 * Getter for property which must exist
272 * @param key Key to get
273 * @return Propety value
275 protected String getProperty (final String key) {
276 return this.properties.getProperty(key);
280 * Name of used database table, handled over to backend
282 * @return the tableName
284 protected final String getTableName () {
285 return this.tableName;
289 * Name of used database table, handled over to backend
291 * @param tableName the tableName to set
293 protected final void setTableName (final String tableName) {
294 this.tableName = tableName;