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; }
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); } }
@Get public Representation getConfig() { try { String result = "{}"; String name = ( String ) getRequest().getAttributes().get( "name" ); File matchingFile = _service.findResourceConfig(name); if (matchingFile != null) { result = _service.replaceProperties(matchingFile); } setStatus(Status.SUCCESS_OK); return new JsonRepresentation( result ); } catch ( Exception e ) { throw new ResourceException( Status.SERVER_ERROR_INTERNAL, "Unable to get configuration", e ); } } }
@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" )); }
@Test(expected = ConfigException.class) public void testReplaceProperties_invalidPathToPropertiesInEnvVar() throws Exception { Map<String,String> newEnv = new HashMap<>(); newEnv.put(CONFIG_ENV_VAR, "invalid/path/to.properties"); setEnv(newEnv); File configFile = new File(this.getClass().getClassLoader().getResource("config-service-unit-test.json").toURI().getPath()); _configService.replaceProperties(configFile); }
@Before public void setUp() throws Exception { ConfigPropertiesService configPropsService = new ConfigPropertiesServiceImpl(); _configService = new ConfigServiceImpl( configPropsService ); }
@Test public void testReplaceTokens() { String text = "The ${some.animal} jumped over the ${some.object}"; Map<String, String> replacements = new HashMap<>(); replacements.put("some.animal", "cow"); replacements.put("some.object", "moon"); String replaced = _configService.replaceTokens(text, replacements, ConfigServiceImpl.PROPERTIES_FILE_REPLACEMENT_REGEX, false); assertEquals("The cow jumped over the moon", replaced); }
@Test public void testFindResourceConfig_found() throws Exception { File resourceConfig = _configService.findResourceConfig("test1.json"); assertNotNull(resourceConfig); }
@Test(expected = ConfigException.class) public void testReplaceProperties_invalidConfigFile() throws Exception { File foo = new File("foo"); _configService.replaceProperties(foo); }
protected Map<String, String> buildReplacements() throws ConfigException { Map<String, String> replacements = new HashMap<>(); Properties properties = _service.getConfigProperties(); if ( properties != null ) { Enumeration e = properties.propertyNames(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); replacements.put(key, properties.getProperty(key)); } } return replacements; }
@Before public void setUp() throws Exception { _service = new ConfigPropertiesServiceImpl(); }
@After public void tearDown() throws Exception { HashMap<String, String> newenv = new HashMap<>(); newenv.put(CONFIG_ENV_VAR, ""); setEnv(newenv); }
@After public void tearDown() throws Exception { HashMap<String, String> newenv = new HashMap<>(); newenv.put(CONFIG_ENV_VAR, ""); setEnv(newenv); }
@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") ); }
@Test public void testReplaceProperties() throws Exception { String pathToProperties = this.getClass().getClassLoader().getResource("config-service-unit-test.properties").getPath(); Map<String,String> newEnv = new HashMap<>(); newEnv.put(CONFIG_ENV_VAR, pathToProperties); setEnv(newEnv); File configFile = new File(this.getClass().getClassLoader().getResource("config-service-unit-test.json").toURI().getPath()); String replaced = _configService.replaceProperties(configFile); assertTrue(replaced.contains("\"foo.zookeeper.quorum\": \"bar.test.local\"")); assertTrue(replaced.contains("\"foo.zookeeper.port\": \"2222\"")); assertTrue(replaced.contains("\"foo.master\": \"bar.test.local:33333\"")); assertTrue(replaced.contains("\"endpoint\": \"http://some.host/some.data/\"")); }
@Test public void testFindResourceConfig_notFound() throws Exception { File resourceConfig = _configService.findResourceConfig("test2.txt"); assertNull(resourceConfig); }
@Test public void testReplaceProperties_envVarIsNull_usesDefaultReplacements() throws Exception { File configFile = new File(this.getClass().getClassLoader().getResource("config-service-unit-test.json").toURI().getPath()); String replaced = _configService.replaceProperties(configFile); // The default properties file only has one replacement that matches a key in the test json file assertTrue(replaced.contains("\"endpoint\": \"http://some.endpoint/somedata\"")); // The other keys remain templated assertTrue(replaced.contains("\"foo.zookeeper.quorum\": \"${foo.zookeeper.quorum}\"")); assertTrue(replaced.contains("\"foo.zookeeper.port\": \"${foo.zookeeper.port}\"")); assertTrue(replaced.contains("\"foo.master\": \"${foo.master}\"")); }
Properties properties = _service.getConfigProperties(); if ( properties != null ) { String translationApiKey = properties.getProperty(TRANSLATE_API_KEY);
@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/") ); }