+++ /dev/null
-/*
- * Copyright (C) 2015 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
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-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;
-
-/**
- * Address book application class. Please see ROADMAP.txt for details.
- * <p>
- * @author Roland Haeder
- * @version 0.0
- */
-public class AddressbookApplication extends BaseApplication implements Application {
-
- /**
- * Application title
- */
- public static final String APP_TITLE = "Adressbuch"; //NOI18N
-
- /**
- * Application version
- */
- public static final String APP_VERSION = "0.0"; //NOI18N
-
- /**
- * Console client is enabled by default
- */
- private boolean consoleClient = true;
-
- /**
- * GUI client is disabled by default
- */
- private boolean guiClient = false;
-
- /**
- * Logger instance
- */
- @Log
- private LoggerBeanLocal logger;
-
- /**
- * Protected constructor
- */
- protected AddressbookApplication () {
- // Try this
- try {
- // Get context
- Context context = new InitialContext();
-
- // Get logger
- this.logger = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal");
- } catch (final NamingException ex) {
- // Output it and exit
- System.err.println(MessageFormat.format("Cannot initialize: {0}", ex));
-
- // Abort here
- System.exit(1);
- }
-
- // Call init method
- this.init();
- }
-
- /**
- * 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
- }
-
- @Override
- public void doBootstrap () {
- this.getLogger().logDebug("Initializing application ..."); //NOI18N
-
- // Init client variable
- Client client = null;
-
- // Is console or Swing choosen?
- if (this.isConsole()) {
- // Debug message
- this.getLogger().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
-
- // Init console instance
- client = new SwingClient(this);
- } else {
- // Not client choosen
- this.getLogger().logError("No client choosen. Cannot launch."); //NOI18N
-
- try {
- this.doShutdown();
- } catch (final SQLException | IOException ex) {
- // Abort here
- this.abortProgramWithException(ex);
- }
-
- // Bye ...
- System.exit(1);
- }
-
- // Init client
- client.init();
-
- // Set client instance
- this.setClient(client);
-
- // The application is running at this point
- this.getClient().enableIsRunning();
-
- // Trace message
- this.getLogger().logTrace("EXIT!"); //NOI18N
- }
-
- @Override
- public void doMainLoop () throws MenuInitializationException {
- // Get client and cast it
- AddressbookClient client = (AddressbookClient) this.getClient();
-
- // Debug message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // TODO The application should be running now
- // Output introduction
- this.showIntro();
-
- // Set current menu to main
- client.setCurrentMenu("main"); //NOI18N
-
- // --- Main loop starts here ---
- while (this.getClient().isRunning()) {
- // The application is still active, show menu selection
- client.showCurrentMenu();
-
- try {
- // Ask for user input and run proper method
- client.doUserMenuChoice();
- } catch (final UnhandledUserChoiceException ex) {
- // Log exception
- this.getLogger().logException(ex);
- }
-
- try {
- // Sleep a little to reduce system load
- Thread.sleep(100);
- } catch (final InterruptedException ex) {
- // Ignore it
- }
- }
- // --- Main loop ends here ---
-
- // Debug message
- this.getLogger().logDebug("Main loop exit - shutting down ..."); //NOI18N
- }
-
- /**
- * Shuts down the application.
- */
- @Override
- public void doShutdown () throws SQLException, IOException {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Shutdown client
- this.getClient().doShutdown();
-
- // Regular exit reached
- this.getLogger().logInfo("End of program (last line)"); //NOI18N
- System.exit(0);
- }
-
- /**
- * 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;
- }
-
- /**
- * Enables GUI client by setting propper flag
- */
- private void enableGuiClient () {
- this.getLogger().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 () {
- return this.consoleClient;
- }
-
- /**
- * 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 () {
- return this.guiClient;
- }
-
- /**
- * Parses all given arguments
- * <p>
- * @param args Arguments from program launch
- */
- private void parseArguments (final String[] args) {
- // Trace message
- this.getLogger().logTrace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
-
- // Debug message
- this.getLogger().logDebug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
-
- for (final String arg : args) {
- // Switch on it
- switch (arg) {
- case "-console": //NOI18N
- enableConsoleClient();
- break;
-
- case "-gui": //NOI18N
- enableGuiClient();
- break;
- }
- }
- }
-
- /**
- * Show introduction which depends on client
- */
- private void showIntro () {
- // Trace message
- this.getLogger().logTrace("CALLED!"); //NOI18N
-
- // Let the client show it
- this.getClient().showWelcome();
-
- // Trace message
- this.getLogger().logTrace("EXIT!"); //NOI18N
- }
-
- /**
- * Launches the application
- * <p>
- * @param args Arguments handled to program
- */
- private void start (final String args[]) {
- this.getLogger().logInfo("Program is started."); //NOI18N
-
- try {
- // Init properties file
- this.initProperties();
- } catch (final IOException ex) {
- // Something bad happened
- this.abortProgramWithException(ex);
- }
-
- // Parse arguments
- this.parseArguments(args);
-
- 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
-
- try {
- this.doShutdown();
- } catch (final SQLException | IOException ex) {
- // Something bad happened
- this.abortProgramWithException(ex);
- }
- }
-
- /**
- * Log exception and abort program.
- * <p>
- * @param throwable Throwable
- */
- protected void abortProgramWithException (final Throwable throwable) {
- // Log exception
- this.logException(throwable);
-
- // Abort here
- System.exit(1);
- }
-
- /**
- * Getter for logger instance
- * <p>
- * @return Logger instance
- */
- protected LoggerBeanLocal getLogger () {
- return this.logger;
- }
-
- /**
- * Logs given exception
- * <p>
- * @param exception Throwable
- */
- protected void logException (final Throwable exception) {
- this.getLogger().logException(exception);
- }
-
-}