Package rs.baselib.prefs
Class PreferencesService
java.lang.Object
rs.baselib.prefs.AbstractPreferencesService
rs.baselib.prefs.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
-
Nested Class Summary
Nested classes/interfaces inherited from class rs.baselib.prefs.AbstractPreferencesService
AbstractPreferencesService.FlushingThread -
Field Summary
Fields Modifier and Type Field Description static IPreferencesServiceINSTANCEThe default global preference service -
Method Summary
Modifier and Type Method Description protected IPreferencescreateRootNode()Creates the root node.protected voidflushSystemPreferences(IPreferences node, java.lang.String applicationName)Flushes the system preferences from the given node.protected voidflushUserPreferences(IPreferences node, java.lang.String applicationName)Flushes the user preferences from the given node.protected java.io.InputStreamgetInputStream(java.io.File f)Returns an input stream that can be safely read.protected java.io.OutputStreamgetOutputStream(java.io.File f)Returns an output stream that can be written to.protected java.io.FilegetSystemHome()Returns the system home dir to store system prefs at.protected java.io.FilegetSystemPreferencesFile(java.lang.String applicationName)Returns the application's preferences file of the system.java.io.FilegetSystemPreferencesHome(java.lang.String applicationName)Returns the home directory for system preferences of the given application.protected java.io.FilegetUserHome()Returns the home directory of the user.protected java.io.FilegetUserPreferencesFile(java.lang.String applicationName)Returns the application's preferences file of the user.java.io.FilegetUserPreferencesHome(java.lang.String applicationName)Returns the home directory for user preferences of the given application.protected voidload(IPreferences node, java.io.InputStream in)Loads the information from the stream into the nodeprotected voidloadSystemPreferences(IPreferences node, java.lang.String applicationName)Loads the system preferences into the given node.protected voidloadUserPreferences(IPreferences node, java.lang.String applicationName)Loads the user preferences into the given node.protected voidput(IPreferences node, java.lang.String key, java.lang.String value)Stores the key at the appropriate sub-node.protected voidsave(java.lang.String prefix, IPreferences node, java.io.PrintWriter out)Recursively stores the given node into the writer.protected voidsave(IPreferences node, java.io.OutputStream out)Saves the node information into the streamMethods inherited from class rs.baselib.prefs.AbstractPreferencesService
checkNodeName, createReadWriteLock, flush, getApplicationName, getApplicationNode, getApplicationPreferences, getLockNode, getReadLock, getReadWriteLock, getSystemPreferences, getUserPreferences, getWriteLock, nodeChanged, sync
-
Field Details
-
INSTANCE
The default global preference service
-
-
Method Details
-
createRootNode
Creates the root node.- Specified by:
createRootNodein classAbstractPreferencesService- Returns:
- the root node for the service
-
loadUserPreferences
protected void loadUserPreferences(IPreferences node, java.lang.String applicationName) throws java.util.prefs.BackingStoreExceptionLoads the user preferences into the given node.- Specified by:
loadUserPreferencesin classAbstractPreferencesService- Parameters:
node- node that shall be populatedapplicationName- name of application to be loaded- Throws:
java.util.prefs.BackingStoreException- when the storage fails
-
loadSystemPreferences
protected void loadSystemPreferences(IPreferences node, java.lang.String applicationName) throws java.util.prefs.BackingStoreExceptionLoads the system preferences into the given node.- Specified by:
loadSystemPreferencesin classAbstractPreferencesService- Parameters:
node- node that shall be populatedapplicationName- name of application to be loaded- Throws:
java.util.prefs.BackingStoreException- when the storage fails
-
flushUserPreferences
protected void flushUserPreferences(IPreferences node, java.lang.String applicationName) throws java.util.prefs.BackingStoreExceptionFlushes the user preferences from the given node.- Specified by:
flushUserPreferencesin classAbstractPreferencesService- Parameters:
node- node that shall be flushedapplicationName- name of application to be flushed- Throws:
java.util.prefs.BackingStoreException- when the storage fails
-
flushSystemPreferences
protected void flushSystemPreferences(IPreferences node, java.lang.String applicationName) throws java.util.prefs.BackingStoreExceptionFlushes the system preferences from the given node.- Specified by:
flushSystemPreferencesin classAbstractPreferencesService- Parameters:
node- node that shall be flushedapplicationName- name of application to be flushed- Throws:
java.util.prefs.BackingStoreException- when the storage fails
-
getInputStream
protected java.io.InputStream getInputStream(java.io.File f) throws java.util.prefs.BackingStoreExceptionReturns 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:
java.util.prefs.BackingStoreException- when the file cannot be opened
-
getOutputStream
protected java.io.OutputStream getOutputStream(java.io.File f) throws java.util.prefs.BackingStoreExceptionReturns an output stream that can be written to.- Parameters:
f- file- Returns:
- output stream (intermediate directories will be created if required)
- Throws:
java.util.prefs.BackingStoreException- when the file cannot be opened
-
load
protected void load(IPreferences node, java.io.InputStream in) throws java.util.prefs.BackingStoreExceptionLoads the information from the stream into the node- Parameters:
node- node to be filledin- input stream- Throws:
java.util.prefs.BackingStoreException- when the stream contains errors
-
put
Stores the key at the appropriate sub-node.- Parameters:
node- nodekey- path of key (including node)value- value of key
-
save
protected void save(IPreferences node, java.io.OutputStream out) throws java.util.prefs.BackingStoreExceptionSaves the node information into the stream- Parameters:
node- node to be savedout- output stream- Throws:
java.util.prefs.BackingStoreException- when the stream produces errors
-
save
protected void save(java.lang.String prefix, IPreferences node, java.io.PrintWriter out) throws java.util.prefs.BackingStoreExceptionRecursively stores the given node into the writer.- Parameters:
prefix- prefix to be used for keys (can be null)node- node to be writtenout- print writer- Throws:
java.util.prefs.BackingStoreException- when a node cannot be written
-
getUserPreferencesHome
public java.io.File getUserPreferencesHome(java.lang.String applicationName)Returns the home directory for user preferences of the given application.- Parameters:
applicationName- name of application- Returns:
- user preferences home
-
getUserHome
protected java.io.File getUserHome()Returns the home directory of the user.- Returns:
- the user home dir
-
getUserPreferencesFile
protected java.io.File getUserPreferencesFile(java.lang.String applicationName)Returns the application's preferences file of the user.- Parameters:
applicationName- name of the application- Returns:
- the preferences file
-
getSystemPreferencesHome
public java.io.File getSystemPreferencesHome(java.lang.String applicationName)Returns the home directory for system preferences of the given application.- Parameters:
applicationName- name of application- Returns:
- system preferences home
-
getSystemPreferencesFile
protected java.io.File getSystemPreferencesFile(java.lang.String applicationName)Returns the application's preferences file of the system.- Parameters:
applicationName- name of the application- Returns:
- the preferences file
-
getSystemHome
protected java.io.File getSystemHome()Returns the system home dir to store system prefs at.- Returns:
- the system home dir (usually /var or C:\Users\AllUsers)
-