Class PreferencesService

All Implemented Interfaces:
IPreferencesService

public class PreferencesService extends AbstractPreferencesService
The default implementation of IPreferencesService. System preferences are stored in a common location depending on the OS. User preferences are stored in a hidden directory of the user's home directory.
Author:
ralph
  • Field Details

    • INSTANCE

      public static final IPreferencesService INSTANCE
      The default global preference service
  • Method Details

    • createRootNode

      protected IPreferences createRootNode()
      Creates the root node.
      Specified by:
      createRootNode in class AbstractPreferencesService
      Returns:
      the root node for the service
    • loadUserPreferences

      protected void loadUserPreferences(IPreferences node, String applicationName) throws BackingStoreException
      Loads the user preferences into the given node.
      Specified by:
      loadUserPreferences in class AbstractPreferencesService
      Parameters:
      node - node that shall be populated
      applicationName - name of application to be loaded
      Throws:
      BackingStoreException - when the storage fails
    • loadSystemPreferences

      protected void loadSystemPreferences(IPreferences node, String applicationName) throws BackingStoreException
      Loads the system preferences into the given node.
      Specified by:
      loadSystemPreferences in class AbstractPreferencesService
      Parameters:
      node - node that shall be populated
      applicationName - name of application to be loaded
      Throws:
      BackingStoreException - when the storage fails
    • flushUserPreferences

      protected void flushUserPreferences(IPreferences node, String applicationName) throws BackingStoreException
      Flushes the user preferences from the given node.
      Specified by:
      flushUserPreferences in class AbstractPreferencesService
      Parameters:
      node - node that shall be flushed
      applicationName - name of application to be flushed
      Throws:
      BackingStoreException - when the storage fails
    • flushSystemPreferences

      protected void flushSystemPreferences(IPreferences node, String applicationName) throws BackingStoreException
      Flushes the system preferences from the given node.
      Specified by:
      flushSystemPreferences in class AbstractPreferencesService
      Parameters:
      node - node that shall be flushed
      applicationName - name of application to be flushed
      Throws:
      BackingStoreException - when the storage fails
    • getInputStream

      protected InputStream getInputStream(File f) throws BackingStoreException
      Returns an input stream that can be safely read.
      Parameters:
      f - file
      Returns:
      input stream (will be empty is file does not exists or cannot be read)
      Throws:
      BackingStoreException - when the file cannot be opened
    • getOutputStream

      protected OutputStream getOutputStream(File f) throws BackingStoreException
      Returns an output stream that can be written to.
      Parameters:
      f - file
      Returns:
      output stream (intermediate directories will be created if required)
      Throws:
      BackingStoreException - when the file cannot be opened
    • load

      protected void load(IPreferences node, InputStream in) throws BackingStoreException
      Loads the information from the stream into the node
      Parameters:
      node - node to be filled
      in - input stream
      Throws:
      BackingStoreException - when the stream contains errors
    • put

      protected void put(IPreferences node, String key, String value)
      Stores the key at the appropriate sub-node.
      Parameters:
      node - node
      key - path of key (including node)
      value - value of key
    • save

      protected void save(IPreferences node, OutputStream out) throws BackingStoreException
      Saves the node information into the stream
      Parameters:
      node - node to be saved
      out - output stream
      Throws:
      BackingStoreException - when the stream produces errors
    • save

      protected void save(String prefix, IPreferences node, PrintWriter out) throws BackingStoreException
      Recursively stores the given node into the writer.
      Parameters:
      prefix - prefix to be used for keys (can be null)
      node - node to be written
      out - print writer
      Throws:
      BackingStoreException - when a node cannot be written
    • getUserPreferencesHome

      public File getUserPreferencesHome(String applicationName)
      Returns the home directory for user preferences of the given application.
      Parameters:
      applicationName - name of application
      Returns:
      user preferences home
    • getUserHome

      protected File getUserHome()
      Returns the home directory of the user.
      Returns:
      the user home dir
    • getUserPreferencesFile

      protected File getUserPreferencesFile(String applicationName)
      Returns the application's preferences file of the user.
      Parameters:
      applicationName - name of the application
      Returns:
      the preferences file
    • getSystemPreferencesHome

      public File getSystemPreferencesHome(String applicationName)
      Returns the home directory for system preferences of the given application.
      Parameters:
      applicationName - name of application
      Returns:
      system preferences home
    • getSystemPreferencesFile

      protected File getSystemPreferencesFile(String applicationName)
      Returns the application's preferences file of the system.
      Parameters:
      applicationName - name of the application
      Returns:
      the preferences file
    • getSystemHome

      protected File getSystemHome()
      Returns the system home dir to store system prefs at.
      Returns:
      the system home dir (usually /var or C:\Users\AllUsers)