|
|
|
@ -3,66 +3,93 @@ package de.srsoftware.widerhall;
@@ -3,66 +3,93 @@ 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; |
|
|
|
|
private File file; |
|
|
|
|
|
|
|
|
|
public static Configuration instance() { |
|
|
|
|
if (singleton == null) singleton = new Configuration().setDefaults(); |
|
|
|
|
return singleton; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Configuration(File configFile) throws IOException, ParseException { |
|
|
|
|
this.file = configFile; |
|
|
|
|
if (!configFile.exists()){ |
|
|
|
|
setDefaults(); |
|
|
|
|
save(); |
|
|
|
|
/** |
|
|
|
|
* 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); |
|
|
|
|
} |
|
|
|
|
var content = Files.readString(configFile.toPath()); |
|
|
|
|
data = (JSONObject) new JSONParser().parse(content); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Configuration setFile(File file) throws IOException, ParseException { |
|
|
|
|
singleton = new Configuration(file); |
|
|
|
|
return singleton; |
|
|
|
|
public Configuration save(File file) throws IOException { |
|
|
|
|
this.file = file; |
|
|
|
|
return save(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Configuration instance() { |
|
|
|
|
return singleton; |
|
|
|
|
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 void setDefaults() throws MalformedURLException { |
|
|
|
|
private Configuration setDefaults() { |
|
|
|
|
if (data == null) data = new JSONObject(); |
|
|
|
|
baseDir(); |
|
|
|
|
serverPort(); |
|
|
|
|
tokenUrl(); |
|
|
|
|
loginUrl(); |
|
|
|
|
configFile(); |
|
|
|
|
dbFile(); |
|
|
|
|
baseUrl(); |
|
|
|
|
clientId(); |
|
|
|
|
clientSecret(); |
|
|
|
|
serverPort(); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void save() throws IOException { |
|
|
|
|
Files.writeString(file.toPath(),data.toJSONString()); |
|
|
|
|
private JSONObject locations() { |
|
|
|
|
Object o = data.get(LOCATIONS); |
|
|
|
|
if (!(o instanceof JSONObject)) data.put(LOCATIONS,o = new JSONObject()); |
|
|
|
|
return (JSONObject) o; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public int serverPort() { |
|
|
|
|
if (!data.containsKey(PORT)) data.put(PORT,80L); |
|
|
|
|
var o = data.get(PORT); |
|
|
|
|
return (int) (long) 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 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 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 String loginUrl() { |
|
|
|
|
if (!data.containsKey(LOGIN_URL)) data.put(LOGIN_URL,"http://localhost:"+serverPort()+"/oauth/login"); |
|
|
|
|
return (String) data.get(LOGIN_URL); |
|
|
|
|
public int serverPort() { |
|
|
|
|
if (!data.containsKey(PORT)) data.put(PORT,80L); |
|
|
|
|
var o = data.get(PORT); |
|
|
|
|
return (int) (long) o; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public String baseUrl() { |
|
|
|
@ -70,23 +97,8 @@ public class Configuration {
@@ -70,23 +97,8 @@ public class Configuration {
|
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|