I have a 4 different configurations to be used and these configuration values are stored in a property file. The properties for all the configurations are same, but the values are different for each.
Ex: The property file configurations I am using:
####Config1####
conf1.password=admin
conf1.username=admin
conf1.context=123
conf1.name=localhost
####config2####
conf2.username=app
conf2.password=app
conf2.context=com
conf2.name=localhost
####config3####
conf3.username=app
conf3.password=app
conf3.context=com
conf3.name=localhost
####config4####
conf4.username=app
conf4.password=app
conf4.context=com
conf4.name=localhost
I can get the properties from the property file. Is it possible to have a single variable to store these values based on configuration and access them in an optimised and readable way?
I tried using hash-map for every configuration separately and fetching it from that. But it is increasing my code redundancy as if I perform same steps for every configuration and if-elsed the configuration name to get the hashmap values.
Currently I am using the properties with hashmap like this:
HashMap<String, String> conf1 = new HashMap<>();
HashMap<String, String> conf2 = new HashMap<>();
HashMap<String, String> conf3 = new HashMap<>();
HashMap<String, String> conf4 = new HashMap<>();
conf1.put("UserName", prop.getProperty(“conf1.username"));
conf1.put("Password",prop.getProperty("conf1.password"));
conf1.put(“name”,prop.getProperty("conf1.name"));
conf1.put("context”,”conf1,context”);
conf2.put("UserName", prop.getProperty(“conf2.username"));
conf2.put("Password",prop.getProperty("conf2.password"));
conf2.put(“name”,prop.getProperty("conf2.name"));
conf2.put("context”,”conf2,context”);
conf3...
conf4...
if (Conf.equalsIgnoreCase(“conf1”)) {
GenerateTestFile(
"Name:“ conf1.get("Name") “-UserName:”
conf1.get("UserName") “-Password:” conf1.get("Password")
"-Context:” conf1.get(“Context”) ,FileName);
} else if (Conf.equalsIgnoreCase(“conf2”)) {
GenerateTestFile(
"Name:“ conf2.get("Name") “-UserName:”
conf2.get("UserName") “-Password:” conf2.get("Password")
"-Context:” conf2.get(“Context”) ,FileName);
}
Else if(conf3){…}
Else if(conf4){…}
CodePudding user response:
You could use a nested HashMap like this:
HashMap<String, HashMap<String, String>> conf = new HashMap<>();
for(int i = 1; i <= 4; i ) {
String currentConfName = "conf" i;
HashMap<String, String> currentConf = new HashMap<>();
currentConf.put("UserName", prop.getProperty(currentConfName ".username"));
//And everythin else you want to add
conf.put(currentConfName, currentConf);
}
Equally you can then generate your files by iterating over the HashMap
CodePudding user response:
You could use a Map<String,Map<String,String>>
where the key is the name of the configuration, and the value (Map<String,String>
) is the configuration parameters.
To load the file, you would do something like this:
private static Pattern RE = Pattern.compile(
"([A-Za-z0-9_-] )\\.([A-Za-z0-9_-] )");
private static Map<String,Map<String,String>> loadConfs(String name)
throws IOException {
Map<String,Map<String,String>> confs = new HashMap<>();
Properties props = new Properties();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try (InputStream in = cl.getResourceAsStream(name)) {
props.load(in);
}
for (String propName: props.stringPropertyNames()) {
Matcher matcher = RE.matcher(propName);
if (matcher.matches()) {
String confName = matcher.group(1);
String parmName = matcher.group(2);
Map<String,String> conf = confs.get(confName);
if (conf == null) {
conf = new HashMap<>();
confs.put(confName, conf);
}
conf.put(parmName, props.getProperty(propName));
}
}
return confs;
}
You could then do something like this:
Map<String,Map<String,String>> confs = loadConfs("conf.properties");
...
Map<String,String> conf = confs.get(confName);
if (conf != null) {
GenerateTestFile(
"Name:“ conf.get("Name")
“-UserName:” conf.get("UserName")
“-Password:” conf.get("Password")
"-Context:” conf.get(“Context”),
FileName);
}