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); } }
protected String getPathToProperties() throws ConfigException { String pathToProperties; String configEnvVar = System.getenv(CONFIG_ENV_VAR); if (StringUtils.isNotEmpty(configEnvVar)) { pathToProperties = configEnvVar; LOGGER.warn("{} environment variable is set, using properties file {} ", CONFIG_ENV_VAR, pathToProperties); } else { pathToProperties = getPathToPropertiesFromClasspath(); LOGGER.warn("{} environment variable NOT set, using default file from classpath {}", CONFIG_ENV_VAR, pathToProperties); } return pathToProperties; }
@Test public void testGetProperties() throws Exception { String pathToProperties = this.getClass().getClassLoader().getResource("config-service-unit-test.properties").toURI().getPath(); Map<String,String> newEnv = new HashMap<>(); newEnv.put(CONFIG_ENV_VAR, pathToProperties); setEnv(newEnv); Properties properties = _service.getConfigProperties(); assertTrue(StringUtils.equals( properties.getProperty("foo.zookeeper.quorum"), "bar.test.local") ); assertTrue(StringUtils.equals( properties.getProperty("foo.zookeeper.port"), "2222") ); assertTrue(StringUtils.equals( properties.getProperty("foo.master"), "bar.test.local:33333") ); assertTrue(StringUtils.equals( properties.getProperty("drilldown.endpoint"), "http://some.host/some.data/") ); }
@Before public void setUp() throws Exception { _service = new ConfigPropertiesServiceImpl(); }
@Test(expected = ConfigException.class) public void testGetProperties_invalidPathToPropertiesInEnvVar() throws Exception { Map<String,String> newEnv = new HashMap<>(); newEnv.put(CONFIG_ENV_VAR, "invalid/path/to.properties"); setEnv(newEnv); Properties properties = _service.getConfigProperties(); assertNull(properties.get( "foo.zookeeper.quorum" )); }
@Before public void setUp() throws Exception { ConfigPropertiesService configPropsService = new ConfigPropertiesServiceImpl(); _configService = new ConfigServiceImpl( configPropsService ); }
@Test public void testReplaceProperties_envVarIsNull_usesDefaultReplacements() throws Exception { HashMap<String, String> newenv = new HashMap<>(); newenv.put(CONFIG_ENV_VAR, ""); setEnv(newenv); Properties properties = _service.getConfigProperties(); assertTrue(StringUtils.equals( properties.getProperty("hbase.zookeeper.quorum"), "some.host") ); assertTrue(StringUtils.equals( properties.getProperty("hbase.zookeeper.port"), "12345") ); assertTrue(StringUtils.equals( properties.getProperty("hbase.master"), "foo") ); assertTrue(StringUtils.equals( properties.getProperty("drilldown.endpoint"), "http://some.endpoint/somedata") ); }