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