]> git.mxchange.org Git - jcore.git/blob - src/org/mxchange/jcore/BaseFrameworkSystem.java
It is part of the model.
[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.model.contact.Contact;
28 import org.mxchange.jcore.manager.Manageable;
29
30 /**
31  * General class
32  *
33  * @author Roland Haeder
34  */
35 public 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 final FrameworkInterface getInstance () {
95                 // Return it
96                 return selfInstance;
97         }
98
99         @Override
100         public final Application getApplication () {
101                 return this.application;
102         }
103
104         @Override
105         public final Logger getLogger () {
106                 return this.LOG;
107         }
108
109         @Override
110         public final Manageable getManager () {
111                 return this.manager;
112         }
113
114         @Override
115         public final String getMessageStringFromKey (final String key) {
116                 // Return message
117                 return this.getBundle().getString(key);
118         }
119
120         /**
121          * Aborts program with given exception
122          *
123          * @param throwable Any type of Throwable
124          */
125         protected final void abortProgramWithException (final Throwable throwable) {
126                 // Log exception ...
127                 this.logException(throwable);
128
129                 // .. and exit
130                 System.exit(1);
131         }
132
133         /**
134          * Application instance
135          *
136          * @param application the application to set
137          */
138         protected final void setApplication (final Application application) {
139                 this.application = application;
140         }
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 BaseFrameworkSystem.bundle;
154         }
155
156         /**
157          * Setter for bundle instance
158          *
159          * @param bundle the bundle to set
160          */
161         protected static void setBundle (final ResourceBundle bundle) {
162                 BaseFrameworkSystem.bundle = bundle;
163         }
164
165         /**
166          * Client instance
167          *
168          * @param client the client to set
169          */
170         protected final void setClient (final Client client) {
171                 this.client = client;
172         }
173
174         @Override
175         public final void logException (final Throwable exception) {
176                 // Log this exception
177                 this.getLogger().catching(exception);
178         }
179
180         /**
181          * Some "getter" for an array from given string and tokenizer
182          *
183          * @param str String to tokenize and get array from
184          * @param delimiter Delimiter
185          * @return Array from tokenized string TODO Get rid of size parameter
186          * TODO: Move to own utility class
187          */
188         protected String[] getArrayFromString (final String str, final String delimiter) {
189                 // Trace message
190                 this.getLogger().trace(MessageFormat.format("str={0},delimiter={1} - CALLED!", str, delimiter)); //NOI18N
191
192                 // Get tokenizer
193                 StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
194
195                 // Init array and index
196                 String[] tokens = new String[tokenizer.countTokens()];
197                 int index = 0;
198
199                 // Run through all tokens
200                 while (tokenizer.hasMoreTokens()) {
201                         // Get current token and add it
202                         tokens[index] = tokenizer.nextToken();
203
204                         // Debug message
205                         this.getLogger().debug(MessageFormat.format("Token at index{0}: {1}", index, tokens[1])); //NOI18N
206
207                         // Increment index
208                         index++;
209                 }
210
211                 // Trace message
212                 this.getLogger().trace(MessageFormat.format("tokens({0})={1} - EXIT!", tokens.length, Arrays.toString(tokens))); //NOI18N
213
214                 // Return it
215                 return tokens;
216         }
217
218         /**
219          * Converts null to empty string or leaves original object untouched.
220          *
221          * @param object Any string
222          * @return Empty string if null or original string
223          * TODO: Move to own utility class
224          */
225         protected Object convertNullToEmpty (final Object object) {
226                 // Trace message
227                 this.getLogger().trace(MessageFormat.format("object={0}", object)); //NOI18N
228
229                 // Is it null?
230                 if (null == object) {
231                         // Return empty string
232                         return ""; //NOI18N
233                 }
234
235                 // Trace message
236                 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
237
238                 // Return it
239                 return object;
240         }
241
242         /**
243          * Manager instance
244          *
245          * @param manager the manager instance to set
246          */
247         protected final void setManager (final Manageable manager) {
248                 this.manager = manager;
249         }
250
251         /**
252          * Getter for Contact instance
253          *
254          * @return Contact instance
255          */
256         protected final Contact getContact () {
257                 return this.contact;
258         }
259
260         /**
261          * Setter for Contact instance
262          *
263          * @param contact A Contact instance
264          */
265         protected final void setContact (final Contact contact) {
266                 this.contact = contact;
267         }
268
269         /**
270          * Initializes i18n bundles
271          */
272         protected void initBundle () {
273                 // Trace message
274                 this.getLogger().trace("CALLED!"); //NOI18N
275
276                 // Is the bundle set?
277                 if (BaseFrameworkSystem.isBundledInitialized()) {
278                         // Is already set
279                         throw new IllegalStateException("called twice"); //NOI18N
280                 }
281
282                 // Set instance
283                 setBundle(ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE)); // NOI18N
284
285                 // Trace message
286                 this.getLogger().trace("EXIT!"); //NOI18N
287         }
288
289         /**
290          * Checks if the bundle is initialized
291          *
292          * @return Whether the bundle has been initialized
293          */
294         protected static boolean isBundledInitialized () {
295                 // Check it
296                 return (bundle instanceof ResourceBundle);
297         }
298 }