]> 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 4dbd1d0df62f603e392a0ea1e7b7a085a31f66a7..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,107 +19,28 @@ package org.mxchange.addressbook.application;
 import java.io.IOException;
 import java.sql.SQLException;
 import java.text.MessageFormat;
-import org.mxchange.addressbook.BaseAddressbookSystem;
+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.BaseFrameworkSystem;
 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;
 
 /**
- * ============================================
- * AddressbookApplication management:
- * ============================================
- * 
- * Inernet("public" service) and Intranet
- * 
- * Version 1.0+:
- * - Single-user local application
- * - Fields:
- *   + Gender
- *   + Surname
- *   + Family name
- *   + Company name
- *   + Street + number
- *   + ZIP code
- *   + City
- *   + Landline number
- *   + Fax number
- *   + Cell phone number
- *   + Email address
- *   + Birth day
- *   + Comment (?)
- * - Edit own data
- * - Add new contact
- * - Edit contacts
- * - Delete contacts
- * - Categorization of contacts
- * 
- * Version 1.1+:
- * - Permanent storage in database
- * 
- * Version 2.0+:
- * - Multi-user web application
- * - Local user registration / login / resend confirmation link / password
- *   recovery
- * - User groups (aka. teams)
- * - Administration area (user role)
- *   + Create/edit/delete groups
- *   + Edit/delete/lock/unlock user
- *   + Assign user roles/rights
- * - Allow other users / groups to view addressbook
- *   + Full addressbook
- *   + Only some categories
- * - VCard export
- *   + Allow users/guests (not recommended)
- * - XML export of addressbook and compressable (ZIP)
- * - Form to contact other user/group without need of mail program
- *   + User can disabled this
- * - Directory for ussers/groups (who allowed to be listed)
- *   + Simple click to add user to own addressbook
- *   + Search form?
- * 
- * Version 2.1+:
- * - Multi-language support
- * 
- * Version 2.2+:("socialized")
- * - "Social login" (OpenID consumer)
- *   + Connect user account to social account
- *   + Sync own data?
- * - "Social profile"
- *   + OpenID provider
- *   + RSS/activity feed 
- * 
- * ============================================
- * Time esitmation:
- * ============================================
- * 1.0 (console):
- *   + 2 days
- * 
- * 1.1 (database):
- *   + 2 day
- *   + Initial tables: contacts, categories, contact_category
- * 
- * 2.0 (web):
- *   + 3 days
- *   + Additional tables: admins (?), admin_rights, groups,
- *    users, user_contacts, user_user_rights, user_category_rights, 
- * 
- * 2.1 (language)
- *   + 1 day
- *   + Additional tables: languages (disable, enable language "pack" ?)
- * 
- * 2.2 (social):
- *   + 3 days
- *   + Additional tables: ???
-*
+ * Address book application class. Please see ROADMAP.txt for details.
+ * <p>
  * @author Roland Haeder
  * @version 0.0
  */
-public class AddressbookApplication extends BaseAddressbookSystem implements Application {
+public class AddressbookApplication extends BaseApplication implements Application {
 
        /**
         * Application title
@@ -131,6 +52,25 @@ public class AddressbookApplication extends BaseAddressbookSystem implements App
         */
        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
         */
@@ -141,28 +81,38 @@ public class AddressbookApplication extends BaseAddressbookSystem implements App
         */
        private boolean guiClient = false;
 
+       /**
+        * Logger instance
+        */
+       @Log
+       private LoggerBeanLocal loggerBeanLocal;
+
        /**
         * Protected constructor
-        * @throws java.io.IOException If any IO error occurs
         */
-       protected AddressbookApplication () throws IOException {
-               // Init properties file
-               this.initProperties();
+       protected AddressbookApplication () {
+               // Try this
+               try {
+                       // Get context
+                       Context context = new InitialContext();
 
-               // Is the bundle initialized?
-               if (!this.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().debug("Initializing application ..."); //NOI18N
+               this.getLoggerBeanLocal().logDebug("Initializing application ..."); //NOI18N
 
                // Init client variable
                Client client = null;
@@ -170,19 +120,28 @@ public class AddressbookApplication extends BaseAddressbookSystem implements App
                // Is console or Swing choosen?
                if (this.isConsole()) {
                        // Debug message
-                       this.getLogger().debug("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().debug("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().error("No client choosen. Cannot launch."); //NOI18N
+                       this.getLoggerBeanLocal().logError("No client choosen. Cannot launch."); //NOI18N
+
+                       try {
+                               this.doShutdown();
+                       } catch (final SQLException | IOException ex) {
+                               // Abort here
+                               this.abortProgramWithException(ex);
+                       }
+
+                       // Bye ...
                        System.exit(1);
                }
 
@@ -196,21 +155,18 @@ public class AddressbookApplication extends BaseAddressbookSystem implements App
                this.getClient().enableIsRunning();
 
                // Trace message
-               this.getLogger().trace("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().trace("CALLED!"); //NOI18N
+               this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
 
-               // @TODO The application should be running now
+               // TODO The application should be running now
                // Output introduction
                this.showIntro();
 
@@ -226,7 +182,8 @@ public class AddressbookApplication extends BaseAddressbookSystem implements App
                                // Ask for user input and run proper method
                                client.doUserMenuChoice();
                        } catch (final UnhandledUserChoiceException ex) {
-                               this.getLogger().catching(ex);
+                               // Log exception
+                               this.getLoggerBeanLocal().logException(ex);
                        }
 
                        try {
@@ -239,7 +196,7 @@ public class AddressbookApplication extends BaseAddressbookSystem implements App
                // --- Main loop ends here ---
 
                // Debug message
-               this.getLogger().debug("Main loop exit - shutting down ..."); //NOI18N
+               this.getLoggerBeanLocal().logDebug("Main loop exit - shutting down ..."); //NOI18N
        }
 
        /**
@@ -248,12 +205,13 @@ public class AddressbookApplication extends BaseAddressbookSystem implements App
        @Override
        public void doShutdown () throws SQLException, IOException {
                // Trace message
-               this.getLogger().trace("CALLED!"); //NOI18N
-               
+               this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
+
                // Shutdown client
                this.getClient().doShutdown();
-               
-               this.getLogger().info("End of program (last line)"); //NOI18N
+
+               // Regular exit reached
+               this.getLoggerBeanLocal().logInfo("End of program (last line)"); //NOI18N
                System.exit(0);
        }
 
@@ -261,7 +219,7 @@ public class AddressbookApplication extends BaseAddressbookSystem implements App
         * Enables console client by setting propper flag
         */
        private void enableConsoleClient () {
-               this.getLogger().debug("Enabling console client (may become optional client) ..."); //NOI18N
+               this.getLoggerBeanLocal().logDebug("Enabling console client (may become optional client) ..."); //NOI18N
                this.consoleClient = true;
                this.guiClient = false;
        }
@@ -270,15 +228,43 @@ public class AddressbookApplication extends BaseAddressbookSystem implements App
         * Enables GUI client by setting propper flag
         */
        private void enableGuiClient () {
-               this.getLogger().debug("Enabling GUI client (may become new default client) ..."); //NOI18N
+               this.getLoggerBeanLocal().logDebug("Enabling GUI client (may become new default client) ..."); //NOI18N
                this.consoleClient = false;
                this.guiClient = true;
        }
 
+       /**
+        * Initializes this application
+        */
+       private void init () {
+               // Try this
+               try {
+                       // Init properties file
+                       this.initProperties();
+               } catch (final IOException ex) {
+                       // Abort here
+                       this.abortProgramWithException(ex);
+               }
+
+               // Is the bundle initialized?
+               if (!isBundledInitialized()) {
+                       // Temporary initialize default bundle
+                       // TODO The enum Gender uses this
+                       this.initBundle();
+               }
+       }
+
+       /**
+        * Initializes properties
+        */
+       private void initProperties () throws IOException {
+               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+       }
+
        /**
         * Checks whether the client shoule be console client should be launched by
         * checking if -console is set.
-        *
+        * <p>
         * @return Whether console client should be taken
         */
        private boolean isConsole () {
@@ -288,7 +274,7 @@ public class AddressbookApplication extends BaseAddressbookSystem implements App
        /**
         * Checks whether the client shoule be GUI client should be launched by
         * checking if -gui is set.
-        *
+        * <p>
         * @return Whether GUI client should be taken
         */
        private boolean isGui () {
@@ -297,25 +283,24 @@ public class AddressbookApplication extends BaseAddressbookSystem implements App
 
        /**
         * Parses all given arguments
-        *
+        * <p>
         * @param args Arguments from program launch
         */
        private void parseArguments (final String[] args) {
                // Trace message
-               this.getLogger().trace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
-
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
                // Debug message
-               this.getLogger().debug(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;
                        }
                }
@@ -326,22 +311,23 @@ public class AddressbookApplication extends BaseAddressbookSystem implements App
         */
        private void showIntro () {
                // Trace message
-               this.getLogger().trace("CALLED!"); //NOI18N
+               this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
 
                // Let the client show it
                this.getClient().showWelcome();
 
                // Trace message
-               this.getLogger().trace("EXIT!"); //NOI18N
+               this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
        }
 
        /**
         * Launches the application
-        *
+        * <p>
         * @param args Arguments handled to program
         */
        private void start (final String args[]) {
-               this.getLogger().info("Program is started."); //NOI18N
+               this.getLoggerBeanLocal().logInfo("Program is started."); //NOI18N
+
                try {
                        // Init properties file
                        this.initProperties();
@@ -353,40 +339,55 @@ public class AddressbookApplication extends BaseAddressbookSystem implements App
                // Parse arguments
                this.parseArguments(args);
 
-               // Launch application
-               ApplicationManager.getManager(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().warn("Unusual exit reached."); //NOI18N
+               this.getLoggerBeanLocal().logWarning("Unusual exit reached."); //NOI18N
+
                try {
                        this.doShutdown();
                } catch (final SQLException | IOException ex) {
+                       // Something bad happened
                        this.abortProgramWithException(ex);
                }
        }
 
        /**
-        * Main method (entry point)
-        *
-        * @param args the command line arguments
+        * Log exception and abort program.
+        * <p>
+        * @param throwable Throwable
         */
-       public static void main (String[] args) {
-               try {
-                       // Start application
-                       new AddressbookApplication().start(args);
-               } catch (final IOException ex) {
-                       // Get instance
-                       BaseFrameworkSystem.getInstance().getLogger().catching(ex);
-                       System.exit(1);
-               }
+       protected void abortProgramWithException (final Throwable throwable) {
+               // Log exception
+               this.logException(throwable);
+
+               // Abort here
+               System.exit(1);
        }
 
        /**
-        * Getter for printable application name
-        *
-        * @return A printable name
+        * Getter for loggerBeanLocal instance
+        * <p>
+        * @return Logger instance
         */
-       public static String printableTitle () {
-               return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
+       protected LoggerBeanLocal getLoggerBeanLocal () {
+               return this.loggerBeanLocal;
+       }
+
+       /**
+        * Logs given exception
+        * <p>
+        * @param exception Throwable
+        */
+       protected void logException (final Throwable exception) {
+               this.getLoggerBeanLocal().logException(exception);
        }
+
+
 }