Class EnvVarResolver

java.lang.Object
ai.mindconnect.common.util.EnvVarResolver

public final class EnvVarResolver extends Object
Resolves ${VAR_NAME} and ${VAR_NAME:default} placeholders in string values by looking them up in environment variables.

Rules:

  • ${MY_VAR} — expands to the env var value; throws if absent.
  • ${MY_VAR:fallback} — expands to the env var value, or fallback if the var is not set.
  • A null input returns null.
  • A value with no placeholders is returned unchanged.
  • Multiple placeholders in a single value are all expanded.
Example JSON config value: "${OPENAI_API_KEY}" or "${LLM_BASE_URL:http://localhost:11434}".
  • Method Details

    • containsPlaceholder

      public static boolean containsPlaceholder(String value)
      True if value contains at least one ${VAR} / ${VAR:default} placeholder. Use this to decide whether a value should be treated as a deferred reference (resolved at use time) rather than a literal — e.g. so a stored secret placeholder is not encrypted.
    • resolve

      public static String resolve(String value)
      Expands all ${…} placeholders in value using environment variables. Returns null if value is null.
      Throws:
      IllegalStateException - if a placeholder has no default and the environment variable is not set
    • resolve

      public static String resolve(String value, Map<String,String> env)
      Same as resolve(String) but accepts an explicit env map — useful for testing without touching real environment variables.