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 * Bootstraps application
98 public void doBootstrap () {
99 this.getLogger().logDebug("Initializing application ..."); //NOI18N
101 // Init client variable
102 Client client = null;
104 // Is console or Swing choosen?
105 if (this.isConsole()) {
107 this.getLogger().logDebug("Initializing console client ..."); //NOI18N
109 // Init console client instance
110 client = new ConsoleClient(this);
111 } else if (this.isGui()) {
113 this.getLogger().logDebug("Initializing GUI (Swing) client ..."); //NOI18N
115 // Init console instance
116 client = new SwingClient(this);
118 // Not client choosen
119 this.getLogger().logError("No client choosen. Cannot launch."); //NOI18N
123 } catch (final SQLException | IOException ex) {
125 this.abortProgramWithException(ex);
135 // Set client instance
136 this.setClient(client);
138 // The application is running at this point
139 this.getClient().enableIsRunning();
142 this.getLogger().logTrace("EXIT!"); //NOI18N
146 * Main loop of the application
149 public void doMainLoop () throws MenuInitializationException {
150 // Get client and cast it
151 AddressbookClient client = (AddressbookClient) this.getClient();
154 this.getLogger().logTrace("CALLED!"); //NOI18N
156 // TODO The application should be running now
157 // Output introduction
160 // Set current menu to main
161 client.setCurrentMenu("main"); //NOI18N
163 // --- Main loop starts here ---
164 while (this.getClient().isRunning()) {
165 // The application is still active, show menu selection
166 client.showCurrentMenu();
169 // Ask for user input and run proper method
170 client.doUserMenuChoice();
171 } catch (final UnhandledUserChoiceException ex) {
173 this.getLogger().logException(ex);
177 // Sleep a little to reduce system load
179 } catch (final InterruptedException ex) {
183 // --- Main loop ends here ---
186 this.getLogger().logDebug("Main loop exit - shutting down ..."); //NOI18N
190 * Shuts down the application.
193 public void doShutdown () throws SQLException, IOException {
195 this.getLogger().logTrace("CALLED!"); //NOI18N
198 this.getClient().doShutdown();
200 // Regular exit reached
201 this.getLogger().logInfo("End of program (last line)"); //NOI18N
206 * Initializes this application
208 private void init () {
211 // Init properties file
212 this.initProperties();
213 } catch (final IOException ex) {
215 this.abortProgramWithException(ex);
218 // Is the bundle initialized?
219 if (!isBundledInitialized()) {
220 // Temporary initialize default bundle
221 // TODO The enum Gender uses this
227 * Logs given exception
229 * @param exception Throwable
231 protected void logException (final Throwable exception) {
232 this.getLogger().logException(exception);
236 * Initializes properties
238 private void initProperties () throws IOException {
239 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
243 * Log exception and abort program.
245 * @param throwable Throwable
247 protected void abortProgramWithException (final Throwable throwable) {
249 this.logException(throwable);
256 * Enables console client by setting propper flag
258 private void enableConsoleClient () {
259 this.getLogger().logDebug("Enabling console client (may become optional client) ..."); //NOI18N
260 this.consoleClient = true;
261 this.guiClient = false;
265 * Enables GUI client by setting propper flag
267 private void enableGuiClient () {
268 this.getLogger().logDebug("Enabling GUI client (may become new default client) ..."); //NOI18N
269 this.consoleClient = false;
270 this.guiClient = true;
274 * Checks whether the client shoule be console client should be launched by
275 * checking if -console is set.
277 * @return Whether console client should be taken
279 private boolean isConsole () {
280 return this.consoleClient;
284 * Checks whether the client shoule be GUI client should be launched by
285 * checking if -gui is set.
287 * @return Whether GUI client should be taken
289 private boolean isGui () {
290 return this.guiClient;
294 * Parses all given arguments
296 * @param args Arguments from program launch
298 private void parseArguments (final String[] args) {
300 this.getLogger().logTrace(MessageFormat.format("args()={0} - CALLED!", args.length)); //NOI18N
303 this.getLogger().logDebug(MessageFormat.format("Parsing {0} arguments ...", args.length)); //NOI18N
305 for (final String arg : args) {
308 case "-console": //NOI18N
309 enableConsoleClient();
312 case "-gui": //NOI18N
320 * Show introduction which depends on client
322 private void showIntro () {
324 this.getLogger().logTrace("CALLED!"); //NOI18N
326 // Let the client show it
327 this.getClient().showWelcome();
330 this.getLogger().logTrace("EXIT!"); //NOI18N
334 * Launches the application
336 * @param args Arguments handled to program
338 private void start (final String args[]) {
339 this.getLogger().logInfo("Program is started."); //NOI18N
342 // Init properties file
343 this.initProperties();
344 } catch (final IOException ex) {
345 // Something bad happened
346 this.abortProgramWithException(ex);
350 this.parseArguments(args);
353 // Launch application
354 ApplicationManager.getSingeltonManager(this).start();
355 } catch (final MenuInitializationException ex) {
356 // Something bad happened
357 this.abortProgramWithException(ex);
360 // Good bye, but this should not be reached ...
361 this.getLogger().logWarning("Unusual exit reached."); //NOI18N
365 } catch (final SQLException | IOException ex) {
366 // Something bad happened
367 this.abortProgramWithException(ex);
372 * Main method (entry point)
374 * @param args the command line arguments
376 public static void main (String[] args) {
378 new AddressbookApplication().start(args);
382 * Getter for printable application name
384 * @return A printable name
386 public static String printableTitle () {
387 return MessageFormat.format("{0} v{1}", APP_TITLE, APP_VERSION); //NOI18N
391 * Getter for logger instance
393 * @return Logger instance
395 protected LoggerBeanLocal getLogger () {