I have a static block in my class, so that when a classloader loads the class definition - a method is called from this static block. The purpose of the method is to load configuration from one of the available paths. I want the first available path to be used.
There are 2 configuration files (metrics_config.json
, metrics_config_new.json
) and each of them could be in one of the 2 paths:
private static final File CONFIG_FILE_1 = new File("file:///dest1/config/metrics_config.json");
private static final File CONFIG_FILE_2 = new File("file:///dest2/config/metrics_config.json");
private static final File CONFIG_FILE_3 = new File("file:///dest1/config/metrics_config_new.json");
private static final File CONFIG_FILE_4 = new File("file:///dest2/config/metrics_config_new.json");
How do I pass only one path that is currently available to the method? I suppose this may look similar to this:
static {
// choose 1st file's path
if (CONFIG_FILE_1.exists() && CONFIG_FILE_1.isFile()) {
loadConfiguration(CONFIG_FILE_1);
}
if (CONFIG_FILE_2.exists() && CONFIG_FILE_2.isFile()) {
loadConfiguration(CONFIG_FILE_2);
}
// choose 2nd file's path
if (CONFIG_FILE_3.exists() && CONFIG_FILE_3.isFile()) {
loadConfiguration(CONFIG_FILE_3);
}
if (CONFIG_FILE_4.exists() && CONFIG_FILE_4.isFile()) {
loadConfiguration(CONFIG_FILE_4);
}
}
The downside of my current solution is that it will check both paths for the first file and for the second file.
What would be the correct way to determine which path to pass to the loadConfiguration
method?