This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/client/ui/Startup.java

134 lines
4.4 KiB
Java
Raw Normal View History

package envoy.client.ui;
import java.io.File;
import java.io.IOException;
2020-06-13 22:36:52 +02:00
import java.util.Date;
import java.util.Properties;
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.Message;
import envoy.event.MessageStatusChangeEvent;
import envoy.exception.EnvoyException;
import envoy.util.EnvoyLog;
/**
2020-06-09 11:31:22 +02:00
* Handles application startup and shutdown.
* <p>
* Project: <strong>envoy-client</strong><br>
* File: <strong>Startup.java</strong><br>
* Created: <strong>26.03.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @since Envoy Client v0.1-beta
*/
public final class Startup extends Application {
2020-06-13 22:36:52 +02:00
private LocalDB localDB;
private Client client;
private Cache<Message> messageCache;
private Cache<MessageStatusChangeEvent> messageStatusCache;
private static final ClientConfig config = ClientConfig.getInstance();
private static final Logger logger = EnvoyLog.getLogger(Startup.class);
/**
2020-06-09 11:31:22 +02:00
* Loads the configuration, initializes the client and the local database and
* delegates the rest of the startup process to {@link LoginScene}.
2020-06-13 22:36:52 +02:00
*
2020-06-09 11:31:22 +02:00
* @since Envoy Client v0.1-beta
*/
@Override
public void start(Stage stage) throws Exception {
try {
// Load the configuration from client.properties first
2020-06-13 22:36:52 +02:00
logger.log(Level.INFO, "Envoy was started at " + new Date());
2020-05-30 15:28:11 +02:00
final Properties properties = new Properties();
properties.load(Startup.class.getClassLoader().getResourceAsStream("client.properties"));
config.load(properties);
// Override configuration values with command line arguments
2020-05-30 15:28:11 +02:00
final String[] args = getParameters().getRaw().toArray(new String[0]);
if (args.length > 0) config.load(args);
// Check if all mandatory configuration values have been initialized
if (!config.isInitialized()) throw new EnvoyException("Configuration is not fully initialized");
2020-05-30 15:28:11 +02:00
} catch (final Exception e) {
new Alert(AlertType.ERROR, "Error loading configuration values:\n" + e);
2020-06-13 22:36:52 +02:00
logger.log(Level.SEVERE, "Error loading configuration values", e);
e.printStackTrace();
System.exit(1);
}
// Setup logger for the envoy package
EnvoyLog.initialize(config);
EnvoyLog.attach("envoy");
EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier());
EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier());
// Initialize the local database
if (config.isIgnoreLocalDB()) {
localDB = new TransientLocalDB();
new Alert(AlertType.WARNING, "Ignoring local database.\nMessages will not be saved!").showAndWait();
2020-06-13 22:36:52 +02:00
} 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
2020-06-13 22:36:52 +02:00
client = new Client();
messageCache = new Cache<>();
messageStatusCache = new Cache<>();
stage.setTitle("Envoy");
stage.getIcons().add(IconUtil.load("/icons/envoy_logo.png"));
final var sceneContext = new SceneContext(stage);
sceneContext.load(SceneInfo.LOGIN_SCENE);
sceneContext.<LoginScene>getController().initializeData(client, localDB, messageCache, messageStatusCache, sceneContext);
}
/**
2020-06-09 11:31:22 +02:00
* Closes the client connection and saves the local database and settings.
2020-06-13 22:36:52 +02:00
*
2020-06-09 11:31:22 +02:00
* @since Envoy Client v0.1-beta
*/
@Override
public void stop() {
try {
2020-06-13 22:36:52 +02:00
logger.log(Level.INFO, "Closing connection...");
client.close();
2020-06-13 22:36:52 +02:00
logger.log(Level.INFO, "Saving local database and settings...");
localDB.save();
Settings.getInstance().save();
2020-06-13 22:36:52 +02:00
logger.log(Level.INFO, "Envoy was stopped as expected at " + new Date());
2020-05-30 15:28:11 +02:00
} catch (final Exception e) {
2020-06-13 22:36:52 +02:00
logger.log(Level.SEVERE, "Unable to save local files: ", e);
}
}
/**
* Starts the application.
*
* @param args the command line arguments are processed by the
* {@link ClientConfig}
* @since Envoy Client v0.1-beta
*/
2020-05-30 15:28:11 +02:00
public static void main(String[] args) { launch(args); }
}