]> git.mxchange.org Git - addressbook-lib.git/blob - Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java
Fixed and added more MySQL-related stuff
[addressbook-lib.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         /**
42          * Class' logger
43          */
44         private final Logger LOG;
45
46         /**
47          * Application instance
48          */
49         private Application application;
50
51         /**
52          * Bundle instance
53          */
54         private final ResourceBundle bundle;
55
56         /**
57          * Client instance
58          */
59         private Client client;
60
61         /**
62          * Contact manager instance
63          */
64         private ManageableContact contactManager;
65
66         /**
67          * Instance for own properties
68          */
69         private final Properties properties;
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 instance with from system
89                 this.properties = new Properties(System.getProperties());
90
91                 // Init properties file
92                 this.initProperties();
93         }
94
95         /**
96          * Application instance
97          *
98          * @return the application
99          */
100         @Override
101         public final Application getApplication () {
102                 return this.application;
103         }
104
105         /**
106          * Client instance
107          *
108          * @return the client
109          */
110         @Override
111         public final Client getClient () {
112                 return this.client;
113         }
114
115         /**
116          * Contact manager instance
117          *
118          * @return the contactManager
119          */
120         @Override
121         public final ManageableContact getContactManager () {
122                 return this.contactManager;
123         }
124
125         /**
126          * Prepares all properties, the file is written if it is not found
127          */
128         private void initProperties () {
129                 // Trace message
130                 this.getLogger().trace("CALLED!"); //NOI18N
131
132                 try {
133                         // Try to read it
134                         this.properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(FrameworkInterface.PROPERTIES_CONFIG_FILE))));
135
136                         // Debug message
137                         this.getLogger().debug(MessageFormat.format("{0} properties has been loaded.", this.properties.size())); //NOI18N
138                 } catch (final FileNotFoundException ex) {
139                         // Debug message
140                         this.getLogger().debug(MessageFormat.format("Properties file {0} not found: {1}", FrameworkInterface.PROPERTIES_CONFIG_FILE, ex)); //NOI18N
141
142                         /*
143                          * The file is not found which is normal for first run, so
144                          * initialize default values.
145                          */
146                         this.initPropertiesWithDefault();
147
148                         // Write file
149                         this.writePropertiesFile();
150                 } catch (final IOException ex) {
151                         // Something else didn't work
152                         this.abortProgramWithException(ex);
153                 }
154
155                 // Trace message
156                 this.getLogger().trace("EXIT!"); //NOI18N
157         }
158
159         /**
160          * Initializes properties with default values
161          */
162         private void initPropertiesWithDefault () {
163                 // Trace message
164                 this.getLogger().trace("CALLED!"); //NOI18N
165
166                 // Init default values:
167                 // Default database backend
168                 this.properties.put("org.mxchange.addressbook.database.backendType", "base64csv"); //NOI18N
169
170                 // For MySQL backend
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
175
176                 // Trace message
177                 this.getLogger().trace("EXIT!"); //NOI18N
178         }
179
180         /**
181          * Writes the properties file to disk
182          */
183         private void writePropertiesFile () {
184                 // Trace message
185                 this.getLogger().trace("CALLED!"); //NOI18N
186
187                 try {
188                         // Write it
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);
192                 }
193
194                 // Trace message
195                 this.getLogger().trace("EXIT!"); //NOI18N
196         }
197
198         /**
199          * Contact manager instance
200          *
201          * @param contactManager the contactManager to set
202          */
203         protected final void setContactManager (final ManageableContact contactManager) {
204                 this.contactManager = contactManager;
205         }
206
207         /**
208          * Client instance
209          *
210          * @param client the client to set
211          */
212         protected final void setClient (final Client client) {
213                 this.client = client;
214         }
215
216         /**
217          * Application instance
218          *
219          * @param application the application to set
220          */
221         protected final void setApplication (final Application application) {
222                 this.application = application;
223         }
224
225         /**
226          * Getter for human-readable string from given key
227          *
228          * @param key Key to return
229          * @return Human-readable message
230          */
231         @Override
232         public final String getMessageStringFromKey (final String key) {
233                 // Return message
234                 return this.getBundle().getString(key);
235         }
236
237         /**
238          * Aborts program with given exception
239          *
240          * @param       throwable Any type of Throwable
241          */
242         protected final void abortProgramWithException (final Throwable throwable) {
243                 // Log exception ...
244                 this.getLogger().catching(throwable);
245                 
246                 // .. and exit
247                 System.exit(1);
248                 
249         }
250
251         /**
252          * Getter for bundle instance
253          *
254          * @return Resource bundle
255          */
256         protected final ResourceBundle getBundle () {
257                 return this.bundle;
258         }
259
260         /**
261          * Getter for logger
262          *
263          * @return Logger
264          */
265         protected final Logger getLogger () {
266                 return this.LOG;
267         }
268
269         /**
270          * Getter for property which must exist
271          *
272          * @param key Key to get
273          * @return Propety value
274          */
275         protected String getProperty (final String key) {
276                 return this.properties.getProperty(key);
277         }
278
279         /**
280          * Name of used database table, handled over to backend
281          *
282          * @return the tableName
283          */
284         protected final String getTableName () {
285                 return this.tableName;
286         }
287
288         /**
289          * Name of used database table, handled over to backend
290          *
291          * @param tableName the tableName to set
292          */
293         protected final void setTableName (final String tableName) {
294                 this.tableName = tableName;
295         }
296 }