@Test public void testReplaceVariablesWithFunctions() throws AmbariException { Map<String, Map<String, String>> configurations = new HashMap<String, Map<String, String>>() { { put("", new HashMap<String, String>() {{ put("delimited.data", "one,two,three,four"); put("realm", "UNIT.TEST"); }}); put("kafka-broker", new HashMap<String, String>() {{ put("listeners", "PLAINTEXT: }}); put("clusterHostInfo", new HashMap<String, String>() {{ put("hive_metastore_host", "host1.unit.test, host2.unit.test , host3.unit.test"); }}); put("foobar-site", new HashMap<String, String>() {{ put("data", "one, two, three, four"); put("hello", "hello"); put("hello_there", "hello, there"); put("hello_there_one", "hello, there, one"); }}); } }; Assert.assertEquals("test=thrift: helper.replaceVariables("test=${delimited.data|each(thrift: Assert.assertEquals("hive.metastore.local=false,hive.metastore.uris=thrift: helper.replaceVariables("hive.metastore.local=false,hive.metastore.uris=${clusterHostInfo/hive_metastore_host | each(thrift: List<String> expected; List<String> actual; expected = new LinkedList<String>(Arrays.asList("four", "hello", "one", "three", "two")); actual = new LinkedList<String>(Arrays.asList(helper.replaceVariables("${foobar-site/hello | append(foobar-site/data, \\,, true)}", configurations).split(","))); Collections.sort(expected); Collections.sort(actual); Assert.assertEquals(expected, actual); expected = new LinkedList<String>(Arrays.asList("four", "hello", "one", "there", "three", "two")); actual = new LinkedList<String>(Arrays.asList(helper.replaceVariables("${foobar-site/hello_there | append(foobar-site/data, \\,, true)}", configurations).split(","))); Collections.sort(expected); Collections.sort(actual); Assert.assertEquals(expected, actual); expected = new LinkedList<String>(Arrays.asList("four", "hello", "one", "there", "three", "two")); actual = new LinkedList<String>(Arrays.asList(helper.replaceVariables("${foobar-site/hello_there_one | append(foobar-site/data, \\,, true)}", configurations).split(","))); Collections.sort(expected); Collections.sort(actual); Assert.assertEquals(expected, actual); expected = new LinkedList<String>(Arrays.asList("four", "hello", "one", "one", "there", "three", "two")); actual = new LinkedList<String>(Arrays.asList(helper.replaceVariables("${foobar-site/hello_there_one | append(foobar-site/data, \\,, false)}", configurations).split(","))); Collections.sort(expected); Collections.sort(actual); Assert.assertEquals(expected, actual); try { helper.replaceVariables("${foobar-site/hello_there_one | append(foobar-site/data, \\,)}", configurations); Assert.fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { } Assert.assertEquals("test=unit.test", helper.replaceVariables("test=${realm|toLower()}", configurations)); Assert.assertEquals("PLAINTEXTSASL: }
public String replaceVariables(String value, Map<String, Map<String, String>> replacementsMap) throws AmbariException { if ((value != null) && (replacementsMap != null) && !replacementsMap.isEmpty()) { int count = 0; boolean replacementPerformed; do { if (++count > 1000) { throw new AmbariException(String.format("Circular reference found while replacing variables in %s", value)); } Matcher matcher = PATTERN_VARIABLE.matcher(value); StringBuffer sb = new StringBuffer(); replacementPerformed = false; while (matcher.find()) { String type = matcher.group(1); String name = matcher.group(2); String function = matcher.group(3); Map<String, String> replacements; if ((name != null) && !name.isEmpty()) { if (type == null) { replacements = replacementsMap.get(""); } else { replacements = replacementsMap.get(type); } if (replacements != null) { String replacement = replacements.get(name); if (replacement != null) { if (function != null) { replacement = applyReplacementFunction(function, replacement, replacementsMap); } matcher.appendReplacement(sb, replacement.replace("\\", "\\\\").replace("$", "\\$")); replacementPerformed = true; } } } } matcher.appendTail(sb); value = sb.toString(); } while (replacementPerformed); } return value; }
VariableReplacementHelper { public String replaceVariables(String value, Map<String, Map<String, String>> replacementsMap) throws AmbariException { if ((value != null) && (replacementsMap != null) && !replacementsMap.isEmpty()) { int count = 0; boolean replacementPerformed; do { if (++count > 1000) { throw new AmbariException(String.format("Circular reference found while replacing variables in %s", value)); } Matcher matcher = PATTERN_VARIABLE.matcher(value); StringBuffer sb = new StringBuffer(); replacementPerformed = false; while (matcher.find()) { String type = matcher.group(1); String name = matcher.group(2); String function = matcher.group(3); Map<String, String> replacements; if ((name != null) && !name.isEmpty()) { if (type == null) { replacements = replacementsMap.get(""); } else { replacements = replacementsMap.get(type); } if (replacements != null) { String replacement = replacements.get(name); if (replacement != null) { if (function != null) { replacement = applyReplacementFunction(function, replacement, replacementsMap); } matcher.appendReplacement(sb, replacement.replace("\\", "\\\\").replace("$", "\\$")); replacementPerformed = true; } } } } matcher.appendTail(sb); value = sb.toString(); } while (replacementPerformed); } return value; } }
VariableReplacementHelper { public String replaceVariables(String value, Map<String, Map<String, String>> replacementsMap) throws AmbariException { if ((value != null) && (replacementsMap != null) && !replacementsMap.isEmpty()) { int count = 0; boolean replacementPerformed; do { if (++count > 1000) { throw new AmbariException(String.format("Circular reference found while replacing variables in %s", value)); } Matcher matcher = PATTERN_VARIABLE.matcher(value); StringBuffer sb = new StringBuffer(); replacementPerformed = false; while (matcher.find()) { String type = matcher.group(1); String name = matcher.group(2); String function = matcher.group(3); Map<String, String> replacements; if ((name != null) && !name.isEmpty()) { if (type == null) { replacements = replacementsMap.get(""); } else { replacements = replacementsMap.get(type); } if (replacements != null) { String replacement = replacements.get(name); if (replacement != null) { if (function != null) { replacement = applyReplacementFunction(function, replacement, replacementsMap); } matcher.appendReplacement(sb, replacement.replace("\\", "\\\\").replace("$", "\\$")); replacementPerformed = true; } } } } matcher.appendTail(sb); value = sb.toString(); } while (replacementPerformed); } return value; }  }
VariableReplacementHelper { public String replaceVariables(String value, Map<String, Map<String, String>> replacementsMap) throws AmbariException { if ((value != null) && (replacementsMap != null) && !replacementsMap.isEmpty()) { int count = 0; boolean replacementPerformed; do { if (++count > 1000) { throw new AmbariException(String.format("Circular reference found while replacing variables in %s", value)); } Matcher matcher = PATTERN_VARIABLE.matcher(value); StringBuffer sb = new StringBuffer(); replacementPerformed = false; while (matcher.find()) { String type = matcher.group(1); String name = matcher.group(2); String function = matcher.group(3); Map<String, String> replacements; if ((name != null) && !name.isEmpty()) { if (type == null) { replacements = replacementsMap.get(""); } else { replacements = replacementsMap.get(type); } if (replacements != null) { String replacement = replacements.get(name); if (replacement != null) { if (function != null) { replacement = applyReplacementFunction(function, replacement, replacementsMap); } matcher.appendReplacement(sb, replacement.replace("\\", "\\\\").replace("$", "\\$")); replacementPerformed = true; } } } } matcher.appendTail(sb); value = sb.toString(); } while (replacementPerformed); } return value; }  String replaceVariables(String value, Map<String, Map<String, String>> replacementsMap); }
VariableReplacementHelper { public String replaceVariables(String value, Map<String, Map<String, String>> replacementsMap) throws AmbariException { if ((value != null) && (replacementsMap != null) && !replacementsMap.isEmpty()) { int count = 0; boolean replacementPerformed; do { if (++count > 1000) { throw new AmbariException(String.format("Circular reference found while replacing variables in %s", value)); } Matcher matcher = PATTERN_VARIABLE.matcher(value); StringBuffer sb = new StringBuffer(); replacementPerformed = false; while (matcher.find()) { String type = matcher.group(1); String name = matcher.group(2); String function = matcher.group(3); Map<String, String> replacements; if ((name != null) && !name.isEmpty()) { if (type == null) { replacements = replacementsMap.get(""); } else { replacements = replacementsMap.get(type); } if (replacements != null) { String replacement = replacements.get(name); if (replacement != null) { if (function != null) { replacement = applyReplacementFunction(function, replacement, replacementsMap); } matcher.appendReplacement(sb, replacement.replace("\\", "\\\\").replace("$", "\\$")); replacementPerformed = true; } } } } matcher.appendTail(sb); value = sb.toString(); } while (replacementPerformed); } return value; }  String replaceVariables(String value, Map<String, Map<String, String>> replacementsMap);  }