Search code examples
javaio

Java - how to choose an available path to load a file in a static block?


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?


Solution

  • You could use else if to do something like this:

    static {
        // choose 1st file's path
        if (CONFIG_FILE_1.exists() && CONFIG_FILE_1.isFile()) {
            loadConfiguration(CONFIG_FILE_1);
        }
        else 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);
        }
        else if (CONFIG_FILE_4.exists() && CONFIG_FILE_4.isFile()) {
             loadConfiguration(CONFIG_FILE_4);
        }
    }
    

    In the best case this would mean that only 2 of the paths have to be checked, however in the worst case all paths still need to be checked.

    To avoid code duplication it might also be a nice idea to create a separate method to fetch the correct files.

    public static File fetchConfigFile(file1, file2) {
        if (file1.exists() && file1.isFile()) {
            return file1;
        }
        else if (file2.exists() && file2.isFile()) {
             return file2;
        }
        // Be aware: returning null might not be what you want. 
        return null;
    }
    

    And than the static block can be simplified to this:

    static {
        loadConfiguration(fetchConfigFile(CONFIG_FILE_1, CONFIG_FILE_2));
        loadConfiguration(fetchConfigFile(CONFIG_FILE_3, CONFIG_FILE_4));
    }