Search code examples
javaapache-config

Configuration vs. PropertiesConfiguration


I am having some conceptual difficulties with the types Configuration and PropertiesConfiguration from Apache Commons Configuration.

PropertiesConfiguration config = createConfig();
BlazeGraphEmbedded graph = BlazeGraphEmbedded.open(repo, config);

createConfig() method:

public static PropertiesConfiguration createConfig()
    {
        PropertiesConfiguration config = null;
        Parameters params = new Parameters();
        BasicConfigurationBuilder<PropertiesConfiguration> builder =
        new BasicConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class)
                        .configure(params.basic()
                        .setListDelimiterHandler(new DefaultListDelimiterHandler(','))
                        .setThrowExceptionOnMissing(true));
        try {
            config = builder.getConfiguration();
            config.addProperty("VALUE_FACTORY", BVF_extendo4000.INSTANCE);
        } catch (ConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    System.out.println("Returning config.");
    return config;
    }

I am trying to create a Configuration object to pass to the open method in the class BlazeGraphEmbedded. However, my createConfig method returns a PropertiesConfiguration object, which the method BlazeGraphEmbedded.open(Repository, Configuration) will not accept. I was not able to cast my PropertiesConfiguration to Configuration, it produced a runtime error:

Exception in thread "main" java.lang.ClassCastException: 
org.apache.commons.configuration2.PropertiesConfiguration cannot be cast to 
org.apache.commons.configuration.Configuration

I understand that Configuration is an interface which is implemented by PropertiesConfiguration, but I'm fuzzy on the details of how I can use the two different types to make my program run.


Solution

  • The problem was in my import statements. I had:

    import org.apache.commons.configuration.Configuration;
    import org.apache.commons.configuration2.PropertiesConfiguration;
    

    So I was importing from two different sources, hence why the types were not compatible.