]> git.mxchange.org Git - jcore.git/blob - src/org/mxchange/jcore/BaseFrameworkSystem.java
ebfe18b0ce45fb3a7083afc73fdc888774be047e
[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  *
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          *
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         /**
105          * Getter for logger instance
106          *
107          * @return Logger instance
108          */
109         protected Logger getLogger () {
110                 return this.LOG;
111         }
112
113         @Override
114         public Manageable getManager () {
115                 return this.manager;
116         }
117
118         @Override
119         public String getMessageStringFromKey (final String key) {
120                 // Return message
121                 return this.getBundle().getString(key);
122         }
123
124         /**
125          * Aborts program with given exception
126          *
127          * @param throwable Any type of Throwable
128          */
129         protected void abortProgramWithException (final Throwable throwable) {
130                 // Log exception ...
131                 this.logException(throwable);
132
133                 // .. and exit
134                 System.exit(1);
135         }
136
137         /**
138          * Application instance
139          *
140          * @param application the application to set
141          */
142         protected void setApplication (final Application application) {
143                 this.application = application;
144         }
145
146         @Override
147         public Client getClient () {
148                 return this.client;
149         }
150
151         /**
152          * Getter for bundle instance
153          *
154          * @return Resource bundle
155          */
156         protected ResourceBundle getBundle () {
157                 return BaseFrameworkSystem.bundle;
158         }
159
160         /**
161          * Setter for bundle instance
162          *
163          * @param bundle the bundle to set
164          */
165         protected static void setBundle (final ResourceBundle bundle) {
166                 BaseFrameworkSystem.bundle = bundle;
167         }
168
169         /**
170          * Client instance
171          *
172          * @param client the client to set
173          */
174         protected void setClient (final Client client) {
175                 this.client = client;
176         }
177
178         @Override
179         public void logException (final Throwable exception) {
180                 // Log this exception
181                 this.getLogger().catching(exception);
182         }
183
184         /**
185          * Some "getter" for an array from given string and tokenizer
186          *
187          * @param str String to tokenize and get array from
188          * @param delimiter Delimiter
189          * @return Array from tokenized string TODO Get rid of size parameter
190          * TODO: Move to own utility class
191          */
192         protected String[] getArrayFromString (final String str, final String delimiter) {
193                 // Trace message
194                 this.getLogger().trace(MessageFormat.format("str={0},delimiter={1} - CALLED!", str, delimiter)); //NOI18N
195
196                 // Get tokenizer
197                 StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
198
199                 // Init array and index
200                 String[] tokens = new String[tokenizer.countTokens()];
201                 int index = 0;
202
203                 // Run through all tokens
204                 while (tokenizer.hasMoreTokens()) {
205                         // Get current token and add it
206                         tokens[index] = tokenizer.nextToken();
207
208                         // Debug message
209                         this.getLogger().debug(MessageFormat.format("Token at index{0}: {1}", index, tokens[1])); //NOI18N
210
211                         // Increment index
212                         index++;
213                 }
214
215                 // Trace message
216                 this.getLogger().trace(MessageFormat.format("tokens({0})={1} - EXIT!", tokens.length, Arrays.toString(tokens))); //NOI18N
217
218                 // Return it
219                 return tokens;
220         }
221
222         /**
223          * Converts null to empty string or leaves original object untouched.
224          *
225          * @param object Any string
226          * @return Empty string if null or original string
227          * TODO: Move to own utility class
228          */
229         protected Object convertNullToEmpty (final Object object) {
230                 // Trace message
231                 this.getLogger().trace(MessageFormat.format("object={0}", object)); //NOI18N
232
233                 // Is it null?
234                 if (null == object) {
235                         // Return empty string
236                         return ""; //NOI18N
237                 }
238
239                 // Trace message
240                 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
241
242                 // Return it
243                 return object;
244         }
245
246         /**
247          * Manager instance
248          *
249          * @param manager the manager instance to set
250          */
251         protected void setManager (final Manageable manager) {
252                 this.manager = manager;
253         }
254
255         /**
256          * Getter for Contact instance
257          *
258          * @return Contact instance
259          */
260         protected Contact getContact () {
261                 return this.contact;
262         }
263
264         /**
265          * Setter for Contact instance
266          *
267          * @param contact A Contact instance
268          */
269         protected void setContact (final Contact contact) {
270                 this.contact = contact;
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          *
296          * @return Whether the bundle has been initialized
297          */
298         protected static boolean isBundledInitialized () {
299                 // Check it
300                 return (bundle instanceof ResourceBundle);
301         }
302 }