Class DbPreferencesFactory
- All Implemented Interfaces:
PreferencesFactory, org.tentackle.prefs.PersistedPreferencesFactory
The DbPreferencesFactory implements PersistedPreferencesFactory which in turn
extends PreferencesFactory. As such, it can be used as a drop-in
replacement for the default JRE preferences factory. By default, however, it is available
to the application only via PersistedPreferencesFactory.getInstance(), which is the
preferred way for tentackle applications to use preferences.
- Author:
- harald
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddNodeChangeListener(DbPreferences node, NodeChangeListener ncl) Adds a node listener.voidAdds a preferences changed listener.voidChecks all preferences and tries to fix if possible.
The method is provided sinceDbPreferencesNodeandDbPreferencesKeyprovide no referential integrity via database-enforced foreign keys, but only at application level.
Invoke this method, whenever the database has been modified manually via SQL.voidflush(DbPreferences node, boolean sync) Flushes a node and all of its sub-nodes to the backing store.static DbPreferencesFactoryGets the factory singleton.getRootTree(Db db, String user) Gets the root node for given user and all of its sub-nodes and keys at once.longGets the current serial version of given user- or system root.protected StringGets the current username.voidbooleanbooleanbooleanvoidkeyChanged(DbPreferences node, DbPreferencesKey key) Invokes listeners when a key is added, updated or removed.voidnodeChanged(DbPreferences node) Invokes listeners when a node is added or removed.protected StringReturns the absolute path name of the node corresponding to the package of the specified object.protected voidReleases the session after use.voidRemoves a node listener.voidRemoves a preferences changed listener.protected voidremoveRoot(DbPreferences node) Removes a root node of given node from the repository.protected DbrequestDb(boolean forUser, boolean forWrite) Requests for a session to work with.
By default, the thread-local session is used.protected voidscheduleUpdate(long nodeMaxSerial, long keyMaxSerial) Schedules the update of the repository on behalf of the modification tracker.voidsetAutoSync(boolean autoSync) voidsetReadOnly(boolean readOnly) voidsetSystemOnly(boolean systemOnly) systemNodeForPackage(Class<?> c) protected voidupdateRepository(Db db, long nodeMaxSerial, long keyMaxSerial) Updates the repository.
Invoked whenever the data in prefnode or prefkey has changed.userNodeForPackage(Class<?> c) userRoot()
-
Constructor Details
-
DbPreferencesFactory
public DbPreferencesFactory()Creates the factory.
-
-
Method Details
-
getInstance
-
invalidate
public void invalidate()- Specified by:
invalidatein interfaceorg.tentackle.prefs.PersistedPreferencesFactory
-
isAutoSync
public boolean isAutoSync()- Specified by:
isAutoSyncin interfaceorg.tentackle.prefs.PersistedPreferencesFactory
-
setAutoSync
public void setAutoSync(boolean autoSync) - Specified by:
setAutoSyncin interfaceorg.tentackle.prefs.PersistedPreferencesFactory
-
isReadOnly
public boolean isReadOnly()- Specified by:
isReadOnlyin interfaceorg.tentackle.prefs.PersistedPreferencesFactory
-
setReadOnly
public void setReadOnly(boolean readOnly) - Specified by:
setReadOnlyin interfaceorg.tentackle.prefs.PersistedPreferencesFactory
-
isSystemOnly
public boolean isSystemOnly()- Specified by:
isSystemOnlyin interfaceorg.tentackle.prefs.PersistedPreferencesFactory
-
setSystemOnly
public void setSystemOnly(boolean systemOnly) - Specified by:
setSystemOnlyin interfaceorg.tentackle.prefs.PersistedPreferencesFactory
-
getSystemRoot
- Specified by:
getSystemRootin interfaceorg.tentackle.prefs.PersistedPreferencesFactory
-
getUserRoot
- Specified by:
getUserRootin interfaceorg.tentackle.prefs.PersistedPreferencesFactory
-
userNodeForPackage
- Specified by:
userNodeForPackagein interfaceorg.tentackle.prefs.PersistedPreferencesFactory
-
systemNodeForPackage
- Specified by:
systemNodeForPackagein interfaceorg.tentackle.prefs.PersistedPreferencesFactory
-
systemRoot
- Specified by:
systemRootin interfacePreferencesFactory
-
userRoot
- Specified by:
userRootin interfacePreferencesFactory
-
getSerial
Gets the current serial version of given user- or system root.- Parameters:
user- the username, null or "" if system- Returns:
- the serial, 0 if no such root loaded
-
addPreferenceChangeListener
Adds a preferences changed listener.Notice that when a node is removed, its listeners will not be removed. So, if the node is added again, the listeners are still there. Listeners have to be removed by the application explicitly.
- Parameters:
node- the nodepcl- the listener
-
removePreferenceChangeListener
Removes a preferences changed listener.- Parameters:
node- the nodepcl- the listener
-
addNodeChangeListener
Adds a node listener.Notice that when a node is removed, its listeners will not be removed. So, if the node is added again, the listeners are still there. Listeners have to be removed by the application explicitly.
- Parameters:
node- the nodencl- the listener
-
removeNodeChangeListener
Removes a node listener.- Parameters:
node- the nodencl- the listener
-
nodeChanged
Invokes listeners when a node is added or removed.- Parameters:
node- the added or removed node
-
keyChanged
Invokes listeners when a key is added, updated or removed.- Parameters:
node- the node holding the keykey- the changed key
-
flush
Flushes a node and all of its sub-nodes to the backing store.The change listeners are invoked once the flush completed, without holding the monitor of this factory. If the flush fails, they are not invoked at all.
If the session is running a transaction, the flush joins it and the modified rows stay locked in the database until the transaction ends. So does the write lock of this factory, see
acquireWriteLock(Db).- Parameters:
node- the preferences nodesync- true if load modifications from storage before flush- Throws:
BackingStoreException- if failed
-
getRootTree
Gets the root node for given user and all of its sub-nodes and keys at once.- Parameters:
db- the optional db, null if request a db temporarilyuser- the username, null or empty if system user- Returns:
- the root node, never null
-
checkAllPreferences
Checks all preferences and tries to fix if possible.
The method is provided sinceDbPreferencesNodeandDbPreferencesKeyprovide no referential integrity via database-enforced foreign keys, but only at application level.
Invoke this method, whenever the database has been modified manually via SQL.- Parameters:
db- the session (must be local)
-
nodeName
Returns the absolute path name of the node corresponding to the package of the specified object.- Parameters:
clazz- the class- Returns:
- the path name
- Throws:
IllegalArgumentException- if the package has no preferences node associated with it
-
removeRoot
Removes a root node of given node from the repository.- Parameters:
node- the root node or one of its sub-nodes
-
updateRepository
Updates the repository.
Invoked whenever the data in prefnode or prefkey has changed.The change listeners are invoked once the repository is updated, without holding the monitor of this factory.
- Parameters:
db- the sessionnodeMaxSerial- the new table serial for prefnodeskeyMaxSerial- the new table serial for prefkeys
-
scheduleUpdate
protected void scheduleUpdate(long nodeMaxSerial, long keyMaxSerial) Schedules the update of the repository on behalf of the modification tracker.The repository is not updated by the tracker thread, since it must not wait for the monitor of this factory. The thread holding the monitor may wait for the tracker thread, for example to configure the modification counter in a flush, and both would wait for each other until the tracker's wait interval elapsed. Instead, the update is performed by a separate thread, using the session of
requestDb(boolean, boolean). Updates scheduled while the thread is still running are merged.- Parameters:
nodeMaxSerial- the new table serial for prefnodes, 0 if unchangedkeyMaxSerial- the new table serial for prefkeys, 0 if unchanged
-
requestDb
Requests for a session to work with.
By default, the thread-local session is used. If there is no thread-local session, a session cloned from the modification tracker's session is used, seegetFallbackDb().In preparation for replication layers, a requested session must be released after use.
- Parameters:
forUser- true if a user session is required (no modification tracker fallback)forWrite- true if the session is used for writing (flush), else read- Returns:
- the session
-
releaseDb
-
getUserName
-