Class FrameworkPropertyFile
- All Implemented Interfaces:
org.apache.commons.io.monitor.FileAlterationListener
Used by Galasa as an out of the box key value store. It is reccommended to use etcd3 for a performant system.
When provded with a URI to a K-V properties file, the FPF class can provide functionality similar to etcd3, including sets, gets, deletes, atomic sets and the watchng of values inside the properties.
-
Constructor Summary
ConstructorDescriptionFrameworkPropertyFile
(URI file) This constructor loads the properties store in the file into memory. -
Method Summary
Modifier and TypeMethodDescriptionvoid
This method deletes a k-v pair from the property store.void
This method does the same as the regular delete for a single k-v pair, but for a set of key values.void
deletePrefix
(String prefix) This method deletes the set of key values with a certain prefix.void
destroy()
This method cleans up the properties in memory, the observers and monitors.This method retrieves the most up to date value from a specified key.This method retrieves a List of namespaces which have properties set in the properties file.This method retrieves a Map of values from the properties file that have a common prefix to the key.void
load()
This method is used for loading the properties from the java properties file in a memory loaded propertiesvoid
onDirectoryChange
(File file) This method is not usedvoid
onDirectoryCreate
(File file) This method is not usedvoid
onDirectoryDelete
(File file) This method is not usedvoid
onFileChange
(File file) This method invoked when the montior thread detects a change to the file being observedvoid
onFileCreate
(File file) This method is not usedvoid
onFileDelete
(File file) This method is not usedvoid
onStart
(org.apache.commons.io.monitor.FileAlterationObserver observer) This method is not usedvoid
onStop
(org.apache.commons.io.monitor.FileAlterationObserver observer) This method is not usedvoid
performActions
(IDssAction... actions) void
This method is used to set a single k-v pair into the properties file.void
This method is used for setting multiple k-v pairs into the file.boolean
This memthod can perform a atomic set.boolean
This methods also performs and atomic set, but with the additional feature of setting a map of other k-v pairs if the old value is found to be the current valuevoid
shutdown()
void
This method is used to stop any watcher service with a given UUID.watch
(IFrameworkPropertyFileWatcher watcher, String key) This method provides a watching service for a key value pair inside properties.watchPrefix
(IFrameworkPropertyFileWatcher watcher, String keyPrefix) This method provides a single watch service to watch multiple k-v pairs with a common prefix in there key.void
write
(FileChannel fileChannel, Properties newProperties) This method is used for the writing of the current properties in memory to be stored in the java properties file defined by the URI.
-
Constructor Details
-
FrameworkPropertyFile
This constructor loads the properties store in the file into memory. For the watchers to get updates from the file in question and not other files in the directory, the observer is intialised with a file filter.
- Parameters:
file
- - URI of the java properties file- Throws:
FrameworkPropertyFileException
-
-
Method Details
-
get
This method retrieves the most up to date value from a specified key.
- Parameters:
key
-- Returns:
- - corresponding value from the key, null if non-exsisting
-
getPrefix
This method retrieves a Map of values from the properties file that have a common prefix to the key.
- Parameters:
keyPrefix
- - a common key prefix to a number of keys in the property store.- Returns:
- A map of any size or null. The key is a string which starts with the specified prefix.
-
getNamespaces
This method retrieves a List of namespaces which have properties set in the properties file.
- Returns:
- - List of namespaces
-
delete
This method deletes a k-v pair from the property store. As the properties file is changed, the fileModified method is invoked to update any watchers.
- Parameters:
key
- - the string key to be removoed from the loaded and file properties.- Throws:
FrameworkPropertyFileException
-
delete
This method does the same as the regular delete for a single k-v pair, but for a set of key values. Again the fileModified is invoked.- Parameters:
keys
- - a set of string keys to remove from properties- Throws:
FrameworkPropertyFileException
-
deletePrefix
This method deletes the set of key values with a certain prefix.- Parameters:
prefix
- - a prefix of keys to remove from properties- Throws:
FrameworkPropertyFileException
-
performActions
public void performActions(IDssAction... actions) throws DynamicStatusStoreException, DynamicStatusStoreMatchException -
write
This method is used for the writing of the current properties in memory to be stored in the java properties file defined by the URI.
- Parameters:
fileChannel
- - a write file channel that has an exclusive lock.newProperties
- - the most up to date properties in memory- Throws:
IOException
-
set
This method is used to set a single k-v pair into the properties file. fileModified is invoked as to update watchers to any changed values that are being watched.
- Parameters:
key
- - String keyvalue
- - String value- Throws:
FrameworkPropertyFileException
-
set
This method is used for setting multiple k-v pairs into the file. This method is sycnronized on the class to ensure all values are set before any other work is completed
- Parameters:
values
- - a String String map of k-v pairs.- Throws:
FrameworkPropertyFileException
IOException
-
watch
public UUID watch(IFrameworkPropertyFileWatcher watcher, String key) throws FrameworkPropertyFileException This method provides a watching service for a key value pair inside properties. The value does not need to exsists to create a watcher. The watcher records the activity and event type on detection of chnageds (Modified, Deleted, Created).
The watcher service uses two methods of detecting changes to the file. A polling service which montiors the file every 50ms for any changes. It also uses the checkAndNotify() methods provided from the observer set up on the class intialiastion, which is a manual check for file changes which notifies any watches.
- Parameters:
watcher
- - an interface for the watchers inplementation.key
- - the string key to watch- Returns:
- - returns a UUID which is used to identify a watcher service.
- Throws:
FrameworkPropertyFileException
-
unwatch
This method is used to stop any watcher service with a given UUID. It removes the given watcher from the watches list. If this is the final watcher in the list the method also shuts down the monitor
- Parameters:
watchId
- - the identifying UUID- Throws:
FrameworkPropertyFileException
-
watchPrefix
public UUID watchPrefix(IFrameworkPropertyFileWatcher watcher, String keyPrefix) throws FrameworkPropertyFileException This method provides a single watch service to watch multiple k-v pairs with a common prefix in there key.
- Parameters:
watcher
- an interface for the watchers inplementation.keyPrefix
- the string prefix to a key set to watch- Returns:
- returns a UUID which is used to identify a watcher service.
- Throws:
FrameworkPropertyFileException
-
setAtomic
public boolean setAtomic(String key, String oldValue, String newValue) throws FrameworkPropertyFileException This memthod can perform a atomic set. This provides a set functionality in the case where a key is currrenly set to a specific value. If the value is not as expected, then no set is performed. fileModified is invoked to update any possible watches.
This method is sycnronized on the class to ensure all values are set before any other work is completed
The method will also fail if the oldvalue is null, which indicates that there is no key of that string.
- Parameters:
key
- the key that is to be changed.oldValue
- the expected current value.newValue
- the value to change to if the expected value is true.- Returns:
- returns a boolean which informs if the set took place.
- Throws:
FrameworkPropertyFileException
-
setAtomic
public boolean setAtomic(String key, String oldValue, String newValue, Map<String, String> otherValues) throws FrameworkPropertyFileExceptionThis methods also performs and atomic set, but with the additional feature of setting a map of other k-v pairs if the old value is found to be the current value
This method is sycnronized on the class to ensure all values are set before any other work is completed
The method will also fail if the oldvalue is null, which indicates that there is no key of that string.
- Parameters:
key
- - String keyoldValue
- - String expected valuenewValue
- - String value to change to if key has oldvalueotherValues
- - Map of k-v pairs to set if key has oldvalue- Returns:
- - boolean for if the atomic set was done
- Throws:
FrameworkPropertyFileException
-
destroy
This method cleans up the properties in memory, the observers and monitors.
- Throws:
FrameworkPropertyFileException
-
load
This method is used for loading the properties from the java properties file in a memory loaded properties
*This method is sycnronized on the class to ensure all values are set before any other work is completed
- Throws:
FrameworkPropertyFileException
-
onStart
public void onStart(org.apache.commons.io.monitor.FileAlterationObserver observer) This method is not used
- Specified by:
onStart
in interfaceorg.apache.commons.io.monitor.FileAlterationListener
-
onStop
public void onStop(org.apache.commons.io.monitor.FileAlterationObserver observer) This method is not used
- Specified by:
onStop
in interfaceorg.apache.commons.io.monitor.FileAlterationListener
-
onFileChange
This method invoked when the montior thread detects a change to the file being observed
As this is an implemented method, it cannot throw a exception so the exception is logged
- Specified by:
onFileChange
in interfaceorg.apache.commons.io.monitor.FileAlterationListener
-
onFileCreate
This method is not used
- Specified by:
onFileCreate
in interfaceorg.apache.commons.io.monitor.FileAlterationListener
-
onFileDelete
This method is not used
- Specified by:
onFileDelete
in interfaceorg.apache.commons.io.monitor.FileAlterationListener
-
onDirectoryCreate
This method is not used
- Specified by:
onDirectoryCreate
in interfaceorg.apache.commons.io.monitor.FileAlterationListener
-
onDirectoryChange
This method is not used
- Specified by:
onDirectoryChange
in interfaceorg.apache.commons.io.monitor.FileAlterationListener
-
onDirectoryDelete
This method is not used
- Specified by:
onDirectoryDelete
in interfaceorg.apache.commons.io.monitor.FileAlterationListener
-
shutdown
- Throws:
FrameworkPropertyFileException
-