@Test public void testReplaceComplicatedVariables() throws AmbariException { Map<String, Map<String, String>> configurations = new HashMap<String, Map<String, String>>() { { put("", new HashMap<String, String>() {{ put("host", "c6401.ambari.apache.org"); put("realm", "EXAMPLE.COM"); }}); } }; Assert.assertEquals("hive.metastore.local=false,hive.metastore.uris=thrift: helper.replaceVariables("hive.metastore.local=false,hive.metastore.uris=thrift: Assert.assertEquals("Hello my realm is {EXAMPLE.COM}", helper.replaceVariables("Hello my realm is {${realm}}", configurations)); Assert.assertEquals("$c6401.ambari.apache.org", helper.replaceVariables("$${host}", configurations)); }
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);  }