From 578b7d1db256d9a582cef45ae5d13d858a977416 Mon Sep 17 00:00:00 2001 From: Yuuta Liang Date: Thu, 26 Oct 2023 05:00:12 +0800 Subject: Add persistence Signed-off-by: Yuuta Liang --- src/main/ui/JCA.java | 95 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 76 insertions(+), 19 deletions(-) (limited to 'src/main/ui/JCA.java') diff --git a/src/main/ui/JCA.java b/src/main/ui/JCA.java index 882c546..420ec10 100644 --- a/src/main/ui/JCA.java +++ b/src/main/ui/JCA.java @@ -1,9 +1,14 @@ package ui; +import model.asn1.exceptions.InvalidDBException; import model.asn1.exceptions.ParseException; import model.ca.CertificationAuthority; +import persistence.Decoder; +import persistence.FS; +import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.util.Scanner; @@ -12,6 +17,11 @@ import java.util.Scanner; * Main program */ public class JCA { + /** + * Default db file (./data/ca.json) + */ + private static final Path PATH_DEFAULT = Path.of("data", "ca.json"); + /** * Instances of the five screens; */ @@ -23,12 +33,17 @@ public class JCA { /** * The CA */ - private final CertificationAuthority ca; + private CertificationAuthority ca; /** * The current screen. */ private UIHandler screen; + /** + * There are unsaved changes. + */ + private boolean unsaved = false; + /** * EFFECTS: Init with main screen and empty CA. No private key and no CA cert. * Throws {@link NoSuchAlgorithmException} when crypto issue happens. @@ -106,34 +121,72 @@ public class JCA { screen.enter(args); } + /** + * EFFECTS: Read the database file and replace all local states. + * MODIFIES: this + */ + private void load() { + if (unsaved) { + System.out.println("Current database is not saved yet."); + return; + } + try { + this.ca = Decoder.decodeCA(FS.read(PATH_DEFAULT)); + } catch (InvalidDBException | NoSuchAlgorithmException e) { + System.out.println(e.getMessage()); + if (e.getCause() != null) { + System.out.println(e.getCause().getMessage()); + e.getCause().printStackTrace(); + } + } + } + private void handleLine(String... args) { if (args[0].equals("log")) { ca.getLogs().forEach(System.out::println); return; } - switch (args[0]) { - case "help": - screen.help(); - System.out.println("log\tView audit logs"); - break; - case "show": - screen.show(); - break; - case "commit": - screen.commit(); - break; - case "exit": - setScreen(screen.exit()); - break; - default: - screen.command(args); - break; + if ("help".equals(args[0])) { + screen.help(); + System.out.println("log\tView audit logs"); + System.out.println("load\tLoad database"); + System.out.println("save\tSave database"); + } else if ("commit".equals(args[0])) { + screen.commit(); + } else if ("show".equals(args[0])) { + screen.show(); + } else if ("exit".equals(args[0])) { + setScreen(screen.exit()); + } else if ("save".equals(args[0])) { + save(); + } else if ("load".equals(args[0])) { + load(); + } else { + screen.command(args); } printPS1(); } + /** + * EFFECTS: Print the '*user@JCA PS1' line + */ private void printPS1() { - System.out.printf("%s@JCA %s ", ca.getUser(), screen.getPS1()); + System.out.printf("%s%s@JCA %s ", + unsaved ? "*" : "", + ca.getUser(), + screen.getPS1()); + } + + /** + * EFFECTS: Save the DB + */ + public void save() { + try { + FS.write(PATH_DEFAULT, Decoder.encodeCA(ca)); + unsaved = false; + } catch (IOException e) { + System.out.println(e.getMessage()); + } } /** @@ -152,6 +205,10 @@ public class JCA { } } + public void setUnsaved(boolean unsaved) { + this.unsaved = unsaved; + } + public CertificationAuthority getCa() { return ca; } -- cgit v1.2.3