olkoza
olkoza

Reputation: 725

Write to properties file on Wildfly which is loaded by ClassLoader

I am loading properties on an Wildfly application server like this:

public String getPropertyValue(String propertyName) throws IOException {
    InputStream inputStream;
    Properties properties = new Properties();

    inputStream = getClass().getClassLoader().getResourceAsStream(propertyFileName);

    if (inputStream != null) {
        properties.load(inputStream);
    } else {
        throw new FileNotFoundException("property file '" + propertyFileName + "' not found in the classpath");
    }

    inputStream.close();
    String property = properties.getProperty(propertyName);
    LOG.debug("Property {} with value {} loaded.", propertyName, property);
    return property;
}

Now I want to write to that very same file. How do I do that correctly? I tried around with new File(configurationFileName), but that creates a new File in a different directory, and I tried around with URL/URI of file from classloader, but that doesn't seem to work either. What is the correct way to do this? Thx for help!

Upvotes: 0

Views: 594

Answers (3)

olkoza
olkoza

Reputation: 725

Raoul Duke is actually right, doing the properties via file raises a lot of problems. I will soon switch to DB for keeping those. In the mean time I did this: When I write properties, they are written to a newly created file. When I read properties, I load the "old" ones, and then create a new properties object with the old ones as defaults, in which I then load the new file.

private Properties loadProperties() throws IOException {
    InputStream inputStream;
    Properties defaultProperties = new Properties();
    inputStream = getClass().getClassLoader().getResourceAsStream(defaultPropertyFileName);
    if (inputStream != null) {
        defaultProperties.load(inputStream);
    } else {
        throw new FileNotFoundException("Property file '" + defaultPropertyFileName + "' not found in the classpath");
    }
    inputStream.close();
    Properties allProps = new Properties(defaultProperties);
    try {
        allProps.load(new FileInputStream(new File(updatedPropertyFileName)));
    } catch (IOException ex) {
        LOG.error("Error loading properties: {}", ex.toString());
        return defaultProperties;
    }
    return allProps;
}

I marked his answer right, since I am technically not writing to the file I wanted to, plus this is only a workaround and his solution is way better and cleaner.

Upvotes: 0

ehsavoie
ehsavoie

Reputation: 3547

try (FileOutputStream out = new FileOutputStream(new File( getClass().getClassLoader().getResource(propertyName).toURI()))){
    properties.store(out,"My Comments);
}

Upvotes: 0

Raoul Duke
Raoul Duke

Reputation: 455

You can't and you shouldn't. I would use a database table to store and load properties. Or if it shall be a properties-file, then store it somewhere external via a file path, but not via the class path.

Upvotes: 1

Related Questions