Configuration using Confort
Confort is a custom-written configuration library designed to unify different configuration formats into one abstract representation for Java applications.
At the moment, the library supports only JSON but other formats can be easily supported by adding an ANTLR v4 grammar and a visitor.
The project is open source too! Check it out!
Confort now has a wiki!
Visit https://github.com/MarkL4YG/confort/wiki for the official library wiki.
The node structure
Confort is currently designed for tree-like configuration formats and builds its internal structure around that:
- A configuration has got a single root node (
IConfigNode
). - A configuration node can one be of the following types:
virtual
: The configuration node does not hold any value (transitively)primitive
: The configuration node holds one of the following types:Integer
,Float
,Double
,String
orBoolean
list
: The configuration node contains a list of other configuration nodesmap
: The configuration node contains aString
->IConfigNode
mapping
- The classes that use the configuration are not supposed to be aware of the configuration format. It is completely hidden away from them.
Source abstraction
Confort allows the source of a configuration to be hidden too. For that, implementations of IConfig
are used. (The JeakBot framework currently only uses the FileConfig
implementation.)
Instances of that interface offer #load
and #save
methods to load and safe the state.
They also hold the currently loaded root configuration node.
Full example
Here's a full example of a plugin loading and saving a configuration using an IConfig
representation:
package de.fearnixx.jeak;
import de.fearnixx.jeak.reflect.Config;
import de.fearnixx.jeak.reflect.Inject;
import de.fearnixx.jeak.reflect.JeakBotPlugin;
@JeakBotPlugin(id = "sometestplugin")
public class TestPlugin {
@Inject
@Config(id = "testConfiguration")
private IConfig configurationRepresentation;
private IConfigNode configRoot;
private void loadConfiguration() {
try {
configurationRepresentation.load();
configRoot = configurationRepresentation.getRoot();
} catch (ParseException | IOException e) {
// A fatal error has occurred!
}
}
private void saveConfiguration() {
try {
configurationRepresentation.save();
} catch (IOException e) {
// A fatal error has occurred!
}
}
public void useConfiguration() {
loadConfiguration();
System.out.println(configRoot.getNode("some-string").asString());
saveConfiguration();
}
}
It is very clear that the plugin does not care where the configuration actually comes from and what format is used.
Note
For more information on how to use
IConfigNode
, please consult its JavaDoc.
The confort project currently does not have an online documentation.
Additional Node
You may take a look at the
Configurable
convenience class and check if extending this class is sufficient for your needs.
Updated over 5 years ago