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

120 lines
3.6 KiB
Java
Raw Normal View History

package envoy.client.ui;
import java.io.File;
import java.io.IOException;
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.exception.EnvoyException;
import envoy.util.EnvoyLog;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>Startup.java</strong><br>
* Created: <strong>26.03.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Client v0.1-beta
*/
public final class Startup extends Application {
private LocalDB localDB;
private Client client;
private Cache<Message> cache;
private static final ClientConfig config = ClientConfig.getInstance();
private static final Logger logger = EnvoyLog.getLogger(Startup.class);
/**
* {@inheritDoc}
*/
@Override
public void start(Stage stage) throws Exception {
try {
// Load the configuration from client.properties first
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);
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();
} else try {
localDB = new PersistentLocalDB(new File(config.getHomeDirectory(), config.getLocalDB().getPath()));
2020-05-30 15:28:11 +02:00
} 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();
cache = 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, cache, sceneContext);
}
/**
* {@inheritDoc}
*/
@Override
public void stop() {
try {
// Save Settings and PersistentLocalDB on shutdown
logger.info("Closing connection...");
client.close();
logger.info("Saving local database and settings...");
localDB.save();
Settings.getInstance().save();
2020-05-30 15:28:11 +02:00
} catch (final Exception e) {
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); }
}