2 * Copyright (C) 2015 Roland Haeder
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 Haeder
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 * Console client is enabled by default
58 private boolean consoleClient = true;
61 * GUI client is disabled by default
63 private boolean guiClient = false;
69 private LoggerBeanLocal logger;
72 * Protected constructor
74 protected AddressbookApplication () {
78 Context context = new InitialContext();
81 this.logger = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal");
82 } catch (final NamingException ex) {
84 System.err.println(MessageFormat.format("Cannot initialize: {0}", ex));
95 * Main method (entry point)
97 * @param args the command line arguments
99 public static void main (String[] args) {
101 new AddressbookApplication().start(args);
105 * Getter for printable application name
107 * @return A printable name
109 public static String printableTitle () {
110 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
114 public void doBootstrap () {
115 this.getLogger().logDebug("Initializing application ..."); //NOI18N
117 // Init client variable
118 Client client = null;
120 // Is console or Swing choosen?
121 if (this.isConsole()) {
123 this.getLogger().logDebug("Initializing console client ..."); //NOI18N
125 // Init console client instance
126 client = new ConsoleClient(this);
127 } else if (this.isGui()) {
129 this.getLogger().logDebug("Initializing GUI (Swing) client ..."); //NOI18N
131 // Init console instance
132 client = new SwingClient(this);
134 // Not client choosen
135 this.getLogger().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.getLogger().logTrace("EXIT!"); //NOI18N
162 public void doMainLoop () throws MenuInitializationException {
163 // Get client and cast it
164 AddressbookClient client = (AddressbookClient) this.getClient();
167 this.getLogger().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.getLogger().logException(ex);
190 // Sleep a little to reduce system load
192 } catch (final InterruptedException ex) {
196 // --- Main loop ends here ---
199 this.getLogger().logDebug("Main loop exit - shutting down ..."); //NOI18N
203 * Shuts down the application.
206 public void doShutdown () throws SQLException, IOException {
208 this.getLogger().logTrace("CALLED!"); //NOI18N
211 this.getClient().doShutdown();
213 // Regular exit reached
214 this.getLogger().logInfo("End of program (last line)"); //NOI18N
219 * Enables console client by setting propper flag
221 private void enableConsoleClient () {
222 this.getLogger().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.getLogger().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 Gender 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.getLogger().logTrace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
294 this.getLogger().logDebug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
296 for (final String arg : args) {
299 case "-console": //NOI18N
300 enableConsoleClient();
303 case "-gui": //NOI18N
311 * Show introduction which depends on client
313 private void showIntro () {
315 this.getLogger().logTrace("CALLED!"); //NOI18N
317 // Let the client show it
318 this.getClient().showWelcome();
321 this.getLogger().logTrace("EXIT!"); //NOI18N
325 * Launches the application
327 * @param args Arguments handled to program
329 private void start (final String args[]) {
330 this.getLogger().logInfo("Program is started."); //NOI18N
333 // Init properties file
334 this.initProperties();
335 } catch (final IOException ex) {
336 // Something bad happened
337 this.abortProgramWithException(ex);
341 this.parseArguments(args);
344 // Launch application
345 ApplicationManager.getSingeltonManager(this).start();
346 } catch (final MenuInitializationException ex) {
347 // Something bad happened
348 this.abortProgramWithException(ex);
351 // Good bye, but this should not be reached ...
352 this.getLogger().logWarning("Unusual exit reached."); //NOI18N
356 } catch (final SQLException | IOException ex) {
357 // Something bad happened
358 this.abortProgramWithException(ex);
363 * Log exception and abort program.
365 * @param throwable Throwable
367 protected void abortProgramWithException (final Throwable throwable) {
369 this.logException(throwable);
376 * Getter for logger instance
378 * @return Logger instance
380 protected LoggerBeanLocal getLogger () {
385 * Logs given exception
387 * @param exception Throwable
389 protected void logException (final Throwable exception) {
390 this.getLogger().logException(exception);