Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use java.util.Preferences under Windows without it using the Registry as the backend?

I want to use the java.util.Preferences API but I don't want my program to attempt to read or write to the Windows registry. How would I go about this?

like image 619
Epaga Avatar asked Oct 16 '08 11:10

Epaga


People also ask

Where java preferences are stored?

The user preferences are typically stored at user-home/. java/. userPrefs .

What is userPrefs?

The userprefs. xml file contains a series of ParameterName elements that contain a ParameterValue element. The names of the parameters you can set are contained in the ParameterName elements. Enter the value for these parameters in the associated ParameterValue elements.

What is preference in Java?

A node in a hierarchical collection of preference data. This class allows applications to store and retrieve user and system preference and configuration data. This data is stored persistently in an implementation-dependent backing store.


2 Answers

I trust you have read the read/write to Windows Registry using Java and you then want to have another back-end than the registry when using the java.util.Preferences API

You could extend the Preference API, like Bernhard or Croft did, as described in this article:

Because the Preferences API is back-end neutral, you need not care whether the data are stored in files, database tables, or a platform-specific storage such as the Windows Registry.

Examples of extensions through new Preferences can be seen here.

That is better, IMO, than to use another API.


For instance, searching for classes extending java.util.prefs.AbstractPreferences:

  • You can use a preference store backed by an XML file:

de.unika.ipd.grgen.util.MyPreferences

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.prefs.AbstractPreferences;
import java.util.prefs.BackingStoreException;

/**
 * Own implementation of the Java preferences API, that does not use
 * a "OS backing store" but relies on importing and exporting the
 * preferences via xml files.
 * Also, If a preference is got, but was not in the tree, it is entered.
 */
public class MyPreferences extends AbstractPreferences {

    private Map<String, String> prefs = new HashMap<String, String>();
    private Map<String, AbstractPreferences> children = new HashMap<String, AbstractPreferences>();

  public MyPreferences(MyPreferences parent, String name) {
    super(parent, name);
  }

  /**
   * @see java.util.prefs.AbstractPreferences#putSpi(java.lang.String, java.lang.String)
   */
  protected void putSpi(String key, String value) {
    prefs.put(key, value);
  }

  • Or you could store those preferences in an LDAP:

de.tarent.ldap.prefs.LDAPSystemPreferences

import java.util.prefs.AbstractPreferences;
import java.util.prefs.BackingStoreException;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;

import de.tarent.ldap.LDAPException;
import de.tarent.ldap.LDAPManager;

/**
 * @author kirchner
 * 
 * Preferences im LDAP
 */
public class LDAPSystemPreferences extends AbstractPreferences {
    LDAPManager     ldm         = null;
    Properties      properties  = new Properties();
    //Map für key/value der Preferences
    Map             cache       = new HashMap();
    //Map für timestamp der Preferences
    Map             timestamp   = new HashMap();
    private Boolean deleted     = Boolean.FALSE;

  • Or you can use a simple property file:

com.adito.boot.PropertyPreferences:

import java.util.prefs.AbstractPreferences;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * A simple implementation for the preferences API. That stores preferences
 * in propery files. We do not have to worry about sharing the preferencese 
 * with other JVM instance so there is no need for any kind of synchronising
 * or locking.
 */
public class PropertyPreferences extends AbstractPreferences {
like image 165
VonC Avatar answered Sep 20 '22 15:09

VonC


It is always possible to extend java.util.prefs.AbstractPreferences.

An alternative could be to use The Configuration package of Apache Commons allows you to read and write configuration data from/to different sources.

like image 35
Ruben Avatar answered Sep 20 '22 15:09

Ruben