220 lines
No EOL
4.7 KiB
Java
220 lines
No EOL
4.7 KiB
Java
package dev.peerat.backend;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedWriter;
|
|
import java.io.File;
|
|
import java.io.FileReader;
|
|
import java.io.FileWriter;
|
|
import java.lang.reflect.Field;
|
|
|
|
import dev.peerat.backend.utils.Mail;
|
|
|
|
public class Configuration {
|
|
|
|
private boolean prod;
|
|
|
|
private String db_host;
|
|
private int db_port;
|
|
private String db_user;
|
|
private String db_database;
|
|
private String db_password;
|
|
|
|
private int tcp_port;
|
|
private boolean use_ssl;
|
|
private String ssl_keystore;
|
|
private String ssl_keystorePasswd;
|
|
|
|
private String users_files;
|
|
|
|
private String token_issuer;
|
|
private int token_expiration;
|
|
|
|
private String token_discord;
|
|
|
|
private int groupJoinMinutes;
|
|
private String groupQuitMinutes;
|
|
|
|
private String mailUsername;
|
|
private String mailPassword;
|
|
private String mailSmtpHost;
|
|
private int mailSmtpPort;
|
|
private String mailFromAddress;
|
|
|
|
private String git_token;
|
|
|
|
private String jwt_key;
|
|
|
|
private File _file;
|
|
|
|
public Configuration(String path) {
|
|
this._file = new File(path);
|
|
System.out.println("Config on " + _file.getAbsolutePath());
|
|
}
|
|
|
|
public <T> Configuration addDefaultValue(String name, T value) throws Exception{
|
|
if(value == null) throw new IllegalArgumentException("Value cannot be null");
|
|
Field field = getClass().getDeclaredField(name);
|
|
field.setAccessible(true);
|
|
field.set(this, value);
|
|
return this;
|
|
}
|
|
|
|
public Configuration load() throws Exception{
|
|
if(!this._file.exists()) return this;
|
|
BufferedReader reader = new BufferedReader(new FileReader(this._file));
|
|
String line;
|
|
while((line = reader.readLine()) != null){
|
|
int index = line.indexOf('=');
|
|
String key = line.substring(0, index);
|
|
String value = line.substring(index+1);
|
|
Field field = getClass().getDeclaredField(key);
|
|
if(field == null) continue;
|
|
field.setAccessible(true);
|
|
injectValue(field, value);
|
|
}
|
|
reader.close();
|
|
return this;
|
|
}
|
|
|
|
private void injectValue(Field field, String value) throws IllegalAccessException {
|
|
if (field.getType().isPrimitive()) {
|
|
switch (field.getType().getName()) {
|
|
case "boolean":
|
|
field.setBoolean(this, Boolean.parseBoolean(value));
|
|
break;
|
|
case "byte":
|
|
field.setByte(this, Byte.parseByte(value));
|
|
break;
|
|
case "char":
|
|
field.setChar(this, value.charAt(0));
|
|
break;
|
|
case "double":
|
|
field.setDouble(this, Double.parseDouble(value));
|
|
break;
|
|
case "float":
|
|
field.setFloat(this, Float.parseFloat(value));
|
|
break;
|
|
case "int":
|
|
field.setInt(this, Integer.parseInt(value));
|
|
break;
|
|
case "long":
|
|
field.setLong(this, Long.parseLong(value));
|
|
break;
|
|
case "short":
|
|
field.setShort(this, Short.parseShort(value));
|
|
break;
|
|
default:
|
|
throw new IllegalArgumentException(value);
|
|
}
|
|
return;
|
|
}
|
|
if (field.getType().equals(String.class)) {
|
|
field.set(this, value);
|
|
return;
|
|
}
|
|
throw new IllegalArgumentException(value);
|
|
}
|
|
|
|
public void save() throws Exception {
|
|
if (!_file.exists()) {
|
|
File parent = _file.getParentFile();
|
|
if(!parent.exists()) parent.mkdirs();
|
|
_file.createNewFile();
|
|
}
|
|
Field[] fields = getClass().getDeclaredFields();
|
|
BufferedWriter writer = new BufferedWriter(new FileWriter(_file));
|
|
for(Field field : fields){
|
|
field.setAccessible(true);
|
|
if(field.getName().startsWith("_")) continue;
|
|
Object value = field.get(this);
|
|
writer.write(field.getName() + "=" + value);
|
|
}
|
|
writer.flush();
|
|
writer.close();
|
|
}
|
|
|
|
public boolean isProduction(){
|
|
return this.prod;
|
|
}
|
|
|
|
public String getDbHost(){
|
|
return this.db_host;
|
|
}
|
|
|
|
public int getDbPort(){
|
|
return this.db_port;
|
|
}
|
|
|
|
public String getDbUser(){
|
|
return this.db_user;
|
|
}
|
|
|
|
public String getDbDatabase(){
|
|
return this.db_database;
|
|
}
|
|
|
|
public String getDbPassword(){
|
|
return this.db_password;
|
|
}
|
|
|
|
public String getSslKeystore(){
|
|
return this.ssl_keystore;
|
|
}
|
|
|
|
public String getTokenIssuer(){
|
|
return this.token_issuer;
|
|
}
|
|
|
|
public int getTokenExpiration(){
|
|
return this.token_expiration;
|
|
}
|
|
|
|
public String getSslKeystorePasswd(){
|
|
return this.ssl_keystorePasswd;
|
|
}
|
|
|
|
public int getTcpPort(){
|
|
return this.tcp_port;
|
|
}
|
|
|
|
public boolean useSsl(){
|
|
return this.use_ssl;
|
|
}
|
|
|
|
public String getUsersFiles(){
|
|
return users_files;
|
|
}
|
|
|
|
public String getDiscordToken(){
|
|
return this.token_discord;
|
|
}
|
|
|
|
public int getGroupJoinMinutes(){
|
|
return this.groupJoinMinutes;
|
|
}
|
|
|
|
public String getGroupQuitMinutes(){
|
|
return this.groupQuitMinutes;
|
|
}
|
|
|
|
public Mail getMail(){
|
|
return new Mail(
|
|
this.mailUsername,
|
|
this.mailPassword,
|
|
this.mailSmtpHost,
|
|
this.mailSmtpPort,
|
|
this.mailFromAddress);
|
|
}
|
|
|
|
public String getGitToken(){
|
|
return this.git_token;
|
|
}
|
|
|
|
public String getJwtKey(){
|
|
return this.jwt_key;
|
|
}
|
|
|
|
public void setJwtKey(String key){
|
|
this.jwt_key = key;
|
|
}
|
|
} |