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.
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.
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
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
Returns current environment name as defined by environment variableACTIVE_ENV
.void
clear()
boolean
containsKey(Object key)
boolean
containsValue(Object value)
entrySet()
static Property
getAsProperty(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 String
getProperty(String key)
Returns property value for a key.static void
init()
boolean
isEmpty()
static boolean
static boolean
static boolean
static boolean
static boolean
Checks if running in a context of a test by checking of a presence of a classorg.junit.Test
on classpath.keySet()
static String
Gets property, synonym forgetProperty(String)
.static Boolean
Read property asBoolean
.static Double
Read property asDouble
.static Float
Read property asFloat
.static Integer
Read property asInteger
.static Long
Read property asLong
.void
static void
reload()
Used in tests.static void
setActiveEnv(String activeEnv)
You can change the environment dynamically.static String
setProperty(String name, String value)
Sets a property in memory.int
size()
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
-
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,
null
if 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:
containsKey
in interfaceMap<String,String>
-
containsValue
- Specified by:
containsValue
in 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.Test
on classpath.- Returns:
- true if class
org.junit.Test
is on classpath, otherwise returnsfalse
-
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
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:List
then 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
.
-