Class BaseProvider

java.lang.Object
software.amazon.lambda.powertools.parameters.BaseProvider
All Implemented Interfaces:
ParamProvider

@ThreadSafe public abstract class BaseProvider extends Object implements ParamProvider
Base class for all parameter providers.

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 Details

  • Constructor Details

  • Method Details

    • getValue

      protected abstract String getValue(String key)
      Retrieve the parameter value from the underlying parameter store.
      Abstract: Implement this method in a child class of BaseProvider
      Parameters:
      key - key of the parameter
      Returns:
      the value of the parameter identified by the key
    • getMultipleValues

      protected abstract Map<String,String> getMultipleValues(String path)
      Retrieve multiple parameter values from the underlying parameter store.
      Abstract: Implement this method in a child class of BaseProvider
      Parameters:
      path - Parameter store path
      Returns:
      Return map of parameter name value pairs
    • withMaxAge

      public BaseProvider withMaxAge(int maxAge, ChronoUnit unit)
      (Optional) Builder method to call before get(String) or get(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

      public BaseProvider withTransformation(Class<? extends Transformer> transformerClass)
      Builder method to call before get(String) (Optional) or get(String, Class) (Mandatory). to provide a Transformer that will transform the String parameter into something else (String, Object, ...)

      Base64Transformer and JsonTransformer are provided for respectively base64 and json content. You can also write your own (see Transformer). 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 use Transformer.json or Transformer.base64 shortcuts.
      Returns:
      the provider itself in order to chain calls (eg.
      provider.withTransformation(json).get("key", MyObject.class)
      ).
    • getMultiple

      public Map<String,String> 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 to get(String) a single value later
      Does not support transformation.
      Specified by:
      getMultiple in interface ParamProvider
      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

      public String get(String key)
      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 a BasicTransformer with withTransformation(Class).

      If you need a more complex transformation (to Object), use get(String, Class) method instead of this one.
      Specified by:
      get in interface ParamProvider
      Parameters:
      key - key of the parameter
      Returns:
      the String value of the parameter
      Throws:
      IllegalStateException - if a wrong transformer class is provided through withTransformation(Class). Needs to be a BasicTransformer.
      TransformationException - if the transformation could not be done, because of a wrong format or an error during transformation.
    • get

      public <T> T get(String key, Class<T> targetClass)
      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 a Transformer with withTransformation(Class).

      If you need a simpler transformation (to String), use get(String) method instead of this one.
      Specified by:
      get in interface ParamProvider
      Parameters:
      key - key of the parameter
      targetClass - class of the target Object (after transformation)
      Returns:
      the Object (T) value of the parameter
      Throws:
      IllegalStateException - if no transformation class was provided through withTransformation(Class)
      TransformationException - if the transformation could not be done, because of a wrong format or an error during transformation.
    • now

      protected Instant now()
    • resetToDefaults

      protected void resetToDefaults()