Wednesday 21 September 2016

Java Preferences API tutorial

Suppose you are developing a desktop application. At the time of installing the application, you generally ask user for the some of the options like, where to install the application, where to store log files, configuration files etc., User supply this information only once, application has to remember this information. Whenever application restarts, it shouldn’t ask this information every time.

Ideally most of the applications store the installation path in system environment variables. Configuration files, log files location are stored in property files. One disadvantage of this is, you need to read the installation path from system environment variables, and load the property files when the application reloads.

There is another better way to handle this
Java provides Preferences API to address this real time issue. Preferences API works like Properties API, but it stores the information in System persistence store like flat files, OS-specific registries, directory servers and SQL databases etc.,  Whenever your application restarts, you can get the data from this persistence store in easy way.

Let me demonstrate with an example.
import java.util.prefs.Preferences;

public class PreferenceTest {
 private Preferences prefs;
 private static final String VERSION = "version";
 private static final String NAME = "name";
 private static final String IS_STABLE_RELEASE = "isStableRelease";

 private static void displayPreferences(Preferences prefs) {
  /* Try to read preference before setting */
  int versionNum = prefs.getInt(VERSION, 1);
  String productName = prefs.get(NAME, "MySports");
  boolean isStableRelease = prefs.getBoolean(IS_STABLE_RELEASE, false);

  System.out.println("versionNum : " + versionNum);
  System.out.println("productName : " + productName);
  System.out.println("isStableRelease : " + isStableRelease);
  System.out.println("*****************************");
 }

 public void setPreference() {
  // This will define a node in which the preferences can be stored
  prefs = Preferences.userRoot().node(this.getClass().getName());

  displayPreferences(prefs);

  /* Set the Preferences */
  prefs.putInt(VERSION, 123);
  prefs.put(NAME, "MyCityInfo");
  prefs.putBoolean(IS_STABLE_RELEASE, true);

  displayPreferences(prefs);
 }

 public static void main(String[] args) {
  PreferenceTest test = new PreferenceTest();
  test.setPreference();
 }
}

Following are the sample outputs.


On Run1 Output
versionNum : 1
productName : MySports
isStableRelease : false
*****************************
versionNum : 123
productName : MyCityInfo
isStableRelease : true
*****************************

On Run2 Output
versionNum : 123
productName : MyCityInfo
isStableRelease : true
*****************************
versionNum : 123
productName : MyCityInfo
isStableRelease : true
*****************************


On Run3 Output
versionNum : 123
productName : MyCityInfo
isStableRelease : true
*****************************
versionNum : 123
productName : MyCityInfo
isStableRelease : true
*****************************


int versionNum = prefs.getInt(VERSION, 1);
String productName = prefs.get(NAME, "MySports");
boolean isStableRelease = prefs.getBoolean(IS_STABLE_RELEASE, false);

Observe above statements, Preferences class provides gettter methods to get the values associated with given properties. If the property is not exist, the getter method return the second argument.

As I said, Preferences stores the information in persistent store, on first run of the application, it stores the values to persistent storage. For later runs of the application it retrieves the values from this persistence storage. You can see this from outputs of Run 2 and 3.

How to create Preferences object?
There are two ways to create Preferences object.

a. Create User Preference object
Following statement creates User Preference object.

Preferences prefs = Preferences.userRoot().node(this.getClass().getName());
        
b. Create System Preference object
Following statement creates System Preference object.

Preferences prefs = Preferences.systemRoot().node(this.getClass().getName());

Difference between User Preference and System Preference object
Once a preference is stored in a userNode it is not accessible to other users of the system, just like user variables. Whereas system preference are available to all users. font choice, color choice, or preferred window location of an user are stored in User Preferences, whereas Application installation path, application version, any other global information can be stored in system preferences.

How to remove the preference?
Preferences class provides 'remove' method to remove a preference.

Ex:
prefs.remove(VERSION);

How to clear all the preferences?
Preferences class provides 'clear' method to remove all the preferences in this preference node.

Ex:
try {
         prefs.clear();
} catch (BackingStoreException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
}

What is the persistent storage location in Windows?
It is at ‘HKEY_CURRENT_USER\Software\JavaSoft\Prefs’.

Go to Start > Run > type "regedit" > Enter > then browse the key ‘HKEY_CURRENT_USER’, you can able to see the preferences associated with this user.

Reference

No comments:

Post a Comment