X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Forg%2Fmxchange%2Faddressbook%2Fclient%2FBaseAddressbookClient.java;h=effa814cc046867f8493481c1733f0e5ff2171cc;hb=d320d13f3f26ab01c36a7d6857918b141977454a;hp=f14cd81cac9566c3cc7671198e4c13c2b39ffdba;hpb=3253eb90031a4b74ac6e2837a1c0af45c3cd470a;p=addressbook-swing.git diff --git a/src/org/mxchange/addressbook/client/BaseAddressbookClient.java b/src/org/mxchange/addressbook/client/BaseAddressbookClient.java index f14cd81..effa814 100644 --- a/src/org/mxchange/addressbook/client/BaseAddressbookClient.java +++ b/src/org/mxchange/addressbook/client/BaseAddressbookClient.java @@ -20,25 +20,35 @@ import java.sql.SQLException; import java.text.MessageFormat; import java.util.HashMap; import java.util.Map; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; import org.mxchange.addressbook.manager.contact.AddressbookContactManager; +import org.mxchange.addressbook.manager.contact.ManageableContactAddressbook; import org.mxchange.addressbook.menu.Menu; import org.mxchange.jcore.client.BaseClient; import org.mxchange.jcore.client.Client; -import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException; -import org.mxchange.jcore.manager.database.ManageableDatabase; +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 Haeder TODO: Find better name */ -public abstract class BaseAddressbookClient extends BaseClient { +public abstract class BaseAddressbookClient extends BaseClient implements AddressbookClient { /** * Current menu choice */ private String currentMenu; + /** + * Logger instance + */ + @Log + private LoggerBeanLocal logger; + /** * Menu system */ @@ -50,33 +60,41 @@ public abstract class BaseAddressbookClient extends BaseClient { protected BaseAddressbookClient () { // Init menu map this.menus = new HashMap<>(10); + + // Try it + try { + // Get context + Context context = new InitialContext(); + + // Lookup logger + this.logger = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); + } catch (final NamingException ex) { + // Continue to throw + throw new RuntimeException(ex); + } } /** * Current menu choice - * + *
* @return the currentMenu */ public final String getCurrentMenu () { return this.currentMenu; } - /** - * Current menu choice - * - * @param currentMenu the currentMenu to set - */ + @Override public final 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; @@ -90,14 +108,36 @@ public abstract class BaseAddressbookClient extends BaseClient { 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 logger instance + *
+ * @return Logger instance + */ + protected LoggerBeanLocal getLogger () { + return this.logger; + } + /** * Getter for menus map - * + *
* @return Map of all menus
*/
protected final Map
* @throws java.sql.SQLException If any SQL error occurs
*/
- protected void initContactManager () throws UnsupportedDatabaseBackendException, SQLException {
+ protected void initContactManager () throws SQLException {
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
-
+ this.getLogger().logTrace("CALLED!"); //NOI18N
+
// Debug message
- this.getLogger().debug("Initializing contact manager ..."); //NOI18N
-
+ this.getLogger().logDebug("Initializing contact manager ..."); //NOI18N
+
// Init contact manager with console client
// TODO Static initial amount of contacts
- ManageableDatabase manager = new AddressbookContactManager((Client) this);
-
+ ManageableContactAddressbook manager = new AddressbookContactManager((Client) this);
+
// Set it here
- this.setContactManager(manager);
-
+ this.setManager(manager);
+
// Debug message
- this.getLogger().debug("Contact manager has been initialized."); //NOI18N
-
+ this.getLogger().logDebug("Contact manager has been initialized."); //NOI18N
+
// Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
+ this.getLogger().logTrace("EXIT!"); //NOI18N
+ }
+
+ /**
+ * Logs an exception
+ *
+ * @param throwable Throwable
+ */
+ protected void logException (final Throwable throwable) {
+ // Deligate to logger
+ this.getLogger().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.getLogger().logTrace(MessageFormat.format("menuType={0} - CALLED!", menuType)); //NOI18N
+ // Get menu from type
Menu menu = this.getMenu(menuType);
// Is the menu set?
@@ -152,6 +203,6 @@ public abstract class BaseAddressbookClient extends BaseClient {
menu.show(this);
// Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
+ this.getLogger().logTrace("EXIT!"); //NOI18N
}
}