Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Commons Configuration2 how to read data from InputStream

How can I read the data from InputStream by using Apache Commons Configuration2?

FileBasedConfigurationBuilder<XMLConfiguration> builder = 
    new FileBasedConfigurationBuilder<XMLConfiguration>(XMLConfiguration.class)
        .configure(
            new Parameters()
            .xml()
            .setFileName("")
            .setExpressionEngine(new XPathExpressionEngine())
        );

XMLConfiguration config = builder.getConfiguration();
config.read(sourceJarFile.getInputStream(sourcePropertiesEntry))

Gives the above code, I will get the below exception if the setFileName is given empty string.

org.apache.commons.configuration2.ex.ConfigurationException: Could not locate: org.apache.commons.configuration2.io.FileLocator@61dc03ce[fileName=tmp.xml,basePath=<null>,sourceURL=,encoding=<null>,fileSystem=<null>,locationStrategy=<null>]
at org.apache.commons.configuration2.io.FileLocatorUtils.locateOrThrow(FileLocatorUtils.java:346)
at org.apache.commons.configuration2.io.FileHandler.load(FileHandler.java:972)
at org.apache.commons.configuration2.io.FileHandler.load(FileHandler.java:702)
at org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder.initFileHandler(FileBasedConfigurationBuilder.java:312)
at org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder.initResultInstance(FileBasedConfigurationBuilder.java:291)
at org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder.initResultInstance(FileBasedConfigurationBuilder.java:60)
at org.apache.commons.configuration2.builder.BasicConfigurationBuilder.createResult(BasicConfigurationBuilder.java:421)
at org.apache.commons.configuration2.builder.BasicConfigurationBuilder.getConfiguration(BasicConfigurationBuilder.java:285)
at com.test.installer.App.getXMLConfigurationProperties(App.java:185)

If I give null or just not call setFileName(); I will get the unable to load configuration exception at the read() line.

org.apache.commons.configuration2.ex.ConfigurationException: Unable to load the configuration
    at org.apache.commons.configuration2.XMLConfiguration.load(XMLConfiguration.java:986)
    at org.apache.commons.configuration2.XMLConfiguration.read(XMLConfiguration.java:954)
    at com.test.installer.App.updateExistedProperties(App.java:84)
like image 238
Bruce Avatar asked Sep 19 '16 13:09

Bruce


2 Answers

From the example in the API documentation:

Set up your file parameters (encoding and such):

   FileBasedBuilderParameters fileparams = ...    
   FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
                    new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class).configure(fileparams);

and then:

FileBasedConfiguration config = builder.getConfiguration();
FileHandler fileHandler = new FileHandler(config);
Inputstream istream = ...
fileHandler.load(istream);

Note that you cannot use autosave with this. To save you'd probably need to provide an OutputStream. Something like:

fh.save(ostream)
like image 195
r33tnup Avatar answered Nov 07 '22 13:11

r33tnup


Proper way of loading XML configuration data from Input Stream (in commons-collections 2.x) is as follows:

XMLConfiguration cfg = new BasicConfigurationBuilder<>(XMLConfiguration.class).configure(new Parameters().xml()).getConfiguration();
FileHandler fh = new FileHandler(cfg);
fh.load(inputStream);

After calling load() cfg will contain loaded configuration.

Also note, that using XMLConfiguration.read() method should not be used, as this method is designed for internal use and probably will be renamed to _read() in future (see: https://issues.apache.org/jira/browse/CONFIGURATION-641).

like image 41
Rafi Avatar answered Nov 07 '22 14:11

Rafi