|
|
@ -3,66 +3,93 @@ package de.srsoftware.widerhall; |
|
|
|
import org.json.simple.JSONObject; |
|
|
|
import org.json.simple.JSONObject; |
|
|
|
import org.json.simple.parser.JSONParser; |
|
|
|
import org.json.simple.parser.JSONParser; |
|
|
|
import org.json.simple.parser.ParseException; |
|
|
|
import org.json.simple.parser.ParseException; |
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.net.MalformedURLException; |
|
|
|
|
|
|
|
import java.net.URL; |
|
|
|
|
|
|
|
import java.nio.file.Files; |
|
|
|
import java.nio.file.Files; |
|
|
|
|
|
|
|
|
|
|
|
import static de.srsoftware.widerhall.Constants.*; |
|
|
|
import static de.srsoftware.widerhall.Constants.*; |
|
|
|
public class Configuration { |
|
|
|
public class Configuration { |
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(Configuration.class); |
|
|
|
private static Configuration singleton = null; |
|
|
|
private static Configuration singleton = null; |
|
|
|
private JSONObject data; |
|
|
|
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; |
|
|
|
* Merges configuration from file into current configuration. |
|
|
|
if (!configFile.exists()){ |
|
|
|
* Existing entries will be replaced. |
|
|
|
setDefaults(); |
|
|
|
* @param file |
|
|
|
save(); |
|
|
|
* @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()); |
|
|
|
return this; |
|
|
|
data = (JSONObject) new JSONParser().parse(content); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static Configuration setFile(File file) throws IOException, ParseException { |
|
|
|
public Configuration save(File file) throws IOException { |
|
|
|
singleton = new Configuration(file); |
|
|
|
this.file = file; |
|
|
|
return singleton; |
|
|
|
return save(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static Configuration instance() { |
|
|
|
public Configuration save() throws IOException { |
|
|
|
return singleton; |
|
|
|
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(); |
|
|
|
if (data == null) data = new JSONObject(); |
|
|
|
baseDir(); |
|
|
|
configFile(); |
|
|
|
serverPort(); |
|
|
|
dbFile(); |
|
|
|
tokenUrl(); |
|
|
|
|
|
|
|
loginUrl(); |
|
|
|
|
|
|
|
baseUrl(); |
|
|
|
baseUrl(); |
|
|
|
clientId(); |
|
|
|
serverPort(); |
|
|
|
clientSecret(); |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void save() throws IOException { |
|
|
|
private JSONObject locations() { |
|
|
|
Files.writeString(file.toPath(),data.toJSONString()); |
|
|
|
Object o = data.get(LOCATIONS); |
|
|
|
|
|
|
|
if (!(o instanceof JSONObject)) data.put(LOCATIONS,o = new JSONObject()); |
|
|
|
|
|
|
|
return (JSONObject) o; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public int serverPort() { |
|
|
|
public String baseDir() { |
|
|
|
if (!data.containsKey(PORT)) data.put(PORT,80L); |
|
|
|
var locations = locations(); |
|
|
|
var o = data.get(PORT); |
|
|
|
if (!locations.containsKey(BASE)) locations.put(BASE,System.getProperty("user.dir")); |
|
|
|
return (int) (long) o; |
|
|
|
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 { |
|
|
|
public File dbFile() { |
|
|
|
if (!data.containsKey(TOKEN_URL)) data.put(TOKEN_URL,"http://localhost:"+serverPort()+"/oauth/token"); |
|
|
|
var locations = locations(); |
|
|
|
return new URL((String) data.get(TOKEN_URL)); |
|
|
|
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() { |
|
|
|
public int serverPort() { |
|
|
|
if (!data.containsKey(LOGIN_URL)) data.put(LOGIN_URL,"http://localhost:"+serverPort()+"/oauth/login"); |
|
|
|
if (!data.containsKey(PORT)) data.put(PORT,80L); |
|
|
|
return (String) data.get(LOGIN_URL); |
|
|
|
var o = data.get(PORT); |
|
|
|
|
|
|
|
return (int) (long) o; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String baseUrl() { |
|
|
|
public String baseUrl() { |
|
|
@ -70,23 +97,8 @@ public class Configuration { |
|
|
|
return (String) data.get(BASE_URL); |
|
|
|
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() { |
|
|
|
public File file() { |
|
|
|
if (!data.containsKey(DB_FILE)) data.put(DB_FILE,System.getProperty("user.home")+"/.config/widerhall/db.sqlite3"); |
|
|
|
return file; |
|
|
|
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); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|