configuration_file_parser.segment.PlatformParser Maven / Gradle / Ivy
package configuration_file_parser.segment;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.configuration2.Configuration;
import configuration_file_parser.ParserConstants;
import configuration_file_parser.ParserUtils;
import constants.BRunnerKeywords;
public class PlatformParser {
public static Map> parse(Configuration config) {
Set executionNames = ParserUtils
.getTreeChildrenNames(BRunnerKeywords.OuterLevel.EXECUTION_PLATFORM_PARAMETERS.kw, config);
Map mapExecutionJvmArgs = new LinkedHashMap<>();
String jvmArgs;
for (String executionName : executionNames) {
if ((jvmArgs = ParserUtils.popPropertyOrNull(
BRunnerKeywords.OuterLevel.EXECUTION_PLATFORM_PARAMETERS.kw + ParserConstants.KEY_DELIMITER
+ executionName + ParserConstants.KEY_DELIMITER + BRunnerKeywords.InnerLevel.JVM_ARGS.kw,
config)) != null) {
mapExecutionJvmArgs.put(executionName, jvmArgs);
}
}
Map> executionParams = ParserUtils
.parsePrefix(BRunnerKeywords.OuterLevel.EXECUTION_PLATFORM_PARAMETERS.kw, config);
for (Map.Entry entry : mapExecutionJvmArgs.entrySet()) {
executionParams.get(entry.getKey()).put(BRunnerKeywords.InnerLevel.JVM_ARGS.kw, entry.getValue());
}
parseMemoryArgs(executionParams);
return executionParams;
}
/**
* Parses the JVM memory parameters. Checks for duplicate memory settings
*/
private static void parseMemoryArgs(Map> executionParams) {
for (Map.Entry> entry : executionParams.entrySet()) {
Map executionMap = entry.getValue();
String memoryArgs = "";
String memoryValue = "";
List memoryProperties = Arrays.asList(BRunnerKeywords.InnerLevel.MIN_MEMORY.kw,
BRunnerKeywords.InnerLevel.MAX_MEMORY.kw);
for (String memoryProperty : memoryProperties) {
if ((memoryValue = executionMap.get(BRunnerKeywords.OuterLevel.EXECUTION_PLATFORM_PARAMETERS.kw
+ ParserConstants.KEY_DELIMITER + memoryProperty)) != null) {
String memoryArg = formatMemoryArg(memoryValue, memoryProperty.substring(0, 3));
memoryArgs = (memoryArgs + " " + memoryArg).trim();
}
}
String preExistingJvmArgs = executionMap.get("jvmArgs");
if (!memoryArgs.isEmpty() && preExistingJvmArgs != null
&& preExistingJvmArgs.matches(".*-Xm[s|x][0-9]+[GgMmKk]?")) {
throw new IllegalArgumentException(
"Detected heap size arguments -Xmx and/or -Xms in execution." + entry.getKey()
+ ".jvmArgs even though heap size was already defined with execution." + entry.getKey()
+ "." + BRunnerKeywords.InnerLevel.MIN_MEMORY.kw + " and/or " + BRunnerKeywords.InnerLevel.MAX_MEMORY.kw + "."
+ "\n\t\t To ensure proper execution only one method must be used.");
}
if (preExistingJvmArgs == null)
preExistingJvmArgs = "";
executionMap.put("jvmArgs", (preExistingJvmArgs + " " + memoryArgs).trim());
executionMap.remove(BRunnerKeywords.InnerLevel.MIN_MEMORY.kw);
executionMap.remove(BRunnerKeywords.InnerLevel.MAX_MEMORY.kw);
}
}
/**
* From a defined memory argument, returns a String formatted for JVM heap
* memory parameters
*
* @param arg
* @param type
* @return a String formatted for JVM heap memory parameters
*/
private static String formatMemoryArg(String arg, String type) {
String regex = "^([0-9]+)([GgMmKk]?)([Bb]?)$";
Matcher matcher = Pattern.compile(regex).matcher(arg);
arg = "";
if (matcher.matches()) {
if (type.equals("min"))
arg = "-Xms";
else
arg = "-Xmx";
arg += matcher.group(1) + matcher.group(2);
}
return arg;
}
}