/*
- * 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
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;
/**
*/
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
*/
/**
* 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;
// 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();
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
// 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 {
// --- Main loop ends here ---
// Debug message
- this.getLogger().logDebug("Main loop exit - shutting down ..."); //NOI18N
+ this.getLoggerBeanLocal().logDebug("Main loop exit - shutting down ..."); //NOI18N
}
/**
@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.
}
/**
*/
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;
}
}
*/
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
}
/**
* @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
// 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();
}
/**
- * 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);
}
+
+
}