File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Groovy and the fly likes Gstring interpolation from properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Gstring interpolation from properties file" Watch "Gstring interpolation from properties file" New topic
Author

Gstring interpolation from properties file

Jon Walser
Greenhorn

Joined: Jul 23, 2012
Posts: 6
I've been handed a piece of groovy code and tasked with making it work. Of course it "worked fine" in its original environment...or so I'm told.

Properties file:

db.env=prod
db.db=${db.env}.cdc1
db.bulkdb=${db.env}.cdc1
...
jdbc.url=${${db.db}.jdbc.url}
jdbc.ro.url=${${db.db}.jdbc.ro.url}
.........................................

Code:
properties = new Properties();
FileInputStream propFile = new FileInputStream(
"conf/groovydb.properties");
properties.load(propFile);
propFile.close();
...

String dbEnv = properties.getProperty("db.db", "");
String url = properties.getProperty(dbEnv + ".jdbc.url", "");
...

System.out.println("env:" + dbEnv);
System.out.println("url:" + url);

output:
env:${db.env}.cdc1
url:

And an error indicating that my url value didn't work.
-------------
I've read about the use of Gstring interpolation, but I don't see how to apply it in a properties file. I can, of course, write some method to walk through the properties, but I'm hoping for a more elegant solution.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Can you change the properties file or does the code have to work with that specifically?


GenRocket - Experts at Building Test Data
Jon Walser
Greenhorn

Joined: Jul 23, 2012
Posts: 6
The properties file is hundreds of lines long and will necessarily grow when the interpolation references are expanded. This will require significant manual effort, with the associated risk of fat-fingering and will increase the future maintenance issues.

I am already creating a simplyfied properties file (one database, one environment etc) so I can test the rest of the program. I was just hoping for a more elegant way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Gstring interpolation from properties file