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

net.pincette.jes.util.Configuration Maven / Gradle / Ivy

There is a newer version: 3.2.1
Show newest version
package net.pincette.jes.util;

import static com.typesafe.config.ConfigFactory.parseFile;
import static java.lang.System.getProperty;
import static net.pincette.util.Util.tryToGetRethrow;
import static net.pincette.util.Util.tryToGetSilent;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import java.io.File;
import java.util.Optional;

/**
 * Some configuration utilities.
 *
 * @since 1.0
 * @author Werner Donn\u00e9
 */
public class Configuration {
  private Configuration() {}

  /**
   * Tries to load the default configuration application.conffrom the conf
   * directory. If the system property config.resource is set its value is also
   * resolved against the conf directory.
   *
   * @return The resolved configuration.
   * @see load
   * @since 1.0
   */
  public static Config loadDefault() {
    return Optional.ofNullable(getProperty("config.resource"))
        .flatMap(resource -> tryToGetRethrow(() -> parseFile(new File(new File("conf"), resource))))
        .orElseGet(
            () ->
                tryToGetSilent(() -> parseFile(new File(new File("conf"), "application.conf")))
                    .orElseGet(ConfigFactory::load));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy