X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Forg%2Fmxchange%2Faddressbook%2Fclient%2FBaseAddressbookClient.java;h=b566d764854f8681ddbc63338de9c20c27b812e8;hb=528eeece943480f5a89d0067cfb6e4cb86f528b5;hp=679bb4a727f12f7794e1d7285d1449e9cb0458e1;hpb=d08c925dc84522e24fc7955d37ab667bb0abceee;p=addressbook-swing.git diff --git a/src/org/mxchange/addressbook/client/BaseAddressbookClient.java b/src/org/mxchange/addressbook/client/BaseAddressbookClient.java index 679bb4a..b566d76 100644 --- a/src/org/mxchange/addressbook/client/BaseAddressbookClient.java +++ b/src/org/mxchange/addressbook/client/BaseAddressbookClient.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 Roland Haeder + * Copyright (C) 2016 - 2020 Free Software Foundation * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,18 +18,23 @@ package org.mxchange.addressbook.client; import java.sql.SQLException; import java.text.MessageFormat; +import java.util.Collections; import java.util.HashMap; import java.util.Map; -import org.mxchange.addressbook.manager.contact.AddressbookContactManager; -import org.mxchange.addressbook.manager.contact.ManageableAddressbookContact; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import org.mxchange.addressbook.facade.contact.AddressbookContactFacade; +import org.mxchange.addressbook.facade.contact.ContactFacade; import org.mxchange.addressbook.menu.Menu; import org.mxchange.jcore.client.BaseClient; -import org.mxchange.jcore.client.Client; +import org.mxchange.jcoreeelogger.beans.local.logger.Log; +import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; /** * A general addressbook client - * - * @author Roland Haeder + *

+ * @author Roland Häder TODO: Find better name */ public abstract class BaseAddressbookClient extends BaseClient implements AddressbookClient { @@ -38,6 +43,12 @@ public abstract class BaseAddressbookClient extends BaseClient implements Addres */ private String currentMenu; + /** + * Logger instance + */ + @Log + private LoggerBeanLocal loggerBeanLocal; + /** * Menu system */ @@ -49,29 +60,42 @@ public abstract class BaseAddressbookClient extends BaseClient implements Addres protected BaseAddressbookClient () { // Init menu map this.menus = new HashMap<>(10); + + // Try it + try { + // Get context + Context context = new InitialContext(); + + // Lookup loggerBeanLocal + this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw + throw new RuntimeException(ex); + } } /** * Current menu choice - * + *

* @return the currentMenu */ - public final String getCurrentMenu () { + public String getCurrentMenu () { return this.currentMenu; } @Override - public final void setCurrentMenu (final String currentMenu) { + public void setCurrentMenu (final String currentMenu) { this.currentMenu = currentMenu; } /** * "Getter" for given menu type - * + *

* @param menuType Menu type instance to return + *

* @return Menu or null if not found */ - public Menu getMenu (final String menuType) { + private Menu getMenu (final String menuType) { // Default is not found Menu menu = null; @@ -85,55 +109,88 @@ public abstract class BaseAddressbookClient extends BaseClient implements Addres return menu; } + /** + * Logs exception and exits program + *

+ * @param throwable Throwable + */ + protected void abortProgramWithException (final Throwable throwable) { + // Log exception + this.logException(throwable); + + // Abort here + System.exit(1); + } + /** * Fills menu map with swing menus */ protected abstract void fillMenuMap (); + /** + * Getter for loggerBeanLocal instance + *

+ * @return Logger instance + */ + protected LoggerBeanLocal getLoggerBeanLocal () { + return this.loggerBeanLocal; + } + /** * Getter for menus map - * + *

* @return Map of all menus */ - protected final Map getMenus () { - return this.menus; + protected Map getMenus () { + return Collections.unmodifiableMap(this.menus); } /** * Initializes contact manager - * + *

* @throws java.sql.SQLException If any SQL error occurs */ protected void initContactManager () throws SQLException { // Trace message - this.getLogger().trace("CALLED!"); //NOI18N + this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N // Debug message - this.getLogger().debug("Initializing contact manager ..."); //NOI18N + this.getLoggerBeanLocal().logDebug("Initializing contact manager ..."); //NOI18N - // Init contact manager with console client + // Init contact facade with console client // TODO Static initial amount of contacts - ManageableAddressbookContact manager = new AddressbookContactManager((Client) this); + ContactFacade facade = new AddressbookContactFacade(this); // Set it here - this.setManager(manager); + this.setFacade(facade); // Debug message - this.getLogger().debug("Contact manager has been initialized."); //NOI18N + this.getLoggerBeanLocal().logDebug("Contact manager has been initialized."); //NOI18N // Trace message - this.getLogger().trace("EXIT!"); //NOI18N + this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N + } + + /** + * Logs an exception + *

+ * @param throwable Throwable + */ + protected void logException (final Throwable throwable) { + // Deligate to loggerBeanLocal + this.getLoggerBeanLocal().logException(throwable); } /** * Shows given menu - * + *

* @param menuType Given menu to show */ protected void showMenu (final String menuType) { // Trace message - this.getLogger().trace(MessageFormat.format("menuType={0} - CALLED!", menuType)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("menuType={0} - CALLED!", menuType)); //NOI18N + // Get menu from type Menu menu = this.getMenu(menuType); // Is the menu set? @@ -147,6 +204,6 @@ public abstract class BaseAddressbookClient extends BaseClient implements Addres menu.show(this); // Trace message - this.getLogger().trace("EXIT!"); //NOI18N + this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N } }