]> git.mxchange.org Git - addressbook-lib.git/blobdiff - src/org/mxchange/addressbook/client/BaseAddressbookClient.java
Prepared for upcoming rewrite:
[addressbook-lib.git] / src / org / mxchange / addressbook / client / BaseAddressbookClient.java
index 8f0dc1aae6374bae4484b98b46cc7718980b52b7..094d0ecee9a485b2d3369cbbb86dfc1658b0a0fd 100644 (file)
@@ -20,17 +20,20 @@ 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.ManageableAddressbookContact;
+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.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
 
 /**
  * A general addressbook client
- *
- * @author Roland Haeder
- * TODO: Find better name
+ * <p>
+ * @author Roland Haeder TODO: Find better name
  */
 public abstract class BaseAddressbookClient extends BaseClient implements AddressbookClient {
 
@@ -44,17 +47,34 @@ public abstract class BaseAddressbookClient extends BaseClient implements Addres
         */
        private final Map<String, Menu> menus;
 
+       /**
+        * Logger instance
+        */
+       private LoggerBeanLocal logger;
+
        /**
         * No instances can be created of this class
         */
        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
-        *
+        * <p>
         * @return the currentMenu
         */
        public final String getCurrentMenu () {
@@ -68,11 +88,11 @@ public abstract class BaseAddressbookClient extends BaseClient implements Addres
 
        /**
         * "Getter" for given menu type
-        *
+        * <p>
         * @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;
 
@@ -93,7 +113,7 @@ public abstract class BaseAddressbookClient extends BaseClient implements Addres
 
        /**
         * Getter for menus map
-        *
+        * <p>
         * @return Map of all menus
         */
        protected final Map<String, Menu> getMenus () {
@@ -102,39 +122,40 @@ public abstract class BaseAddressbookClient extends BaseClient implements Addres
 
        /**
         * Initializes contact manager
-        *
+        * <p>
         * @throws java.sql.SQLException If any SQL error occurs
         */
        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
-               ManageableAddressbookContact manager = new AddressbookContactManager((Client) this);
+               ManageableContactAddressbook manager = new AddressbookContactManager((Client) this);
 
                // Set it here
                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
        }
 
        /**
         * Shows given menu
-        *
+        * <p>
         * @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?
@@ -148,6 +169,38 @@ public abstract class BaseAddressbookClient extends BaseClient implements Addres
                menu.show(this);
 
                // Trace message
-               this.getLogger().trace("EXIT!"); //NOI18N
+               this.getLogger().logTrace("EXIT!"); //NOI18N
+       }
+
+       /**
+        * Getter for logger instance
+        *
+        * @return Logger instance
+        */
+       protected LoggerBeanLocal getLogger () {
+               return this.logger;
+       }
+
+       /**
+        * Logs an exception
+        *
+        * @param throwable Throwable
+        */
+       protected void logException (final Throwable throwable) {
+               // Deligate to logger
+               this.getLogger().logException(throwable);
+       }
+
+       /**
+        * Logs exception and exits program
+        *
+        * @param throwable Throwable
+        */
+       protected void abortProgramWithException (final Throwable throwable) {
+               // Log exception
+               this.logException(throwable);
+
+               // Abort here
+               System.exit(1);
        }
 }