|
@@ -20,7 +20,6 @@ package org.apache.ambari.server.state.kerberos;
|
|
|
|
|
|
import com.google.inject.Singleton;
|
|
|
import org.apache.ambari.server.AmbariException;
|
|
|
-
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.regex.Matcher;
|
|
@@ -49,6 +48,7 @@ public class VariableReplacementHelper {
|
|
|
{
|
|
|
put("each", new EachFunction());
|
|
|
put("toLower", new ToLowerFunction());
|
|
|
+ put("replace", new ReplaceValue());
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -226,6 +226,37 @@ public class VariableReplacementHelper {
|
|
|
return "";
|
|
|
}
|
|
|
}
|
|
|
+ /**
|
|
|
+ * ReplaceValue is a Function implementation that replaces the value in the string
|
|
|
+ * <p/>
|
|
|
+ * This function expects the following arguments (in order) within the args array:
|
|
|
+ * <ol>
|
|
|
+ * <li>regular expression that should be replaced</li>
|
|
|
+ * <li>replacement value for the string</li>
|
|
|
+ * </ol>
|
|
|
+ */
|
|
|
+ private static class ReplaceValue implements Function {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String perform(String[] args, String data) {
|
|
|
+ if ((args == null) || (args.length != 2)) {
|
|
|
+ throw new IllegalArgumentException("Invalid number of arguments encountered");
|
|
|
+ }
|
|
|
+ if (data != null) {
|
|
|
+ StringBuffer builder = new StringBuffer();
|
|
|
+ String regex = args[0];
|
|
|
+ String replacement = args[1];
|
|
|
+ Pattern pattern = Pattern.compile(regex);
|
|
|
+ Matcher matcher = pattern.matcher(data);
|
|
|
+ while(matcher.find()) {
|
|
|
+ matcher.appendReplacement(builder, replacement);
|
|
|
+ }
|
|
|
+ matcher.appendTail(builder);
|
|
|
+ return builder.toString();
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* ToLowerFunction is a Function implementation that converts a String to lowercase
|