Skip to content

Introduction

ljacqu edited this page Feb 2, 2020 · 4 revisions

ConfigMe is a configuration management library with support for YAML that comes out of the box. In particular, it has the following features:

  • Lightweight and flexible (few dependencies, i.e. small JAR size)
  • Null-safe retrieval of values—no need to ever check for null
  • You can add comments to your config that will be kept
  • Easy to extend migration so your configuration can evolve without losing your user's settings
  • Possibility to reload the entire configuration from the file at any given time

How it works

  • Each configurable value is a Property in ConfigMe. Properties are declared as public static final fields in classes which implement the SettingsHolder interface.
  • Configurations are read from a PropertyResource (e.g. the provided YamlFileResource), which abstracts reading and writing.
  • The property resource may be checked for completeness with the MigrationService, which allows you also to move renamed properties or to remove obsolete ones.
  • The SettingsManager unifies the members above. On creation, it calls the migration service and allows you to get and change property values.

Short example

We'll see the details later, but as a short example, you could have properties as such:

public class TitleConfig implements SettingsHolder {

    @Comment("The text of the title")
    public static final Property<String> TITLE_TEXT =
        newProperty("title.text", "-Default-");

    @Comment("The size that the title will have")
    public static final Property<Integer> TITLE_SIZE =
        newProperty("title.size", 10);
}

We can retrieve the values from the settings manager, and as mentioned, the value returned is guaranteed never null:

SettingsManager settingsManager = SettingsManagerBuilder
  .withYamlFile("config.yml")
  .configurationData(TitleConfig.class)
  .useDefaultMigrationService()
  .create();
int titleSize = settingsManager.getProperty(TitleConfig.TITLE_SIZE);
System.out.println("Title size is " + titleSize);

It either reads the value from the configuration file, or if it is absent or invalid, it will take the default value that we've defined with the property (in this case: 10).

Saving the configuration to the file will yield something like this:

title:
    # The text of the title
    text: '-Default-'
    # The size that the title will have
    size: 10

Navigation

Home Introduction Technical overview »