You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.6 KiB
120 lines
3.6 KiB
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 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("Konnte Konfiguration nicht aus {} laden:",file,e); |
|
} |
|
return this; |
|
} |
|
|
|
public Configuration save(File file) throws IOException { |
|
this.file = file; |
|
return save(); |
|
} |
|
|
|
public Configuration save() throws IOException { |
|
if (file == null) throw new NullPointerException("Konnte Konfiguration nicht speichern: Datei ist 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; |
|
} |
|
|
|
private JSONObject locations() { |
|
Object o = data.get(LOCATIONS); |
|
if (!(o instanceof JSONObject)) data.put(LOCATIONS,o = new JSONObject()); |
|
return (JSONObject) o; |
|
} |
|
|
|
public String baseDir() { |
|
var locations = locations(); |
|
if (!locations.containsKey(BASE)) locations.put(BASE,System.getProperty("user.dir")); |
|
return (String) locations.get(BASE); |
|
} |
|
|
|
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 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 int serverPort() { |
|
if (!data.containsKey(PORT)) data.put(PORT,80L); |
|
var o = data.get(PORT); |
|
return (int) (long) o; |
|
} |
|
|
|
public String baseUrl() { |
|
if (!data.containsKey(BASE_URL)) data.put(BASE_URL,"http://localhost"); |
|
return (String) data.get(BASE_URL); |
|
} |
|
|
|
|
|
public File file() { |
|
return file; |
|
} |
|
|
|
}
|
|
|