From 1f08fa0d9d243e5a6a3ec69cd3ff264a59f5a04c Mon Sep 17 00:00:00 2001 From: Pingex Date: Fri, 8 Apr 2016 01:26:25 +0200 Subject: [PATCH] Configuration Class --- .gitignore | 1 + build.gradle | 6 +- config.example.ini | 0 .../net/pingex/discordbot/Configuration.java | 88 +++++++++++++++++++ .../net/pingex/discordbot/DiscordBot.java | 24 ++++- 5 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 config.example.ini create mode 100644 src/main/java/net/pingex/discordbot/Configuration.java diff --git a/.gitignore b/.gitignore index 666905c..9271d4f 100644 --- a/.gitignore +++ b/.gitignore @@ -69,3 +69,4 @@ gradle-app.setting # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 # gradle/wrapper/gradle-wrapper.properties +config.ini \ No newline at end of file diff --git a/build.gradle b/build.gradle index 00b2f76..45368cb 100644 --- a/build.gradle +++ b/build.gradle @@ -3,10 +3,6 @@ apply plugin: 'java' group 'net.pingex.discordbot' version '0.1-dev' -apply plugin: 'java' - -sourceCompatibility = 1.5 - repositories { mavenCentral() jcenter() @@ -18,4 +14,6 @@ repositories { dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' compile "com.github.austinv11:Discord4j:2.4.2" + compile "org.mod4j.org.apache.commons:cli:1.0.0" + compile "org.ini4j:ini4j:0.5.4" } diff --git a/config.example.ini b/config.example.ini new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/net/pingex/discordbot/Configuration.java b/src/main/java/net/pingex/discordbot/Configuration.java new file mode 100644 index 0000000..e86b08f --- /dev/null +++ b/src/main/java/net/pingex/discordbot/Configuration.java @@ -0,0 +1,88 @@ +package net.pingex.discordbot; + +import org.ini4j.Ini; +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.logging.Logger; + +/** + * Contains configuration keys for the whole app. Also parses a .ini configuration file. + * @version 0.1-dev + * @author Raphael "Pingex" NAAS + */ +public final class Configuration +{ + private static Logger LOGGER = Logger.getLogger(Configuration.class.getName()); + + /** + * Main Configuration Datastore + */ + private static Ini datastore = new Ini(); + + /** + * Load keys from a file to the datastore + * @param path Relative or absolute path to the config file + * @throws IOException + */ + public static void loadConfiguration(String path) throws IOException + { + LOGGER.info("Loading configuration file: " + path); + datastore.load(new File(path)); + } + + /** + * Request a value from the datastore + * @param section Section of the datastore + * @param key Key wanted + * @return A String representation of the value, `null` if the key doesn't exist + */ + public static String getValue(String section, String key) + { + return getValue(section, key, String.class); + } + + /** + * Request a value from the datastore + * @param section Section of the datastore + * @param key Key wanted + * @param returnType Method will return a variable of this type + * @param Method will return a variable of this type + * @return A representation of the value, `null` if the key doesn't exist + */ + public static T getValue(String section, String key, Class returnType) + { + return datastore.get(section, key, returnType); + } + + /** + * Return all the values associated with this key + * @param section Section of the datastore + * @param key Key wanted + * @return A List of String containing all the values + */ + public static List getMultiValue(String section, String key) + { + return datastore.get(section).getAll(key); + } + + /** + * Change a key value. If value is null or an empty String, then the key will be removed from the datastore. + * Please note this method doesn't support multivalue keys. + * @param section Section of the datastore + * @param key Key to change + * @param value New key value + */ + public static void setValue(String section, String key, String value) + { + if(value == null || value.isEmpty()) + datastore.remove(section, key); + else + { + if(datastore.get(section, key) == null) + datastore.add(section, key, value); + else + datastore.get(section).replace(key, value); + } + } +} diff --git a/src/main/java/net/pingex/discordbot/DiscordBot.java b/src/main/java/net/pingex/discordbot/DiscordBot.java index eaa23cb..c4ced47 100644 --- a/src/main/java/net/pingex/discordbot/DiscordBot.java +++ b/src/main/java/net/pingex/discordbot/DiscordBot.java @@ -1,5 +1,8 @@ package net.pingex.discordbot; +import java.io.IOException; +import java.util.logging.Logger; + /** * Main Entry Point * @version 0.1-dev @@ -7,8 +10,27 @@ package net.pingex.discordbot; */ public class DiscordBot { + private static final Logger LOGGER = Logger.getLogger(DiscordBot.class.getName()); + + /** + * Entry point + * @param args Arguments passed to java + */ public static void main(String[] args) { - System.out.println("Hello World"); + LOGGER.info("Hello World, starting up"); + loadConfigurationSubroutine(); + } + + private static void loadConfigurationSubroutine() // TODO: Configuration file passed via CLI + { + try + { + Configuration.loadConfiguration("config.ini"); + } catch (IOException e) + { + LOGGER.severe("Could not load Configuration, reason: " + e.getMessage()); + System.exit(10); + } } }