With JDK 1.4 Java has expanded on its Properties class and now provides a Preferences API. This is the product of JSR-10 (Java Specification Request) Preferences API Specification: A simple API allowing programs to manipulate user preference data and configuration data, and is a substantial improvement on the Preferences functionality.

Part of the reason I'm looking into this is that given it provides a hierarchical preferences model, it might suit the needs of WikiFarms (see MultipleWikis, MultiWikiDevelopment, IdeasMultipleWikiWebs), since it would be possible to share both system configuration and user-level information across multiple wiki. -- MurrayAltheim

Overview #

[this overview c/o Sun's J2SE documentation]

Applications require preference and configuration data to adapt to the needs of different users and environments. The java.util.prefs package provides a way for applications to store and retrieve user and system preference and configuration data. The data is stored persistently in an implementation-dependent backing store. There are two separate trees of preference nodes, one for user preferences and one for system preferences.

All of the methods that modify preference data are permitted to operate asynchronously. They may return immediately, and changes will eventually propagate to the persistent backing store. The flush method can be used to force updates to the backing store.

The methods in the Preferences class may be invoked concurrently by multiple threads in a single JVM without the need for external synchronization, and the results will be equivalent to some serial execution. If this class is used concurrently by multiple JVMs that store their preference data in the same backing store, the data store will not be corrupted, but no other guarantees are made concerning the consistency of the preference data.

For more details, choose from the following links:

[end of Sun's Overview]

Notes on "Implementation"#

The J2SE includes documentation and information about the persistent storage implementation, but that's all directed at implementors at the J2SE level. Every J2SE implementation must have an implementation (and does, apparently). This seems to confuse some people: there's no reason why any developer using the Preferences API would need to either replace those implementations unless one were developing software for an OS that didn't already have a J2SE available; the whole point of the Preferences API was to simplify this stuff (for developers) not make it more complicated. Only if one were developing a J2SE implementation would you need to pay attention to this. There's a note in bold in the javadocs for java.util.prefs.PreferencesFactory:

This class is for Preferences implementers only. Normal users of the Preferences facility should have no need to consult this documentation.

The Preferences API permits XML import and export of preference data. The DTD is posted as part of the javadocs for the java.util.prefs.Preferences class. There are methods for both importing and exporting the XML using

importPreferences(InputStream is)
Imports all of the preferences represented by the XML document on the specified input stream.
exportNode(OutputStream os)
Emits on the specified output stream an XML document representing all of the preferences contained in this node (but not its descendants).
exportSubtree(OutputStream os)
Emits an XML document representing all of the preferences contained in this node and all of its descendants.

but normally developers wouldn't even have to deal with these unless one wanted to control where the J2SE implementation was storing the prefs, e.g., to permit the XML to be hand-edited.

This class contains an export/import facility, allowing preferences to be "exported" to an XML document, and XML documents representing preferences to be "imported" back into the system. This facility may be used to back up all or part of a preference tree, and subsequently restore from the backup.


Articles and Projects#



Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-3) was last changed on 17-Dec-2006 17:04 by MurrayAltheim