Shifted state maintenance logic to JSON file

This commit is contained in:
Rohit Awate 2018-06-29 09:34:15 +05:30
parent f65d2083ff
commit 1efaf40a3c
No known key found for this signature in database
GPG key ID: 1051D7B79CF2EE25
3 changed files with 27 additions and 29 deletions

View file

@ -16,7 +16,9 @@
package com.rohitawate.everest.controllers;
import com.fasterxml.jackson.core.type.TypeReference;
import com.jfoenix.controls.JFXButton;
import com.rohitawate.everest.misc.EverestUtilities;
import com.rohitawate.everest.misc.KeyMap;
import com.rohitawate.everest.misc.Services;
import com.rohitawate.everest.misc.ThemeManager;
@ -39,7 +41,8 @@ import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.*;
@ -243,39 +246,36 @@ public class HomeWindowController implements Initializable {
}
private void saveState() {
List<DashboardState> dashboardStates = new ArrayList<>();
ArrayList<DashboardState> dashboardStates = new ArrayList<>();
// Get the states of all the tabs
for (DashboardController controller : tabControllerMap.values())
dashboardStates.add(controller.getState());
try {
File configFolder = new File("Everest/config/");
if (!configFolder.exists())
configFolder.mkdirs();
OutputStream fileStream = new FileOutputStream("Everest/config/everest.state");
ObjectOutputStream objectStream = new ObjectOutputStream(fileStream);
objectStream.writeObject(dashboardStates);
objectStream.close();
fileStream.close();
Services.loggingService.logInfo("Application state was saved successfully.", LocalDateTime.now());
File stateFile = new File("Everest/config/state.json");
EverestUtilities.jsonMapper.writeValue(stateFile, dashboardStates);
Services.loggingService.logInfo("Application state saved.", LocalDateTime.now());
} catch (IOException e) {
Services.loggingService.logSevere("Failed to save the application's state.", e, LocalDateTime.now());
Services.loggingService.logSevere("Failed to save application state.", e, LocalDateTime.now());
}
}
private void recoverState() {
try {
InputStream fileStream = new FileInputStream("Everest/config/everest.state");
ObjectInputStream objectStream = new ObjectInputStream(fileStream);
File stateFile = new File("Everest/config/state.json");
Services.loggingService.logInfo("Application state file found.", LocalDateTime.now());
if (!stateFile.exists()) {
Services.loggingService.logInfo("Application state file not found. Loading default state.", LocalDateTime.now());
addTab();
return;
}
List<DashboardState> dashboardStates = (List<DashboardState>) objectStream.readObject();
objectStream.close();
fileStream.close();
ArrayList<DashboardState> dashboardStates = EverestUtilities.jsonMapper
.reader()
.forType(new TypeReference<ArrayList<DashboardState>>() {
})
.readValue(stateFile);
if (dashboardStates.size() > 0) {
for (DashboardState dashboardState : dashboardStates)
@ -283,15 +283,12 @@ public class HomeWindowController implements Initializable {
} else {
addTab();
}
} catch (FileNotFoundException e) {
Services.loggingService.logWarning("Application state file not found. Loading default state.", e, LocalDateTime.now());
addTab();
} catch (IOException | ClassNotFoundException e) {
Services.loggingService.logWarning("Application state file is possibly corrupted. Could not recover the state.\nLoading default state.", e, LocalDateTime.now());
addTab();
} catch (IOException e) {
Services.loggingService.logWarning("Application state file is possibly corrupted. State recovery failed. Loading default state.", e, LocalDateTime.now());
} finally {
Services.loggingService.logInfo("Application loaded.", LocalDateTime.now());
}
}
public void addHistoryItem(DashboardState state) {

View file

@ -18,17 +18,17 @@ package com.rohitawate.everest.models;
import com.rohitawate.everest.models.requests.DataDispatchRequest;
import java.io.Serializable;
import java.util.HashMap;
/**
* Convenience class to abstract the state of the application.
*/
public class DashboardState extends DataDispatchRequest implements Serializable {
public class DashboardState extends DataDispatchRequest {
private HashMap<String, String> params;
private String httpMethod;
public DashboardState() {
}
/*

View file

@ -41,6 +41,7 @@ public class SettingsLoader implements Runnable {
try {
File settingsFile = new File("Everest/config/settings.json");
if (settingsFile.exists())
System.out.println("Settings file found. Loading settings... ");
nodes = EverestUtilities.jsonMapper.readTree(settingsFile);