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

info.javaspec.AppConfigGateway Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
package info.javaspec;

import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;

/** Loads configuration from a properties file, encoded in the given stream */
final class AppConfigGateway {
  private final Properties config;

  public static AppConfigGateway fromPropertyResource() {
    return fromPropertyResource("/javaspec.properties");
  }

  public static AppConfigGateway fromPropertyResource(String resourcePath) {
    return Optional.ofNullable(AppConfigGateway.class.getResourceAsStream(resourcePath))
      .map(AppConfigGateway::loadProperties)
      .map(AppConfigGateway::new)
      .orElseThrow(() -> new InvalidProperties(resourcePath));
  }

  private AppConfigGateway(Properties config) {
    this.config = config;
  }

  public String version() {
    return Optional.ofNullable(config.getProperty("javaspec.version"))
      .orElseThrow(() -> new MissingProperty("javaspec.version"));
  }

  private static Properties loadProperties(InputStream stream) {
    Properties properties = new Properties();
    try {
      properties.load(stream);
    } catch(IOException e) {
      throw new PropertyLoadFailed(e);
    }

    return properties;
  }

  public static final class InvalidProperties extends RuntimeException {
    public InvalidProperties(String path) {
      super(String.format("Invalid property stream: %s", path));
    }
  }

  public static final class PropertyLoadFailed extends RuntimeException {
    public PropertyLoadFailed(Exception cause) {
      super("Failed to read properties", cause);
    }
  }

  public static final class MissingProperty extends RuntimeException {
    public MissingProperty(String name) {
      super(String.format("Missing property: %s", name));
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy