java.lang.Object
io.jooby.Environment
Application environment contains configuration object and active environment names.
The active environment names serve the purpose of allowing loading different configuration
files depending on the environment. Also, Extension
modules might configure application
services differently depending on the environment too. For example: turn on/off caches, reload
files, etc.
The application.env
property controls the active environment names.
- Since:
- 2.0.0
- Author:
- edgar
-
Constructor Summary
ConstructorDescriptionEnvironment
(ClassLoader classLoader, com.typesafe.config.Config config, String... actives) Creates a new environment.Environment
(ClassLoader classLoader, com.typesafe.config.Config config, List<String> actives) Creates a new environment. -
Method Summary
Modifier and TypeMethodDescriptionstatic com.typesafe.config.Config
defaults()
Creates a default configuration properties with some common values like: application.tmpdir, application.charset and pid (process ID).Active environment names.Application class loader.com.typesafe.config.Config
Application configuration.getProperties
(String key) List all the properties under the given key.getProperties
(String key, String prefix) List all the properties under the given key.getProperty
(String key) Get a property under the given key ornull
when missing.getProperty
(String key, String defaults) Get a property under the given key or use the given default value when missing.boolean
Test is the given environment names are active.Loaded class or empty.static Environment
loadEnvironment
(EnvironmentOptions options) This method search for an application.conf file in three location (first-listed are higher priority): ${user.dir}/conf: This is a file system location, useful is you want to externalize configuration (outside of jar file).static String
pid()
Find JVM process ID.setConfig
(com.typesafe.config.Config config) Set configuration properties.static com.typesafe.config.Config
Creates aConfig
object fromSystem.getenv()
.static com.typesafe.config.Config
Creates aConfig
object fromSystem.getProperties()
.toString()
-
Constructor Details
-
Environment
public Environment(@NonNull ClassLoader classLoader, @NonNull com.typesafe.config.Config config, @NonNull String... actives) Creates a new environment.- Parameters:
classLoader
- Class loader.config
- Application configuration.actives
- Active environment names.
-
Environment
public Environment(@NonNull ClassLoader classLoader, @NonNull com.typesafe.config.Config config, @NonNull List<String> actives) Creates a new environment.- Parameters:
classLoader
- Class loader.config
- Application configuration.actives
- Active environment names.
-
-
Method Details
-
getProperty
Get a property under the given key or use the given default value when missing.- Parameters:
key
- Property key.defaults
- Default value.- Returns:
- Property or default value.
-
getProperty
Get a property under the given key ornull
when missing.- Parameters:
key
- Property key.- Returns:
- Property value or
null
when missing.
-
getProperties
List all the properties under the given key. Example:user.name = "name" user.password = "pass"
A call togetProperties("user")
give you a map like:{user.name: name, user.password: pass}
- Parameters:
key
- Key.- Returns:
- Properties under that key or empty map.
-
getProperties
List all the properties under the given key. Example:user.name = "name" user.password = "pass"
A call togetProperties("user", "u")
give you a map like:{u.name: name, u.password: pass}
- Parameters:
key
- Key.prefix
- Prefix to use ornull
for none.- Returns:
- Properties under that key or empty map.
-
getConfig
@NonNull public com.typesafe.config.Config getConfig()Application configuration.- Returns:
- Application configuration.
-
setConfig
Set configuration properties. Please note setting a configuration object must be done at very early application stage.- Parameters:
config
- Configuration properties.- Returns:
- This environment.
-
getActiveNames
Active environment names.- Returns:
- Active environment names.
-
isActive
Test is the given environment names are active.- Parameters:
name
- Environment name.names
- Optional environment names.- Returns:
- True if any of the given names is active.
-
getClassLoader
Application class loader.- Returns:
- Application class loader.
-
loadClass
Loaded class or empty.- Parameters:
className
- Class name.- Returns:
- Load a class or get an empty value.
-
toString
-
systemProperties
@NonNull public static com.typesafe.config.Config systemProperties()Creates aConfig
object fromSystem.getProperties()
.- Returns:
- Configuration object.
-
systemEnv
@NonNull public static com.typesafe.config.Config systemEnv()Creates aConfig
object fromSystem.getenv()
.- Returns:
- Configuration object.
-
loadEnvironment
This method search for an application.conf file in three location (first-listed are higher priority):- ${user.dir}/conf: This is a file system location, useful is you want to externalize configuration (outside of jar file).
- ${user.dir}: This is a file system location, useful is you want to externalize configuration (outside of jar file)
- classpath:// (root of classpath). No external configuration, configuration file lives inside the jar file
- Program arguments
- System properties
- Environment variables
- Environment property file
- Property file
- Parameters:
options
- Options like basedir, filename, etc.- Returns:
- A new environment.
-
defaults
@NonNull public static com.typesafe.config.Config defaults()Creates a default configuration properties with some common values like: application.tmpdir, application.charset and pid (process ID).- Returns:
- A configuration object.
-
pid
Find JVM process ID.- Returns:
- JVM process ID or
null
.
-