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

com.scalar.db.config.ConfigUtils Maven / Gradle / Ivy

Go to download

A universal transaction manager that achieves database-agnostic transactions and distributed transactions that span multiple databases

There is a newer version: 3.14.0
Show newest version
package com.scalar.db.config;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import java.util.Properties;
import javax.annotation.Nullable;
import org.apache.commons.text.StringSubstitutor;
import org.apache.commons.text.lookup.StringLookupFactory;

/**
 * A utility class for retrieving configuration values from {@link Properties}. You can use
 * placeholders in the values, and they are replaced with environment variables
 * (${env:<environment variable name>}) or system properties (${sys:<system property
 * name>}). You can also specify default values in placeholders like ${sys:<system property
 * name>:-defaultValue}.
 */
public final class ConfigUtils {

  /**
   * A {@link StringSubstitutor} instance with the environment variable string lookup and system
   * property string lookup.
   */
  private static final StringSubstitutor stringSubstitutor =
      new StringSubstitutor(
          StringLookupFactory.INSTANCE.interpolatorStringLookup(
              ImmutableMap.of(
                  StringLookupFactory.KEY_ENV,
                  StringLookupFactory.INSTANCE.environmentVariableStringLookup(),
                  StringLookupFactory.KEY_SYS,
                  StringLookupFactory.INSTANCE.systemPropertyStringLookup()),
              StringLookupFactory.INSTANCE.nullStringLookup(),
              false));

  private ConfigUtils() {}

  public static String getString(Properties properties, String name, String defaultValue) {
    String value = trimAndReplace(properties.getProperty(name));
    if (Strings.isNullOrEmpty(value)) {
      return defaultValue;
    }
    return value;
  }

  public static int getInt(Properties properties, String name, int defaultValue) {
    String value = trimAndReplace(properties.getProperty(name));
    if (Strings.isNullOrEmpty(value)) {
      return defaultValue;
    }
    try {
      return Integer.parseInt(value);
    } catch (NumberFormatException ignored) {
      throw new IllegalArgumentException(
          "the specified value of '" + name + "' is not a number. value: " + value);
    }
  }

  @Nullable
  public static Integer getInt(Properties properties, String name, @Nullable Integer defaultValue) {
    String value = trimAndReplace(properties.getProperty(name));
    if (Strings.isNullOrEmpty(value)) {
      return defaultValue;
    }
    try {
      return Integer.parseInt(value);
    } catch (NumberFormatException ignored) {
      throw new IllegalArgumentException(
          "the specified value of '" + name + "' is not a number. value: " + value);
    }
  }

  public static long getLong(Properties properties, String name, long defaultValue) {
    String value = trimAndReplace(properties.getProperty(name));
    if (Strings.isNullOrEmpty(value)) {
      return defaultValue;
    }
    try {
      return Long.parseLong(value);
    } catch (NumberFormatException ignored) {
      throw new IllegalArgumentException(
          "the specified value of '" + name + "' is not a number. value: " + value);
    }
  }

  @Nullable
  public static Long getLong(Properties properties, String name, @Nullable Long defaultValue) {
    String value = trimAndReplace(properties.getProperty(name));
    if (Strings.isNullOrEmpty(value)) {
      return defaultValue;
    }
    try {
      return Long.parseLong(value);
    } catch (NumberFormatException ignored) {
      throw new IllegalArgumentException(
          "the specified value of '" + name + "' is not a number. value: " + value);
    }
  }

  public static boolean getBoolean(Properties properties, String name, boolean defaultValue) {
    String value = trimAndReplace(properties.getProperty(name));
    if (Strings.isNullOrEmpty(value)) {
      return defaultValue;
    }
    if (Boolean.TRUE.toString().equalsIgnoreCase(value)
        || Boolean.FALSE.toString().equalsIgnoreCase(value)) {
      return Boolean.parseBoolean(value);
    } else {
      throw new IllegalArgumentException(
          "the specified value of '" + name + "' is not a boolean value. value: " + value);
    }
  }

  @Nullable
  public static Boolean getBoolean(
      Properties properties, String name, @Nullable Boolean defaultValue) {
    String value = trimAndReplace(properties.getProperty(name));
    if (Strings.isNullOrEmpty(value)) {
      return defaultValue;
    }
    if (Boolean.TRUE.toString().equalsIgnoreCase(value)
        || Boolean.FALSE.toString().equalsIgnoreCase(value)) {
      return Boolean.parseBoolean(value);
    } else {
      throw new IllegalArgumentException(
          "the specified value of '" + name + "' is not a boolean value. value: " + value);
    }
  }

  public static String[] getStringArray(Properties properties, String name, String[] defaultValue) {
    String value = trimAndReplace(properties.getProperty(name));
    if (Strings.isNullOrEmpty(value)) {
      return defaultValue;
    }
    return value.split("\\s*,\\s*");
  }

  @VisibleForTesting
  static String trimAndReplace(@Nullable String value) {
    if (value == null) {
      return null;
    }
    return stringSubstitutor.replace(value.trim());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy