Package org.javalite.app_config
Class AppConfig
- java.lang.Object
-
- org.javalite.app_config.AppConfig
-
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, wherenameis 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 calledACTIVE_ENVor system propertyactive_env. The system property will override the environment variable! Such configuration is easy to achieve in Unix shell:export ACTIVE_ENV=testIf environment variableACTIVE_ENVis missing, it defaults to "development".You can also provide an environment as a system property
active_env. System property overrides environment variableACTIVE_ENVExample:
If there are four files packed into a/app_configpackage:- global.properties
- development.properties
- staging.properties
- production.properties
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.propertiesThe 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 printAnd the name is John:System.out.println(p("phrase"));Note: The order of properties does not matter.- Author:
- Igor Polevoy
-
-
Constructor Summary
Constructors Constructor Description AppConfig()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static StringactiveEnv()Returns current environment name as defined by environment variableACTIVE_ENV.voidclear()booleancontainsKey(Object key)booleancontainsValue(Object value)Set<Map.Entry<String,String>>entrySet()Stringget(Object key)static Map<String,String>getAllProperties()static PropertygetAsProperty(String key)Returns property instance corresponding to key.static List<String>getKeys(String prefix)Returns all keys that start with a prefixstatic List<String>getProperties(String prefix)Return all numbered properties with a prefix.static StringgetProperty(String key)Returns property value for a key.static voidinit()booleanisEmpty()static booleanisInDevelopment()static booleanisInProduction()static booleanisInStaging()static booleanisInTestEnv()static booleanisInTestMode()Checks if running in a context of a test by checking of a presence of a classorg.junit.Teston classpath.Set<String>keySet()static Stringp(String key)Gets property, synonym forgetProperty(String).static BooleanpBoolean(String propertyName)Read property asBoolean.static DoublepDouble(String propertyName)Read property asDouble.static FloatpFloat(String propertyName)Read property asFloat.static IntegerpInteger(String propertyName)Read property asInteger.Stringput(String key, String value)voidputAll(Map<? extends String,? extends String> m)static voidreload()Used in tests.Stringremove(Object key)static voidsetActiveEnv(String activeEnv)You can change the environment dynamically.static StringsetProperty(String name, String value)Sets a property in memory.intsize()Collection<String>values()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Method Detail
-
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 propertyvalue- - 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,
nullif not found.
-
p
public static String p(String key)
Gets property, synonym forgetProperty(String).- Parameters:
key- key of property- Returns:
- property value
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKeyin interfaceMap<String,String>
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValuein interfaceMap<String,String>
-
activeEnv
public static String activeEnv()
Returns current environment name as defined by environment variableACTIVE_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 classorg.junit.Teston classpath.- Returns:
- true if class
org.junit.Testis on classpath, otherwise returnsfalse
-
isInTestEnv
public static boolean isInTestEnv()
- Returns:
- true if environment name as defined by environment variable
ACTIVE_ENVis "testenv".
-
isInProduction
public static boolean isInProduction()
- Returns:
- true if environment name as defined by environment variable
ACTIVE_ENVis "production".
-
isInDevelopment
public static boolean isInDevelopment()
- Returns:
- true if environment name as defined by environment variable
ACTIVE_ENVis "development".
-
isInStaging
public static boolean isInStaging()
- Returns:
- true if environment name as defined by environment variable
ACTIVE_ENVis "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:Listthen the resulting list will have all properties starting fromprops = AppConfig.getProperties("prop"); 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 asInteger.- Parameters:
propertyName- name of property.- Returns:
- property as
Integer.
-
pDouble
public static Double pDouble(String propertyName)
Read property asDouble.- Parameters:
propertyName- name of property.- Returns:
- property as
Double.
-
pFloat
public static Float pFloat(String propertyName)
Read property asFloat.- Parameters:
propertyName- name of property.- Returns:
- property as
Float.
-
-