]> git.mxchange.org Git - addressbook-swing.git/blobdiff - src/org/mxchange/addressbook/application/AddressbookApplication.java
Updated copyright year
[addressbook-swing.git] / src / org / mxchange / addressbook / application / AddressbookApplication.java
index 2859e5ca11c5feb02c19190c9f8b5b78c32c4be7..d89899933c0b022bfa7f2c616385f4152760c7df 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Roland Haeder
+ * Copyright (C) 2016 Roland Haeder
  *
  * 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
@@ -19,14 +19,19 @@ package org.mxchange.addressbook.application;
 import java.io.IOException;
 import java.sql.SQLException;
 import java.text.MessageFormat;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
 import org.mxchange.addressbook.client.AddressbookClient;
 import org.mxchange.addressbook.client.console.ConsoleClient;
 import org.mxchange.addressbook.client.gui.SwingClient;
 import org.mxchange.jcore.application.Application;
 import org.mxchange.jcore.application.BaseApplication;
 import org.mxchange.jcore.client.Client;
+import org.mxchange.jcore.exceptions.MenuInitializationException;
 import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
 import org.mxchange.jcore.manager.application.ApplicationManager;
+import org.mxchange.jcoreeelogger.beans.local.logger.Log;
 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
 
 /**
@@ -47,6 +52,25 @@ public class AddressbookApplication extends BaseApplication implements Applicati
         */
        public static final String APP_VERSION = "0.0"; //NOI18N
 
+       /**
+        * Main method (entry point)
+        * <p>
+        * @param args the command line arguments
+        */
+       public static void main (String[] args) {
+               // Start application
+               new AddressbookApplication().start(args);
+       }
+
+       /**
+        * Getter for printable application name
+        * <p>
+        * @return A printable name
+        */
+       public static String printableTitle () {
+               return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
+       }
+
        /**
         * Console client is enabled by default
         */
@@ -60,29 +84,35 @@ public class AddressbookApplication extends BaseApplication implements Applicati
        /**
         * Logger instance
         */
-       private LoggerBeanLocal logger;
+       @Log
+       private LoggerBeanLocal loggerBeanLocal;
 
        /**
         * Protected constructor
         */
        protected AddressbookApplication () {
-               // Init properties file
-               this.initProperties();
+               // Try this
+               try {
+                       // Get context
+                       Context context = new InitialContext();
 
-               // Is the bundle initialized?
-               if (!isBundledInitialized()) {
-                       // Temporary initialize default bundle
-                       // TODO The enum Gender uses this
-                       this.initBundle();
+                       // Get loggerBeanLocal
+                       this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
+               } catch (final NamingException ex) {
+                       // Output it and exit
+                       System.err.println(MessageFormat.format("Cannot initialize: {0}", ex)); //NOI18N
+
+                       // Abort here
+                       System.exit(1);
                }
+
+               // Call init method
+               this.init();
        }
 
-       /**
-        * Bootstraps application
-        */
        @Override
        public void doBootstrap () {
-               this.getLogger().logDebug("Initializing application ..."); //NOI18N
+               this.getLoggerBeanLocal().logDebug("Initializing application ..."); //NOI18N
 
                // Init client variable
                Client client = null;
@@ -90,19 +120,19 @@ public class AddressbookApplication extends BaseApplication implements Applicati
                // Is console or Swing choosen?
                if (this.isConsole()) {
                        // Debug message
-                       this.getLogger().logDebug("Initializing console client ..."); //NOI18N
+                       this.getLoggerBeanLocal().logDebug("Initializing console client ..."); //NOI18N
 
                        // Init console client instance
                        client = new ConsoleClient(this);
                } else if (this.isGui()) {
                        // Debug message
-                       this.getLogger().logDebug("Initializing GUI (Swing) client ..."); //NOI18N
+                       this.getLoggerBeanLocal().logDebug("Initializing GUI (Swing) client ..."); //NOI18N
 
                        // Init console instance
                        client = new SwingClient(this);
                } else {
                        // Not client choosen
-                       this.getLogger().logError("No client choosen. Cannot launch."); //NOI18N
+                       this.getLoggerBeanLocal().logError("No client choosen. Cannot launch."); //NOI18N
 
                        try {
                                this.doShutdown();
@@ -125,19 +155,16 @@ public class AddressbookApplication extends BaseApplication implements Applicati
                this.getClient().enableIsRunning();
 
                // Trace message
-               this.getLogger().logTrace("EXIT!"); //NOI18N
+               this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
        }
 
-       /**
-        * Main loop of the application
-        */
        @Override
-       public void doMainLoop () {
+       public void doMainLoop () throws MenuInitializationException {
                // Get client and cast it
                AddressbookClient client = (AddressbookClient) this.getClient();
 
                // Debug message
-               this.getLogger().logTrace("CALLED!"); //NOI18N
+               this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
 
                // TODO The application should be running now
                // Output introduction
@@ -155,7 +182,8 @@ public class AddressbookApplication extends BaseApplication implements Applicati
                                // Ask for user input and run proper method
                                client.doUserMenuChoice();
                        } catch (final UnhandledUserChoiceException ex) {
-                               this.getLogger().logException(ex);
+                               // Log exception
+                               this.getLoggerBeanLocal().logException(ex);
                        }
 
                        try {
@@ -168,7 +196,7 @@ public class AddressbookApplication extends BaseApplication implements Applicati
                // --- Main loop ends here ---
 
                // Debug message
-               this.getLogger().logDebug("Main loop exit - shutting down ..."); //NOI18N
+               this.getLoggerBeanLocal().logDebug("Main loop exit - shutting down ..."); //NOI18N
        }
 
        /**
@@ -177,60 +205,60 @@ public class AddressbookApplication extends BaseApplication implements Applicati
        @Override
        public void doShutdown () throws SQLException, IOException {
                // Trace message
-               this.getLogger().logTrace("CALLED!"); //NOI18N
+               this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
 
                // Shutdown client
                this.getClient().doShutdown();
 
-               this.getLogger().logInfo("End of program (last line)"); //NOI18N
+               // Regular exit reached
+               this.getLoggerBeanLocal().logInfo("End of program (last line)"); //NOI18N
                System.exit(0);
        }
 
        /**
-        * Logs given exception
-        *
-        * @param exception Throwable
+        * Enables console client by setting propper flag
         */
-       protected void logException (final Throwable exception) {
-               this.getLogger().logException(exception);
+       private void enableConsoleClient () {
+               this.getLoggerBeanLocal().logDebug("Enabling console client (may become optional client) ..."); //NOI18N
+               this.consoleClient = true;
+               this.guiClient = false;
        }
 
        /**
-        * Initializes properties
+        * Enables GUI client by setting propper flag
         */
-       private void initProperties () throws IOException {
-               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+       private void enableGuiClient () {
+               this.getLoggerBeanLocal().logDebug("Enabling GUI client (may become new default client) ..."); //NOI18N
+               this.consoleClient = false;
+               this.guiClient = true;
        }
 
        /**
-        * Log exception and abort program.
-        *
-        * @param throwable Throwable
+        * Initializes this application
         */
-       protected void abortProgramWithException (final Throwable throwable) {
-               // Log exception
-               this.logException(throwable);
-
-               // Abort here
-               System.exit(1);
-       }
+       private void init () {
+               // Try this
+               try {
+                       // Init properties file
+                       this.initProperties();
+               } catch (final IOException ex) {
+                       // Abort here
+                       this.abortProgramWithException(ex);
+               }
 
-       /**
-        * Enables console client by setting propper flag
-        */
-       private void enableConsoleClient () {
-               this.getLogger().logDebug("Enabling console client (may become optional client) ..."); //NOI18N
-               this.consoleClient = true;
-               this.guiClient = false;
+               // Is the bundle initialized?
+               if (!isBundledInitialized()) {
+                       // Temporary initialize default bundle
+                       // TODO The enum Gender uses this
+                       this.initBundle();
+               }
        }
 
        /**
-        * Enables GUI client by setting propper flag
+        * Initializes properties
         */
-       private void enableGuiClient () {
-               this.getLogger().logDebug("Enabling GUI client (may become new default client) ..."); //NOI18N
-               this.consoleClient = false;
-               this.guiClient = true;
+       private void initProperties () throws IOException {
+               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
 
        /**
@@ -260,20 +288,19 @@ public class AddressbookApplication extends BaseApplication implements Applicati
         */
        private void parseArguments (final String[] args) {
                // Trace message
-               this.getLogger().logTrace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
-
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
                // Debug message
-               this.getLogger().logDebug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
-
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
                for (final String arg : args) {
                        // Switch on it
                        switch (arg) {
-                               case "-console": //NOI18N
-                                       enableConsoleClient();
+                               case "-console":
+                                       //NOI18N
+                                       this.enableConsoleClient();
                                        break;
-
-                               case "-gui": //NOI18N
-                                       enableGuiClient();
+                               case "-gui":
+                                       //NOI18N
+                                       this.enableGuiClient();
                                        break;
                        }
                }
@@ -284,13 +311,13 @@ public class AddressbookApplication extends BaseApplication implements Applicati
         */
        private void showIntro () {
                // Trace message
-               this.getLogger().logTrace("CALLED!"); //NOI18N
+               this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
 
                // Let the client show it
                this.getClient().showWelcome();
 
                // Trace message
-               this.getLogger().logTrace("EXIT!"); //NOI18N
+               this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
        }
 
        /**
@@ -299,7 +326,7 @@ public class AddressbookApplication extends BaseApplication implements Applicati
         * @param args Arguments handled to program
         */
        private void start (final String args[]) {
-               this.getLogger().logInfo("Program is started."); //NOI18N
+               this.getLoggerBeanLocal().logInfo("Program is started."); //NOI18N
 
                try {
                        // Init properties file
@@ -312,11 +339,16 @@ public class AddressbookApplication extends BaseApplication implements Applicati
                // Parse arguments
                this.parseArguments(args);
 
-               // Launch application
-               ApplicationManager.getSingeltonManager(this).start();
+               try {
+                       // Launch application
+                       ApplicationManager.getSingeltonManager(this).start();
+               } catch (final MenuInitializationException ex) {
+                       // Something bad happened
+                       this.abortProgramWithException(ex);
+               }
 
                // Good bye, but this should not be reached ...
-               this.getLogger().logWarning("Unusual exit reached."); //NOI18N
+               this.getLoggerBeanLocal().logWarning("Unusual exit reached."); //NOI18N
 
                try {
                        this.doShutdown();
@@ -327,30 +359,35 @@ public class AddressbookApplication extends BaseApplication implements Applicati
        }
 
        /**
-        * Main method (entry point)
+        * Log exception and abort program.
         * <p>
-        * @param args the command line arguments
+        * @param throwable Throwable
         */
-       public static void main (String[] args) {
-               // Start application
-               new AddressbookApplication().start(args);
+       protected void abortProgramWithException (final Throwable throwable) {
+               // Log exception
+               this.logException(throwable);
+
+               // Abort here
+               System.exit(1);
        }
 
        /**
-        * Getter for printable application name
+        * Getter for loggerBeanLocal instance
         * <p>
-        * @return A printable name
+        * @return Logger instance
         */
-       public static String printableTitle () {
-               return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
+       protected LoggerBeanLocal getLoggerBeanLocal () {
+               return this.loggerBeanLocal;
        }
 
        /**
-        * Getter for logger instance
-        *
-        * @return Logger instance
+        * Logs given exception
+        * <p>
+        * @param exception Throwable
         */
-       protected LoggerBeanLocal getLogger () {
-               return this.logger;
+       protected void logException (final Throwable exception) {
+               this.getLoggerBeanLocal().logException(exception);
        }
+
+
 }