Class Config

java.lang.Object
io.inversion.utils.Config

public class Config extends Object
Global configuration properties access through a singleton wrapper around a Commons Configuration CompositeConfiguration object.

The name/value pairs found in configuration are used by the Configurator to reflectively set bean properties on Apis, Endpoints, Actions, Dbs and other Api model objects during Engine startup.

Automatic/reflective parameter setting via the Configurator is the preferred way to do runtime dependency injection. You can however directly use this classes Config.getXYX() methods to look up key/value pairs at runtime.

You can access and modify the underlying CompositeConfiguration object to change where properties are pulled form.

By default, listed in order of priority, properties are merged from:

  1. a "${user.dir}/.env" properties file if one exists.
  2. system properties
  3. environment variables
  4. ${configPath}/inversion[-][0-100]-${configProfile}.properties files if they exist
  5. ${configPath}/inversion-${configProfile}[-][0-100].properties files if they exist
  6. ${configPath}/inversion[-][0-100].properties files if they exist

For example:

  • if key "foo" is found in "${user.dir}/.env" the corresponding value will always be returned no matter what other source have key "foo"
  • if key "bar" exists in inversion-${configProfile}.properties and inversion.properties, the value from inversion-${configProfile}.properties will be returned
  • if key "abc" is environment and inversion-${configProfile}.properties, the value from the environment will be returned
  • if key "xyz" is in inversion.properties and inversion-10.properties, the value from inversion-10.properties will be returned

If you have a custom configuration data source, such as a database or secrets key vault, you can add those to the FRONT of the CompositeConfiguration via Config.getConfiguration().addConfigurationFirst(MY_CONFIG_OBJECT) to make sure those values are given the highest priority. Use CompositeConfiguration.addConfiguration to give your custom props lowest priority.

See Also:
  • Method Details

    • hasConfiguration

      public static boolean hasConfiguration()
    • getConfiguration

      public static org.apache.commons.configuration2.CompositeConfiguration getConfiguration()
      If configuration is null, loadConfiguration is called to lazy load the default config.
      Returns:
      the system wide CompositeConfiguration
    • setConfiguration

      public static void setConfiguration(org.apache.commons.configuration2.CompositeConfiguration configuration)
      Sets the system wide CompositeConfiguration.

      Generally, you don't need to explicitly call this, as accessing any of this classes getters will cause the default configuration to be loaded via loadConfiguration() if configuration is null.

      Parameters:
      configuration - the configuration to use
    • clearConfiguration

      public static void clearConfiguration()
      Nulls out the system wide CompositeConfiguration, same as setConfiguration(null)
    • loadConfiguration

      public static void loadConfiguration(String configPath, String configProfile)
      Creates a new CompositeConfiguration with individual Configuration objects loaded with key/value pairs from sources as described in the class comment above.

      If configPath is null, it will be looked up via Utils.findProperty with the following keys:

      1. "inversion.configPath"
      2. "configPath"

      If configProfile is null, it will be looked up via Utils.findProperty with the following keys:

      1. "inversion.configProfile"
      2. "inversion.profile"
      3. "spring.profiles.active"
      4. "configProfile"
      5. "profile"
      Parameters:
      configPath - the path use to locate 'inversion.properties' files via getResource
      configProfile - the runtime profile used to load some inversion-${configProfile}-[0-100].properties files and not others.
      See Also:
    • findUrl

      protected static URL findUrl(String name)
      Attempts to locate a resource URL for name via the ClassLoader or as a file path relative to ${user.dir}.
      Parameters:
      name - a string identifier for the resource to find
      Returns:
      a url
    • getKeys

      public static Iterator<String> getKeys()
    • getProperty

      public static Object getProperty(String key)
    • getBoolean

      public static boolean getBoolean(String key)
    • getBoolean

      public static boolean getBoolean(String key, boolean defaultValue)
    • getBoolean

      public static Boolean getBoolean(String key, Boolean defaultValue)
    • getByte

      public static byte getByte(String key)
    • getByte

      public static byte getByte(String key, byte defaultValue)
    • getByte

      public static Byte getByte(String key, Byte defaultValue)
    • getDouble

      public static double getDouble(String key)
    • getDouble

      public static double getDouble(String key, double defaultValue)
    • getDouble

      public static Double getDouble(String key, Double defaultValue)
    • getFloat

      public static float getFloat(String key)
    • getFloat

      public static float getFloat(String key, float defaultValue)
    • getFloat

      public static Float getFloat(String key, Float defaultValue)
    • getInt

      public static int getInt(String key)
    • getInt

      public static int getInt(String key, int defaultValue)
    • getInteger

      public static Integer getInteger(String key, Integer defaultValue)
    • getLong

      public static long getLong(String key)
    • getLong

      public static long getLong(String key, long defaultValue)
    • getLong

      public static Long getLong(String key, Long defaultValue)
    • getShort

      public static short getShort(String key)
    • getShort

      public static short getShort(String key, short defaultValue)
    • getShort

      public static Short getShort(String key, Short defaultValue)
    • getBigDecimal

      public static BigDecimal getBigDecimal(String key)
    • getBigDecimal

      public static BigDecimal getBigDecimal(String key, BigDecimal defaultValue)
    • getBigInteger

      public static BigInteger getBigInteger(String key)
    • getBigInteger

      public static BigInteger getBigInteger(String key, BigInteger defaultValue)
    • getString

      public static String getString(String key)
    • getString

      public static String getString(String key, String defaultValue)