2 * Copyright (C) 2016, 2017 Roland Häder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.application;
19 import java.io.IOException;
20 import java.sql.SQLException;
21 import java.text.MessageFormat;
22 import javax.naming.Context;
23 import javax.naming.InitialContext;
24 import javax.naming.NamingException;
25 import org.mxchange.addressbook.client.AddressbookClient;
26 import org.mxchange.addressbook.client.console.ConsoleClient;
27 import org.mxchange.addressbook.client.gui.SwingClient;
28 import org.mxchange.jcore.application.Application;
29 import org.mxchange.jcore.application.BaseApplication;
30 import org.mxchange.jcore.client.Client;
31 import org.mxchange.jcore.exceptions.MenuInitializationException;
32 import org.mxchange.jcore.exceptions.UnhandledUserChoiceException;
33 import org.mxchange.jcore.manager.application.ApplicationManager;
34 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
35 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
38 * Address book application class. Please see ROADMAP.txt for details.
40 * @author Roland Häder<roland@mxchange.org>
43 public class AddressbookApplication extends BaseApplication implements Application {
48 public static final String APP_TITLE = "Adressbuch"; //NOI18N
53 public static final String APP_VERSION = "0.0"; //NOI18N
56 * Main method (entry point)
58 * @param args the command line arguments
60 public static void main (String[] args) {
62 new AddressbookApplication().start(args);
66 * Getter for printable application name
68 * @return A printable name
70 public static String printableTitle () {
71 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
75 * Console client is enabled by default
77 private boolean consoleClient = true;
80 * GUI client is disabled by default
82 private boolean guiClient = false;
88 private LoggerBeanLocal loggerBeanLocal;
91 * Protected constructor
93 protected AddressbookApplication () {
97 Context context = new InitialContext();
99 // Get loggerBeanLocal
100 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
101 } catch (final NamingException ex) {
102 // Output it and exit
103 System.err.println(MessageFormat.format("Cannot initialize: {0}", ex)); //NOI18N
114 public void doBootstrap () {
115 this.getLoggerBeanLocal().logDebug("Initializing application ..."); //NOI18N
117 // Init client variable
118 Client client = null;
120 // Is console or Swing choosen?
121 if (this.isConsole()) {
123 this.getLoggerBeanLocal().logDebug("Initializing console client ..."); //NOI18N
125 // Init console client instance
126 client = new ConsoleClient(this);
127 } else if (this.isGui()) {
129 this.getLoggerBeanLocal().logDebug("Initializing GUI (Swing) client ..."); //NOI18N
131 // Init console instance
132 client = new SwingClient(this);
134 // Not client choosen
135 this.getLoggerBeanLocal().logError("No client choosen. Cannot launch."); //NOI18N
139 } catch (final SQLException | IOException ex) {
141 this.abortProgramWithException(ex);
151 // Set client instance
152 this.setClient(client);
154 // The application is running at this point
155 this.getClient().enableIsRunning();
158 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
162 public void doMainLoop () throws MenuInitializationException {
163 // Get client and cast it
164 AddressbookClient client = (AddressbookClient) this.getClient();
167 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
169 // TODO The application should be running now
170 // Output introduction
173 // Set current menu to main
174 client.setCurrentMenu("main"); //NOI18N
176 // --- Main loop starts here ---
177 while (this.getClient().isRunning()) {
178 // The application is still active, show menu selection
179 client.showCurrentMenu();
182 // Ask for user input and run proper method
183 client.doUserMenuChoice();
184 } catch (final UnhandledUserChoiceException ex) {
186 this.getLoggerBeanLocal().logException(ex);
190 // Sleep a little to reduce system load
192 } catch (final InterruptedException ex) {
196 // --- Main loop ends here ---
199 this.getLoggerBeanLocal().logDebug("Main loop exit - shutting down ..."); //NOI18N
203 * Shuts down the application.
206 public void doShutdown () throws SQLException, IOException {
208 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
211 this.getClient().doShutdown();
213 // Regular exit reached
214 this.getLoggerBeanLocal().logInfo("End of program (last line)"); //NOI18N
219 * Enables console client by setting propper flag
221 private void enableConsoleClient () {
222 this.getLoggerBeanLocal().logDebug("Enabling console client (may become optional client) ..."); //NOI18N
223 this.consoleClient = true;
224 this.guiClient = false;
228 * Enables GUI client by setting propper flag
230 private void enableGuiClient () {
231 this.getLoggerBeanLocal().logDebug("Enabling GUI client (may become new default client) ..."); //NOI18N
232 this.consoleClient = false;
233 this.guiClient = true;
237 * Initializes this application
239 private void init () {
242 // Init properties file
243 this.initProperties();
244 } catch (final IOException ex) {
246 this.abortProgramWithException(ex);
249 // Is the bundle initialized?
250 if (!isBundledInitialized()) {
251 // Temporary initialize default bundle
252 // TODO The enum PersonalTitle uses this
258 * Initializes properties
260 private void initProperties () throws IOException {
261 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
265 * Checks whether the client shoule be console client should be launched by
266 * checking if -console is set.
268 * @return Whether console client should be taken
270 private boolean isConsole () {
271 return this.consoleClient;
275 * Checks whether the client shoule be GUI client should be launched by
276 * checking if -gui is set.
278 * @return Whether GUI client should be taken
280 private boolean isGui () {
281 return this.guiClient;
285 * Parses all given arguments
287 * @param args Arguments from program launch
289 private void parseArguments (final String[] args) {
291 this.getLoggerBeanLocal().logTrace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
293 this.getLoggerBeanLocal().logDebug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
294 for (final String arg : args) {
299 this.enableConsoleClient();
303 this.enableGuiClient();
310 * Show introduction which depends on client
312 private void showIntro () {
314 this.getLoggerBeanLocal().logTrace("CALLED!"); //NOI18N
316 // Let the client show it
317 this.getClient().showWelcome();
320 this.getLoggerBeanLocal().logTrace("EXIT!"); //NOI18N
324 * Launches the application
326 * @param args Arguments handled to program
328 private void start (final String args[]) {
329 this.getLoggerBeanLocal().logInfo("Program is started."); //NOI18N
332 // Init properties file
333 this.initProperties();
334 } catch (final IOException ex) {
335 // Something bad happened
336 this.abortProgramWithException(ex);
340 this.parseArguments(args);
343 // Launch application
344 ApplicationManager.getSingeltonManager(this).start();
345 } catch (final MenuInitializationException ex) {
346 // Something bad happened
347 this.abortProgramWithException(ex);
350 // Good bye, but this should not be reached ...
351 this.getLoggerBeanLocal().logWarning("Unusual exit reached."); //NOI18N
355 } catch (final SQLException | IOException ex) {
356 // Something bad happened
357 this.abortProgramWithException(ex);
362 * Log exception and abort program.
364 * @param throwable Throwable
366 protected void abortProgramWithException (final Throwable throwable) {
368 this.logException(throwable);
375 * Getter for loggerBeanLocal instance
377 * @return Logger instance
379 protected LoggerBeanLocal getLoggerBeanLocal () {
380 return this.loggerBeanLocal;
384 * Logs given exception
386 * @param exception Throwable
388 protected void logException (final Throwable exception) {
389 this.getLoggerBeanLocal().logException(exception);