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 * Getter for logger instance
107 * @return Logger instance
109 protected Logger getLogger () {
114 public Manageable getManager () {
119 public String getMessageStringFromKey (final String key) {
121 return this.getBundle().getString(key);
125 * Aborts program with given exception
127 * @param throwable Any type of Throwable
129 protected void abortProgramWithException (final Throwable throwable) {
131 this.logException(throwable);
138 * Application instance
140 * @param application the application to set
142 protected void setApplication (final Application application) {
143 this.application = application;
147 public Client getClient () {
152 * Getter for bundle instance
154 * @return Resource bundle
156 protected ResourceBundle getBundle () {
157 return BaseFrameworkSystem.bundle;
161 * Setter for bundle instance
163 * @param bundle the bundle to set
165 protected static void setBundle (final ResourceBundle bundle) {
166 BaseFrameworkSystem.bundle = bundle;
172 * @param client the client to set
174 protected void setClient (final Client client) {
175 this.client = client;
179 public void logException (final Throwable exception) {
180 // Log this exception
181 this.getLogger().catching(exception);
185 * Some "getter" for an array from given string and tokenizer
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
192 protected String[] getArrayFromString (final String str, final String delimiter) {
194 this.getLogger().trace(MessageFormat.format("str={0},delimiter={1} - CALLED!", str, delimiter)); //NOI18N
197 StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
199 // Init array and index
200 String[] tokens = new String[tokenizer.countTokens()];
203 // Run through all tokens
204 while (tokenizer.hasMoreTokens()) {
205 // Get current token and add it
206 tokens[index] = tokenizer.nextToken();
209 this.getLogger().debug(MessageFormat.format("Token at index{0}: {1}", index, tokens[1])); //NOI18N
216 this.getLogger().trace(MessageFormat.format("tokens({0})={1} - EXIT!", tokens.length, Arrays.toString(tokens))); //NOI18N
223 * Converts null to empty string or leaves original object untouched.
225 * @param object Any string
226 * @return Empty string if null or original string
227 * TODO: Move to own utility class
229 protected Object convertNullToEmpty (final Object object) {
231 this.getLogger().trace(MessageFormat.format("object={0}", object)); //NOI18N
234 if (null == object) {
235 // Return empty string
240 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
249 * @param manager the manager instance to set
251 protected void setManager (final Manageable manager) {
252 this.manager = manager;
256 * Getter for Contact instance
258 * @return Contact instance
260 protected Contact getContact () {
265 * Setter for Contact instance
267 * @param contact A Contact instance
269 protected void setContact (final Contact contact) {
270 this.contact = contact;
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);