Class BaseProvider
- All Implemented Interfaces:
ParamProvider
This class is thread-safe when used as a singleton in multi-threaded environments.
Configuration methods (withMaxAge(int, ChronoUnit), withTransformation(Class))
use thread-local storage to support concurrent requests with different requirements.
The cache and transformation managers are thread-safe with zero synchronization overhead, using lock-free data structures (ThreadLocal, AtomicReference, ConcurrentHashMap) for optimal performance. The cache storage is shared across all threads, allowing cached values to be reused across requests.
Implementation Requirements: Subclasses must ensure that implementations of
getValue(String) and getMultipleValues(String) are thread-safe to
guarantee overall thread-safety of the provider.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionBaseProvider(CacheManager cacheManager, TransformationManager transformationManager) -
Method Summary
Modifier and TypeMethodDescriptionGet the value of a parameter, either from the underlying store or a cached value (if not expired).
Using this method, you can apply a basic transformation (to String).<T> TGet the value of a parameter, either from the underlying store or a cached value (if not expired).
Using this method, you must apply a transformation (eg. json/xml to Object).getMultiple(String path) Retrieve multiple parameter values either from the underlying store or a cached value (if not expired).
Cache all values with the 'path' as the key and also individually to be able toget(String)a single value later
Does not support transformation.getMultipleValues(String path) Retrieve multiple parameter values from the underlying parameter store.
Abstract: Implement this method in a child class ofBaseProviderprotected abstract StringRetrieve the parameter value from the underlying parameter store.
Abstract: Implement this method in a child class ofBaseProviderprotected Instantnow()protected voidwithMaxAge(int maxAge, ChronoUnit unit) (Optional) Builder method to call beforeget(String)orget(String, Class)to set cache max age for the parameter to get.
The max age is reset to default (either 5 or a custom value that may be set on the CacheManager) after each get, so you need to use this method for each parameter to cache with non-default max age.
Not Thread Safe: calling this method simultaneously by several threads can lead to unwanted cache time for some parameters.withTransformation(Class<? extends Transformer> transformerClass) Builder method to call beforeget(String)(Optional) orget(String, Class)(Mandatory).
-
Field Details
-
PARAMETERS
- See Also:
-
cacheManager
-
-
Constructor Details
-
BaseProvider
-
-
Method Details
-
getValue
Retrieve the parameter value from the underlying parameter store.
Abstract: Implement this method in a child class ofBaseProvider- Parameters:
key- key of the parameter- Returns:
- the value of the parameter identified by the key
-
getMultipleValues
Retrieve multiple parameter values from the underlying parameter store.
Abstract: Implement this method in a child class ofBaseProvider- Parameters:
path- Parameter store path- Returns:
- Return map of parameter name value pairs
-
withMaxAge
(Optional) Builder method to call beforeget(String)orget(String, Class)to set cache max age for the parameter to get.
The max age is reset to default (either 5 or a custom value that may be set on the CacheManager) after each get, so you need to use this method for each parameter to cache with non-default max age.
Not Thread Safe: calling this method simultaneously by several threads can lead to unwanted cache time for some parameters.- Parameters:
maxAge- Maximum time to cache the parameter, before calling the underlying parameter store.unit- Unit of time- Returns:
- the provider itself in order to chain calls (eg.
provider.withMaxAge(10, SECONDS).get("key")).
-
withTransformation
Builder method to call beforeget(String)(Optional) orget(String, Class)(Mandatory). to provide aTransformerthat will transform the String parameter into something else (String, Object, ...)
Base64TransformerandJsonTransformerare provided for respectively base64 and json content. You can also write your own (seeTransformer). Not Thread Safe: calling this method simultaneously by several threads can lead to errors (one Transformer for the wrong target type)- Parameters:
transformerClass- Class of the transformer to apply. For convenience, you can useTransformer.jsonorTransformer.base64shortcuts.- Returns:
- the provider itself in order to chain calls (eg.
provider.withTransformation(json).get("key", MyObject.class)).
-
getMultiple
Retrieve multiple parameter values either from the underlying store or a cached value (if not expired).
Cache all values with the 'path' as the key and also individually to be able toget(String)a single value later
Does not support transformation.- Specified by:
getMultiplein interfaceParamProvider- Parameters:
path- path of the parameter- Returns:
- a map containing parameters keys and values. The key is a subpart of the path
eg. getMultiple("/foo/bar") will retrieve [key="baz", value="valuebaz"] for parameter "/foo/bar/baz"
-
get
Get the value of a parameter, either from the underlying store or a cached value (if not expired).
Using this method, you can apply a basic transformation (to String).
Set aBasicTransformerwithwithTransformation(Class).
If you need a more complex transformation (to Object), useget(String, Class)method instead of this one.- Specified by:
getin interfaceParamProvider- Parameters:
key- key of the parameter- Returns:
- the String value of the parameter
- Throws:
IllegalStateException- if a wrong transformer class is provided throughwithTransformation(Class). Needs to be aBasicTransformer.TransformationException- if the transformation could not be done, because of a wrong format or an error during transformation.
-
get
Get the value of a parameter, either from the underlying store or a cached value (if not expired).
Using this method, you must apply a transformation (eg. json/xml to Object).
Set aTransformerwithwithTransformation(Class).
If you need a simpler transformation (to String), useget(String)method instead of this one.- Specified by:
getin interfaceParamProvider- Parameters:
key- key of the parametertargetClass- class of the target Object (after transformation)- Returns:
- the Object (T) value of the parameter
- Throws:
IllegalStateException- if no transformation class was provided throughwithTransformation(Class)TransformationException- if the transformation could not be done, because of a wrong format or an error during transformation.
-
now
-
resetToDefaults
protected void resetToDefaults()
-