Added reading to the config manager
This commit is contained in:
@@ -18,40 +18,47 @@ import org.json.JSONObject;
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import src.main.utilities.JsonUtils;
|
||||||
|
|
||||||
|
|
||||||
public class ConfigManager {
|
public class ConfigManager {
|
||||||
|
|
||||||
private static final Path CONFIGS_DIR = Paths.get("src/resources/configs");
|
private static final Path CONFIGS_DIR = Paths.get("src/resources/configs");
|
||||||
private static final Path CONFIGS_BACKUP_DIR = Paths.get("src/resources/configs/backups");
|
private static final Path CONFIGS_BACKUP_DIR = Paths.get("src/resources/configs/backups");
|
||||||
|
|
||||||
|
// Map entry of database config contents and file path
|
||||||
private static final Map.Entry<String, String[]> DATABASE_CONFIG = new AbstractMap.SimpleEntry<String, String[]>("database.json",
|
private static final Map.Entry<String, String[]> DATABASE_CONFIG = new AbstractMap.SimpleEntry<String, String[]>("database.json",
|
||||||
new String[]{
|
new String[] {
|
||||||
"DatabaseAddress",
|
"address",
|
||||||
"DataBasePort",
|
"port",
|
||||||
"DataBaseUsername",
|
"username",
|
||||||
"DataBasePassword"
|
"password",
|
||||||
|
"ssl",
|
||||||
|
"defaultTable"
|
||||||
});
|
});
|
||||||
|
|
||||||
public ConfigManager () {
|
// Default constructor
|
||||||
|
public ConfigManager() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Boolean constructor for instanciating first time setup
|
||||||
|
public ConfigManager(boolean firstTimeSetup) {
|
||||||
|
|
||||||
makeBackups(CONFIGS_DIR, CONFIGS_BACKUP_DIR);
|
makeBackups(CONFIGS_DIR, CONFIGS_BACKUP_DIR);
|
||||||
|
|
||||||
if(!Files.exists(Path.of(CONFIGS_DIR.toString(), DATABASE_CONFIG.getKey()))) {
|
if(!Files.exists(Path.of(CONFIGS_DIR.toString(), DATABASE_CONFIG.getKey())) || firstTimeSetup) {
|
||||||
writeConfig(Paths.get(CONFIGS_DIR.toString(), DATABASE_CONFIG.getKey()), DATABASE_CONFIG.getValue());
|
writeConfig(Paths.get(CONFIGS_DIR.toString(), DATABASE_CONFIG.getKey()), DATABASE_CONFIG.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
//HashMap<String, Object> databaseConfig;
|
|
||||||
//databaseConfig = readConfig(Paths.get(CONFIGS_DIR.toString(), "database.json"));
|
|
||||||
//System.out.println(databaseConfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void getUserInputConfig(HashMap<String, Object> config, Scanner scanner, String configEntry) {
|
|
||||||
System.out.println("Please enter " + configEntry);
|
|
||||||
config.put(configEntry, scanner.nextLine());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Writes the config content to the config path
|
||||||
private void writeConfig(Path configPath, String[] configContent) {
|
private void writeConfig(Path configPath, String[] configContent) {
|
||||||
|
|
||||||
Scanner inputScanner = new Scanner(System.in);
|
Scanner inputScanner = new Scanner(System.in);
|
||||||
HashMap<String, Object> configData = new HashMap<String, Object>();
|
HashMap<String, Object> configData = new HashMap<String, Object>();
|
||||||
|
|
||||||
@@ -69,18 +76,42 @@ public class ConfigManager {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private HashMap<String, Object> readConfig(Path configPath) {
|
// Reads out the config based on the config file Path
|
||||||
|
public HashMap<String, Object> readConfig(Path configPath) {
|
||||||
|
|
||||||
HashMap<String, Object> config = new HashMap<String, Object>();
|
HashMap<String, Object> configContents = new HashMap<String, Object>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String configContent = new String(Files.readAllBytes(configPath));
|
String configContent = new String(Files.readAllBytes(configPath));
|
||||||
JSONObject jsonObject = new JSONObject(configContent);
|
JSONObject jsonObject = new JSONObject(configContent);
|
||||||
System.out.println(jsonObject);
|
configContents = JsonUtils.jsonToHashMap(jsonObject);
|
||||||
} catch (Exception error) {
|
} catch (Exception error) {
|
||||||
error.printStackTrace();
|
error.printStackTrace();
|
||||||
}
|
}
|
||||||
return config;
|
|
||||||
|
return configContents;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reads out the config based on the config file String
|
||||||
|
public HashMap<String, Object> readConfig(String config) {
|
||||||
|
|
||||||
|
// Checks if the string passed is of type .json
|
||||||
|
if (config.length() > 5 && config.substring(config.length() - 4) == ".json") {
|
||||||
|
System.out.println("Getting config ");
|
||||||
|
}
|
||||||
|
|
||||||
|
Path configPath = Path.of(CONFIGS_DIR.toString(), config);
|
||||||
|
HashMap<String, Object> configContents = new HashMap<String, Object>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
String configContent = new String(Files.readAllBytes(configPath));
|
||||||
|
JSONObject jsonObject = new JSONObject(configContent);
|
||||||
|
configContents = JsonUtils.jsonToHashMap(jsonObject);
|
||||||
|
} catch (Exception error) {
|
||||||
|
error.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return configContents;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void makeBackups(Path source, Path destination) {
|
private void makeBackups(Path source, Path destination) {
|
||||||
@@ -89,7 +120,6 @@ public class ConfigManager {
|
|||||||
System.err.println("Source directory does not exist or is not a directory.");
|
System.err.println("Source directory does not exist or is not a directory.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure the destination exists, if not create it
|
// Ensure the destination exists, if not create it
|
||||||
if (!Files.exists(destination)) {
|
if (!Files.exists(destination)) {
|
||||||
try {
|
try {
|
||||||
@@ -101,7 +131,6 @@ public class ConfigManager {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to copy files from source to destination
|
// Attempt to copy files from source to destination
|
||||||
try (Stream<Path> pathStream = Files.list(source)) {
|
try (Stream<Path> pathStream = Files.list(source)) {
|
||||||
// Get a list of files, filter out non-file types
|
// Get a list of files, filter out non-file types
|
||||||
@@ -124,4 +153,11 @@ public class ConfigManager {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void getUserInputConfig(HashMap<String, Object> config, Scanner scanner, String configEntry) {
|
||||||
|
|
||||||
|
System.out.println("Please enter " + configEntry);
|
||||||
|
config.put(configEntry, scanner.nextLine());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
16
src/main/utilities/Configs.java
Normal file
16
src/main/utilities/Configs.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package src.main.utilities;
|
||||||
|
|
||||||
|
public enum Configs {
|
||||||
|
|
||||||
|
database("database.json");
|
||||||
|
|
||||||
|
private final String fileName;
|
||||||
|
|
||||||
|
private Configs(String fileName) {
|
||||||
|
this.fileName = fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFileName() {
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
}
|
||||||
68
src/main/utilities/JsonUtils.java
Normal file
68
src/main/utilities/JsonUtils.java
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package src.main.utilities;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
public class JsonUtils {
|
||||||
|
|
||||||
|
public static HashMap<String, Object> jsonToHashMap(JSONObject json) throws JSONException {
|
||||||
|
if (json == null || json == JSONObject.NULL) {
|
||||||
|
return new HashMap<>();
|
||||||
|
}
|
||||||
|
return new HashMap<>(toMap(json));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, Object> jsonToMap(JSONObject json) throws JSONException {
|
||||||
|
if (json == null || json == JSONObject.NULL) {
|
||||||
|
return new HashMap<>();
|
||||||
|
}
|
||||||
|
return toMap(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, Object> toMap(JSONObject object) throws JSONException {
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
|
||||||
|
Iterator<String> keysItr = object.keys();
|
||||||
|
while (keysItr.hasNext()) {
|
||||||
|
String key = keysItr.next();
|
||||||
|
Object value = object.get(key);
|
||||||
|
|
||||||
|
if (value == JSONObject.NULL) {
|
||||||
|
value = null;
|
||||||
|
} else if (value instanceof JSONArray) {
|
||||||
|
value = toList((JSONArray) value);
|
||||||
|
} else if (value instanceof JSONObject) {
|
||||||
|
value = toMap((JSONObject) value);
|
||||||
|
}
|
||||||
|
|
||||||
|
map.put(key, value);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<Object> toList(JSONArray array) throws JSONException {
|
||||||
|
List<Object> list = new ArrayList<>();
|
||||||
|
|
||||||
|
for (int i = 0; i < array.length(); i++) {
|
||||||
|
Object value = array.get(i);
|
||||||
|
|
||||||
|
if (value == JSONObject.NULL) {
|
||||||
|
value = null;
|
||||||
|
} else if (value instanceof JSONArray) {
|
||||||
|
value = toList((JSONArray) value);
|
||||||
|
} else if (value instanceof JSONObject) {
|
||||||
|
value = toMap((JSONObject) value);
|
||||||
|
}
|
||||||
|
|
||||||
|
list.add(value);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user