Skip to content

Latest commit

 

History

History
86 lines (58 loc) · 4.03 KB

README.adoc

File metadata and controls

86 lines (58 loc) · 4.03 KB

Teragrep Configuration Library for Java

CNF-01 is a library that provides immutable configuration for Java projects. Immutability is achieved by converting the configuration source into an immutable Map. The resulting configuration Map can’t be altered even with modifying the underlying source.

Features

  • Provides immutable configurations for:

    1. configuration files / path properties (PathConfiguration)

    2. System Properties (PropertiesConfiguration)

    3. environment variables (EnvironmentConfiguration)

    4. command line arguments (ArgsConfiguration)

  • Default configurations in case the provided configurations from a source are not found or are otherwise broken (DefaultConfiguration)

How to use

Configuration

All sources of configuration can be used with the Configuration interface. It provides the method asMap() which returns the immutable configurations.

An example with PathConfiguration:

Configuration configuration = new PathConfiguration("file/path");
Map<String, String> configurationMap = configuration.asMap();

Default configuration

You might want to specify default configurations in case something brakes. For example, the file path in the code block above might not be found. In the following example, notice how the Map with default configurations are given through the ImmutabilitySupportedMap object to ensure type safety. DefaultConfiguration only takes an ImmutableMap (provided in CNF-01) as a parameter for the defaults.

Map<String, String> map = new HashMap<>();
ImmutableMap<String, String> defaults = new ImmutabilitySupportedMap<>(map).toImmutableMap();

DefaultConfiguration defaultConfiguration = new DefaultConfiguration(
        new PathConfiguration("file/path"),
        defaults
);
Map<String, String> configurationMap = defaultConfiguration.asMap();

Configuration objects in your project

The configuration Map from CNF-01 shouldn’t be used directly in regular objects. It should only be passed to objects that are responsible for providing configured versions of other objects, in other words, Factories. The regular objects must not be configurable!

Small example with an Example object:

ExampleFactory exampleFactory = new ExampleFactory(configurationMap);
Example example = exampleFactory.example();

Here, the logic for instantiating an Example is in the ExampleFactory object, which receives the configuration map from CNF-01 as a parameter. This ensures that the main object Example is as clear as it can be.

Contributing

You can involve yourself with our project by opening an issue or submitting a pull request.

Contribution requirements:

  1. All changes must be accompanied by a new or changed test. If you think testing is not required in your pull request, include a sufficient explanation as why you think so.

  2. Security checks must pass

  3. Pull requests must align with the principles and values of extreme programming.

  4. Pull requests must follow the principles of Object Thinking and Elegant Objects (EO).

Read more in our Contributing Guideline.

Contributor License Agreement

Contributors must sign Teragrep Contributor License Agreement before a pull request is accepted to organization’s repositories.

You need to submit the CLA only once. After submitting the CLA you can contribute to all Teragrep’s repositories.