public Properties getConfigProperties() throws ConfigException { String pathToProperties = getPathToProperties(); if (StringUtils.isEmpty(pathToProperties)) { return null; } try (InputStream input = new FileInputStream(pathToProperties)) { // Load properties Properties properties = new Properties(); properties.load(input); // Build a new properties object, replacing any environment variables that were in the properties file. Properties replacedProperties = new Properties(); Map<String, String> environmentVariables = System.getenv(); for (String propertyName : properties.stringPropertyNames()) { String propertyValue = properties.getProperty(propertyName); String newPropertyValue = replaceTokens(propertyValue, environmentVariables, ENV_VAR_REPLACEMENT_REGEX, true); replacedProperties.setProperty(propertyName, newPropertyValue); } return replacedProperties; } catch (IOException e) { throw new ConfigException(String.format("Unable to read properties file %s", pathToProperties), e); } }
@Override public String replaceProperties(File configFile) throws ConfigException { try { LOGGER.info("Processing configFile {}", configFile); Map<String, String> replacements = buildReplacements(); String configFileContent = new String(Files.readAllBytes(Paths.get(configFile.getPath())), StandardCharsets.UTF_8); // Do configuration file replacements String replacedContent = configFileContent; if (replacements != null) { replacedContent = replaceTokens(configFileContent, replacements, PROPERTIES_FILE_REPLACEMENT_REGEX, false); } else { LOGGER.warn("No config properties found, using file as is {}", configFile.getAbsolutePath()); } // Do environment variable replacements // Replace undefined environment variables with empty string // (Windows doesn't allow you to set empty environment variables - it just deletes them) return replaceTokens(replacedContent, System.getenv(), ENV_VAR_REPLACEMENT_REGEX, true); } catch (IOException e) { throw new ConfigException(String.format("Unable to read config file %s", configFile), e); } }