Module io.jooby
Package io.jooby

Class Environment

java.lang.Object
io.jooby.Environment

public class Environment extends Object
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 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

      @NonNull public String getProperty(@NonNull String key, @NonNull String defaults)
      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

      @Nullable public String getProperty(@NonNull String key)
      Get a property under the given key or null when missing.
      Parameters:
      key - Property key.
      Returns:
      Property value or null when missing.
    • getProperties

      @NonNull public Map<String,String> getProperties(@NonNull String key)
      List all the properties under the given key. Example:
       user.name = "name"
       user.password = "pass"
       
      A call to getProperties("user") give you a map like: {user.name: name, user.password: pass}
      Parameters:
      key - Key.
      Returns:
      Properties under that key or empty map.
    • getProperties

      @NonNull public Map<String,String> getProperties(@NonNull String key, @Nullable String prefix)
      List all the properties under the given key. Example:
       user.name = "name"
       user.password = "pass"
       
      A call to getProperties("user", "u") give you a map like: {u.name: name, u.password: pass}
      Parameters:
      key - Key.
      prefix - Prefix to use or null for none.
      Returns:
      Properties under that key or empty map.
    • getConfig

      @NonNull public com.typesafe.config.Config getConfig()
      Application configuration.
      Returns:
      Application configuration.
    • setConfig

      public Environment setConfig(@NonNull com.typesafe.config.Config config)
      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

      @NonNull public List<String> getActiveNames()
      Active environment names.
      Returns:
      Active environment names.
    • isActive

      public boolean isActive(@NonNull String name, String... names)
      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

      @NonNull public ClassLoader getClassLoader()
      Application class loader.
      Returns:
      Application class loader.
    • loadClass

      @NonNull public Optional<Class> loadClass(@NonNull String className)
      Loaded class or empty.
      Parameters:
      className - Class name.
      Returns:
      Load a class or get an empty value.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • systemProperties

      @NonNull public static com.typesafe.config.Config systemProperties()
      Creates a Config object from System.getProperties().
      Returns:
      Configuration object.
    • systemEnv

      @NonNull public static com.typesafe.config.Config systemEnv()
      Creates a Config object from System.getenv().
      Returns:
      Configuration object.
    • loadEnvironment

      @NonNull public static Environment loadEnvironment(@NonNull 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).
      • ${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
      Property overrides is done in the following order (first-listed are higher priority):
      • 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

      @Nullable public static String pid()
      Find JVM process ID.
      Returns:
      JVM process ID or null.