pyetti
pyetti

Reputation: 197

Apache Commons Configuration - PropertiesConfiguration closed

PropertiesConfiguration.java doesn't have a close() method. Is there something else that needs to be done to release the file? I want to be sure before I use this in production. I've looked through the code and I don't see anything. I'm not entirely sure how PropertiesConfiguration.setProperty() is working without opening a connection to the file that would then have to be closed.

Upvotes: 0

Views: 3152

Answers (1)

davidxxx
davidxxx

Reputation: 131486

In org.apache.commons.configuration.PropertiesConfiguration the input (stream, path, url, etc...) is of course closed when the properties were loaded in the PropertiesConfiguration instance.

You could have the confirmation in the void load(URL url) method of org.apache.commons.configuration.AbstractFileConfiguration.

Here is how this method is called :

1) PropertiesConfiguration constructor is invoked :

public PropertiesConfiguration(File file) throws ConfigurationException

2) which calls its super constructor :

public AbstractFileConfiguration(File file) throws ConfigurationException
{
    this();

    // set the file and update the url, the base path and the file name
    setFile(file);

    // load the file
    if (file.exists())
    {
        load(); // method which interest you
    }
}

3) which calls load() :

public void load() throws ConfigurationException
{
    if (sourceURL != null)
    {
        load(sourceURL);
    }
    else
    {
        load(getFileName());
    }
}

4) which calls load(String fileName):

public void load(String fileName) throws ConfigurationException
{
    try
    {
        URL url = ConfigurationUtils.locate(this.fileSystem, basePath, fileName);

        if (url == null)
        {
            throw new ConfigurationException("Cannot locate configuration source " + fileName);
        }
        load(url);
    }
    catch (ConfigurationException e)
    {
        throw e;
    }
    catch (Exception e)
    {
        throw new ConfigurationException("Unable to load the configuration file " + fileName, e);
    }
}

5) which calls load(URL url)

public void load(URL url) throws ConfigurationException
{
    if (sourceURL == null)
    {
        if (StringUtils.isEmpty(getBasePath()))
        {
            // ensure that we have a valid base path
            setBasePath(url.toString());
        }
        sourceURL = url;
    }

    InputStream in = null;

    try
    {
        in = fileSystem.getInputStream(url);
        load(in);
    }
    catch (ConfigurationException e)
    {
        throw e;
    }
    catch (Exception e)
    {
        throw new ConfigurationException("Unable to load the configuration from the URL " + url, e);
    }
    finally
    {
        // close the input stream
        try
        {
            if (in != null)
            {
                in.close();
            }
        }
        catch (IOException e)
        {
            getLogger().warn("Could not close input stream", e);
        }
    }
}

And in the finally statement, you can see that the inputstream is closed in any case.

Upvotes: 2

Related Questions