Improved Config machanism with ConfigItems
Added logger levels and home directory to Config
This commit is contained in:
parent
f9a1db90ea
commit
c2f7bd7b21
@ -1,7 +1,10 @@
|
|||||||
package envoy.client;
|
package envoy.client;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import envoy.exception.EnvoyException;
|
import envoy.exception.EnvoyException;
|
||||||
|
|
||||||
@ -20,14 +23,19 @@ import envoy.exception.EnvoyException;
|
|||||||
*/
|
*/
|
||||||
public class Config {
|
public class Config {
|
||||||
|
|
||||||
private String server;
|
private Map<String, ConfigItem<?>> items = new HashMap<>();
|
||||||
private int port;
|
|
||||||
private File localDB;
|
|
||||||
private int syncTimeout;
|
|
||||||
|
|
||||||
private static Config config;
|
private static Config config;
|
||||||
|
|
||||||
private Config() {}
|
private Config() {
|
||||||
|
items.put("server", new ConfigItem<>("server", "s", (input) -> input, null));
|
||||||
|
items.put("port", new ConfigItem<>("port", "p", (input) -> Integer.parseInt(input), null));
|
||||||
|
items.put("localDB", new ConfigItem<>("localDB", "db", (input) -> new File(input), new File("localDB")));
|
||||||
|
items.put("syncTimeout", new ConfigItem<>("syncTimeout", "st", (input) -> Integer.parseInt(input), 1000));
|
||||||
|
items.put("homeDirectory", new ConfigItem<>("homeDirectory", "h", (input) -> new File(input), new File(System.getProperty("user.home"))));
|
||||||
|
items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", (input) -> Level.parse(input), Level.CONFIG));
|
||||||
|
items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", (input) -> Level.parse(input), Level.FINEST));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the singleton instance of the {@link Config}
|
* @return the singleton instance of the {@link Config}
|
||||||
@ -53,10 +61,7 @@ public class Config {
|
|||||||
try {
|
try {
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
properties.load(loader.getResourceAsStream("client.properties"));
|
properties.load(loader.getResourceAsStream("client.properties"));
|
||||||
if (properties.containsKey("server")) server = properties.getProperty("server");
|
items.forEach((name, item) -> { if (properties.containsKey(name)) item.parse(properties.getProperty(name)); });
|
||||||
if (properties.containsKey("port")) port = Integer.parseInt(properties.getProperty("port"));
|
|
||||||
localDB = new File(properties.getProperty("localDB", ".\\localDB"));
|
|
||||||
syncTimeout = Integer.parseInt(properties.getProperty("syncTimeout", "1000"));
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new EnvoyException("Failed to load client.properties", e);
|
throw new EnvoyException("Failed to load client.properties", e);
|
||||||
}
|
}
|
||||||
@ -72,84 +77,57 @@ public class Config {
|
|||||||
*/
|
*/
|
||||||
public void load(String[] args) throws EnvoyException {
|
public void load(String[] args) throws EnvoyException {
|
||||||
for (int i = 0; i < args.length; i++)
|
for (int i = 0; i < args.length; i++)
|
||||||
switch (args[i]) {
|
for (ConfigItem<?> item : items.values())
|
||||||
case "--server":
|
if (args[i].startsWith("--")) {
|
||||||
case "-s":
|
if (args[i].length() == 2) throw new EnvoyException("Malformed command line argument at position " + i);
|
||||||
server = args[++i];
|
final String commandLong = args[i].substring(2);
|
||||||
|
if (item.getCommandLong().equals(commandLong)) {
|
||||||
|
item.parse(args[++i]);
|
||||||
break;
|
break;
|
||||||
case "--port":
|
|
||||||
case "-p":
|
|
||||||
port = Integer.parseInt(args[++i]);
|
|
||||||
break;
|
|
||||||
case "--localDB":
|
|
||||||
case "-db":
|
|
||||||
localDB = new File(args[++i]);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new EnvoyException("Unknown token " + args[i] + " found");
|
|
||||||
}
|
}
|
||||||
|
} else if (args[i].startsWith("-")) {
|
||||||
|
if (args[i].length() == 1) throw new EnvoyException("Malformed command line argument at position " + i);
|
||||||
|
final String commandShort = args[i].substring(1);
|
||||||
|
if (item.getCommandShort().equals(commandShort)) {
|
||||||
|
item.parse(args[++i]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else throw new EnvoyException("Malformed command line argument at position " + i);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return {@code true} if server, port and localDB directory are known.
|
* @return {@code true} if server, port and localDB directory are known.
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public boolean isInitialized() { return server != null && !server.isEmpty() && port > 0 && port < 65566 && localDB != null; }
|
public boolean isInitialized() { return items.values().stream().noneMatch(item -> item.get() == null); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the host name of the Envoy server
|
* @return the host name of the Envoy server
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public String getServer() { return server; }
|
public String getServer() { return (String) items.get("server").get(); }
|
||||||
|
|
||||||
/**
|
|
||||||
* Changes the default server host name.
|
|
||||||
* Exclusively intended for development purposes.
|
|
||||||
*
|
|
||||||
* @param server the host name of the Envoy server
|
|
||||||
* @since Envoy v0.1-alpha
|
|
||||||
*/
|
|
||||||
public void setServer(String server) { this.server = server; }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the port at which the Envoy server is located on the host
|
* @return the port at which the Envoy server is located on the host
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public int getPort() { return port; }
|
public int getPort() { return (int) items.get("port").get(); }
|
||||||
|
|
||||||
/**
|
|
||||||
* Changes the default port.
|
|
||||||
* Exclusively intended for development purposes.
|
|
||||||
*
|
|
||||||
* @param port the port where an Envoy server is located
|
|
||||||
* @since Envoy v0.1-alpha
|
|
||||||
*/
|
|
||||||
public void setPort(int port) { this.port = port; }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the local database specific to the client user
|
* @return the local database specific to the client user
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
**/
|
**/
|
||||||
public File getLocalDB() { return localDB; }
|
public File getLocalDB() { return (File) items.get("localDB").get(); }
|
||||||
|
|
||||||
/**
|
|
||||||
* Changes the default local database.
|
|
||||||
* Exclusively intended for development purposes.
|
|
||||||
*
|
|
||||||
* @param localDB the file containing the local database
|
|
||||||
* @since Envoy v0.1-alpha
|
|
||||||
**/
|
|
||||||
public void setLocalDB(File localDB) { this.localDB = localDB; }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the current time (milliseconds) that is waited between Syncs
|
* @return the current time (milliseconds) that is waited between Syncs
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public int getSyncTimeout() { return syncTimeout; }
|
public int getSyncTimeout() { return (int) items.get("syncTimeout").get(); }
|
||||||
|
|
||||||
/**
|
public File getHomeDirectory() { return (File) items.get("homeDirectory").get(); }
|
||||||
* @param syncTimeout sets the time (milliseconds) during which Sync waits
|
|
||||||
* @since Envoy v0.1-alpha
|
public Level getFileLevelBarrier() { return (Level) items.get("fileLevelBarrier").get(); }
|
||||||
*/
|
|
||||||
public void setSyncTimeout(int syncTimeout) { this.syncTimeout = syncTimeout; }
|
public Level getConsoleLevelBarrier() { return (Level) items.get("consoleLevelBarrier").get(); }
|
||||||
}
|
}
|
||||||
|
32
src/main/java/envoy/client/ConfigItem.java
Normal file
32
src/main/java/envoy/client/ConfigItem.java
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package envoy.client;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Project: <strong>envoy-clientChess</strong><br>
|
||||||
|
* File: <strong>ConfigItem.javaEvent.java</strong><br>
|
||||||
|
* Created: <strong>21.12.2019</strong><br>
|
||||||
|
*
|
||||||
|
* @author Kai S. K. Engelbart
|
||||||
|
*/
|
||||||
|
public class ConfigItem<T> {
|
||||||
|
|
||||||
|
private String commandLong, commandShort;
|
||||||
|
private Function<String, T> parseFunction;
|
||||||
|
private T value;
|
||||||
|
|
||||||
|
public ConfigItem(String commandLong, String commandShort, Function<String, T> parseFunction, T defaultValue) {
|
||||||
|
this.commandLong = commandLong;
|
||||||
|
this.commandShort = commandShort;
|
||||||
|
this.parseFunction = parseFunction;
|
||||||
|
value = defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void parse(String input) { value = parseFunction.apply(input); }
|
||||||
|
|
||||||
|
public String getCommandLong() { return commandLong; }
|
||||||
|
|
||||||
|
public String getCommandShort() { return commandShort; }
|
||||||
|
|
||||||
|
public T get() { return value; }
|
||||||
|
}
|
@ -62,6 +62,9 @@ public class Startup {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EnvoyLog.setFileLevelBarrier(Config.getInstance().getFileLevelBarrier());
|
||||||
|
EnvoyLog.setConsoleLevelBarrier(Config.getInstance().getConsoleLevelBarrier());
|
||||||
|
|
||||||
// Ask the user for their user name
|
// Ask the user for their user name
|
||||||
String userName = JOptionPane.showInputDialog("Please enter your username");
|
String userName = JOptionPane.showInputDialog("Please enter your username");
|
||||||
if (userName == null || userName.isEmpty()) {
|
if (userName == null || userName.isEmpty()) {
|
||||||
|
@ -2,11 +2,9 @@ package envoy.client.util;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.logging.ConsoleHandler;
|
import java.util.logging.*;
|
||||||
import java.util.logging.FileHandler;
|
|
||||||
import java.util.logging.Level;
|
import envoy.client.Config;
|
||||||
import java.util.logging.Logger;
|
|
||||||
import java.util.logging.SimpleFormatter;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>envoy-client</strong><br>
|
* Project: <strong>envoy-client</strong><br>
|
||||||
@ -14,16 +12,18 @@ import java.util.logging.SimpleFormatter;
|
|||||||
* Created: <strong>14 Dec 2019</strong><br>
|
* Created: <strong>14 Dec 2019</strong><br>
|
||||||
*
|
*
|
||||||
* @author Leon Hofmeister
|
* @author Leon Hofmeister
|
||||||
|
* @author Kai S. K. Engelbart
|
||||||
* @since Envoy v0.2-alpha
|
* @since Envoy v0.2-alpha
|
||||||
*/
|
*/
|
||||||
public class EnvoyLog {
|
public class EnvoyLog {
|
||||||
|
|
||||||
private static Level fileLevelBarrier = Level.CONFIG;
|
private static Level fileLevelBarrier = Config.getInstance().getFileLevelBarrier(), consoleLevelBarrier = Config.getInstance().getConsoleLevelBarrier();
|
||||||
|
|
||||||
private EnvoyLog() {}
|
private EnvoyLog() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a {@link Logger} with a specified name
|
* Creates a {@link Logger} with a specified name
|
||||||
|
*
|
||||||
* @param name the name of the {@link Logger} to create
|
* @param name the name of the {@link Logger} to create
|
||||||
* @return the created {@link Logger}
|
* @return the created {@link Logger}
|
||||||
*/
|
*/
|
||||||
@ -46,7 +46,7 @@ public class EnvoyLog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ConsoleHandler ch = new ConsoleHandler();
|
ConsoleHandler ch = new ConsoleHandler();
|
||||||
ch.setLevel(Level.FINEST);
|
ch.setLevel(consoleLevelBarrier);
|
||||||
ch.setFormatter(formatter);
|
ch.setFormatter(formatter);
|
||||||
logger.addHandler(ch);
|
logger.addHandler(ch);
|
||||||
|
|
||||||
@ -66,5 +66,9 @@ public class EnvoyLog {
|
|||||||
* from 0 - 1000 or with the according name of the level
|
* from 0 - 1000 or with the according name of the level
|
||||||
* @since Envoy v0.2-alpha
|
* @since Envoy v0.2-alpha
|
||||||
*/
|
*/
|
||||||
public static void setFileLevel(String fileLevelBarrier) { EnvoyLog.fileLevelBarrier = Level.parse(fileLevelBarrier); }
|
public static void setFileLevelBarrier(Level fileLevelBarrier) { EnvoyLog.fileLevelBarrier = fileLevelBarrier; }
|
||||||
|
|
||||||
|
public static Level getConsoleLevelBarrier() { return consoleLevelBarrier; }
|
||||||
|
|
||||||
|
public static void setConsoleLevelBarrier(Level consoleLevelBarrier) { EnvoyLog.consoleLevelBarrier = consoleLevelBarrier; }
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user