Class Environment

java.lang.Object
org.jpos.core.Environment
All Implemented Interfaces:
Loggeable

public class Environment extends Object implements Loggeable
Manages environment-specific configuration for jPOS applications.

Environment provides property resolution with support for:

  • YAML (.yml) and properties (.cfg) configuration files
  • Property expressions: ${property.name}
  • Default values: ${property.name:default}
  • Equality tests: ${property.name=expected}
  • Boolean negation: ${!property.name}
  • Prefix-specific lookups:
    • $env{VAR} - OS environment variable only
    • $sys{prop} - Java system property only
    • $cfg{prop} - Configuration file only
    • $verb{text} - Verbatim (no expansion)
  • Nested expressions: ${outer:${inner:default}}

The default property resolution order (for ${prop}) is:

  1. OS environment variable (prop)
  2. OS environment variable (PROP with dots replaced by underscores)
  3. Java system property
  4. Configuration file property

Configuration is loaded from the directory specified by jpos.envdir (default: "cfg") with the filename from jpos.env (default: "default").

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static Map<String,String>
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    dump(PrintStream p, String indent)
     
    static void
    flat(Properties properties, String prefix, Map<String,Object> c, boolean dereference)
    Flattens a nested Map structure into a flat Properties object using dot notation.
    static String
    Resolves a property expression using the singleton Environment.
    static String
    get(String p, String def)
    Resolves a property expression using the singleton Environment.
    Returns the directory where environment configuration files are located.
    Returns the singleton Environment instance.
    Returns any error message from the last configuration load attempt.
    Returns the name of the current environment.
    If property name has the pattern ${propname}, this method will Attempt to get it from an operating system environment variable called 'propname' If not present, it will try to pick it from the Java system.property If not present either, it will try the target environment (either .yml or .cfg Otherwise it returns null The special pattern $env{propname} would just try to pick it from the OS environment.
    Resolves a property expression with a default fallback.
    Reloads the environment configuration from disk.
    static String
    Resolves the given expression using the current Environment instance, applying the standard priority resolution rules.
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface Loggeable

    dump
  • Field Details

  • Method Details

    • getName

      public String getName()
      Returns the name of the current environment. Determined by the jpos.env system property, defaults to "default".
      Returns:
      the environment name
    • getEnvDir

      public String getEnvDir()
      Returns the directory where environment configuration files are located. Determined by the jpos.envdir system property, defaults to "cfg".
      Returns:
      the environment directory path
    • reload

      public static Environment reload() throws IOException
      Reloads the environment configuration from disk. Reads the jpos.env and jpos.envdir system properties and reloads the corresponding configuration files.
      Returns:
      the newly loaded Environment instance
      Throws:
      IOException - if an error occurs reading configuration files
    • getEnvironment

      public static Environment getEnvironment()
      Returns the singleton Environment instance.
      Returns:
      the current Environment
    • get

      public static String get(String p)
      Resolves a property expression using the singleton Environment. If the property cannot be resolved, returns the original expression.
      Parameters:
      p - the property expression to resolve (e.g., "${my.property}")
      Returns:
      the resolved value, or the original expression if unresolved
      See Also:
    • resolve

      public static String resolve(String p)
      Resolves the given expression using the current Environment instance, applying the standard priority resolution rules.
      Parameters:
      p - the expression or literal value to resolve; may contain placeholders using the ${...} syntax
      Returns:
      the resolved value after applying environment priority rules.
      See Also:
    • get

      public static String get(String p, String def)
      Resolves a property expression using the singleton Environment. If the property cannot be resolved, returns the specified default.
      Parameters:
      p - the property expression to resolve
      def - the default value to return if the property is unresolved
      Returns:
      the resolved value, or def if unresolved
      See Also:
    • getProperty

      public String getProperty(String p, String def)
      Resolves a property expression with a default fallback.
      Parameters:
      p - the property expression to resolve
      def - the default value to return if the property resolves to null
      Returns:
      the resolved value, or def if null
      See Also:
    • getErrorString

      Returns any error message from the last configuration load attempt. Typically set when YAML parsing fails.
      Returns:
      the error message, or null if no error occurred
    • getProperty

      If property name has the pattern ${propname}, this method will
      • Attempt to get it from an operating system environment variable called 'propname'
      • If not present, it will try to pick it from the Java system.property
      • If not present either, it will try the target environment (either .yml or .cfg
      • Otherwise it returns null
      The special pattern $env{propname} would just try to pick it from the OS environment. $sys{propname} will just try to get it from a System.property and $verb{propname} will return a verbatim copy of the value.
      Parameters:
      s - property name
      Returns:
      property value
    • flat

      public static void flat(Properties properties, String prefix, Map<String,Object> c, boolean dereference)
      Flattens a nested Map structure into a flat Properties object using dot notation. For example, a nested structure like {server: {port: 8080}} becomes the property server.port=8080.

      List values are comma-encoded using ISOUtil.commaEncode(String[]).

      Parameters:
      properties - the Properties object to populate
      prefix - the current key prefix (null for root level)
      c - the Map to flatten
      dereference - if true, resolve property expressions in string values
    • dump

      public void dump(PrintStream p, String indent)
      Specified by:
      dump in interface Loggeable
    • toString

      public String toString()
      Overrides:
      toString in class Object