package de.srsoftware.widerhall; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; import java.nio.file.Files; import static de.srsoftware.widerhall.Constants.*; public class Configuration { private static final Logger LOG = LoggerFactory.getLogger(Configuration.class); private static Configuration singleton = null; private JSONObject data; private File file; public File archiveDir() { var locations = locations(); if (!locations.containsKey(ARCHIVE)) locations.put(ARCHIVE, String.join(File.separator,baseDir(),"archive")); return new File((String) locations.get(ARCHIVE)); } public String baseDir() { var locations = locations(); if (!locations.containsKey(BASE)) locations.put(BASE,System.getProperty("user.dir")); return (String) locations.get(BASE); } public String baseUrl() { if (!data.containsKey(BASE_URL)) data.put(BASE_URL,"http://localhost"); return (String) data.get(BASE_URL); } public File configFile() { var locations = locations(); if (!locations.containsKey(CONFIG)) locations.put(CONFIG, String.join(File.separator,baseDir(),"config","config.json")); return new File((String) locations.get(CONFIG)); } public File dbFile() { var locations = locations(); if (!locations.containsKey(DB)) locations.put(DB, String.join(File.separator,baseDir(),"db","db.sqlite3")); return new File((String) locations.get(DB)); } public Configuration dbFile(File dbFile){ var locations = locations(); locations.put(DB,dbFile.toString()); return this; } public File file() { return file; } public static Configuration instance() { if (singleton == null) singleton = new Configuration().setDefaults(); return singleton; } /** * Merges configuration from file into current configuration. * Existing entries will be replaced. * @param file * @return * @throws IOException * @throws ParseException */ public Configuration load(File file) { this.file = file; if (file.exists()) try { LOG.info("Loading configuration from {}",file); var newVals = (JSONObject) new JSONParser().parse(Files.readString(file.toPath())); data.putAll(newVals); } catch (ParseException | IOException e){ LOG.warn("Was not able to load configuration from {}:",file,e); } return this; } private JSONObject locations() { Object o = data.get(LOCATIONS); if (!(o instanceof JSONObject)) data.put(LOCATIONS,o = new JSONObject()); return (JSONObject) o; } public Configuration save(File file) throws IOException { this.file = file; return save(); } public Configuration save() throws IOException { if (file == null) throw new NullPointerException("Cannot save configuration: file is null!"); file.getParentFile().mkdirs(); Files.writeString(file.toPath(),data.toJSONString()); return this; } private Configuration setDefaults() { if (data == null) data = new JSONObject(); configFile(); dbFile(); baseUrl(); serverPort(); archiveDir(); return this; } public int serverPort() { if (!data.containsKey(PORT)) data.put(PORT,80L); var o = data.get(PORT); return (int) (long) o; } }