Slashdot Mirror


Store Objects Using the JDK 1.4 Preferences API

An anonymous reader writes "The Preferences API -- a lightweight, cross-platform persistence API introduced in JDK 1.4 -- is designed to store small amounts of data (string, simple byte arrays, and so on.) and was not intended to be an interface to a traditional database. It can, however, be effective as a storage device if your data can be expressed as simple objects. This article offers an introduction to the API, explains how objects are stored, demonstrates the process in action, and provides a code library to do the work."

16 comments

  1. Half an hour and no first post? by Anonymous Coward · · Score: 0

    I mean, come on, people. Java is one of the top three languages used on this planet, after all.

    1. Re:Half an hour and no first post? by Anonymous Coward · · Score: 0

      > Java is one of the top three languages used on this planet, after all.

      I thought the top three languages were C#, Managed C++, and VB.Net?

    2. Re:Half an hour and no first post? by laa · · Score: 1

      The other two being English and awk?

      --
      Why does the kernel go through stable and then unstable forks? Can't it always be a stable build, like with Windows?
    3. Re:Half an hour and no first post? by BigGerman · · Score: 1

      It is not just language the article is pretty much pointless.
      People did exactly the same things with Properties since Jdk1.1

    4. Re:Half an hour and no first post? by Pieroxy · · Score: 1

      Giving that the first one is obviously Chinese, I doubt it...

    5. Re:Half an hour and no first post? by Anonymous Coward · · Score: 0

      You're all missing the point. This article isn't about a different way to use prefs, it's a different place to store objects. Sure, you can put objects in a file, but then you have to find the file. If you put it in the prefs tree, then you don't. (This argument is the same one you would use to defend the prefs api itself, or the Windows Registry. But that's a different fight.)

  2. why not... by Anonymous Coward · · Score: 0

    just use the serialization API?

    1. Re:why not... by dr_steel · · Score: 2, Informative

      That's effectively what he does. Uses the serialization api to allow storing arbitrary objects with the Preferences api...

  3. Decent idea... only limited use, though by jtheory · · Score: 4, Informative

    Quick summary: The Preferences API in Java 1.4 doesn't support storing an Object value, but we can hack around that by serializing the Object into a byte array, and storing limited length chunks as prefs.

    This is NOT a replacement for an object db, just a way to make storing your prefs more flexible.

    Personally, I don't think it's worth the trouble. Preferences are already hierarchichal (so there's no real gain in data organization). In most cases this will save only a few lines of code.

    And in exchange for those lines of code, we do lose some things: the complexity added by putting another layer on top of the prefs API, for one, and the fact that debugging would suck (instead of readable data in your preferences, you now have chunks of serialized object).

    So.. go for it if it would make your life much easier to store prefs as objects. Everyone else... read the article ANYWAY -- because it shows nicely how simple it is to use the Preferences API -- but you probably needn't bother to download the source.

    Caveat -- I don't maintain any large Java apps with lots of preferences... but it seems to me that most prefs won't come automatically as objects.

    --
    There are only 10 types of people: those who understand decimal, those who don't, and, uh, 8 other types I forget.
  4. XMLEncoder??? by Anonymous Coward · · Score: 0

    Can someone explain why anyone would choose this method of serialization over XMLEncoder?

    This seems like just another case of look-what-i-can-do-without-any-real-reason-for-doi ng-so syndrome (only slightly more useful than TCP over carrier pigeon).

  5. Is this guy serious? by animaal · · Score: 5, Insightful

    I had to look twice at the article, I couldn't believe it was written by IBM. I really don't like this guy's approach. I'd much rather store the values that were used to create the objects, rather than the objects themselves. Here's my reasoning:

    1. It's quite likely that the objects will be much larger than the intiialisation parameters. For example, for a Font object on given JDK, the initialisation parameters are a string (name) and integer (size). The Font object created could potentially have all sorts of internal data structures.

    2. Storing the parameters rather than the object will allow the user/admin to manually alter the preferences later. Think of the Windows registry. It's much easier to change an ASCII value than a binary object. It also becomes easier to examine/inspect the values. Never mind having a binary object spread over multiple binary preference values.

    3. What about debugging the code for the application that created the preferences? Related to (2) above, I'd hate to be examining a dump of the binary objects, spread over multiple property values.

    4. Dumping binary objects leads to a JVM-specific repository. Again, using the Font example, storing the name and size would produce two values that could be used on any other JVM to produce a valid font (at least I think Java still uses default font values if it can't find the requested one?). Whereas, dumping an actual Font object results in the storing of the specific implementation of the object used. This means that if the user upgrades their JVM to a more recent version (e.g. JDK1.3 to JDK1.4), all their previously stored preferences may be unreadable.

    5. Realated to (4) above, it also makes reproducing bugs difficult. I.e. a user says "I hve this crash...". You can't ask for an export of the user's preferences, because they're specific to the JVM that user used. It's also impossible to tell, from a glance, exactly what those preferences even are.

    I usually find IBM's articles extremely interesting, but I'm afraid I'm very disappointed with this one.

    1. Re:Is this guy serious? by psychofox · · Score: 1
      4. Dumping binary objects leads to a JVM-specific repository. Again, using the Font example, storing the name and size would produce two values that could be used on any other JVM to produce a valid font (at least I think Java still uses default font values if it can't find the requested one?). Whereas, dumping an actual Font object results in the storing of the specific implementation of the object used. This means that if the user upgrades their JVM to a more recent version (e.g. JDK1.3 to JDK1.4), all their previously stored preferences may be unreadable.

      If there were the case, this would be a significant argument against using Java serilization in general... Fortunately, Sun take great pains to make serializable objects portable between JVM implementations.

      Read up on the serialVersionUID

  6. Bad copy of windows registry by Anonymous Coward · · Score: 0

    The preferences API IMHO is one of the worst designed in the Java standard library. It is a poor copy of the windows registry interface designed to integrate well with the registry. It does not have a decent back end for unix, has no notification for concurrent modification of preferences, is limited in the type and size of data which can be stored (bad hacks like the one in the article aside) and has limited support for systemwide and personal data storage. Also there is no transactional capability.
    I'd strongly recommend not using it.

  7. Poorly designed -- no decoupling of persistence by rdeadman · · Score: 4, Insightful
    The Preferences API has one fatal flaw, it binds persistence to a JVM implementation. That is, if you store preferences using the Sun SDK and then run your application later using the IBM JVM, your Preferences are not available. This coupling of persistence to the JVM should never have taken place and breaks the tacit assumption that applications are decoupled from the JVM they run on. For enterprise applications, this could be devastating during a JVM upgrade.

    For the record, I did object during the JCP process, and even offered a modification to the architecture, but was dismissed.

    I wrote a more extensive criticism of this API in the Java Developer's Journal back in March of 2002. An on-line version (with some of the preamble truncated) is available here.

  8. GConf anyone? by ebassi · · Score: 1

    Preferences tree? XML backend?

    Why this API reminds me of Havoc Pennington's GConf used by GNOME 2?

    --
    You can save space. Or you can save time. Don't ever count on saving both at once. -- First Law of Algorithmic Analisys
  9. not worth it by sna899 · · Score: 1
    I don't think it's worth the trouble. I also don't like this guy's approach.

    Overtime Laws