• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Alternatives to java.util.Properties?

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Philip Shanks
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Properties? How 20th century!
http://www.javaranch.com/newsletter/Oct2002/newsletteroct2002.jsp#preferences
 
Philip Shanks
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 18671
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 .
     
    Ranch Hand
    Posts: 2379
    MySQL Database Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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?
     
    Ranch Hand
    Posts: 2545
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi:
    please refer to:
    Class Properties;
    Class ContextProperties.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic