Package org.javalite.app_config
Class AppConfig
java.lang.Object
org.javalite.app_config.AppConfig
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.
your program will read these values. The Environment Variables have the highest precedent and will override any other properties defined for this environment.
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
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.
3. Environment Variables
You can set the environment variables as well. As long as they are read as usual:String val = p("env_var_name")
your program will read these values. The Environment Variables have the highest precedent and will override any other properties defined for this environment.
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
-
Nested Class Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringReturns current environment name as defined by environment variableACTIVE_ENV.voidclear()booleancontainsKey(Object key)booleancontainsValue(Object value)entrySet()static PropertygetAsProperty(String key)Returns property instance corresponding to key.Returns all keys that start with a prefixgetProperties(String prefix)Return all numbered properties with a prefix.static StringgetProperty(String key)Returns property value for a key.static voidinit()booleanisEmpty()static booleanstatic booleanstatic booleanstatic booleanstatic booleanChecks if running in a context of a test by checking of a presence of a classorg.junit.Teston classpath.keySet()static StringGets property, synonym forgetProperty(String).static BooleanRead property asBoolean.static DoubleRead property asDouble.static FloatRead property asFloat.static IntegerRead property asInteger.static LongRead property asLong.voidstatic voidreload()Used in tests.static voidsetActiveEnv(String activeEnv)You can change the environment dynamically.static StringsetProperty(String name, String value)Sets a property in memory.intsize()values()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
AppConfig
public AppConfig()
-
-
Method Details
-
setActiveEnv
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
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
Returns property instance corresponding to key.- Parameters:
key- key for property.- Returns:
- Property for this key.
-
getProperty
Returns property value for a key.- Parameters:
key- key of property.- Returns:
- value for this key,
nullif not found.
-
p
Gets property, synonym forgetProperty(String).- Parameters:
key- key of property- Returns:
- property value
-
getAllProperties
-
size
public int size() -
isEmpty
public boolean isEmpty() -
containsKey
- Specified by:
containsKeyin interfaceMap<String,String>
-
containsValue
- Specified by:
containsValuein interfaceMap<String,String>
-
get
-
put
-
remove
-
putAll
-
clear
public void clear() -
keySet
-
values
-
entrySet
-
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
Returns all keys that start with a prefix- Parameters:
prefix- prefix for properties.
-
getProperties
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
Read property asInteger.- Parameters:
propertyName- name of property.- Returns:
- property as
Integer.
-
pDouble
Read property asDouble.- Parameters:
propertyName- name of property.- Returns:
- property as
Double.
-
pLong
Read property asLong.- Parameters:
propertyName- name of property.- Returns:
- property as
Long.
-
pFloat
Read property asFloat.- Parameters:
propertyName- name of property.- Returns:
- property as
Float.
-
pBoolean
Read property asBoolean.- Parameters:
propertyName- name of property.- Returns:
- property as
Boolean.
-