aspose file tools*
The moose likes Java in General and the fly likes Best way to handle properties Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Best way to handle properties" Watch "Best way to handle properties" New topic
Author

Best way to handle properties

Pete Hitchman
Greenhorn

Joined: Oct 29, 2002
Posts: 4
Hi,
I have a large amount of Java code that contains a number of property files, that specify such things a JDBC connection strings. These property files are held in side of a JAR file. This is fine until things like server/database names start to change and we have to update the right property file in the jar file.
What is the best way to handle such things? I have thought about using the "-D" option with the Java command and then accessing the setting via the System's object properties. I would prefer if at least some of the settings could be changed without having to mess with updating a jar file.

Thanks

Pete
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Originally posted by Pete Hitchman:
Hi,
I have a large amount of Java code that contains a number of property files, that specify such things a JDBC connection strings. These property files are held in side of a JAR file. This is fine until things like server/database names start to change and we have to update the right property file in the jar file.
What is the best way to handle such things? I have thought about using the "-D" option with the Java command and then accessing the setting via the System's object properties. I would prefer if at least some of the settings could be changed without having to mess with updating a jar file.

Thanks

Pete


-Have PM'd as well-

Hi Pete,

There are many 'solutions' to this problem and I'm sure others will post theirs, one that I use often is to have the properties files stored under the WEB-INF directory structure (I assume you're deploying a web app here?) or other secure folder but _not_ in a JAR file. I usually then use a singleton pattern that reads in and caches the properties.

Then depending on user requirements I'll implement a refresh of the properties every x seconds, or check to see if a property has changed every time a request comes in or ... etc. In the apps that I've been asked to build/deploy, production support staff and/or clients typically want to be able to alter certain properties on the fly, scary I know!

Hope that helps!

Karianna


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Pete Hitchman
Greenhorn

Joined: Oct 29, 2002
Posts: 4
Hi,
In this case a web application is not involved. I have considered taking the property files out of the jar file. This might be the easiest solution.

Regards

Pete
Fletcher Estes
Ranch Hand

Joined: Jul 01, 2004
Posts: 108
Generally the approach I take is to have everything un-compiled, un-jarred and un-everythinged in one place, where all development takes place (including updates to properties files). Any time a change is made, I run an Ant script which will re-compile, re-pack etc. everything the way God intended, so I don't need to worry about finding the right Jar, and correctly updating a single file therein. This makes sense during the development phase of an app.

The purpose of packaging things in JARs is to make "complete" code or packages available for easy transmission and use for the end user - they are not designed to allow flexibility of updates. So, if you need to make frequent changes to a property (independent of the application), this property, as you have concluded, should definitely not be hidden in a JAR file.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Best way to handle properties