]> git.mxchange.org Git - addressbook-swing.git/blob - Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java
333f68bf638ffa789a556a63e33137462f5a4ae8
[addressbook-swing.git] / Addressbook / src / org / mxchange / addressbook / BaseFrameworkSystem.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook;
18
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;
33
34 /**
35  * General class
36  *
37  * @author Roland Haeder
38  */
39 public class BaseFrameworkSystem implements FrameworkInterface {
40         /**
41          * Instance for own properties
42          */
43         private static final Properties properties = new Properties(System.getProperties());
44
45         /**
46          * Class' logger
47          */
48         private final Logger LOG;
49
50         /**
51          * Application instance
52          */
53         private Application application;
54
55         /**
56          * Bundle instance
57          */
58         private final ResourceBundle bundle;
59
60         /**
61          * Client instance
62          */
63         private Client client;
64
65         /**
66          * Contact manager instance
67          */
68         private ManageableContact contactManager;
69
70
71         /**
72          * Name of used database table, handled over to backend
73          */
74         private String tableName;
75
76         /**
77          * Initialize object
78          */
79         {
80                 LOG = LogManager.getLogger(this);
81                 bundle = ResourceBundle.getBundle("org/mxchange/addressbook/localization/bundle"); // NOI18N
82         }
83
84         /**
85          * No instances can be created of this class
86          */
87         protected BaseFrameworkSystem () {
88                 // Init properties file
89                 this.initProperties();
90         }
91
92         /**
93          * Application instance
94          *
95          * @return the application
96          */
97         @Override
98         public final Application getApplication () {
99                 return this.application;
100         }
101
102         /**
103          * Getter for human-readable string from given key
104          *
105          * @param key Key to return
106          * @return Human-readable message
107          */
108         @Override
109         public final String getMessageStringFromKey (final String key) {
110                 // Return message
111                 return this.getBundle().getString(key);
112         }
113
114         /**
115          * Aborts program with given exception
116          *
117          * @param       throwable Any type of Throwable
118          */
119         protected final void abortProgramWithException (final Throwable throwable) {
120                 // Log exception ...
121                 this.getLogger().catching(throwable);
122                 
123                 // .. and exit
124                 System.exit(1);
125                 
126         }
127
128         /**
129          * Application instance
130          *
131          * @param application the application to set
132          */
133         protected final void setApplication (final Application application) {
134                 this.application = application;
135         }
136
137         /**
138          * Client instance
139          *
140          * @return the client
141          */
142         @Override
143         public final Client getClient () {
144                 return this.client;
145         }
146
147         /**
148          * Getter for bundle instance
149          *
150          * @return Resource bundle
151          */
152         protected final ResourceBundle getBundle () {
153                 return this.bundle;
154         }
155
156         /**
157          * Client instance
158          *
159          * @param client the client to set
160          */
161         protected final void setClient (final Client client) {
162                 this.client = client;
163         }
164
165         /**
166          * Contact manager instance
167          *
168          * @return the contactManager
169          */
170         @Override
171         public final ManageableContact getContactManager () {
172                 return this.contactManager;
173         }
174
175         /**
176          * Contact manager instance
177          *
178          * @param contactManager the contactManager to set
179          */
180         protected final void setContactManager (final ManageableContact contactManager) {
181                 this.contactManager = contactManager;
182         }
183
184         /**
185          * Log exception
186          *
187          * @param exception Exception to log
188          */
189         @Override
190         public void logException (final Throwable exception) {
191                 // Log this exception
192                 this.getLogger().catching(exception);
193         }
194
195         /**
196          * Prepares all properties, the file is written if it is not found
197          */
198         private void initProperties () {
199                 // Trace message
200                 this.getLogger().trace("CALLED!"); //NOI18N
201
202                 // Debug message
203                 this.getLogger().debug(MessageFormat.format("{0} properties are loaded already.", BaseFrameworkSystem.properties.size())); //NOI18N
204
205                 // Are some properties loaded?
206                 if (!BaseFrameworkSystem.properties.isEmpty()) {
207                         // Some are already loaded, abort here
208                         return;
209                 }
210
211                 try {
212                         // Try to read it
213                         BaseFrameworkSystem.properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(FrameworkInterface.PROPERTIES_CONFIG_FILE))));
214
215                         // Debug message
216                         this.getLogger().debug(MessageFormat.format("{0} properties has been loaded.", BaseFrameworkSystem.properties.size())); //NOI18N
217                 } catch (final FileNotFoundException ex) {
218                         // Debug message
219                         this.getLogger().debug(MessageFormat.format("Properties file {0} not found: {1}", FrameworkInterface.PROPERTIES_CONFIG_FILE, ex)); //NOI18N
220
221                         /*
222                          * The file is not found which is normal for first run, so
223                          * initialize default values.
224                          */
225                         this.initPropertiesWithDefault();
226
227                         // Write file
228                         this.writePropertiesFile();
229                 } catch (final IOException ex) {
230                         // Something else didn't work
231                         this.abortProgramWithException(ex);
232                 }
233
234                 // Trace message
235                 this.getLogger().trace("EXIT!"); //NOI18N
236         }
237
238         /**
239          * Initializes properties with default values
240          */
241         private void initPropertiesWithDefault () {
242                 // Trace message
243                 this.getLogger().trace("CALLED!"); //NOI18N
244
245                 // Init default values:
246                 // Default database backend
247                 BaseFrameworkSystem.properties.put("org.mxchange.addressbook.database.backendType", "base64csv"); //NOI18N
248
249                 // For MySQL backend
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
254
255                 // Trace message
256                 this.getLogger().trace("EXIT!"); //NOI18N
257         }
258
259         /**
260          * Writes the properties file to disk
261          */
262         private void writePropertiesFile () {
263                 // Trace message
264                 this.getLogger().trace("CALLED!"); //NOI18N
265
266                 try {
267                         // Write it
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);
271                 }
272
273                 // Trace message
274                 this.getLogger().trace("EXIT!"); //NOI18N
275         }
276
277         /**
278          * Getter for logger
279          *
280          * @return Logger
281          */
282         protected final Logger getLogger () {
283                 return this.LOG;
284         }
285
286         /**
287          * Getter for property which must exist
288          *
289          * @param key Key to get
290          * @return Propety value
291          */
292         protected String getProperty (final String key) {
293                 return BaseFrameworkSystem.properties.getProperty(String.format("org.mxchange.addressbook.%s", key)); //NOI18N
294         }
295
296         /**
297          * Name of used database table, handled over to backend
298          *
299          * @return the tableName
300          */
301         protected final String getTableName () {
302                 return this.tableName;
303         }
304
305         /**
306          * Name of used database table, handled over to backend
307          *
308          * @param tableName the tableName to set
309          */
310         protected final void setTableName (final String tableName) {
311                 this.tableName = tableName;
312         }
313 }