All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cz.jalasoft.util.configuration.provider.HoconConfigProvider Maven / Gradle / Ivy

The newest version!
package cz.jalasoft.util.configuration.provider;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import cz.jalasoft.util.configuration.source.ConfigSource;

import java.io.IOException;
import java.io.Reader;

/**
 * @author Honza Lastovicka ([email protected])
 * @since 2016-07-27.
 */
public final class HoconConfigProvider implements ConfigProvider {

    private Config config;

    public HoconConfigProvider() {
        checkTypeSafeConfingOnClasspath();
    }

    private void checkTypeSafeConfingOnClasspath() {
        try {
            Class.forName("com.typesafe.config.Config");
        } catch (ClassNotFoundException exc) {
            throw new IllegalStateException("Cannot load TypeSafeConfig library as a source of hocon configuration infrastructure.");
        }
    }

    @Override
    public void reload(ConfigSource source) throws IOException {
        try (Reader input = source.load()) {
            config = ConfigFactory.parseReader(input);
        }
    }

    @Override
    public String readProperty(String key) {
        if (config == null) {
            throw new IllegalStateException("First call reload()");
        }

        return config.getString(key);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy