Class FilePreferencesFactory

java.lang.Object
is.codion.common.model.preferences.FilePreferencesFactory
All Implemented Interfaces:
PreferencesFactory

public final class FilePreferencesFactory extends Object implements PreferencesFactory
A file-based preferences factory that removes the length restrictions of the default Java Preferences implementation.

The default Java Preferences API imposes the following restrictions:

  • Maximum key length: 80 characters
  • Maximum value length: 8,192 characters (8 KB)
  • Maximum node name length: 80 characters

This implementation removes these restrictions by storing preferences in a JSON file, allowing unlimited key and value lengths. This is particularly useful for storing configuration data such as serialized table column preferences or other structured data that may exceed the default limits.

File Location:

  • Windows: %LOCALAPPDATA%\Codion\preferences.json
  • macOS: ~/Library/Preferences/Codion/preferences.json
  • Linux: ~/.config/codion/preferences.json
  • Other: ~/.codion/preferences.json

File Format:

Preferences are stored as a flat JSON object:

 {
   "normal.key": "normal value",
   "very.long.key.that.exceeds.eighty.characters": "value",
   "key.with.large.value": "... 100KB of text ...",
   "key.with.newlines": "Line 1\nLine 2\nLine 3"
 }
 

Usage:

To use this factory, set the system property before accessing preferences:

 // At application startup
 System.setProperty("java.util.prefs.PreferencesFactory",
     "is.codion.common.model.preferences.FilePreferencesFactory");

 // Then use preferences normally
 Preferences prefs = Preferences.userRoot();
 prefs.put("my.very.long.key.name.that.exceeds.80.chars", "my huge value...");
 prefs.flush(); // Writes to ~/.codion/preferences.json
 

Limitations:

  • Currently only supports user preferences (not system preferences)

Thread Safety and Concurrency:

This implementation is thread-safe both within a single JVM and across multiple JVMs:

  • Internal synchronization ensures thread safety within a JVM
  • File locking prevents concurrent writes from multiple JVMs
  • Atomic writes prevent file corruption
  • External changes are detected and reloaded via sync()

Migration:

On first use, if the preferences file doesn't exist, this factory will automatically attempt to migrate existing preferences from the default Java implementation. To disable automatic migration:

 System.setProperty("is.codion.common.model.preferences.FilePreferencesFactory.migrate", "false");
 
See Also:
  • Field Details

    • MIGRATE

      public static final PropertyValue<Boolean> MIGRATE
      Specifies whether existing default java preferences should be migrated.
      • Value type:Boolean
      • Default value: true
  • Constructor Details

    • FilePreferencesFactory

      public FilePreferencesFactory()
  • Method Details