added static initialize methods
This commit is contained in:
parent
4ab6b7072a
commit
cce991e5f5
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -26,4 +26,6 @@ bin/
|
||||||
.vscode/
|
.vscode/
|
||||||
|
|
||||||
### Mac OS ###
|
### Mac OS ###
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
|
.idea/workspace.xml
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,10 @@ import dev.asdf00.confindibus.annotations.Configuration;
|
||||||
import dev.asdf00.confindibus.annotations.Section;
|
import dev.asdf00.confindibus.annotations.Section;
|
||||||
import dev.asdf00.confindibus.annotations.Value;
|
import dev.asdf00.confindibus.annotations.Value;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
|
import java.io.PrintWriter;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
|
@ -54,12 +56,56 @@ public class Configurator {
|
||||||
_debug = debugMsgStream;
|
_debug = debugMsgStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides easy access for initializing a config class.
|
||||||
|
*
|
||||||
|
* @param configClass class containing the annotations for a config
|
||||||
|
* @param createIfMissing permission to create new config file if none is found
|
||||||
|
*/
|
||||||
|
public static void initialize(Class<?> configClass, boolean createIfMissing) {
|
||||||
|
Configurator.of(configClass, null).init(createIfMissing);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides easy access for initializing a config class. If no or a faulty config is found,
|
||||||
|
* the user is queried if they want to (re)initialize the config file.
|
||||||
|
*
|
||||||
|
* @param configClass class containing the annotations for a config
|
||||||
|
* @param out the PrintWriter the user query is written to
|
||||||
|
* @param in the BufferedReader where the user response is expected
|
||||||
|
* @return true if a valid config file was found
|
||||||
|
*/
|
||||||
|
public static boolean initialize(Class<?> configClass, PrintWriter out, BufferedReader in) {
|
||||||
|
var conf = Configurator.of(configClass, null);
|
||||||
|
try {
|
||||||
|
initialize(configClass, false);
|
||||||
|
return true;
|
||||||
|
} catch (ConfigurationException e) {
|
||||||
|
try {
|
||||||
|
out.print("Configuration file is missing or faulty. Reset config and exit? [yN] ");
|
||||||
|
var input = in.readLine().toLowerCase();
|
||||||
|
if ("y".equals(input)) {
|
||||||
|
var confAnnot = configClass.getAnnotation(Configuration.class);
|
||||||
|
var cPath = Path.of(".").resolve(confAnnot.path());
|
||||||
|
if (Files.exists(cPath)) {
|
||||||
|
Files.delete(cPath);
|
||||||
|
}
|
||||||
|
initialize(configClass, true);
|
||||||
|
out.println("Config has been reset successfully!");
|
||||||
|
}
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new ConfigurationException(ex, "Initialization of config failed!");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates helper instance.
|
* Creates helper instance.
|
||||||
* @param configClass class containing the annotations for a config
|
*
|
||||||
|
* @param configClass class containing the annotations for a config
|
||||||
* @param debugMsgStream {@link PrintStream} where debug output shall be written to.
|
* @param debugMsgStream {@link PrintStream} where debug output shall be written to.
|
||||||
* Can be {@code null} if no debug output should be written
|
* Can be {@code null} if no debug output should be written
|
||||||
*/
|
*/
|
||||||
public static Configurator of(Class<?> configClass, PrintStream debugMsgStream) {
|
public static Configurator of(Class<?> configClass, PrintStream debugMsgStream) {
|
||||||
return new Configurator(configClass, debugMsgStream);
|
return new Configurator(configClass, debugMsgStream);
|
||||||
|
|
@ -67,6 +113,7 @@ public class Configurator {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize values in {@code configClass}.
|
* Initialize values in {@code configClass}.
|
||||||
|
*
|
||||||
* @param createIfMissing permission to create new config file if none is found
|
* @param createIfMissing permission to create new config file if none is found
|
||||||
*/
|
*/
|
||||||
public void init(boolean createIfMissing) {
|
public void init(boolean createIfMissing) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user