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.manager.Manageable;
28 import org.mxchange.jcore.model.contact.Contact;
33 * @author Roland Haeder<roland@mxchange.org>
35 public abstract 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 FrameworkInterface getInstance () {
100 public Application getApplication () {
101 return this.application;
105 public Client getClient () {
110 public void logException (final Throwable exception) {
111 // Log this exception
112 this.getLogger().catching(exception);
116 * Converts null to empty string or leaves original object untouched.
118 * @param object Any string
119 * @return Empty string if null or original string TODO: Move to own utility
122 protected Object convertNullToEmpty (final Object object) {
124 this.getLogger().trace(MessageFormat.format("object={0}", object)); //NOI18N
127 if (null == object) {
128 // Return empty string
133 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
140 * Some "getter" for an array from given string and tokenizer
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
147 protected String[] getArrayFromString (final String str, final String delimiter) {
149 this.getLogger().trace(MessageFormat.format("str={0},delimiter={1} - CALLED!", str, delimiter)); //NOI18N
152 StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
154 // Init array and index
155 String[] tokens = new String[tokenizer.countTokens()];
158 // Run through all tokens
159 while (tokenizer.hasMoreTokens()) {
160 // Get current token and add it
161 tokens[index] = tokenizer.nextToken();
164 this.getLogger().debug(MessageFormat.format("Token at index{0}: {1}", index, tokens[1])); //NOI18N
171 this.getLogger().trace(MessageFormat.format("tokens({0})={1} - EXIT!", tokens.length, Arrays.toString(tokens))); //NOI18N
180 * @param client the client to set
182 protected void setClient (final Client client) {
183 this.client = client;
187 * Application instance
189 * @param application the application to set
191 protected void setApplication (final Application application) {
192 this.application = application;
196 public Manageable getManager () {
201 * Getter for Contact instance
203 * @return Contact instance
205 protected Contact getContact () {
210 * Setter for Contact instance
212 * @param contact A Contact instance
214 protected void setContact (final Contact contact) {
215 this.contact = contact;
221 * @param manager the manager instance to set
223 protected void setManager (final Manageable manager) {
224 this.manager = manager;
228 public String getMessageStringFromKey (final String key) {
230 return this.getBundle().getString(key);
234 * Aborts program with given exception
236 * @param throwable Any type of Throwable
238 protected void abortProgramWithException (final Throwable throwable) {
240 this.logException(throwable);
247 * Getter for bundle instance
249 * @return Resource bundle
251 protected ResourceBundle getBundle () {
252 return BaseFrameworkSystem.bundle;
256 * Setter for bundle instance
258 * @param bundle the bundle to set
260 protected static void setBundle (final ResourceBundle bundle) {
261 BaseFrameworkSystem.bundle = bundle;
265 * Getter for logger instance
267 * @return Logger instance
269 protected Logger getLogger () {
274 * Initializes i18n bundles
276 protected void initBundle () {
278 this.getLogger().trace("CALLED!"); //NOI18N
280 // Is the bundle set?
281 if (BaseFrameworkSystem.isBundledInitialized()) {
283 throw new IllegalStateException("called twice"); //NOI18N
287 setBundle(ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE)); // NOI18N
290 this.getLogger().trace("EXIT!"); //NOI18N
294 * Checks if the bundle is initialized
296 * @return Whether the bundle has been initialized
298 protected static boolean isBundledInitialized () {
300 return (bundle instanceof ResourceBundle);