1

I am trying to utilize PropertiesConfiguration to manipulate property files in coldfusion. Using org.apache.commons.configuration version 1.10.

propertyFile = "usergui.properties";

config = createObject("java","org.apache.commons.configuration.PropertiesConfiguration").init(propertyFile);

I am getting to matching function init that takes a string. I have tried doing java.io.file.

jonesk
  • 103
  • 8
  • The question "how to use" is kind of broad for S.O. Better to narrow it down to one specific question, like "I'm trying to do X. I've tried code Y and it's throwing error Z". – SOS Jan 25 '19 at 01:19
  • Can't you use ColdFusions property functions? – James A Mohler Jan 25 '19 at 03:16

1 Answers1

3

I figured it out problem was that I didn't include all the dependencies. DUH!

function updatePropFile(string propFile, struct propStruct, struct removeStruct){
    propertyFile = propFile;
    javaFile = createObject("java", "java.io.File").init(propertyFile);
    fileStream = createObject("java", "java.io.FileInputStream").init(javaFile);


    config = createObject("java","org.apache.commons.configuration.PropertiesConfiguration").init(javaFile);

    configLayout = config.getLayout();

    for(key in propStruct){
        if(config.containsKey(key)){
            config.setProperty(key, propStruct[key]);
        }else{
            config.addProperty(key, propStruct[key]);
        }
    }

    for(key in removeStruct){
        if(config.containsKey(key)){
            /* clear prop and add as comment */
            value = config.getProperty(key).toString();
            config.clearProperty(key);
            config.addProperty('##'&key, key & "=" & value);
        }
    }

    configLayout.save(createObject("java", "java.io.FileWriter").init(propFile, false));
}
jonesk
  • 103
  • 8
  • 1
    Nice followup! Two small suggestions 1) Don't forget to localize all function local variables 2) Though relative file paths (i.e. "usergui.properties") do work, usually absolute paths are preferable, so it's clear exactly where files are saved – SOS Jan 25 '19 at 10:20
  • 1
    .. 3) Also loading and saving is slightly if you use `init()` with no args, then `setPath()` (to set the source). Then you can just call `load()` and `save()` without the extra java.io file objects. https://pastebin.com/j1NK9mck Just watch out for synchronization if the function can be accessed by multiple threads. See https://commons.apache.org/proper/commons-configuration/javadocs/v1.10/apidocs/index.html – SOS Jan 25 '19 at 10:46