aspose file tools*
The moose likes Java in General and the fly likes Alternatives to java.util.Properties? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Alternatives to java.util.Properties?" Watch "Alternatives to java.util.Properties?" New topic
Author

Alternatives to java.util.Properties?

Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
I wrote a MVC application that creates web reports from a database. I designed it in a way that allows me to add new reports simply by adding lines to a pair of properties files; one file has the title for each report, and one has the SQL statement for each report (matching keys). I find that this works well, except for one thing.
Because the Properties class is implemented as a hash, I have no control over the order when extracting the keys. I don't simply want to sort them, but would rather access them by order of appearance in the files.
Any good implementation suggestions for this? I suspect that an XML implementation would be a good approach, except that it seems like a lot of work (not to mention overkill) just to load a few configuration lines. There are currently no more than seven or eight key/value pairs per file, and this might grow to a dozen or so. There is also a properties file for my database connection parameters, but this works fine with the standard Properties implementation.
Thanks!
PCS


Philip Shanks, SCJP - Castro Valley, CA
My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
There are probably a number of other implementations of this sort of thing floating around, but here's one I made which uses JDK 1.4's LinkedHashMap and XML parsing:

The main() method shows an example of use. A sample properties file looks like this:
Modify as you wish...


"I'm not back." - Bill Harding, Twister
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
Thanks for the hint to use a LinkedHashMap. I've decided to created my own OrderedProperties class based upon this data structure, but that will use the properties files I already have (plain text, "key=value" format) for backwards compatibility.
I am also going to incorporate Joshua Bloch's advice to "Favor composition over inheritance," which is Item 14 in "Effective Java."
I will be able to swap it right into my current project with minimal disruption.
PCS
[ November 07, 2002: Message edited by: Philip Shanks ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Properties? How 20th century!
http://www.javaranch.com/newsletter/Oct2002/newsletteroct2002.jsp#preferences


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
The Preferences approach looks interesting, but seems to be geared ever so slightly more toward client-side applications.
My interest in this thread was to overcome the Hashtable behavior of the Properties class with minimal re-work to my existing applications that use the current Properties class.
Primarily, I did not like the way that the ordering of the Enumeration from propertyNames() method is dictated by the hashing function.
While it would be an easy thing to sort the keys alphabetically, I wanted to get the keys in their order of appearance in the properties file.
To this end, I have re-implemented the Properties class, based on the java.util.LinkedHashMap data structure. It is a work in progress, though, so I haven't yet implemented all of the methods, rules and behavior found in the original java.util.Properties class. For instance, I haven't yet implemented any capability for reading values that span multiple lines using continuation characters.
Among the liberties I am taking:
  • OrderedProperties contains a data structure, instead of extending one.
  • Property values must be Strings. This is enforced throughout.
  • I used the java.util.regex classes to parse the key/value lines.
  • I might implement an Iterator, which is preferred over Enumerations.


  • Given the greater flexibility that XML-based configuration files can afford, I will probably cut development of this class short. It is more of a coding exersize for me anyway, but it does meet a current need as well.
    For the curious, here is the code. Of course, this is limited to Java 1.4 platforms, but that is not a problem for me. I have tested this code, and so far it works just as expected.

    PCS
    [ November 07, 2002: Message edited by: Philip Shanks ]
    Aaron Nielsen
    Greenhorn

    Joined: Nov 08, 2002
    Posts: 1
    Here is a very simple solution to getting you properties in the order they appear in the properties file, by giving them an order, instead of just implying there is order.
    You could easily do the following in your properties file
    key1=value a
    key2=value b
    key3=value c
    Then you could just iterate through the key names in order using a counter.
    int x=1;
    while(myProperties.containsKey("key" + x)
    {
    something = myProperties.getProperty("key" + x);

    x++;
    }
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    Properties? How 20th century!
    Thanks, Thomas. I knew I'd seen something about a more modern XML-based approach, but forgot where it was. Lots of cool new toys were in 1.4 - I didn't get around to playing with them all. My code was something I'd cooked up previously, and later modifed to use LinkedHashMap rather than HashMap.
    Aaron - I suppose that would work, if you give up the ability to give the keys meaningful names that tell users what a given property actually does .
    Ashik Uzzaman
    Ranch Hand

    Joined: Jul 05, 2001
    Posts: 2370

    Philip and Jim,
    That's a pretty good thought. However, does this OrderProperties stuff overcome the limitation of multiple values for a single property in multiple lines constraint? For example, I have a file named "config.ini" that has some values like this ---
    [xml_handlers]
    handler = handleByTagName
    handler = handleByAttribute
    handler = handleByElelement
    handler = handleByNode
    I find java properties are not the way to deal this scenario and so going for a custimized collection consisting of a Map and a List to store all keys in Map and values in List. But if this could be dealt in your way then I would be very glad.
    What do you think?


    Ashik Uzzaman
    Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
    John Lee
    Ranch Hand

    Joined: Aug 05, 2001
    Posts: 2545
    hi:
    please refer to:
    Class Properties;
    Class ContextProperties.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Alternatives to java.util.Properties?
     
    Similar Threads
    two forign keys
    Pattern Suggestion: Vendor providing challenging problem
    print all docs togather without interruption from other computers
    Fantastic!! My RMI surprised me!!! :confused:
    Properties ignoring case