Class AppConfig

java.lang.Object
org.javalite.app_config.AppConfig
All Implemented Interfaces:
Map<String,​String>

public class AppConfig extends Object implements Map<String,​String>
This class allows configuration of applications for different deployment environments, such as development, test, staging, production, etc. Configuration is done either with property files on the classpath, or on a file system.

1. Classpath configuration

Applications could have environment-specific files, whose names follow this pattern: name.properties, where name is a name of a deployment environment, such as "development", "staging", "production", etc. You can also provide a global file, properties from which will be loaded in all environments: global.properties.

In all cases the files need to be on the classpath in package /app_config.

Environment-specific file will have an "environment" part of the file name match to an environment variable called ACTIVE_ENV or system property active_env. The system property will override the environment variable!

Such configuration is easy to achieve in Unix shell:

export ACTIVE_ENV=test

If environment variable ACTIVE_ENV is missing, it defaults to "development".

You can also provide an environment as a system property active_env. System property overrides environment variable ACTIVE_ENV

Example:

If there are four files packed into a /app_config package:
  • global.properties
  • development.properties
  • staging.properties
  • production.properties
And the ACTIVE_ENV=staging, then properties will be loaded from the following files:
  • global.properties
  • staging.properties

2. File configuration

In addition to properties on classpath, you can also specify a single file for properties to loaded from a file system. Use a system property with a full path to a file like:
     java -cp $CLASSPATH com.myproject.Main -Dapp_config.properties=/opt/directory1/myproject.properties
 
The file-based configuration overrides classpath one. If you have a property defined in both, the classpath configuration will be completely ignored and the file property will be used.

Property substitution

AppConfig allows a property substitution to make it possible to refactor large property files by specifying a repeating value once. If your property file has these properties:
 first.name=John
   phrase= And the name is ${first.name}
 
than this code will print And the name is John:
 System.out.println(p("phrase"));
 
Note: The order of properties does not matter.
Author:
Igor Polevoy
  • Constructor Details

    • AppConfig

      public AppConfig()
  • Method Details

    • setActiveEnv

      public static void setActiveEnv(String activeEnv)
      You can change the environment dynamically. Attention!!! This method should only be used for tests! Careful out there...
      Parameters:
      activeEnv - new environment value
    • init

      public static void init()
    • reload

      public static void reload()
      Used in tests.
    • setProperty

      public static String setProperty(String name, String value)
      Sets a property in memory. If property exists, it will be overwritten, if not, a new one will be created.
      Parameters:
      name - - name of property
      value - - value of property
      Returns:
      old value
    • getAsProperty

      public static Property getAsProperty(String key)
      Returns property instance corresponding to key.
      Parameters:
      key - key for property.
      Returns:
      Property for this key.
    • getProperty

      public static String getProperty(String key)
      Returns property value for a key.
      Parameters:
      key - key of property.
      Returns:
      value for this key, null if not found.
    • p

      public static String p(String key)
      Gets property, synonym for getProperty(String).
      Parameters:
      key - key of property
      Returns:
      property value
    • getAllProperties

      public static Map<String,​String> getAllProperties()
    • size

      public int size()
      Specified by:
      size in interface Map<String,​String>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<String,​String>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<String,​String>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<String,​String>
    • get

      public String get(Object key)
      Specified by:
      get in interface Map<String,​String>
    • put

      public String put(String key, String value)
      Specified by:
      put in interface Map<String,​String>
    • remove

      public String remove(Object key)
      Specified by:
      remove in interface Map<String,​String>
    • putAll

      public void putAll(Map<? extends String,​? extends String> m)
      Specified by:
      putAll in interface Map<String,​String>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<String,​String>
    • keySet

      public Set<String> keySet()
      Specified by:
      keySet in interface Map<String,​String>
    • values

      public Collection<String> values()
      Specified by:
      values in interface Map<String,​String>
    • entrySet

      public Set<Map.Entry<String,​String>> entrySet()
      Specified by:
      entrySet in interface Map<String,​String>
    • activeEnv

      public static String activeEnv()
      Returns current environment name as defined by environment variable ACTIVE_ENV.
      Returns:
      current environment name as defined by environment variable ACTIVE_ENV.
    • isInTestMode

      public static boolean isInTestMode()
      Checks if running in a context of a test by checking of a presence of a class org.junit.Test on classpath.
      Returns:
      true if class org.junit.Test is on classpath, otherwise returns false
    • isInTestEnv

      public static boolean isInTestEnv()
      Returns:
      true if environment name as defined by environment variable ACTIVE_ENV is "testenv".
    • isInProduction

      public static boolean isInProduction()
      Returns:
      true if environment name as defined by environment variable ACTIVE_ENV is "production".
    • isInDevelopment

      public static boolean isInDevelopment()
      Returns:
      true if environment name as defined by environment variable ACTIVE_ENV is "development".
    • isInStaging

      public static boolean isInStaging()
      Returns:
      true if environment name as defined by environment variable ACTIVE_ENV is "staging".
    • getKeys

      public static List<String> getKeys(String prefix)
      Returns all keys that start with a prefix
      Parameters:
      prefix - prefix for properties.
    • getProperties

      public static List<String> getProperties(String prefix)
      Return all numbered properties with a prefix. For instance if there is a file:
           prop.1=one
           prop.2=two
       
      .. and this method is called:
           List props = AppConfig.getProperties("prop");
       
      then the resulting list will have all properties starting from prop. This method presumes consecutive numbers in the suffix.
      Parameters:
      prefix - prefix of numbered properties.
      Returns:
      list of property values.
    • pInteger

      public static Integer pInteger(String propertyName)
      Read property as Integer.
      Parameters:
      propertyName - name of property.
      Returns:
      property as Integer.
    • pDouble

      public static Double pDouble(String propertyName)
      Read property as Double.
      Parameters:
      propertyName - name of property.
      Returns:
      property as Double.
    • pLong

      public static Long pLong(String propertyName)
      Read property as Long.
      Parameters:
      propertyName - name of property.
      Returns:
      property as Long.
    • pFloat

      public static Float pFloat(String propertyName)
      Read property as Float.
      Parameters:
      propertyName - name of property.
      Returns:
      property as Float.
    • pBoolean

      public static Boolean pBoolean(String propertyName)
      Read property as Boolean.
      Parameters:
      propertyName - name of property.
      Returns:
      property as Boolean.