package envoy.client.ui; import java.io.File; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.stage.Stage; import envoy.client.data.*; import envoy.client.net.Client; import envoy.client.ui.SceneContext.SceneInfo; import envoy.client.ui.controller.LoginScene; import envoy.data.GroupMessage; import envoy.data.Message; import envoy.event.GroupMessageStatusChange; import envoy.event.MessageStatusChange; import envoy.util.EnvoyLog; /** * Handles application startup and shutdown. *

* Project: envoy-client
* File: Startup.java
* Created: 26.03.2020
* * @author Kai S. K. Engelbart * @author Maximilian Käfer * @since Envoy Client v0.1-beta */ public final class Startup extends Application { /** * The version of this client. Used to verify compatibility with the server. * * @since Envoy Client v0.1-beta */ public static final String VERSION = "0.1-beta"; private LocalDB localDB; private Client client; private static final ClientConfig config = ClientConfig.getInstance(); private static final Logger logger = EnvoyLog.getLogger(Startup.class); /** * Loads the configuration, initializes the client and the local database and * delegates the rest of the startup process to {@link LoginScene}. * * @since Envoy Client v0.1-beta */ @Override public void start(Stage stage) throws Exception { try { config.loadAll(Startup.class, "client.properties", getParameters().getRaw().toArray(new String[0])); EnvoyLog.initialize(config); } catch (final IllegalStateException e) { new Alert(AlertType.ERROR, "Error loading configuration values:\n" + e); logger.log(Level.SEVERE, "Error loading configuration values: ", e); System.exit(1); } logger.log(Level.INFO, "Envoy starting..."); // Initialize the local database if (config.isIgnoreLocalDB()) { localDB = new TransientLocalDB(); new Alert(AlertType.WARNING, "Ignoring local database.\nMessages will not be saved!").showAndWait(); } else try { localDB = new PersistentLocalDB(new File(config.getHomeDirectory(), config.getLocalDB().getPath())); } catch (final IOException e3) { logger.log(Level.SEVERE, "Could not initialize local database: ", e3); new Alert(AlertType.ERROR, "Could not initialize local database!\n" + e3).showAndWait(); System.exit(1); return; } // Initialize client and unread message cache client = new Client(); final var cacheMap = new CacheMap(); cacheMap.put(Message.class, new Cache()); cacheMap.put(GroupMessage.class, new Cache()); cacheMap.put(MessageStatusChange.class, new Cache()); cacheMap.put(GroupMessageStatusChange.class, new Cache()); stage.setTitle("Envoy"); stage.getIcons().add(IconUtil.loadIcon("envoy_logo")); final var sceneContext = new SceneContext(stage); sceneContext.load(SceneInfo.LOGIN_SCENE); sceneContext.getController().initializeData(client, localDB, cacheMap, sceneContext); } /** * Closes the client connection and saves the local database and settings. * * @since Envoy Client v0.1-beta */ @Override public void stop() { try { logger.log(Level.INFO, "Saving local database and settings..."); localDB.save(client.isOnline()); Settings.getInstance().save(); logger.log(Level.INFO, "Closing connection..."); client.close(); logger.log(Level.INFO, "Envoy was terminated by its user"); } catch (final Exception e) { logger.log(Level.SEVERE, "Unable to save local files: ", e); } } }