2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jcore;
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;
33 * @author Roland Haeder
35 public class BaseFrameworkSystem implements FrameworkInterface {
40 private static ResourceBundle bundle;
45 private static FrameworkInterface selfInstance;
50 private final Logger LOG;
53 * Application instance
55 private Application application;
60 private Client client;
65 private Contact contact;
70 private Manageable manager;
77 this.LOG = LogManager.getLogger(this);
79 // Need to set it here
84 * No instances can be created of this class
86 protected BaseFrameworkSystem () {
90 * Getter for this application
92 * @return Instance from this application
94 public static final FrameworkInterface getInstance () {
100 public final Application getApplication () {
101 return this.application;
105 public final Logger getLogger () {
110 public final Manageable getManager () {
115 public final String getMessageStringFromKey (final String key) {
117 return this.getBundle().getString(key);
121 * Aborts program with given exception
123 * @param throwable Any type of Throwable
125 protected final void abortProgramWithException (final Throwable throwable) {
127 this.logException(throwable);
134 * Application instance
136 * @param application the application to set
138 protected final void setApplication (final Application application) {
139 this.application = application;
143 public final Client getClient () {
148 * Getter for bundle instance
150 * @return Resource bundle
152 protected final ResourceBundle getBundle () {
153 return BaseFrameworkSystem.bundle;
157 * Setter for bundle instance
159 * @param bundle the bundle to set
161 protected static void setBundle (final ResourceBundle bundle) {
162 BaseFrameworkSystem.bundle = bundle;
168 * @param client the client to set
170 protected final void setClient (final Client client) {
171 this.client = client;
175 public final void logException (final Throwable exception) {
176 // Log this exception
177 this.getLogger().catching(exception);
181 * Some "getter" for an array from given string and tokenizer
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
188 protected String[] getArrayFromString (final String str, final String delimiter) {
190 this.getLogger().trace(MessageFormat.format("str={0},delimiter={1} - CALLED!", str, delimiter)); //NOI18N
193 StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
195 // Init array and index
196 String[] tokens = new String[tokenizer.countTokens()];
199 // Run through all tokens
200 while (tokenizer.hasMoreTokens()) {
201 // Get current token and add it
202 tokens[index] = tokenizer.nextToken();
205 this.getLogger().debug(MessageFormat.format("Token at index{0}: {1}", index, tokens[1])); //NOI18N
212 this.getLogger().trace(MessageFormat.format("tokens({0})={1} - EXIT!", tokens.length, Arrays.toString(tokens))); //NOI18N
219 * Converts null to empty string or leaves original object untouched.
221 * @param object Any string
222 * @return Empty string if null or original string
223 * TODO: Move to own utility class
225 protected Object convertNullToEmpty (final Object object) {
227 this.getLogger().trace(MessageFormat.format("object={0}", object)); //NOI18N
230 if (null == object) {
231 // Return empty string
236 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
245 * @param manager the manager instance to set
247 protected final void setManager (final Manageable manager) {
248 this.manager = manager;
252 * Getter for Contact instance
254 * @return Contact instance
256 protected final Contact getContact () {
261 * Setter for Contact instance
263 * @param contact A Contact instance
265 protected final void setContact (final Contact contact) {
266 this.contact = contact;
270 * Initializes i18n bundles
272 protected void initBundle () {
274 this.getLogger().trace("CALLED!"); //NOI18N
276 // Is the bundle set?
277 if (BaseFrameworkSystem.isBundledInitialized()) {
279 throw new IllegalStateException("called twice"); //NOI18N
283 setBundle(ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE)); // NOI18N
286 this.getLogger().trace("EXIT!"); //NOI18N
290 * Checks if the bundle is initialized
292 * @return Whether the bundle has been initialized
294 protected static boolean isBundledInitialized () {
296 return (bundle instanceof ResourceBundle);