diff --git a/src/json-simple-1.1.1.jar b/src/json-simple-1.1.1.jar new file mode 100644 index 0000000..66347a6 Binary files /dev/null and b/src/json-simple-1.1.1.jar differ diff --git a/src/main/extensions/Extension.java b/src/main/extensions/Extension.java index 0c3a22b..525582c 100644 --- a/src/main/extensions/Extension.java +++ b/src/main/extensions/Extension.java @@ -69,7 +69,7 @@ public abstract class Extension { } if (getArgument(args, PORT_FLAG) == null) { - System.err.println("Don't forget to include G-Earth's port as program parameters (-p {port})"); + System.err.println("Don't forget to include G-Earth's port in your program parameters (-p {port})"); isCorrupted = true; } } diff --git a/src/main/misc/Cacher.java b/src/main/misc/Cacher.java index be593c1..021be4f 100644 --- a/src/main/misc/Cacher.java +++ b/src/main/misc/Cacher.java @@ -1,183 +1,71 @@ package main.misc; -import java.io.*; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.nio.file.Files; import java.util.ArrayList; import java.util.List; +import java.util.Map; /** - * Created by Jonas on 05/04/18. + * Created by Jonas on 28/09/18. */ public class Cacher { + private static final String CACHEFILENAME = "jsoncache.json"; + private static String getCacheDir() { return System.getProperty("user.home") + File.separator + ".G-Earth" + File.separator; } - public static boolean exists(String key) { - File f = new File(getCacheDir(), "cache.txt"); - if (f.exists()) { + private static boolean cacheFileExists() { + File f = new File(getCacheDir(), CACHEFILENAME); + return (f.exists() && !f.isDirectory()); + } + + private static JSONObject getCacheContents() { + if (cacheFileExists()) { try { - List lines = Files.readAllLines(f.toPath()); + File f = new File(getCacheDir(), CACHEFILENAME); + String contents = String.join("\n", Files.readAllLines(f.toPath())); - for (String line : lines) { - if (line.startsWith(key+":")) { - return true; - } - } - - } catch (IOException e) { + JSONParser parser = new JSONParser(); + return (JSONObject) parser.parse(contents); + } catch (IOException | ParseException e) { e.printStackTrace(); } } - return false; + return new JSONObject(); } + private static void updateCache(JSONObject contents) { + try (FileWriter file = new FileWriter(new File(getCacheDir(), CACHEFILENAME))) { - public static String get(String key) { - File f = new File(getCacheDir(), "cache.txt"); - if (f.exists()) { - try { - List lines = Files.readAllLines(f.toPath()); + file.write(contents.toJSONString()); + file.flush(); - for (String line : lines) { - if (line.startsWith(key+":")) { - return line.split(":")[1]; - } - } - - } catch (IOException e) { - e.printStackTrace(); - } - } - return ""; - } - - public static void remove(String key) { - File targetFile = new File(getCacheDir() + File.separator + "cache.txt"); - File parent = targetFile.getParentFile(); - if (!parent.exists() && !parent.mkdirs()) { - throw new IllegalStateException("Couldn't create dir: " + parent); - } - if (!targetFile.exists()) { - try { - targetFile.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - try - { - ArrayList lines = new ArrayList(); - File f1 = new File(getCacheDir() + File.separator + "cache.txt"); - FileReader fr = new FileReader(f1); - BufferedReader br = new BufferedReader(fr); - String line = null; - while ((line = br.readLine()) != null) - { - if (!line.startsWith(key + ":")) - lines.add(line); - - } - fr.close(); - br.close(); - - FileWriter fw = new FileWriter(f1); - BufferedWriter out = new BufferedWriter(fw); - - for (int i = 0; i < lines.size(); i++) { - out.write(lines.get(i)); - if (i != lines.size() - 1) out.write("\n"); - } - out.flush(); - out.close(); - } - catch (Exception ex) - { - ex.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); } } - public static void add(String key, String value) { - File targetFile = new File(getCacheDir() + File.separator + "cache.txt"); - File parent = targetFile.getParentFile(); - if (!parent.exists() && !parent.mkdirs()) { - throw new IllegalStateException("Couldn't create dir: " + parent); - } - if (!targetFile.exists()) { - try { - targetFile.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - } - } + public static void put(String key, Object val) { + JSONObject object = getCacheContents(); + if (object.containsKey(key)) object.remove(key); -// File f = new File(getCacheDir(), "cache.txt"); -// if (!f.exists()) { -// try { -// PrintWriter writer = new PrintWriter(f.getPath(), "UTF-8"); -// writer.write(""); -// writer.close(); -// } catch (FileNotFoundException | UnsupportedEncodingException e) { -// e.printStackTrace(); -// } -// } - - try - { - ArrayList lines = new ArrayList(); - File f1 = new File(getCacheDir() + File.separator + "cache.txt"); - FileReader fr = new FileReader(f1); - BufferedReader br = new BufferedReader(fr); - String line = null; - boolean containmmm = false; - while ((line = br.readLine()) != null) - { - if (line.startsWith(key+":")) - containmmm = true; - lines.add(line); - - } - fr.close(); - br.close(); - - FileWriter fw = new FileWriter(f1); - BufferedWriter out = new BufferedWriter(fw); - - if (!containmmm) { - out.write(key+":"+value); - } - - for (int i = 0; i < lines.size(); i++) { - out.write("\n"+ lines.get(i)); - } - - out.flush(); - out.close(); - } - catch (Exception ex) - { - ex.printStackTrace(); - } + object.put(key, val); + updateCache(object); } + public static Object get(String key) { + JSONObject object = getCacheContents(); - public static void update(String key, String value) { - remove(key); - add(key, value); + return object.get(key); } - - - public static void main(String[] args) { -// System.out.println(exists("hallo")); -// System.out.println(get("hallo")); -// -// add("hallo","doei"); -// -// System.out.println(exists("hallo")); -// System.out.println(get("hallo")); -// -// remove("hallo"); -// System.out.println(get("hallo")); - System.out.println(get("PRODUCTION-201804032203-770536283-pingHeader")); + public static void clear() { + updateCache(new JSONObject()); } }