]> git.mxchange.org Git - jcore.git/blob - src/org/mxchange/jcore/BaseFrameworkSystem.java
auto-formatted project
[jcore.git] / src / org / mxchange / jcore / 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.jcore;
18
19 import java.text.MessageFormat;
20 import java.util.Arrays;
21 import java.util.ResourceBundle;
22 import java.util.StringTokenizer;
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.mxchange.jcore.application.Application;
26 import org.mxchange.jcore.client.Client;
27 import org.mxchange.jcore.manager.Manageable;
28 import org.mxchange.jcore.model.contact.Contact;
29
30 /**
31  * General class
32  * <p>
33  * @author Roland Haeder<roland@mxchange.org>
34  */
35 public abstract class BaseFrameworkSystem implements FrameworkInterface {
36
37         /**
38          * Bundle instance
39          */
40         private static ResourceBundle bundle;
41
42         /**
43          * Self instance
44          */
45         private static FrameworkInterface selfInstance;
46
47         /**
48          * Class' logger
49          */
50         private final Logger LOG;
51
52         /**
53          * Application instance
54          */
55         private Application application;
56
57         /**
58          * Client instance
59          */
60         private Client client;
61
62         /**
63          * Contact instance
64          */
65         private Contact contact;
66
67         /**
68          * Manager instance
69          */
70         private Manageable manager;
71
72         /**
73          * Initialize object
74          */
75         {
76                 // Init logger
77                 this.LOG = LogManager.getLogger(this);
78
79                 // Need to set it here
80                 selfInstance = this;
81         }
82
83         /**
84          * No instances can be created of this class
85          */
86         protected BaseFrameworkSystem () {
87         }
88
89         /**
90          * Getter for this application
91          * <p>
92          * @return Instance from this application
93          */
94         public static FrameworkInterface getInstance () {
95                 // Return it
96                 return selfInstance;
97         }
98
99         @Override
100         public Application getApplication () {
101                 return this.application;
102         }
103
104         @Override
105         public Client getClient () {
106                 return this.client;
107         }
108
109         @Override
110         public void logException (final Throwable exception) {
111                 // Log this exception
112                 this.getLogger().catching(exception);
113         }
114
115         /**
116          * Converts null to empty string or leaves original object untouched.
117          * <p>
118          * @param object Any string
119          * @return Empty string if null or original string TODO: Move to own utility
120          * class
121          */
122         protected Object convertNullToEmpty (final Object object) {
123                 // Trace message
124                 this.getLogger().trace(MessageFormat.format("object={0}", object)); //NOI18N
125
126                 // Is it null?
127                 if (null == object) {
128                         // Return empty string
129                         return ""; //NOI18N
130                 }
131
132                 // Trace message
133                 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
134
135                 // Return it
136                 return object;
137         }
138
139         /**
140          * Some "getter" for an array from given string and tokenizer
141          * <p>
142          * @param str String to tokenize and get array from
143          * @param delimiter Delimiter
144          * @return Array from tokenized string TODO Get rid of size parameter TODO:
145          * Move to own utility class
146          */
147         protected String[] getArrayFromString (final String str, final String delimiter) {
148                 // Trace message
149                 this.getLogger().trace(MessageFormat.format("str={0},delimiter={1} - CALLED!", str, delimiter)); //NOI18N
150
151                 // Get tokenizer
152                 StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
153
154                 // Init array and index
155                 String[] tokens = new String[tokenizer.countTokens()];
156                 int index = 0;
157
158                 // Run through all tokens
159                 while (tokenizer.hasMoreTokens()) {
160                         // Get current token and add it
161                         tokens[index] = tokenizer.nextToken();
162
163                         // Debug message
164                         this.getLogger().debug(MessageFormat.format("Token at index{0}: {1}", index, tokens[1])); //NOI18N
165
166                         // Increment index
167                         index++;
168                 }
169
170                 // Trace message
171                 this.getLogger().trace(MessageFormat.format("tokens({0})={1} - EXIT!", tokens.length, Arrays.toString(tokens))); //NOI18N
172
173                 // Return it
174                 return tokens;
175         }
176
177         /**
178          * Client instance
179          * <p>
180          * @param client the client to set
181          */
182         protected void setClient (final Client client) {
183                 this.client = client;
184         }
185
186         /**
187          * Application instance
188          * <p>
189          * @param application the application to set
190          */
191         protected void setApplication (final Application application) {
192                 this.application = application;
193         }
194
195         @Override
196         public Manageable getManager () {
197                 return this.manager;
198         }
199
200         /**
201          * Getter for Contact instance
202          * <p>
203          * @return Contact instance
204          */
205         protected Contact getContact () {
206                 return this.contact;
207         }
208
209         /**
210          * Setter for Contact instance
211          * <p>
212          * @param contact A Contact instance
213          */
214         protected void setContact (final Contact contact) {
215                 this.contact = contact;
216         }
217
218         /**
219          * Manager instance
220          * <p>
221          * @param manager the manager instance to set
222          */
223         protected void setManager (final Manageable manager) {
224                 this.manager = manager;
225         }
226
227         @Override
228         public String getMessageStringFromKey (final String key) {
229                 // Return message
230                 return this.getBundle().getString(key);
231         }
232
233         /**
234          * Aborts program with given exception
235          * <p>
236          * @param throwable Any type of Throwable
237          */
238         protected void abortProgramWithException (final Throwable throwable) {
239                 // Log exception ...
240                 this.logException(throwable);
241
242                 // .. and exit
243                 System.exit(1);
244         }
245
246         /**
247          * Getter for bundle instance
248          * <p>
249          * @return Resource bundle
250          */
251         protected ResourceBundle getBundle () {
252                 return BaseFrameworkSystem.bundle;
253         }
254
255         /**
256          * Setter for bundle instance
257          * <p>
258          * @param bundle the bundle to set
259          */
260         protected static void setBundle (final ResourceBundle bundle) {
261                 BaseFrameworkSystem.bundle = bundle;
262         }
263
264         /**
265          * Getter for logger instance
266          * <p>
267          * @return Logger instance
268          */
269         protected Logger getLogger () {
270                 return this.LOG;
271         }
272
273         /**
274          * Initializes i18n bundles
275          */
276         protected void initBundle () {
277                 // Trace message
278                 this.getLogger().trace("CALLED!"); //NOI18N
279
280                 // Is the bundle set?
281                 if (BaseFrameworkSystem.isBundledInitialized()) {
282                         // Is already set
283                         throw new IllegalStateException("called twice"); //NOI18N
284                 }
285
286                 // Set instance
287                 setBundle(ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE)); // NOI18N
288
289                 // Trace message
290                 this.getLogger().trace("EXIT!"); //NOI18N
291         }
292
293         /**
294          * Checks if the bundle is initialized
295          * <p>
296          * @return Whether the bundle has been initialized
297          */
298         protected static boolean isBundledInitialized () {
299                 // Check it
300                 return (bundle instanceof ResourceBundle);
301         }
302 }