/** * Replace periods with an underscore, convert to uppercase, and prefix with <code>env</code> * * <pre> * foo.bar -> env.FOO_BAR * </pre> */ public static final String getEnvironmentVariableKey(String key) { return ENV_PREFIX + "." + convertToEnvironmentVariable(key); }
/** * Return a new properties object where the keys have been converted to upper case and periods have been replaced with an underscore. */ public static final Properties reformatKeysAsEnvVars(Properties properties) { Properties newProperties = new Properties(); for (String key : properties.stringPropertyNames()) { String value = properties.getProperty(key); String newKey = convertToEnvironmentVariable(key); newProperties.setProperty(newKey, value); } return newProperties; }
/** * Examine both system properties and environment variables to get a value for <code>key</code>. Return <code>defaultValue</code> if nothing is found * * <pre> * foo.bar -> System property check for "foo.bar" * foo.bar -> Environment check for "FOO_BAR" * </pre> */ public static final String getGlobalProperty(String key, String defaultValue) { Assert.noNullsWithMsg("key is required", key); // Check to see if there is a system property for this key String systemValue = System.getProperty(key); // If so, we are done if (systemValue != null) { return systemValue; } // Reformat the key as an environment variable key String environmentVariable = convertToEnvironmentVariable(key); // Check to see if we have a match for an environment variable String environmentValue = System.getenv(environmentVariable); if (environmentValue != null) { // If so, return the value of the environment variable return environmentValue; } else { // If not, return the default value return defaultValue; } }