pingex
/
DiscordBot
Archived
1
0
Fork 0

Configuration Class

master
Pingex aka Raphaël 9 years ago
parent 047c7ab322
commit 1f08fa0d9d

1
.gitignore vendored

@ -69,3 +69,4 @@ gradle-app.setting
# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
# gradle/wrapper/gradle-wrapper.properties
config.ini

@ -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"
}

@ -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 <T> Method will return a variable of this type
* @return A <T> representation of the value, `null` if the key doesn't exist
*/
public static<T> T getValue(String section, String key, Class<T> 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<String> 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);
}
}
}

@ -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);
}
}
}