working on configuration
This commit is contained in:
@@ -3,50 +3,87 @@ 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.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
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 final File file;
|
||||
|
||||
public Configuration(File configFile) throws IOException, ParseException {
|
||||
this.file = configFile;
|
||||
if (!configFile.exists()){
|
||||
setDefaults();
|
||||
save();
|
||||
}
|
||||
var content = Files.readString(configFile.toPath());
|
||||
data = (JSONObject) new JSONParser().parse(content);
|
||||
}
|
||||
|
||||
public static Configuration setFile(File file) throws IOException, ParseException {
|
||||
singleton = new Configuration(file);
|
||||
return singleton;
|
||||
}
|
||||
private File file;
|
||||
|
||||
public static Configuration instance() {
|
||||
if (singleton == null) singleton = new Configuration().setDefaults();
|
||||
return singleton;
|
||||
}
|
||||
|
||||
private void setDefaults() throws MalformedURLException {
|
||||
if (data == null) data = new JSONObject();
|
||||
baseDir();
|
||||
serverPort();
|
||||
tokenUrl();
|
||||
loginUrl();
|
||||
baseUrl();
|
||||
clientId();
|
||||
clientSecret();
|
||||
/**
|
||||
* 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 {
|
||||
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 void save() throws IOException {
|
||||
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();
|
||||
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 int serverPort() {
|
||||
@@ -55,38 +92,13 @@ public class Configuration {
|
||||
return (int) (long) o;
|
||||
}
|
||||
|
||||
public URL tokenUrl() throws MalformedURLException {
|
||||
if (!data.containsKey(TOKEN_URL)) data.put(TOKEN_URL,"http://localhost:"+serverPort()+"/oauth/token");
|
||||
return new URL((String) data.get(TOKEN_URL));
|
||||
}
|
||||
|
||||
public String loginUrl() {
|
||||
if (!data.containsKey(LOGIN_URL)) data.put(LOGIN_URL,"http://localhost:"+serverPort()+"/oauth/login");
|
||||
return (String) data.get(LOGIN_URL);
|
||||
}
|
||||
|
||||
public String baseUrl() {
|
||||
if (!data.containsKey(BASE_URL)) data.put(BASE_URL,"http://localhost");
|
||||
return (String) data.get(BASE_URL);
|
||||
}
|
||||
|
||||
public String clientId() {
|
||||
if (!data.containsKey(CLIENT_ID)) data.put(CLIENT_ID,"widerhall");
|
||||
return (String) data.get(CLIENT_ID);
|
||||
}
|
||||
|
||||
public Object clientSecret() {
|
||||
if (!data.containsKey(CLIENT_SECRET)) data.put(CLIENT_SECRET,"changeme");
|
||||
return (String) data.get(CLIENT_SECRET);
|
||||
}
|
||||
|
||||
public String dbLocation() {
|
||||
if (!data.containsKey(DB_FILE)) data.put(DB_FILE,System.getProperty("user.home")+"/.config/widerhall/db.sqlite3");
|
||||
return (String) data.get(DB_FILE);
|
||||
}
|
||||
|
||||
public String baseDir() {
|
||||
if (!data.containsKey(Constants.BASE_DIR)) data.put(BASE_DIR,System.getProperty("user.dir")+"/static");
|
||||
return (String) data.get(BASE_DIR);
|
||||
public File file() {
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user